this.validateCredential = function(credential, next) {
   log.debug(credential);
   
   var compareVersion = require('compare-version');
   if (compareVersion('5.4.0', process.version.slice(1)) >= 0) {
     // https://github.com/Azure/azure-documentdb-node/issues/102
     log.warn('Node version < 5.4.0 can trigger a known issue. Skip this test case.');
     return next(statusCode.PASS);
   }
   
   try {
     var collectionName = 'docdbcol' + Math.floor(Math.random()*1000);
     var collectionDefinition = { id: collectionName };
     var documentdbclient = new documentdb.DocumentClient(credential['documentdb_host_endpoint'], {masterKey: credential['documentdb_master_key']});
     documentdbclient.createCollection(credential['documentdb_database_link'], collectionDefinition, function(error) {
       if (error) {
         next(statusCode.FAIL);
       } else {
         next(statusCode.PASS);  
       }
     });
   } catch (ex) {
     log.error('Got exception: ' + ex);
     next(statusCode.FAIL);
   }
 };
Пример #2
0
 .then(function(doc){
     client.replaceDocument(doc._self, doc, function (err, replacedDoc) {
         if(err)
             deferred.reject(err);
         deferred.resolve(replacedDoc);
     });
 })
Пример #3
0
                        waitForIndexTransformToComplete(collLink, function (err) {
                            console.log('Index transform completed');
                            
                            var querySpec = {
                                query: 'SELECT * FROM root r WHERE r.stringField > @value',
                                parameters: [
                                    {
                                        name: '@value',
                                        value: 'a'
                                    }
                                ]
                            };

                            //queryDocuments doing a range operation on a string (this would've failed without the transform)
                            client.queryDocuments(collLink, querySpec).toArray(function (err, results) {
                                if (err) {
                                    handleError(err);

                                } else if (results.length == 0) {
                                    callback(new Error('Should\'ve found a doc'));

                                } else {
                                    var doc = results[0];
                                    console.log('Document with id \'' + doc.id + '\' found');
                                    
                                    callback();
                                }
                            });
                        })
Пример #4
0
function findDatabaseById(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) {
            handleError(err);
        }
        
        if (results.length === 0) {
            // no error occured, but there were no results returned 
            // indicating no database exists matching the query            
            // so, explictly return null
            callback(null, null);
        } else {
            // we found a database, so return it
            callback(null, results[0]);
        }
    });
};
Пример #5
0
                                                client.replaceDocument(docLink, doc, function (err, updated) {
                                                    //now let's try another update to doc with accessCondition and etag set
                                                    doc.foo = 'should never get set';
                                                    client.replaceDocument(docLink, doc, { accessCondition : { type: 'IfMatch', condition: doc._etag } }, function (err, updated) {
                                                        if (!err) {
                                                            throw ('That was not meant to succeed. Something went wrong.');
                                                        } else if (err.code == 412) {
                                                            console.log('As expected, the replace document failed with a pre-condition failure');
                                                        }

                                                        //6.
                                                        console.log('\n6. deleteDocument \'' + docLink + '\'');
                                                        client.deleteDocument(docLink, function (err) {
                                                            if (err) {
                                                                handleError(err);
                                                            } else {
                                                                console.log('Document deleted');
                                                                
                                                                //cleanup & end
                                                                console.log('\nCleaning up ...');
                                                                finish();
                                                            }
                                                        });
                                                    });
                                                });
Пример #6
0
        return new Promise((resolve, reject) => {
            const querySpec = {
                query: 'SELECT * FROM root r WHERE r.id=@id',
                parameters: [{
                    name: '@id',
                    value: collectionId
                }]
            };             

            client.queryCollections(databaseLink, querySpec).toArray((err, results) => {
                if (err) {
                    reject(err);
                } else {        
                    if (results.length === 0) {
                        const collectionSpec = {
                            id: collectionId
                        };
                        const requestOptions = {
                            offerType: 'S1'
                        };

                        client.createCollection(databaseLink, collectionSpec, requestOptions, (err, created) => {
                            resolve(created);
                        });

                    } else {
                        resolve(results[0]);
                    }
                }
            });
        });
Пример #7
0
                client.queryDocuments(collLink, querySpec).toArray(function (err, results) {
                    if (err) {
                        callback(err);
                    } 

                    else if (results != 0) {
                        callback(new Error('there were not meant to be results'));

                    } else {
                        console.log('No results found');
                        
                        console.log('readDocument should still find the doc');
                        client.readDocument(collLink + '/docs/doc', function (err, doc) {
                            if (err) {
                                callback(err);
                        
                            } else {
                                console.log('readDocument found doc and its _self is \'' + doc._self + '\'');

                                deleteCollection(collLink, function (result) {
                                    console.log('Collection \'' +  collId + '\' deleted');
                                    callback();
                                });                                                            
                            }
                        });
                    }
                });
Пример #8
0
function deleteDatabase(dbLink) {
    client.deleteDatabase(dbLink, function (err) {
        if (err) {
            handleError(err);
        }
    });
}
Пример #9
0
        return new Promise((resolve, reject) => {
            const querySpec = {
                query: 'SELECT * FROM root r WHERE r.id= @id',
                parameters: [{
                    name: '@id',
                    value: databaseId
                }]
            };
            
            client.queryDatabases(querySpec).toArray((err, results) => {
                if (err) {
                    reject(err);
                } else {
                    if (results.length === 0) {
                        const databaseSpec = {
                            id: databaseId
                        };

                        client.createDatabase(databaseSpec, (err, created) => resolve(created));
                    } else {
                        resolve(results[0]);
                    }
                }
            });
        });
Пример #10
0
function readDatabase(database, callback) {
    client.readDatabase(database._self, function (err, db) {
        if (err) {
            handleError(err);
        }

        callback(db);
    });
}
Пример #11
0
function listDatabases(callback) {
    var queryIterator = client.readDatabases().toArray(function (err, dbs) {
        if (err) {
            handleError(err);
        }

        callback(dbs);
    });
}
Пример #12
0
function getCollectionLength(path, cb) {
  client.readDocuments(path).toArray(function(err, results) {
    if (err) {
      console.log(err);
    } else {
      cb(results.length);
    }
  }); 
}
Пример #13
0
 replace: function (docLink, doc, callback) {
     client.replaceDocument(docLink, doc, function (err, result) {
         if (err) {
             callback(err);
         } else {
             callback(null, result);
         }
     });
 },
Пример #14
0
var listItems = function (collection, callback) {
    client.queryDocuments(collection._self+'0', 'SELECT * FROM root r').toArray(function (err, docs) {
        if (err) {
            console.log(err)
        }

        callback(docs);
    });
}
Пример #15
0
var listLinks = function (userId, categoryId, collection, callback) {
    client.queryDocuments(collection._self, 'SELECT r.links FROM '+userId+' r WHERE r.id="' + categoryId + '"').toArray(function (err, docs) {
        if (err) {
          console.log(err);
        } else {
          callback(docs);
        }
    });
}
Пример #16
0
var listItems = function (userId, collection, callback) {
    client.queryDocuments(collection._self, 'SELECT r.id, r.name,r.icon,r.color, r.links FROM '+userId+ ' r').toArray(function (err, docs) {
        if (err) {
          console.log(err);
        } else {
          callback(docs);
        }
    });
}
Пример #17
0
 del: function (docLink, callback) {
     client.deleteDocument(docLink, function (err, result) {
         if (err) {
             callback(err);
         } else {
             callback(null, result);
         }
     });
 }
Пример #18
0
 fetch: function (query, callback) {        
     client.queryDocuments(collectionLink, query).toArray(function (err, docs) {
         if (err) {
             callback(err);
         } else {
             callback(null, docs);
         }
     })
 },
Пример #19
0
function putDocument(path, index, data, cb) {
  client.createDocument(path, { id: index, content: data }, function(err, doc) {
    if (err) { 
      console.log(err); 
    } else {
      cb(doc);
    }
  }); 
}
Пример #20
0
 create: function(doc, callback) {
     client.createDocument(collectionLink, doc, function(err, created) {
         if (err) {
             callback(err);
         } else {
             callback(null, created.id);
         }
     })
 },
Пример #21
0
function deleteCollection(collLink, callback){
    client.deleteCollection(collLink, function (err, result) {
        if (err) {
            handleError(err);
        } else {
            callback(result);
        }
    });
}
Пример #22
0
        client.createDocument(collLink, { id: "doc", stringField: "a string value" }, function (err, doc) {
            if (err) {
                handleError(err)

            } else {
                console.log('Document created');
                
                //try a range query on the document, expect an error            
                var querySpec = {
                    query: 'SELECT * FROM root r WHERE r.stringField > @value',
                    parameters: [
                        {
                            name: '@value',
                            value: 'a'
                        }
                    ]
                };
                
                console.log('Querying for document where stringField > \'a\', should fail');
                client.queryDocuments(collLink, querySpec).toArray(function (err, results) {
                    if (!err) {
                        callback(err);
                
                    } else {
                        console.log('Query failed with ' + err.code);
                        
                        //try same range query again, this time specifying the directive to do a scan, 
                        //be wary of high RU cost that you could get for even a single document!
                        //we won't particularly see a high charge this time because there is only 1 doc in the collection
                        //so a scan on 1 document isn't costly. a few thousand documents will be very different
                        console.log('Repeating query for document where stringField > \'a\', this time with enableScanInQuery: true');
                        
                        //notice how we're switching to queryIterator.executeNext instead of calling .toArray() as before
                        //reason being, toArray will issue multiple requests to the server until it has fetched all results
                        //here we can control this using executeNext.
                        //now we can get the headers for each request which includes the charge, continuation tokens etc. 

                        var queryIterator = client.queryDocuments(collLink, querySpec, {enableScanInQuery: true} );
                        queryIterator.executeNext(function (err, docs, headers) {
                            if (err) {
                                handleError(err);
                            } else {
                                var charge = headers['x-ms-request-charge'];
                                var doc = docs[0];
                                
                                console.log('Document \'' + doc.id + '\' found, request charge: ' + charge);
                                
                                deleteCollection(collLink, function (result) {
                                    console.log('Collection \'' + collId + '\' deleted');
                                    callback();
                                });
                            }
                        });
                    }
                });
            }
        });       
