collection samples updated for ID Based Routing

This commit is contained in:
unknown 2015-08-15 16:57:12 -07:00
Родитель 1e483fb6b0
Коммит cf4beb8ead
4 изменённых файлов: 146 добавлений и 112 удалений

Просмотреть файл

@ -1,6 +1,7 @@
Samples for performing basic CRUD operations on DocumentDB Collection
- listCollections - example of using the QueryIterator to get a list of Collections in a Database in to an array
Samples for performing basic CRUD operations on DocumentDB Collection
- createCollection - given an id, create a new Collection with the default indexingPolicy
- getOrCreateDatabase - given an id for a database, attempt to find existing Database. if none found, create a new Database
- deleteCollection - using the supplied collection object, delete the collection
- deleteDatabase - using the supplied database object, delete the database
- listCollections - example of using the QueryIterator to get a list of Collections in a Database
- getOfferType - get the Offer.OfferType for a collection. This is what determines if a Collection is S1, S2, or S3
- modifyOfferType - change the Offer.OfferType for a collection. This is how you scale a Collection up or down
- deleteCollection - given just the collection id, delete the collection

Просмотреть файл

@ -16,44 +16,77 @@ var masterKey = config.connection.authKey;
// Establish a new instance of the DocumentDBClient to be used throughout this demo
var client = new DocumentDBClient(host, { masterKey: masterKey });
var dbLink;
//---------------------------------------------------------------------------------
// This demo performs a few steps
// 1. Create a collection in the database
// 2 List all collections on a database
// 2. Read a specific collection using its Id
// 3. Get the OfferType for a collection
// 4. Scale the collection up by changing the Offer.OfferType it is linked to
// 5. Delete collection and the database we created
// 6. finish()
// 1. createCollection - given an id, create a new Collectionwith thedefault indexingPolicy
// 2. listCollections - example of using the QueryIterator to get a list of Collections in a Database
// 3. readCollection - Read a collection by its _self
// 4. readCollection - Read a collection by its id (using new ID Based Routing)
// 5. getOfferType - get the Offer.OfferType for a collection. This is what determines if aCollection is S1, S2, or S3
// 6. changeOfferType - change the Offer.OfferType for a collection. This is how you scale a Collection up or down
// 7. deleteCollection - given just the collection id, delete the collection
//---------------------------------------------------------------------------------
// ensuring a database exists for us to work with
console.log('Looking for a database with id of \'' + databaseId + '\'...');
getOrCreateDatabase(databaseId, function (db) {
//NOTE: when using the new ID Based Routing URIs, instead of the _self, as we're doing in this sample
// ensure that the URI does not end with a trailing '/' character
var dbLink = 'dbs/' + databaseId
dbLink = 'dbs/' + databaseId;
// 1.
console.log();
console.log('Creating a collection with id \'' + collectionId + '\'...');
//1.
console.log('1. createCollection ith id \'' + collectionId + '\'');
createCollection(dbLink, collectionId, function (col) {
// 2 .
console.log();
console.log('Listing all collections on this database...');
//2.
console.log('\n2. listCollections in database');
listCollections(dbLink, function (cols) {
for (var i = 0; i < cols.length; i++) {
console.log(cols[i].id);
}
// 6.
console.log('\n');
console.log('Cleaning up ...');
deleteDatabase(dbLink, function () {
finish();
//3.
console.log('\n3. readCollection by its _self');
readCollection(col._self, function (result) {
if (result) {
console.log('Collection with _self \'' + result._self + '\' was found its id is \'' + result.id);
}
//4.
console.log('\n4. readCollection by its id');
readCollectionById(collectionId, function (result) {
if (result) {
console.log('Collection with id of \'' + collectionId + '\' was found its _self is \'' + result._self + '\'');
}
//5.
console.log('\n5. getOfferType by its id');
getOfferType(col, function (offer) {
if (offer) {
console.log('Collection with id of \'' + collectionId + '\' has an Offer Type of \'' + offer.offerType + '\'');
}
//6.
console.log('\n6. changeOfferType to an S2');
offer.offerType = 'S2';
changeOfferType(offer.id, offer, function (offer) {
if (offer) {
console.log('Collection now has offerType of \'' + offer.offerType + '\'');
}
//7.
console.log('\n7. deleteCollection \'' + collectionId + '\'');
deleteCollection(collectionId, function () {
//cleanup & end
console.log('\nCleaning up ...');
finish();
});
});
});
});
});
});
});
@ -68,7 +101,10 @@ function createCollection(databaseLink, collectionId, callback) {
//for more information on OfferTypes please consult the DocumentDB Documentation on
//http://azure.microsoft.com/en-us/documentation/services/documentdb/
client.createCollection(databaseLink, { id: collectionId }, { offerType: "S1" }, function (err, created) {
var collSpec = { id: collectionId };
var options = { offerType: "S1" };
client.createCollection(databaseLink, collSpec, options, function (err, created) {
if (err) {
handleError(err);
@ -91,58 +127,91 @@ function listCollections(databaseLink, callback) {
});
}
//Collections and OfferTypes are loosely coupled.
//An Offer's resource -- Collection's _self property
//And an Offer's offerResourceId -- Collection's _rid property
//To find the OfferType for a Collection, query for Offers by resourceLink matching collectionLink
function getOfferType(collectionLink, callback) {
var querySpec = {
query: 'SELECT * FROM root r WHERE r.resource = @link',
parameters: [
{
name: '@link',
value: collectionLink
}
]
};
client.queryOffers(querySpec).toArray(function (err, offers) {
if (err) handleError(err);
var offer = offers[0];
callback(offer);
function readCollection(selfLink, callback) {
client.readCollection(selfLink, function (err, coll) {
if (err) {
handleError(err);
} else {
callback(coll);
}
});
}
function changeOfferType(collectionLink, newOfferType, callback) {
function readCollectionById(collectionId, callback) {
//when using the new ID Based Routing URIs, the URI must NOT have a trailing / character
//i.e. instead of dbs/databaseId/colls/collectionId/ (which is the format of a db._self) the URI must be dbs/databaseId/colls/collectionId
var collLink = dbLink + '/colls/' + collectionId;
client.readCollection(collLink, function (err, coll) {
if (err) {
handleError(err);
} else {
callback(coll);
}
});
}
function getOfferType(collection, callback) {
//Collections and OfferTypes are loosely coupled.
//Offer.resource == collection._self And Offer.offerResourceId == collection._rid
//Therefore, to find the OfferType for a Collection, query for Offers by resourceLink matching collectionSelfLink
var querySpec = {
query: 'SELECT * FROM root r WHERE r.resource = @link',
parameters: [
{
name: '@link',
value: collectionLink
value: collection._self
}
]
};
client.queryOffers(querySpec).toArray(function (err, offers) {
if (err) handleError(err);
var offer = offers[0];
offer.offerType = newOfferType;
client.replaceOffer(offer._self, offer, function (err, replacedOffer) {
if (err) {
handleError(err);
} else if (replacedOffer.offerType != newOfferType) {
throw 'OfferType was not updated';
} else {
callback(replacedOffer);
}
})
if (err) {
handleError(err);
} else if (offers.length === 0) {
console.log('No offer found for collection');
} else {
console.log('Offer found for collection');
var offer = offers[0];
callback(offer);
}
});
}
function changeOfferType(offerId, updatedOffer, callback) {
var offerLink = 'offers/' + offerId;
client.replaceOffer(offerLink, updatedOffer, function (err, replacedOffer) {
if (err) {
handleError(err);
} else if (replacedOffer.offerType != updatedOffer.offerType) {
throw 'OfferType was not updated';
} else {
callback(replacedOffer);
}
})
}
function deleteCollection(collectionId, callback) {
var collLink = dbLink + '/colls/' + collectionId;
client.deleteCollection(collLink, function (err) {
if (err) {
handleError(err);
} else {
console.log('Collection \'' + collectionId + '\'deleted');
callback();
}
});
}
function getOrCreateDatabase(databaseId, callback){
//we're using queryDatabases here and not readDatabase
//readDatabase will throw an exception if resource is not found
@ -164,76 +233,39 @@ function getOrCreateDatabase(databaseId, callback){
//database not found, create it
} else if (results.length === 0) {
console.log('Database \'' + databaseId + '\'not found');
var databaseDef = { id: databaseId };
client.createDatabase(databaseDef, function (err, created) {
if (err) {
handleError(err);
}
console.log('Database \'' + databaseId + '\'created');
callback(created);
});
//database found, return it
} else {
console.log('Database \'' + databaseId + '\'found');
callback(results[0]);
}
});
}
function deleteCollection(collection, callback) {
client.deleteCollection(collection._self, function (err) {
if (err) {
handleError(err);
} else {
console.log('Collection \'' + collection.id + '\'deleted');
callback();
}
});
}
function deleteDatabase(dbLink, callback) {
function deleteDatabase(dbLink) {
client.deleteDatabase(dbLink, function (err) {
if (err) {
handleError(err);
} else {
console.log('Database \'' + database.id + '\'deleted');
callback();
}
});
}
function deleteDatabase(databaseId, callback) {
findDatabaseById(databaseId, function (err, db) {
if (err) {
handleError(err);
}
if (db != null) {
client.deleteDatabase(db._self, function (err) {
if (err) {
handleError(err);
} else {
callback();
}
});
}
});
}
function handleError(error) {
console.log();
console.log('An error with code \'' + error.code + '\' has occurred:');
console.log('\nAn error with code \'' + error.code + '\' has occurred:');
console.log('\t' + JSON.parse(error.body).message);
console.log();
finish();
}
function finish() {
console.log();
console.log('End of demo.');
deleteDatabase(dbLink);
console.log('\nEnd of demo.');
}

Просмотреть файл

@ -1,8 +1,9 @@
Samples for performing basic CRUD operations on DocumentDB Database
- findDatabaseByID - Attempt to find a database by Id, if found then just complete the sample
- createDatabase - If the database was not found, try create it
- listDatabases - Once the database was created, list all the databases on the account
- readDatbase - Read a database by its _self
- readDatabase - Read a database by its id (using new ID Based Routing)
- deleteDatabase - Delete a database given its id
- createCollection - given an id, create a new Collectionwith thedefault indexingPolicy
- listCollections - example of using the QueryIterator to get a list of Collections in a Database
- readCollection - Read a collection by its _self
- readCollection - Read a collection by its id (using new ID Based Routing)
- getOfferType - get the Offer.OfferType for a collection. This is what determines if aCollection is S1, S2, or S3
- modifyOfferType - change the Offer.OfferType for a collection. This is how you scale a Collection up or down
- deleteCollection - given just the collection id, delete the collection

Просмотреть файл

@ -86,9 +86,9 @@ findDatabaseById(databaseId, function (err, db) {
}
});
//when using the new ID Based Routing URIs, the URI must NOT have a trailing / character
//i.e. instead of dbs/db/ (which is the format of a db._self) the URI should be dbs/db
function readDatabaseById(databaseId, callback) {
//when using the new ID Based Routing URIs, the URI must NOT have a trailing / character
//i.e. instead of dbs/db/ (which is the format of a db._self) the URI should be dbs/db
client.readDatabase('dbs/' + databaseId, function (err, db) {
if (err) {
handleError(err);