Esempio n. 1
0
        public async Task <JObject> CreateAsync(JObject document, Option <object?> partitionKey, CancellationToken cancellationToken)
        {
            var response = await _containerGetter().CreateItemAsync(
                document,
                PartitionKeyHelper.Create(partitionKey),
                null,
                cancellationToken);

            return(response.Resource);
        }
Esempio n. 2
0
        public async Task DeleteAsync(string id, Option <object?> partitionKey, string?eTag, CancellationToken cancellationToken)
        {
            var options = new ItemRequestOptionsBuilder()
                          .IfMatch(eTag)
                          .Build();

            await _containerGetter().DeleteItemAsync <JObject>(
                id,
                PartitionKeyHelper.Create(partitionKey) ?? PartitionKey.None,
                options,
                cancellationToken);
        }
Esempio n. 3
0
        public async Task <JObject> ReplaceAsync(string id, JObject document, Option <object?> partitionKey, string?eTag, CancellationToken cancellationToken)
        {
            var options = new ItemRequestOptionsBuilder()
                          .IfMatch(eTag)
                          .Build();

            var response = await _containerGetter().ReplaceItemAsync(
                document,
                id,
                PartitionKeyHelper.Create(partitionKey),
                options,
                cancellationToken);

            return(response.Resource);
        }
Esempio n. 4
0
        public async Task <JObject?> GetAsync(string id, Option <object?> partitionKey, CancellationToken cancellationToken)
        {
            try
            {
                var response = await _containerGetter().ReadItemAsync <JObject>(
                    id,
                    PartitionKeyHelper.Create(partitionKey) ?? PartitionKey.None,
                    null,
                    cancellationToken);

                return(response.Resource);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }
        }
Esempio n. 5
0
        public async Task <StoredProcedureResult> ExecuteStoredProcedureAsync(CosmosStoredProcedure storedProcedure, object?partitionKey, object?[] parameters, CancellationToken cancellationToken)
        {
            var result    = new StoredProcedureResult();
            var stopwatch = new Stopwatch();

            try
            {
                stopwatch.Start();
                var response = await _containerGetter().Scripts.ExecuteStoredProcedureAsync <JToken>(
                    storedProcedure.Id,
                    PartitionKeyHelper.Create(partitionKey),
                    parameters,
                    new StoredProcedureRequestOptions {
                    EnableScriptLogging = true
                },
                    cancellationToken);

                stopwatch.Stop();

                result.Body          = response.Resource;
                result.ScriptLog     = response.ScriptLog ?? string.Empty;
                result.RequestCharge = response.RequestCharge;
                result.StatusCode    = response.StatusCode;
            }
            catch (CosmosException ex)
            {
                result.Error      = ex;
                result.StatusCode = ex.StatusCode;
                var scriptLog = ex.Headers["x-ms-documentdb-script-log-results"] ?? string.Empty;
                result.ScriptLog = Uri.UnescapeDataString(scriptLog);
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }
            finally
            {
                stopwatch.Stop();
            }

            result.TimeElapsed = stopwatch.Elapsed;
            return(result);
        }
 protected override void Build(QueryRequestOptions options)
 {
     options.PartitionKey = PartitionKeyHelper.Create(_partitionKey);
     options.MaxItemCount = _maxItemCount;
 }