Example #1
0
        /// <summary>
        /// Creates the DocumentCollection if it does not exit otherwise returns the existing one.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="documentCollection">Document collection.</param>
        /// <param name="requestOptions">Request options.</param>
        /// <returns>Created document collection.</returns>
        public async Task <ResourceResponse <DocumentCollection> > GetOrCreateCollectionAsync(
            string dbId, DocumentCollection documentCollection, RequestOptions requestOptions = null)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b1d1 /* tag_961hr */));
            Code.ExpectsArgument(documentCollection, nameof(documentCollection), TaggingUtilities.ReserveTag(0x2381b1d2 /* tag_961hs */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1d3 /* tag_961ht */),
                                                              () => client.CreateDocumentCollectionIfNotExistsAsync(
                                                                  UriFactory.CreateDatabaseUri(dbId),
                                                                  documentCollection,
                                                                  requestOptions)).ConfigureAwait(false));
        }
        /// <summary>
        /// Upserts a document.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="entity">Entity to replace in document db collection.</param>
        /// <param name="requestOptions">Request options</param>
        /// <param name="disableIdGeneration">Disables automatic id generation</param>
        /// <returns>Upserted document.</returns>
        public async Task <ResourceResponse <Document> > UpsertDocumentAsync <T>(
            string dbId, string collectionId, T entity, RequestOptions requestOptions = null, bool disableIdGeneration = true)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b19f /* tag_961g5 */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b1a0 /* tag_961g6 */));
            Code.ExpectsArgument(entity, nameof(entity), TaggingUtilities.ReserveTag(0x2381b1a1 /* tag_961g7 */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1a2 /* tag_961g8 */),
                                                              () => client.UpsertDocumentAsync(
                                                                  UriFactory.CreateDocumentCollectionUri(dbId, collectionId), entity, requestOptions, disableIdGeneration))
                   .ConfigureAwait(false));
        }
        /// <summary>
        /// Deletes a document.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="docId">Document id</param>
        /// <param name="requestOptions">Request options</param>
        /// <returns>Deleted document.</returns>
        public async Task <ResourceResponse <Document> > DeleteDocumentAsync(
            string dbId, string collectionId, string docId, RequestOptions requestOptions = null)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b1c3 /* tag_961hd */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b1c4 /* tag_961he */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(docId, nameof(docId), TaggingUtilities.ReserveTag(0x2381b1c5 /* tag_961hf */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1c6 /* tag_961hg */),
                                                              () => client.DeleteDocumentAsync(
                                                                  UriFactory.CreateDocumentUri(dbId, collectionId, docId), requestOptions))
                   .ConfigureAwait(false));
        }
        /// <summary>
        /// Gets the stored procedure, or creates a new one if one with the specified storedProcedure.id doesn't exist.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="storedProcedure">The stored procedure to get or create.</param>
        /// <param name="deleteStoredProcedure">Indicator to delete the stored procedure before creating it.</param>
        /// <returns>The matched, or created, StoredProcedure object</returns>
        public async Task <StoredProcedure> GetOrCreateStoredProcedureAsync(
            string dbId,
            string collectionId,
            StoredProcedure storedProcedure,
            bool deleteStoredProcedure = false)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b14a /* tag_961fk */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b14b /* tag_961fl */));
            Code.ExpectsArgument(storedProcedure, nameof(storedProcedure), TaggingUtilities.ReserveTag(0x2381b14c /* tag_961fm */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            StoredProcedure sproc  = null;
            var             colUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b14d /* tag_961fn */),
                                                              async() =>
            {
                try
                {
                    if (deleteStoredProcedure)
                    {
                        await DeleteStoredProcedureAsync(dbId, collectionId, storedProcedure.Id).ConfigureAwait(false);
                    }

                    sproc = client.CreateStoredProcedureQuery(colUri)
                            .Where(p => p.Id == storedProcedure.Id).AsEnumerable().FirstOrDefault();

                    if (sproc != null)
                    {
                        return sproc;
                    }

                    sproc = await client.CreateStoredProcedureAsync(colUri, storedProcedure).ConfigureAwait(false);
                    return sproc;
                }
                catch (DocumentClientException ex)
                {
                    if (ex.StatusCode == HttpStatusCode.Conflict)
                    {
                        sproc = client.CreateStoredProcedureQuery(colUri)
                                .Where(p => p.Id == storedProcedure.Id).AsEnumerable().FirstOrDefault();

                        return sproc;
                    }

                    throw;
                }
            }).ConfigureAwait(false));
        }
        /// <summary>
        /// Replaces a document.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="docId">Document id</param>
        /// <param name="entity">Entity to replace in document db collection.</param>
        /// <param name="requestOptions">Request options</param>
        /// <returns>Replaced document.</returns>
        public async Task <ResourceResponse <Document> > ReplaceDocumentAsync <T>(
            string dbId, string collectionId, string docId, T entity, RequestOptions requestOptions = null)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b19a /* tag_961g0 */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b19b /* tag_961g1 */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(docId, nameof(docId), TaggingUtilities.ReserveTag(0x2381b19c /* tag_961g2 */));
            Code.ExpectsArgument(entity, nameof(entity), TaggingUtilities.ReserveTag(0x2381b19d /* tag_961g3 */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b19e /* tag_961g4 */),
                                                              () => client.ReplaceDocumentAsync(
                                                                  UriFactory.CreateDocumentUri(dbId, collectionId, docId), entity, requestOptions))
                   .ConfigureAwait(false));
        }
        /// <summary>
        /// Creates a document.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="document">Object to create.</param>
        /// <param name="requestOptions">Request options</param>
        /// <param name="disableIdGeneration">Disables automatic id generation</param>
        /// <returns>Created document</returns>
        public async Task <ResourceResponse <Document> > CreateDocumentAsync(
            string dbId, string collectionId, object document, RequestOptions requestOptions = null, bool disableIdGeneration = true)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b15e /* tag_961f4 */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b15f /* tag_961f5 */));
            Code.ExpectsArgument(document, nameof(document), TaggingUtilities.ReserveTag(0x2381b160 /* tag_961f6 */));

            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId);

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(
                       0,
                       () => client.CreateDocumentAsync(collectionUri, document, requestOptions, disableIdGeneration))
                   .ConfigureAwait(false));
        }
        /// <summary>
        /// Gets a document and converts to a POCO using specified converter.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="docId">Document id.</param>
        /// <param name="requestOptions">Request options.</param>
        /// <param name="converter">Delegate to convert a document to a POCO object.</param>
        /// <returns>POCO object converted from the retrieved Document.</returns>
        public async Task <T> GetDocumentAsync <T>(
            string dbId, string collectionId, string docId, Func <Document, T> converter, RequestOptions requestOptions = null)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b18c /* tag_961gm */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b18d /* tag_961gn */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(docId, nameof(docId), TaggingUtilities.ReserveTag(0x2381b18e /* tag_961go */));
            Code.ExpectsArgument(converter, nameof(converter), TaggingUtilities.ReserveTag(0x2381b18f /* tag_961gp */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            ResourceResponse <Document> response = await DocumentDbAdapter.ExecuteAndLogAsync(
                0, () => client.ReadDocumentAsync(
                    UriFactory.CreateDocumentUri(dbId, collectionId, docId), requestOptions)).ConfigureAwait(false);

            return(converter(response.Resource));
        }
        /// <summary>
        /// Gets the trigger, or creates a new one if one with the specified trigger.id doesn't exist.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="trigger">The trigger to get or create.</param>
        /// <param name="deleteTrigger">Indicator to delete the trigger before creating it.</param>
        /// <returns>The matched, or created, Trigger object</returns>
        public async Task <Trigger> GetOrCreateTriggerAsync(
            string dbId,
            string collectionId,
            Trigger trigger,
            bool deleteTrigger = false)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b14e /* tag_961fo */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b14f /* tag_961fp */));
            Code.ExpectsArgument(trigger, nameof(trigger), TaggingUtilities.ReserveTag(0x2381b150 /* tag_961fq */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            Uri colUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b151 /* tag_961fr */),
                                                              async() =>
            {
                try
                {
                    if (deleteTrigger)
                    {
                        await DeleteTriggerAsync(dbId, collectionId, trigger.Id).ConfigureAwait(false);
                    }

                    Trigger tr = client.CreateTriggerQuery(colUri).Where(t => t.Id == trigger.Id).AsEnumerable().FirstOrDefault();

                    if (tr != null)
                    {
                        return tr;
                    }

                    return await client.CreateTriggerAsync(colUri, trigger).ConfigureAwait(false);
                }
                catch (DocumentClientException ex)
                {
                    if (ex.StatusCode != HttpStatusCode.Conflict)
                    {
                        throw;
                    }

                    return client.CreateTriggerQuery(colUri)
                    .Where(p => p.Id == trigger.Id).AsEnumerable().FirstOrDefault();
                }
            }));
        }
Example #9
0
        /// <summary>
        /// Gets document client.
        /// </summary>
        /// <param name="config">Document Db settings config, containing information like region, environment, access type etc.</param>
        /// <returns>The IDocumentClient interface.</returns>
        public async Task <IDocumentClient> GetDocumentClientAsync(DocumentDbSettingsConfig config = null)
        {
            DocumentDbSettings settings = await DocumentDbAdapter.ExecuteAndLogAsync(
                0, () => m_DocumentDbSettingsProvider.GetSettingsAsync(config)).ConfigureAwait(false);

            DocumentClient client = new DocumentClient(settings.Endpoint, settings.Key);

            await DocumentDbAdapter.ExecuteAndLogAsync(
                0,
                async() =>
            {
                await client.OpenAsync().ConfigureAwait(false);

                return(true);
            }).ConfigureAwait(false);

            return(client);
        }
        /// <summary>
        /// Registers the provided stored procedures and triggers.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="scriptOptions">The script options object holding references to stored procedures and triggers to register.</param>
        public Task RegisterScriptsAsync(string dbId, string collectionId, ScriptOptions scriptOptions)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b15a /* tag_961f0 */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b15b /* tag_961f1 */));
            Code.ExpectsArgument(scriptOptions, nameof(scriptOptions), TaggingUtilities.ReserveTag(0x2381b15c /* tag_961f2 */));

            return(DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b15d /* tag_961f3 */), () =>
            {
                IEnumerable <Task> tasks = Enumerable.Empty <Task>();
                tasks = tasks.Union(scriptOptions?.Triggers?.Select(t => GetOrCreateTriggerAsync(dbId, collectionId, t, scriptOptions.ResetScripts)) ??
                                    Enumerable.Empty <Task>());

                tasks = tasks.Union(scriptOptions?.StoredProcedures?.Select(s => GetOrCreateStoredProcedureAsync(dbId, collectionId, s, scriptOptions.ResetScripts)) ??
                                    Enumerable.Empty <Task>());

                return Task.WhenAll(tasks);
            }));
        }
        /// <summary>
        /// Deletes the trigger.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="triggerId">The trigger to get or create.</param>
        /// <returns>True if operation is successful, false otherwise</returns>
        public Task DeleteTriggerAsync(string dbId, string collectionId, string triggerId)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b156 /* tag_961fw */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b157 /* tag_961fx */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(triggerId, nameof(triggerId), TaggingUtilities.ReserveTag(0x2381b158 /* tag_961fy */));

            return(DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b159 /* tag_961fz */),
                                                        async() =>
            {
                IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

                try
                {
                    await client.DeleteTriggerAsync(UriFactory.CreateTriggerUri(dbId, collectionId, triggerId))
                    .ConfigureAwait(false);
                }
                catch (DocumentClientException clientEx) when(clientEx.StatusCode == HttpStatusCode.NotFound)
                {
                }
            }));
        }
        /// <summary>
        /// Deletes the stored procedure.
        /// </summary>
        /// <param name="dbId">The id of the Database to search for, or create.</param>
        /// <param name="collectionId">The id of the document db collection.</param>
        /// <param name="storedProcedureId">The stored procedure to get or create.</param>
        /// <returns>True if operation is successful, false otherwise</returns>
        public Task DeleteStoredProcedureAsync(string dbId, string collectionId, string storedProcedureId)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b152 /* tag_961fs */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b153 /* tag_961ft */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(storedProcedureId, nameof(storedProcedureId), TaggingUtilities.ReserveTag(0x2381b154 /* tag_961fu */));

            return(DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b155 /* tag_961fv */),
                                                        async() =>
            {
                try
                {
                    IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

                    await client.DeleteStoredProcedureAsync(
                        UriFactory.CreateStoredProcedureUri(dbId, collectionId, storedProcedureId))
                    .ConfigureAwait(false);
                }
                catch (DocumentClientException clientEx) when(clientEx.StatusCode == HttpStatusCode.NotFound)
                {
                }
            }));
        }
