Skip to content

Instantly share code, notes, and snippets.

@htuomola
Last active February 12, 2016 19:02
Show Gist options
  • Save htuomola/620f13f572a2dfd4618a to your computer and use it in GitHub Desktop.
Save htuomola/620f13f572a2dfd4618a to your computer and use it in GitHub Desktop.
DocumentDB node.js SDK not passing continuation token properly on query
node_modules
var DocumentDBClient = require('documentdb').DocumentClient;
var DocDbUtils = require('./DocDbUtils');
var config = {}
config.host = process.env.HOST || "";
config.authKey = process.env.AUTH_KEY || "";
config.databaseId = "";
config.collectionId = "";
var docDbClient = new DocumentDBClient(config.host, {
masterKey: config.authKey
});
var collection;
var database;
var init = function(callback){
DocDbUtils.getOrCreateDatabase(docDbClient, config.databaseId, (err, db) => {
if (err && callback) {
callback(err);
} else {
database = db;
DocDbUtils.getOrCreateCollection(docDbClient, database._self, config.collectionId, (err, coll) => {
if (err && callback) {
callback(err);
} else {
collection = coll;
callback();
}
});
}
});
};
init(function(){
var queryDocuments = function(query, opts, callback){
console.log("Querying: "+ query.query);
console.log("Continuation: "+ opts.continuation);
var iterator = docDbClient.queryDocuments(collection._self, query, opts)
var resultCallback = function(error, results, resultArray){
if(error)
throw error;
console.log("got "+results.length+" results, continuation token " + iterator.continuation);
console.log("id of first item: "+results[0].id);
if(callback)
callback(iterator.continuation);
}
iterator.executeNext(resultCallback);
}
var querySpec = {
query: 'SELECT * FROM images i ORDER BY i._ts DESC'
};
var options = {
maxItemCount: 1
};
queryDocuments(querySpec, options, function(continuation){
console.log("")
console.log("2nd query:")
options.continuation = continuation;
queryDocuments(querySpec, options);
});
});
var DocDBUtils = {
getOrCreateDatabase: function (client, databaseId, callback) {
var querySpec = {
query: 'SELECT * FROM root r WHERE r.id=@id',
parameters: [{
name: '@id',
value: databaseId
}]
};
client.queryDatabases(querySpec).toArray(function (err, results) {
if (err) {
callback(err);
} else {
if (results.length === 0) {
var databaseSpec = {
id: databaseId
};
client.createDatabase(databaseSpec, function (err, created) {
callback(null, created);
});
} else {
callback(null, results[0]);
}
}
});
},
getOrCreateCollection: function (client, databaseLink, collectionId, callback) {
var querySpec = {
query: 'SELECT * FROM root r WHERE r.id=@id',
parameters: [{
name: '@id',
value: collectionId
}]
};
client.queryCollections(databaseLink, querySpec).toArray(function (err, results) {
if (err) {
callback(err);
} else {
if (results.length === 0) {
var collectionSpec = {
id: collectionId
};
var requestOptions = {
offerType: 'S1'
};
client.createCollection(databaseLink, collectionSpec, requestOptions, function (err, created) {
callback(null, created);
});
} else {
callback(null, results[0]);
}
}
});
}
};
module.exports = DocDBUtils;
{
"name": "documentdb-node",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"documentdb": "^1.5.5"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment