Example #1
0
        protected override async Task <IDataSinkAdapter> CreateAsync(DocumentDbClient client, IDataItemTransformation transformation,
                                                                     IDocumentDbBulkSinkAdapterConfiguration configuration, IEnumerable <string> collectionNames)
        {
            var sink = new DocumentDbBulkSinkAdapterDispatcher(client, transformation, GetInstanceConfiguration(configuration, collectionNames));
            await sink.InitializeAsync();

            return(sink);
        }
Example #2
0
        public async Task CreateIndex(IncludedPath index)
        {
            var collection = new DocumentCollection {
                Id = _configurationService.DocumentDb.Collection
            };

            collection.IndexingPolicy.IncludedPaths.Add(index);
            await DocumentDbClient.ReplaceDocumentCollectionAsync(collection);
        }
Example #3
0
        public async Task SetIndexMode(IndexingMode mode)
        {
            var collection = new DocumentCollection {
                Id = _configurationService.DocumentDb.Collection
            };

            collection.IndexingPolicy.IndexingMode = mode;
            await DocumentDbClient.ReplaceDocumentCollectionAsync(collection);
        }
Example #4
0
        public Document GetDocument(Expression <Func <Document, bool> > predicate)
        {
            Document doc = DocumentDbClient.CreateDocumentQuery <Document>(CollectionUri)
                           .Where(predicate)
                           .AsEnumerable()
                           .SingleOrDefault();

            return(doc);
        }
Example #5
0
        //public async Task CreateCollection(bool checkIfExists)
        //{
        //    await CreateCollection(_configurationService.DocumentDb.Collection, checkIfExists);
        //}



        //private Database Database
        //    =>
        //        _database ??
        //        (_database = DocumentDbClient.CreateDatabaseQuery().Where(d => d.Id == _configurationService.DocumentDb.Database).AsEnumerable().FirstOrDefault());

        //public async Task CreateCollection(string collectionName, bool checkIfExists)
        //{
        //    var collectionDefinition = new DocumentCollection
        //    {
        //        Id = collectionName,
        //        IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 })
        //    };

        //    if (checkIfExists)
        //    {
        //        DocumentCollection dbCollection =
        //            DocumentDbClient.CreateDocumentCollectionQuery(Database.SelfLink)
        //                .Where(c => c.Id == collectionName)
        //                .AsEnumerable()
        //                .FirstOrDefault();

        //        if (dbCollection == null)
        //        {
        //            await DocumentDbClient.CreateDocumentCollectionAsync(Database.SelfLink, collectionDefinition,
        //                    new RequestOptions());
        //        }
        //    }
        //    else
        //    {
        //        await DocumentDbClient.CreateDocumentCollectionAsync(Database.SelfLink, collectionDefinition, new RequestOptions());
        //    }
        //}

        public async Task UpsertDocument <T>(T document)
        {
            //var doc = JsonConvert.SerializeObject(document,
            //    new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()});
            //JObject.Parse(doc)

            ResourceResponse <Document> result = await DocumentDbClient.UpsertDocumentAsync(
                CollectionUri,
                document);
        }
Example #6
0
        public async Task <ResourceResponse <StoredProcedure> > GetStoredProcedure(string id)
        {
            StoredProcedure dbSp =
                DocumentDbClient.CreateStoredProcedureQuery(CollectionUri)
                .Where(c => c.Id == id)
                .AsEnumerable()
                .FirstOrDefault();

            return(await DocumentDbClient.ReadStoredProcedureAsync(dbSp.SelfLink));
        }
