UNPKG

motorq-documentdb

Version:
1,033 lines (909 loc) 134 kB
/* The MIT License (MIT) Copyright (c) 2017 Microsoft Corporation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict"; var Base = require("./base") , https = require("https") , url = require("url") , tunnel = require("tunnel") , AzureDocuments = require("./documents") , QueryIterator = require("./queryIterator") , RequestHandler = require("./request") , RetryOptions = require("./retryOptions") , GlobalEndpointManager = require("./globalEndpointManager") , Constants = require("./constants") , Helper = require("./helper").Helper , util = require("util") , Platform = require("./platform") , SessionContainer = require("./sessionContainer") , StatusCodes = require("./statusCodes").StatusCodes , SubStatusCodes = require("./statusCodes").SubStatusCodes; //SCRIPT START var DocumentClient = Base.defineClass( /** * Provides a client-side logical representation of the Azure Cosmos DB database account. * This client is used to configure and execute requests in the Azure Cosmos DB database service. * @constructor DocumentClient * @param {string} urlConnection - The service endpoint to use to create the client. * @param {object} auth - An object that is used for authenticating requests and must contains one of the options * @param {string} [auth.masterKey] - The authorization master key to use to create the client. * @param {Object} [auth.resourceTokens] - An object that contains resources tokens. Keys for the object are resource Ids and values are the resource tokens. * @param {Array} [auth.permissionFeed] - An array of {@link Permission} objects. * @param {object} [connectionPolicy] - An instance of {@link ConnectionPolicy} class. This parameter is optional and the default connectionPolicy will be used if omitted. * @param {string} [consistencyLevel] - An optional parameter that represents the consistency level. It can take any value from {@link ConsistencyLevel}. */ function DocumentClient(urlConnection, auth, connectionPolicy, consistencyLevel) { this.urlConnection = urlConnection; if (auth !== undefined) { this.masterKey = auth.masterKey; this.resourceTokens = auth.resourceTokens; if (auth.permissionFeed) { this.resourceTokens = {}; for (var i = 0; i < auth.permissionFeed.length; i++) { var resourceId = Helper.getResourceIdFromPath(auth.permissionFeed[i].resource); if (!resourceId) { throw new Error("authorization error: " + resourceId + "is an invalid resourceId in permissionFeed"); } this.resourceTokens[resourceId] = auth.permissionFeed[i]._token; } } } this.connectionPolicy = connectionPolicy || new AzureDocuments.ConnectionPolicy(); this.consistencyLevel = consistencyLevel; this.defaultHeaders = {}; this.defaultHeaders[Constants.HttpHeaders.CacheControl] = "no-cache"; this.defaultHeaders[Constants.HttpHeaders.Version] = Constants.CurrentVersion; if (consistencyLevel !== undefined) { this.defaultHeaders[Constants.HttpHeaders.ConsistencyLevel] = consistencyLevel; } var platformDefaultHeaders = Platform.getPlatformDefaultHeaders() || {}; for (var platformDefaultHeader in platformDefaultHeaders) { this.defaultHeaders[platformDefaultHeader] = platformDefaultHeaders[platformDefaultHeader]; } this.defaultHeaders[Constants.HttpHeaders.UserAgent] = Platform.getUserAgent(); // overide this for default query params to be added to the url. this.defaultUrlParams = ""; // Query compatibility mode. // Allows to specify compatibility mode used by client when making query requests. Should be removed when // application/sql is no longer supported. this.queryCompatibilityMode = AzureDocuments.QueryCompatibilityMode.Default; this.partitionResolvers = {}; this.partitionKeyDefinitionCache = {}; this._globalEndpointManager = new GlobalEndpointManager(this); this.sessionContainer = new SessionContainer(this.urlConnection); // Initialize request agent var requestAgentOptions = { keepAlive: true, maxSockets: 256, maxFreeSockets: 256 }; if (!!this.connectionPolicy.ProxyUrl) { var proxyUrl = url.parse(this.connectionPolicy.ProxyUrl); requestAgentOptions.proxy = { host: proxyUrl.hostname, port: proxyUrl.port }; if (!!proxyUrl.auth) { requestAgentOptions.proxy.proxyAuth = proxyUrl.auth; } this.requestAgent = proxyUrl.protocol.toLowerCase() === "https:" ? tunnel.httpsOverHttps(requestAgentOptions) : tunnel.httpsOverHttp(requestAgentOptions); } else { this.requestAgent = new https.Agent(requestAgentOptions); } }, { /** Gets the curent write endpoint for a geo-replicated database account. * @memberof DocumentClient * @instance * @param {function} callback - The callback function which takes endpoint(string) as an argument. */ getWriteEndpoint: function (callback) { this._globalEndpointManager.getWriteEndpoint(function (writeEndpoint) { callback(writeEndpoint); }); }, /** Gets the curent read endpoint for a geo-replicated database account. * @memberof DocumentClient * @instance * @param {function} callback - The callback function which takes endpoint(string) as an argument. */ getReadEndpoint: function (callback) { this._globalEndpointManager.getReadEndpoint(function (readEndpoint) { callback(readEndpoint); }); }, /** Send a request for creating a database. * <p> * A database manages users, permissions and a set of collections. <br> * Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data. <br> * Each Database consists of one or more collections, each of which in turn contain one or more documents. Since databases are an an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs. <br> * </p> * @memberof DocumentClient * @instance * @param {Object} body - A json object that represents The database to be created. * @param {string} body.id - The id of the database. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createDatabase: function (body, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var err = {}; if (!this.isResourceValid(body, err)) { callback(err); return; } var path = "/dbs"; this.create(body, path, "dbs", undefined, undefined, options, callback); }, /** * Creates a collection. * <p> * A collection is a named logical container for documents. <br> * A database may contain zero or more named collections and each collection consists of zero or more JSON documents. <br> * Being schema-free, the documents in a collection do not need to share the same structure or fields. <br> * Since collections are application resources, they can be authorized using either the master key or resource keys. <br> * </p> * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {object} body - Represents the body of the collection. * @param {string} body.id - The id of the collection. * @param {IndexingPolicy} body.indexingPolicy - The indexing policy associated with the collection. * @param {number} body.defaultTtl - The default time to live in seconds for documents in a collection. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createCollection: function (databaseLink, body, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var err = {}; if (!this.isResourceValid(body, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(databaseLink); var path = this.getPathFromLink(databaseLink, "colls", isNameBased); var id = this.getIdFromLink(databaseLink, isNameBased); this.create(body, path, "colls", id, undefined, options, callback); }, /** * Create a document. * <p> * There is no set schema for JSON documents. They may contain any number of custom properties as well as an optional list of attachments. <br> * A Document is an application resource and can be authorized using the master key or resource keys * </p> * @memberof DocumentClient * @instance * @param {string} documentsFeedOrDatabaseLink - The collection link or database link if using a partition resolver * @param {object} body - Represents the body of the document. Can contain any number of user defined properties. * @param {string} [body.id] - The id of the document, MUST be unique for each document. * @param {number} body.ttl - The time to live in seconds of the document. * @param {RequestOptions} [options] - The request options. * @param {boolean} [options.disableAutomaticIdGeneration] - Disables the automatic id generation. If id is missing in the body and this option is true, an error will be returned. * @param {RequestCallback} callback - The callback for the request. */ createDocument: function (documentsFeedOrDatabaseLink, body, options, callback) { var partitionResolver = this.partitionResolvers[documentsFeedOrDatabaseLink]; var collectionLink; if (partitionResolver === undefined || partitionResolver === null) { collectionLink = documentsFeedOrDatabaseLink; } else { collectionLink = this.resolveCollectionLinkForCreate(partitionResolver, body); } this.createDocumentPrivate(collectionLink, body, options, callback); }, /** * Create an attachment for the document object. * <p> * Each document may contain zero or more attachments. Attachments can be of any MIME type - text, image, binary data. <br> * These are stored externally in Azure Blob storage. Attachments are automatically deleted when the parent document is deleted. * </P> * @memberof DocumentClient * @instance * @param {string} documentLink - The self-link of the document. * @param {Object} body - The metadata the defines the attachment media like media, contentType. It can include any other properties as part of the metedata. * @param {string} body.contentType - The MIME contentType of the attachment. * @param {string} body.media - Media link associated with the attachment content. * @param {RequestOptions} options - The request options. * @param {RequestCallback} callback - The callback for the request. */ createAttachment: function (documentLink, body, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var err = {}; if (!this.isResourceValid(body, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(documentLink); var path = this.getPathFromLink(documentLink, "attachments", isNameBased); var id = this.getIdFromLink(documentLink, isNameBased); this.create(body, path, "attachments", id, undefined, options, callback); }, /** * Create a database user. * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {object} body - Represents the body of the user. * @param {string} body.id - The id of the user. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createUser: function (databaseLink, body, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var err = {}; if (!this.isResourceValid(body, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(databaseLink); var path = this.getPathFromLink(databaseLink, "users", isNameBased); var id = this.getIdFromLink(databaseLink, isNameBased); this.create(body, path, "users", id, undefined, options, callback); }, /** * Create a permission. * <p> A permission represents a per-User Permission to access a specific resource e.g. Document or Collection. </p> * @memberof DocumentClient * @instance * @param {string} userLink - The self-link of the user. * @param {object} body - Represents the body of the permission. * @param {string} body.id - The id of the permission * @param {string} body.permissionMode - The mode of the permission, must be a value of {@link PermissionMode} * @param {string} body.resource - The link of the resource that the permission will be applied to. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createPermission: function (userLink, body, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var err = {}; if (!this.isResourceValid(body, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(userLink); var path = this.getPathFromLink(userLink, "permissions", isNameBased); var id = this.getIdFromLink(userLink, isNameBased); this.create(body, path, "permissions", id, undefined, options, callback); }, /** * Create a trigger. * <p> * Azure Cosmos DB supports pre and post triggers defined in JavaScript to be executed on creates, updates and deletes. <br> * For additional details, refer to the server-side JavaScript API documentation. * </p> * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {object} trigger - Represents the body of the trigger. * @param {string} trigger.id - The id of the trigger. * @param {string} trigger.triggerType - The type of the trigger, should be one of the values of {@link TriggerType}. * @param {string} trigger.triggerOperation - The trigger operation, should be one of the values of {@link TriggerOperation}. * @param {function} trigger.serverScript - The body of the trigger, it can be passed as stringified too. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createTrigger: function (collectionLink, trigger, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; if (trigger.serverScript) { trigger.body = trigger.serverScript.toString(); } else if (trigger.body) { trigger.body = trigger.body.toString(); } var err = {}; if (!this.isResourceValid(trigger, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(collectionLink); var path = this.getPathFromLink(collectionLink, "triggers", isNameBased); var id = this.getIdFromLink(collectionLink, isNameBased); this.create(trigger, path, "triggers", id, undefined, options, callback); }, /** * Create a UserDefinedFunction. * <p> * Azure Cosmos DB supports JavaScript UDFs which can be used inside queries, stored procedures and triggers. <br> * For additional details, refer to the server-side JavaScript API documentation. * </p> * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {object} udf - Represents the body of the userDefinedFunction. * @param {string} udf.id - The id of the udf. * @param {string} udf.userDefinedFunctionType - The type of the udf, it should be one of the values of {@link UserDefinedFunctionType} * @param {function} udf.serverScript - Represents the body of the udf, it can be passed as stringified too. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createUserDefinedFunction: function (collectionLink, udf, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; if (udf.serverScript) { udf.body = udf.serverScript.toString(); } else if (udf.body) { udf.body = udf.body.toString(); } var err = {}; if (!this.isResourceValid(udf, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(collectionLink); var path = this.getPathFromLink(collectionLink, "udfs", isNameBased); var id = this.getIdFromLink(collectionLink, isNameBased); this.create(udf, path, "udfs", id, undefined, options, callback); }, /** * Create a StoredProcedure. * <p> * Azure Cosmos DB allows stored procedures to be executed in the storage tier, directly against a document collection. The script <br> * gets executed under ACID transactions on the primary storage partition of the specified collection. For additional details, <br> * refer to the server-side JavaScript API documentation. * </p> * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {object} sproc - Represents the body of the stored procedure. * @param {string} sproc.id - The id of the stored procedure. * @param {function} sproc.serverScript - The body of the stored procedure, it can be passed as stringified too. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createStoredProcedure: function (collectionLink, sproc, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; if (sproc.serverScript) { sproc.body = sproc.serverScript.toString(); } else if (sproc.body) { sproc.body = sproc.body.toString(); } var err = {}; if (!this.isResourceValid(sproc, err)) { callback(err); return; } var isNameBased = Base.isLinkNameBased(collectionLink); var path = this.getPathFromLink(collectionLink, "sprocs", isNameBased); var id = this.getIdFromLink(collectionLink, isNameBased); this.create(sproc, path, "sprocs", id, undefined, options, callback); }, /** * Create an attachment for the document object. * @memberof DocumentClient * @instance * @param {string} documentLink - The self-link of the document. * @param {stream.Readable} readableStream - the stream that represents the media itself that needs to be uploaded. * @param {MediaOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ createAttachmentAndUploadMedia: function (documentLink, readableStream, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var initialHeaders = Base.extend({}, this.defaultHeaders); initialHeaders = Base.extend(initialHeaders, options && options.initialHeaders); // Add required headers slug and content-type. if (options.slug) { initialHeaders[Constants.HttpHeaders.Slug] = options.slug; } if (options.contentType) { initialHeaders[Constants.HttpHeaders.ContentType] = options.contentType; } else { initialHeaders[Constants.HttpHeaders.ContentType] = Constants.MediaTypes.OctetStream; } var isNameBased = Base.isLinkNameBased(documentLink); var path = this.getPathFromLink(documentLink, "attachments", isNameBased); var id = this.getIdFromLink(documentLink, isNameBased); this.create(readableStream, path, "attachments", id, initialHeaders, options, callback); }, /** Reads a database. * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readDatabase: function (databaseLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(databaseLink); var path = this.getPathFromLink(databaseLink, "", isNameBased); var id = this.getIdFromLink(databaseLink, isNameBased); this.read(path, "dbs", id, undefined, options, callback); }, /** * Reads a collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readCollection: function (collectionLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(collectionLink); var path = this.getPathFromLink(collectionLink, "", isNameBased); var id = this.getIdFromLink(collectionLink, isNameBased); var that = this; this.read(path, "colls", id, undefined, options, function (err, collection, headers) { if (err) return callback(err, collection, headers); that.partitionKeyDefinitionCache[collectionLink] = collection.partitionKey; callback(err, collection, headers); }); }, /** * Reads a document. * @memberof DocumentClient * @instance * @param {string} documentLink - The self-link of the document. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readDocument: function (documentLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(documentLink); var path = this.getPathFromLink(documentLink, "", isNameBased); var id = this.getIdFromLink(documentLink, isNameBased); this.read(path, "docs", id, undefined, options, callback); }, /** * Reads an Attachment object. * @memberof DocumentClient * @instance * @param {string} attachmentLink - The self-link of the attachment. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readAttachment: function (attachmentLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(attachmentLink); var path = this.getPathFromLink(attachmentLink, "", isNameBased); var id = this.getIdFromLink(attachmentLink, isNameBased); this.read(path, "attachments", id, undefined, options, callback); }, /** * Reads a user. * @memberof DocumentClient * @instance * @param {string} userLink - The self-link of the user. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readUser: function (userLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(userLink); var path = this.getPathFromLink(userLink, "", isNameBased); var id = this.getIdFromLink(userLink, isNameBased); this.read(path, "users", id, undefined, options, callback); }, /** * Reads a permission. * @memberof DocumentClient * @instance * @param {string} permissionLink - The self-link of the permission. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readPermission: function (permissionLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(permissionLink); var path = this.getPathFromLink(permissionLink, "", isNameBased); var id = this.getIdFromLink(permissionLink, isNameBased); this.read(path, "permissions", id, undefined, options, callback); }, /** * Reads a trigger object. * @memberof DocumentClient * @instance * @param {string} triggerLink - The self-link of the trigger. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readTrigger: function (triggerLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var resourceInfo = Base.parseLink(triggerLink); var isNameBased = Base.isLinkNameBased(triggerLink); var path = this.getPathFromLink(triggerLink, "", isNameBased); var id = this.getIdFromLink(triggerLink, isNameBased); this.read(path, "triggers", id, undefined, options, callback); }, /** * Reads a udf object. * @memberof DocumentClient * @instance * @param {string} udfLink - The self-link of the user defined function. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readUserDefinedFunction: function (udfLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(udfLink); var path = this.getPathFromLink(udfLink, "", isNameBased); var id = this.getIdFromLink(udfLink, isNameBased); this.read(path, "udfs", id, undefined, options, callback); }, /** * Reads a StoredProcedure object. * @memberof DocumentClient * @instance * @param {string} sprocLink - The self-link of the stored procedure. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readStoredProcedure: function (sprocLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(sprocLink); var path = this.getPathFromLink(sprocLink, "", isNameBased); var id = this.getIdFromLink(sprocLink, isNameBased); this.read(path, "sprocs", id, undefined, options, callback); }, /** * Reads a conflict. * @memberof DocumentClient * @instance * @param {string} conflictLink - The self-link of the conflict. * @param {RequestOptions} [options] - The request options. * @param {RequestCallback} callback - The callback for the request. */ readConflict: function (conflictLink, options, callback) { var optionsCallbackTuple = this.validateOptionsAndCallback(options, callback); options = optionsCallbackTuple.options; callback = optionsCallbackTuple.callback; var isNameBased = Base.isLinkNameBased(conflictLink); var path = this.getPathFromLink(conflictLink, "", isNameBased); var id = this.getIdFromLink(conflictLink, isNameBased); this.read(path, "conflicts", id, undefined, options, callback); }, /** Lists all databases. * @memberof DocumentClient * @instance * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readDatabases: function (options) { return this.queryDatabases(undefined, options); }, /** * Get all collections in this database. * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readCollections: function (databaseLink, options) { return this.queryCollections(databaseLink, undefined, options); }, /** * Get all documents in this collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readDocuments: function (collectionLink, options) { return this.queryDocuments(collectionLink, undefined, options); }, /** * Get all Partition key Ranges in this collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. * @ignore */ readPartitionKeyRanges: function (collectionLink, options) { return this.queryPartitionKeyRanges(collectionLink, undefined, options); }, /** * Get all attachments for this document. * @memberof DocumentClient * @instance * @param {string} documentLink - The self-link of the document. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readAttachments: function (documentLink, options) { return this.queryAttachments(documentLink, undefined, options); }, /** * Get all users in this database. * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {FeedOptions} [feedOptions] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readUsers: function (databaseLink, options) { return this.queryUsers(databaseLink, undefined, options); }, /** * Get all permissions for this user. * @memberof DocumentClient * @instance * @param {string} userLink - The self-link of the user. * @param {FeedOptions} [feedOptions] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readPermissions: function (userLink, options) { return this.queryPermissions(userLink, undefined, options); }, /** * Get all triggers in this collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readTriggers: function (collectionLink, options) { return this.queryTriggers(collectionLink, undefined, options); }, /** * Get all UserDefinedFunctions in this collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readUserDefinedFunctions: function (collectionLink, options) { return this.queryUserDefinedFunctions(collectionLink, undefined, options); }, /** * Get all StoredProcedures in this collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ readStoredProcedures: function (collectionLink, options) { return this.queryStoredProcedures(collectionLink, undefined, options); }, /** * Get all conflicts in this collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of QueryIterator to handle reading feed. */ readConflicts: function (collectionLink, options) { return this.queryConflicts(collectionLink, undefined, options); }, /** Lists all databases that satisfy a query. * @memberof DocumentClient * @instance * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - The feed options. * @returns {QueryIterator} - An instance of QueryIterator to handle reading feed. */ queryDatabases: function (query, options) { var that = this; return new QueryIterator(this, query, options, function (options, callback) { that.queryFeed.call(that, that, "/dbs", "dbs", "", function (result) { return result.Databases; }, function (parent, body) { return body; }, query, options, callback); }); }, /** * Query the collections for the database. * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ queryCollections: function (databaseLink, query, options) { var that = this; var isNameBased = Base.isLinkNameBased(databaseLink); var path = this.getPathFromLink(databaseLink, "colls", isNameBased); var id = this.getIdFromLink(databaseLink, isNameBased); return new QueryIterator(this, query, options, function (options, callback) { that.queryFeed.call(that, that, path, "colls", id, function (result) { return result.DocumentCollections; }, function (parent, body) { return body; }, query, options, callback); }); }, /** * Query the documents for the collection. * @memberof DocumentClient * @instance * @param {string} documentsFeedOrDatabaseLink - The collection link or database link if using a partition resolver * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @param {object} [options.partitionKey] - Optional partition key to be used with the partition resolver * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ queryDocuments: function (documentsFeedOrDatabaseLink, query, options) { var partitionResolver = this.partitionResolvers[documentsFeedOrDatabaseLink]; var collectionLinks; if (partitionResolver === undefined || partitionResolver === null) { collectionLinks = [documentsFeedOrDatabaseLink]; } else { collectionLinks = partitionResolver.resolveForRead(options && options.partitionKey); } return this.queryDocumentsPrivate(collectionLinks, query, options); }, /** * Query the partition key ranges * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. * @ignore */ queryPartitionKeyRanges: function (collectionLink, query, options) { var that = this; var isNameBased = Base.isLinkNameBased(collectionLink); var path = this.getPathFromLink(collectionLink, "pkranges", isNameBased); var id = this.getIdFromLink(collectionLink, isNameBased); return new QueryIterator(this, query, options, function (options, callback) { that.queryFeed.call(that, that, path, "pkranges", id, function (result) { return result.PartitionKeyRanges; }, function (parent, body) { return body; }, query, options, callback); }); }, /** * Query the attachments for the document. * @memberof DocumentClient * @instance * @param {string} documentLink - The self-link of the document. * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ queryAttachments: function (documentLink, query, options) { var that = this; var isNameBased = Base.isLinkNameBased(documentLink); var path = this.getPathFromLink(documentLink, "attachments", isNameBased); var id = this.getIdFromLink(documentLink, isNameBased); return new QueryIterator(this, query, options, function (options, callback) { that.queryFeed.call(that, that, path, "attachments", id, function (result) { return result.Attachments; }, function (parent, body) { return body; }, query, options, callback); }); }, /** * Query the users for the database. * @memberof DocumentClient * @instance * @param {string} databaseLink - The self-link of the database. * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ queryUsers: function (databaseLink, query, options) { var that = this; var isNameBased = Base.isLinkNameBased(databaseLink); var path = this.getPathFromLink(databaseLink, "users", isNameBased); var id = this.getIdFromLink(databaseLink, isNameBased); return new QueryIterator(this, query, options, function (options, callback) { that.queryFeed.call(that, that, path, "users", id, function (result) { return result.Users; }, function (parent, body) { return body; }, query, options, callback); }); }, /** * Query the permission for the user. * @memberof DocumentClient * @instance * @param {string} userLink - The self-link of the user. * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ queryPermissions: function (userLink, query, options) { var that = this; var isNameBased = Base.isLinkNameBased(userLink); var path = this.getPathFromLink(userLink, "permissions", isNameBased); var id = this.getIdFromLink(userLink, isNameBased); return new QueryIterator(this, query, options, function (options, callback) { that.queryFeed.call(that, that, path, "permissions", id, function (result) { return result.Permissions; }, function (parent, body) { return body; }, query, options, callback); }); }, /** * Query the triggers for the collection. * @memberof DocumentClient * @instance * @param {string} collectionLink - The self-link of the collection. * @param {SqlQuerySpec | string} query - A SQL query. * @param {FeedOptions} [options] - Represents the feed options. * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. */ queryTrig