/// <summary> /// Registers the stored procedures, triggers and UDFs in the collection spec/template. /// </summary> /// <param name="client">The DocumentDB client.</param> /// <param name="collectionInfo">The collection spec/template.</param> /// <param name="collection">The collection.</param> /// <returns>The Task object for asynchronous execution.</returns> public static async Task RegisterScripts(DocumentClient client, DocumentCollectionInfo collectionInfo, DocumentCollection collection) { if (collectionInfo.StoredProcedures != null) { foreach (StoredProcedure sproc in collectionInfo.StoredProcedures) { await client.CreateStoredProcedureAsync(collection.SelfLink, sproc); } } if (collectionInfo.Triggers != null) { foreach (Trigger trigger in collectionInfo.Triggers) { await client.CreateTriggerAsync(collection.SelfLink, trigger); } } if (collectionInfo.UserDefinedFunctions != null) { foreach (UserDefinedFunction udf in collectionInfo.UserDefinedFunctions) { await client.CreateUserDefinedFunctionAsync(collection.SelfLink, udf); } } }
/// <summary> /// Creates a new collection. /// </summary> /// <param name="client">The DocumentDB client instance.</param> /// <param name="database">The Database where this DocumentCollection exists / will be created</param> /// <param name="collectionId">The id of the DocumentCollection to search for, or create.</param> /// <param name="collectionInfo">The spec/template to create collections from.</param> /// <returns>The created DocumentCollection object</returns> public static async Task <DocumentCollection> CreateNewCollection( DocumentClient client, Database database, string collectionId, DocumentCollectionInfo collectionInfo) { DocumentCollection collectionDefinition = new DocumentCollection { Id = collectionId }; if (collectionInfo != null) { CopyIndexingPolicy(collectionInfo, collectionDefinition); } DocumentCollection collection = await CreateDocumentCollectionWithRetriesAsync( client, database, collectionDefinition, (collectionInfo != null)?collectionInfo.OfferType : null); if (collectionInfo != null) { await RegisterScripts(client, collectionInfo, collection); } return(collection); }
/// <summary> /// Get a DocumentCollection by id, or create a new one if one with the id provided doesn't exist. /// </summary> /// <param name="client">The DocumentDB client instance.</param> /// <param name="database">The Database where this DocumentCollection exists / will be created</param> /// <param name="collectionId">The id of the DocumentCollection to search for, or create.</param> /// <param name="collectionInfo">The spec/template to create collections from.</param> /// <returns>The matched, or created, DocumentCollection object</returns> public static async Task <DocumentCollection> GetCollectionAsync( DocumentClient client, Database database, string collectionId, DocumentCollectionInfo collectionInfo) { DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink) .Where(c => c.Id == collectionId).ToArray().FirstOrDefault(); if (collection == null) { collection = await CreateNewCollection(client, database, collectionId, collectionInfo); } return(collection); }
/// <summary> /// Copies the indexing policy from the collection spec. /// </summary> /// <param name="collectionInfo">The collection spec/template</param> /// <param name="collectionDefinition">The collection definition to create.</param> public static void CopyIndexingPolicy(DocumentCollectionInfo collectionInfo, DocumentCollection collectionDefinition) { if (collectionInfo.IndexingPolicy != null) { collectionDefinition.IndexingPolicy.Automatic = collectionInfo.IndexingPolicy.Automatic; collectionDefinition.IndexingPolicy.IndexingMode = collectionInfo.IndexingPolicy.IndexingMode; if (collectionInfo.IndexingPolicy.IncludedPaths != null) { foreach (IndexingPath path in collectionInfo.IndexingPolicy.IncludedPaths) { collectionDefinition.IndexingPolicy.IncludedPaths.Add(path); } } if (collectionInfo.IndexingPolicy.ExcludedPaths != null) { foreach (string path in collectionInfo.IndexingPolicy.ExcludedPaths) { collectionDefinition.IndexingPolicy.ExcludedPaths.Add(path); } } } }