Пример #23
0
function readDatabaseById(databaseId, callback) {
    client.readDatabase('dbs/' + databaseId, function (err, db) {
        if (err) {
            handleError(err);
        }
        
        callback(db);
    });
}
Пример #24
0
    createCollection(dbLink, collId, null, function (coll) {
        var collLink = dbLink + '/colls/' + coll.id;        
        var docSpec = { id : 'doc', foo : "bar" };
        
        console.log('Create document, but exclude from index')
        
        //createDocument takes RequestOptions as 3rd parameter. 
        //One of these options is indexingDirectives which can be include, or exclude
        //we're using exclude this time to manually exclude this document from being indexed
        client.createDocument(collLink, docSpec, { indexingDirective: 'exclude' }, function (err, document) {
            if (err) {
                handleError(err)

            } else {
                console.log('Document with id \'' + document.id + '\' created');

                var querySpec = {
                    query: 'SELECT * FROM root r WHERE r.foo=@foo',
                    parameters: [
                        {
                            name: '@foo',
                            value: "bar"
                        }
                    ]
                };

                console.log('queryDocuments for doc should not find any results');
                client.queryDocuments(collLink, querySpec).toArray(function (err, results) {
                    if (err) {
                        callback(err);
                    } 

                    else if (results != 0) {
                        callback(new Error('there were not meant to be results'));

                    } else {
                        console.log('No results found');
                        
                        console.log('readDocument should still find the doc');
                        client.readDocument(collLink + '/docs/doc', function (err, doc) {
                            if (err) {
                                callback(err);
                        
                            } else {
                                console.log('readDocument found doc and its _self is \'' + doc._self + '\'');

                                deleteCollection(collLink, function (result) {
                                    console.log('Collection \'' +  collId + '\' deleted');
                                    callback();
                                });                                                            
                            }
                        });
                    }
                });
            }
        });
    })
Пример #25
0
var readDocuments = function (userId, collectionId, collection, callback) {

    client.queryDocuments(collection._self, 'SELECT * FROM '+userId+' r WHERE r.id="'+collectionId + '"').toArray(function (err, docs) {
    if (err) {
      console.log(err);
    } else {
      callback(docs[0]);
    }
  });
};
Пример #26
0
 var getchmparametersforamdata = function(request,collection,callback){ 
  var query ='SELECT r.amdata from root r where r.type="'+request.params.type+'" and r.uid="'+request.params.uid+'"';   
    client.queryDocuments(collection._self,query).toArray(function (err, docs) {
        if (err) {
            throw (err);
        }     
          
        callback(docs);
    });
}
Пример #27
0
var createItem = function (collection, documentDefinition, callback) {
        //documentDefinition.completed = false;
    client.createDocument(collection._self, documentDefinition, function (err, doc) {
        if (err) {
            throw (err);
        }
        
        callback();
    });
}
Пример #28
0
  var getpulseCurrentQtr = function(request,collection,callback){ 
     var query= 'SELECT r.Pulse from root r where r.type="customersatisfaction" and r.uid="'+request.params.uid+'" and r.quarter="currentQtr"';
     client.queryDocuments(collection._self,query).toArray(function (err, docs) {
        if (err) {
            throw (err);
        }     
          
        callback(docs);
    });
 }
Пример #29
0
 var getmanagertiles = function(request,collection,callback){ 
  
  var query ='SELECT r.tiles from root r where r.type="tiles" and r.screen="'+request.params.screennum+'" and r.uid="'+request.params.uid+'"';   
    client.queryDocuments(collection._self,query).toArray(function (err, docs) {
        if (err) {
            throw (err);
        }     
          
        callback(docs);
    });
}
Пример #30
0
 var getlocations = function(request,collection,callback){ 
  
  var query ='SELECT r.locations from root r where r.type="locations" and r.uid="'+request.params.uid+'"';   
    client.queryDocuments(collection._self,query).toArray(function (err, docs) {
        if (err) {
            throw (err);
        }     
          
        callback(docs);
    });
}