Example #7
0
 private async Task EnsureDatabaseExistsAsync()
 {
     try
     {
         await DocumentDbClient.CreateDatabaseIfNotExistsAsync(new Database { Id = _settings.DatabaseId });
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #8
0
        /// <summary>
        /// Execute stored procedure in this collection
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="procId"></param>
        /// <param name="partitionKey"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public async Task <TResult> ExecuteStoredProcedureAsync <TResult>(string procId, object partitionKey, object args)
        {
            var url = UriFactory.CreateStoredProcedureUri(_settings.DatabaseId, _settings.CollectionId, procId);

            RequestOptions options = new RequestOptions()
            {
                PartitionKey = new PartitionKey(partitionKey)
            };

            return(await DocumentDbClient.ExecuteStoredProcedureAsync <TResult>(url, options, args));
        }
Example #9
0
 public void Dispose()
 {
     if (_sourceClient != null)
     {
         _sourceClient.Dispose();
         _sourceClient = null;
     }
     if (_targetClient != null)
     {
         _targetClient.Dispose();
         _targetClient = null;
     }
 }
Example #10
0
        public IQueryable <T> GetItems <T>(Expression <Func <T, bool> > predicate)
        {
            if (predicate == null)
            {
                return(DocumentDbClient.CreateDocumentQuery <T>(CollectionUri, new FeedOptions {
                    MaxItemCount = -1
                }));
            }

            return(DocumentDbClient.CreateDocumentQuery <T>(CollectionUri, new FeedOptions {
                MaxItemCount = -1
            })
                   .Where(predicate));
        }
Example #11
0
        public IQueryable <T> GetItemsOrderByDescending <T>(Expression <Func <T, bool> > wherePredicate, Expression <Func <T, DateTime> > orderPredicate)
        {
            if (wherePredicate == null)
            {
                return(DocumentDbClient.CreateDocumentQuery <T>(CollectionUri, new FeedOptions {
                    MaxItemCount = -1
                }));
            }

            return(DocumentDbClient.CreateDocumentQuery <T>(CollectionUri, new FeedOptions {
                MaxItemCount = -1
            })
                   .Where(wherePredicate).OrderByDescending(orderPredicate));
        }
Example #12
0
        public async Task <T> ExecuteStoredProcedure <T>(string storedProcedureName, params dynamic[] parameters)
        {
            var storedProc = DocumentDbClient.CreateStoredProcedureQuery(CollectionUri).Where(p => p.Id == storedProcedureName).AsEnumerable().FirstOrDefault();

            if (storedProc == null)
            {
                throw new ArgumentNullException($"Stored Procedure {storedProcedureName} does not exists on the database");
            }

            StoredProcedureResponse <T> response = null;

            response = await DocumentDbClient.ExecuteStoredProcedureAsync <T>(storedProc.SelfLink, parameters);

            return(response);
        }
Example #13
0
        private async Task EnsureCollectionExistsAsync()
        {
            DocumentCollection myCollection = new DocumentCollection
            {
                Id = _settings.CollectionId
            };

            if (!string.IsNullOrWhiteSpace(_settings.PartitionPath))
            {
                myCollection.PartitionKey.Paths.Add(_settings.PartitionPath);
            }
            RequestOptions options = new RequestOptions()
            {
                OfferThroughput = _settings.InitialOfferThroughput ?? DefaultInitialOfferThroughput
            };

            await DocumentDbClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(_settings.DatabaseId), myCollection);
        }
Example #14
0
        protected override void Load(ContainerBuilder builder)
        {
            // register the DocumentClient for DocumentDB API
            builder.Register(c =>
            {
                var cosmosDbClient = c.Resolve <IOptions <CosmosDbOptions> >().Value;
                return(new DocumentClient(cosmosDbClient.EndpointUri, cosmosDbClient.PrimaryKey));
            }).SingleInstance();

            // create the DocumentDB client and initialize it
            builder.Register(c =>
            {
                var databaseClient = new DocumentDbClient(
                    c.Resolve <IOptions <CosmosDbOptions> >(),
                    c.Resolve <ILogger <DocumentDbClient> >(),
                    c.Resolve <DocumentClient>());
                databaseClient.Initialize().Wait();
                return(databaseClient);
            }).As <IDatabaseClient>();

            // services
            builder.Register(c => new UserService(c.Resolve <IOptions <CosmosDbOptions> >(), c.Resolve <IDatabaseClient>()))
            .As <IUserService>();
        }
Example #15
0
 /// <summary>
 /// Creates the database if it does not exist using the database name from configuration setting
 /// </summary>
 /// <returns></returns>
 public async Task CreateDatabase()
 {
     var dbName = _configurationService.DocumentDb.Database;
     await DocumentDbClient.CreateDatabaseIfNotExistsAsync(new Database { Id = dbName });
 }
Example #16
0
 public async Task DeleteDocuments(string documentLink)
 {
     ResourceResponse <Document> result = await DocumentDbClient.DeleteDocumentAsync(documentLink);
 }
Example #17
0
 private async Task CreateStoredProcedure(StoredProcedure storedProcedure)
 {
     await DocumentDbClient.CreateStoredProcedureAsync(CollectionUri, storedProcedure);
 }
Example #18
0
 public IQueryable <T> GetItemsByExpression <T>(string sqlExpression)
 {
     return(DocumentDbClient.CreateDocumentQuery <T>(CollectionUri, sqlExpression));
 }
Example #19
0
        /// <summary>
        /// Execute stored procedure in this collection
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="procId"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public async Task <TResult> ExecuteStoredProcedureAsync <TResult>(string procId, object args)
        {
            var url = UriFactory.CreateStoredProcedureUri(_settings.DatabaseId, _settings.CollectionId, procId);

            return(await DocumentDbClient.ExecuteStoredProcedureAsync <TResult>(url, args));
        }
Example #20
0
 public async Task UpsertDocument(object document)
 {
     ResourceResponse <Document> result = await DocumentDbClient.UpsertDocumentAsync(CollectionUri, JObject.FromObject(document));
 }
Example #21
0
 public async Task CreateCollection()
 {
     await DocumentDbClient.CreateDocumentCollectionIfNotExistsAsync(
         UriFactory.CreateDatabaseUri(_configurationService.DocumentDb.Database),
         new DocumentCollection { Id = _configurationService.DocumentDb.Collection });
 }
Example #22
0
 public async Task UpsertDocument <T>(T document)
 {
     ResourceResponse <Document> result = await DocumentDbClient.UpsertDocumentAsync(
         CollectionUri,
         document);
 }
 protected abstract Task <IDataSinkAdapter> CreateAsync(DocumentDbClient client, IDataItemTransformation transformation,
                                                        TConfiguration configuration, IEnumerable <string> collectionNames, CancellationToken cancellation);
Example #24
0
 protected abstract Task <IDataSinkAdapter> CreateAsync(DocumentDbClient client, IDataItemTransformation transformation, TConfiguration configuration);
Example #25
0
        public async Task <ResourceResponse <Document> > ReplaceDocument(object document)
        {
            ResourceResponse <Document> updated = await DocumentDbClient.UpsertDocumentAsync(CollectionUri, document);

            return(updated);
        }