Example #13
0
        /// <summary>
        /// Gets the document collection.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="requestOptions">Request options</param>
        /// <returns>DocumentCollection with specified id.</returns>
        public async Task <ResourceResponse <DocumentCollection> > GetCollectionAsync(
            string dbId, string collectionId, RequestOptions requestOptions = null)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b1d4 /* tag_961hu */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b1d5 /* tag_961hv */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1d6 /* tag_961hw */),
                                                              async() =>
            {
                try
                {
                    return await client.ReadDocumentCollectionAsync(
                        UriFactory.CreateDocumentCollectionUri(dbId, collectionId),
                        requestOptions);
                }
                catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound)
                {
                    return default(ResourceResponse <DocumentCollection>);
                }
            }).ConfigureAwait(false));
        }
        /// <summary>
        /// Gets a document and converts to a POCO type.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="collectionId">Collection id.</param>
        /// <param name="docId">Document id.</param>
        /// <param name="requestOptions">Request options.</param>
        /// <returns>POCO object deserialized by default default json deserialization settings by document db sdk.</returns>
        public async Task <T> GetDocumentAsync <T>(
            string dbId, string collectionId, string docId, RequestOptions requestOptions = null) where T : class
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b188 /* tag_961gi */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(collectionId, nameof(collectionId), TaggingUtilities.ReserveTag(0x2381b189 /* tag_961gj */));
            Code.ExpectsNotNullOrWhiteSpaceArgument(docId, nameof(docId), TaggingUtilities.ReserveTag(0x2381b18a /* tag_961gk */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b18b /* tag_961gl */),
                                                              async() =>
            {
                try
                {
                    return await client.ReadDocumentAsync <T>(UriFactory.CreateDocumentUri(dbId, collectionId, docId), requestOptions)
                    .ConfigureAwait(false);
                }
                catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound)
                {
                    return default(T);
                }
            }).ConfigureAwait(false));
        }
        /// <summary>
        /// Queries the collection using the specified document query.
        /// </summary>
        /// <param name="query">Document query.</param>
        /// <param name="feedOptions">Feed options.</param>
        /// <returns>List of objects of specified type returned from document query.</returns>
        public async Task <IReadOnlyList <T> > QueryDocumentsAsync <T>(
            IDocumentQuery <T> query, FeedOptions feedOptions = null)
        {
            Code.ExpectsArgument(query, nameof(query), TaggingUtilities.ReserveTag(0x2381b199 /* tag_961gz */));
            List <T> data = new List <T>();

            try
            {
                while (query.HasMoreResults)
                {
                    foreach (T t in await DocumentDbAdapter.ExecuteAndLogAsync(
                                 0, () => query.ExecuteNextAsync <T>()).ConfigureAwait(false))
                    {
                        data.Add(t);
                    }
                }
            }
            catch (DocumentClientException e) when(e.StatusCode == HttpStatusCode.NotFound)
            {
                return(new List <T>());
            }

            return(data);
        }
Example #16
0
 /// <summary>
 /// Gets IDocumentClient. The underlying tak is lazy initialized.
 /// Once the task runs to completion, it will return the same
 /// </summary>
 /// <returns>The IDocumentClient interface.</returns>
 public Task <IDocumentClient> GetDocumentClientAsync()
 {
     return(DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1de /* tag_961h4 */), () => m_DocumentClient.Value));
 }