public async Task <IEnumerable <T> > ConvertAsync(CosmosDBAttribute attribute, CancellationToken cancellationToken) { CosmosDBContext context = _config.CreateContext(attribute); Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName); List <T> finalResults = new List <T>(); string continuation = null; SqlQuerySpec sqlSpec = new SqlQuerySpec { QueryText = context.ResolvedAttribute.SqlQuery, Parameters = context.ResolvedAttribute.SqlQueryParameters ?? new SqlParameterCollection() }; do { DocumentQueryResponse <T> response = await context.Service.ExecuteNextAsync <T>(collectionUri, sqlSpec, continuation); finalResults.AddRange(response.Results); continuation = response.ResponseContinuation; }while (!string.IsNullOrEmpty(continuation)); return(finalResults); }
internal Task <IValueBinder> BindForItemAsync(CosmosDBAttribute attribute, Type type) { if (string.IsNullOrEmpty(attribute.Id)) { throw new InvalidOperationException("The 'Id' property of a CosmosDB single-item input binding cannot be null or empty."); } CosmosDBContext context = CreateContext(attribute); Type genericType = typeof(CosmosDBItemValueBinder <>).MakeGenericType(type); IValueBinder binder = (IValueBinder)Activator.CreateInstance(genericType, context); return(Task.FromResult(binder)); }
internal static async Task UpsertDocument(CosmosDBContext context, T item) { Uri collectionUri = UriFactory.CreateDocumentCollectionUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName); // DocumentClient does not accept strings directly. object convertedItem = item; if (item is string) { convertedItem = JObject.Parse(item.ToString()); } await context.Service.UpsertDocumentAsync(collectionUri, convertedItem); }
internal static async Task SetValueInternalAsync(JObject originalItem, T newItem, CosmosDBContext context) { // We can short-circuit here as strings are immutable. if (newItem is string) { return; } JObject currentValue = JObject.FromObject(newItem); if (HasChanged(originalItem, currentValue)) { // make sure it's not the id that has changed if (TryGetId(currentValue, out string currentId) && !string.IsNullOrEmpty(currentId) && TryGetId(originalItem, out string originalId) && !string.IsNullOrEmpty(originalId)) { // make sure it's not the Id that has changed if (!string.Equals(originalId, currentId, StringComparison.Ordinal)) { throw new InvalidOperationException("Cannot update the 'Id' property."); } } else { // If the serialzied object does not have a lowercase 'id' property, DocDB will reject it. // We'll just short-circuit here since we validate that the 'id' hasn't changed. throw new InvalidOperationException(string.Format("The document must have an 'id' property.")); } Uri documentUri = UriFactory.CreateDocumentUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName, originalId); await context.Service.ReplaceDocumentAsync(documentUri, newItem); } }
public CosmosDBItemValueBinder(CosmosDBContext context) { _context = context; }
internal static async Task CreateDatabaseAndCollectionIfNotExistAsync(CosmosDBContext context) { await CreateDatabaseAndCollectionIfNotExistAsync(context.Service, context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName, context.ResolvedAttribute.PartitionKey, context.ResolvedAttribute.CollectionThroughput); }
public CosmosDBAsyncCollector(CosmosDBContext docDBContext) { _docDBContext = docDBContext; }