public CosmosContainer Convert(CosmosAttribute attribute) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } ICosmosService service = configProvider.GetService( configProvider.ResolveConnectionString(attribute.ConnectionStringSetting), attribute.ApplicationName, attribute.ApplicationRegion); if (attribute.CreateIfNotExists) { var context = new CosmosContext { ResolvedAttribute = attribute, Service = service }; CosmosUtility.CreateDatabaseAndContainerNameIfNotExistAsync(context).Wait(); } return(service.GetContainer(attribute.DatabaseName, attribute.ContainerName)); }
public async Task <IEnumerable <T> > ConvertAsync(CosmosAttribute attribute, CancellationToken cancellationToken) { CosmosContext context = configProvider.CreateContext(attribute); List <T> finalResults = new List <T>(); string continuation = null; QueryDefinition query = new QueryDefinition(context.ResolvedAttribute.SqlQuery); if ((context.ResolvedAttribute.SqlQueryParameters?.Count ?? 0) > 0) { foreach (var parameter in context.ResolvedAttribute.SqlQueryParameters) { query = query.WithParameter(parameter.Key, parameter.Value); } } var response = context.Service.GetItemQueryIterator <T>(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.ContainerName, query); do { await foreach (var page in response.AsPages(continuation)) { finalResults.AddRange(page.Values); continuation = page.ContinuationToken; } }while (!string.IsNullOrEmpty(continuation)); return(finalResults); }
internal static async Task CreateDatabaseAndContainerNameIfNotExistAsync(CosmosContext context) { await CreateDatabaseAndContainerNameIfNotExistAsync( context.Service, context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.ContainerName, context.ResolvedAttribute.PartitionKey, context.ResolvedAttribute.DatabaseThroughput, context.ResolvedAttribute.ContainerThroughput); }
internal static async Task UpsertDocument(CosmosContext context, T item) { // CosmosClient does not accept strings directly. object convertedItem = item; if (item is string) { convertedItem = JObject.Parse(item.ToString()); } await context.Service.UpsertItemAsync(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.ContainerName, convertedItem); }
internal Task <IValueBinder> BindForItemAsync(CosmosAttribute attribute, Type type) { if (string.IsNullOrEmpty(attribute.Id)) { throw new InvalidOperationException("The 'Id' property of a Cosmos single-item input binding cannot be null or empty."); } CosmosContext context = CreateContext(attribute); Type genericType = typeof(CosmosItemValueBinder <>).MakeGenericType(type); IValueBinder binder = (IValueBinder)Activator.CreateInstance(genericType, context); return(Task.FromResult(binder)); }
internal static async Task SetValueInternalAsync(JObject originalItem, T newItem, CosmosContext 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 serialized 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.")); } PartitionKey partitionKey = PartitionKey.Null; if (!string.IsNullOrEmpty(context.ResolvedAttribute.PartitionKey)) { partitionKey = new PartitionKey(context.ResolvedAttribute.PartitionKey); } await context.Service.ReplaceItemAsync( context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.ContainerName, newItem, context.ResolvedAttribute.Id, partitionKey); } }
public CosmosItemValueBinder(CosmosContext context) { this.context = context; }
public CosmosAsyncCollector(CosmosContext cosmosContext) { this.cosmosContext = cosmosContext; }
public IAsyncCollector <T> Convert(CosmosAttribute attribute) { CosmosContext context = configProvider.CreateContext(attribute); return(new CosmosAsyncCollector <T>(context)); }