Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Play with Cosmos DB from LINQPad
<Query Kind="Program">
<NuGetReference>Microsoft.Azure.DocumentDB</NuGetReference>
<Namespace>Microsoft.Azure.Documents</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>Microsoft.Azure.Documents.Client</Namespace>
<Namespace>Microsoft.Azure.Documents.Linq</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
</Query>
private static readonly string DatabaseId = "ToDoList";
private static readonly string CollectionId = "Items";
private static readonly string Endpoint = "https://dev-sh-cosmos.documents.azure.com:443/";
private static readonly string AuthKey = Util.GetPassword("dev-sh-cosmos-authkey");
void Main()
{
DocumentDBRepository<Item>.Initialize();
var items = DocumentDBRepository<Item>.GetItemsAsync(item => true).Result;
items.Dump();
}
public class Item
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "isComplete")]
public bool Completed { get; set; }
}
public static class DocumentDBRepository<T> where T : class
{
private static DocumentClient client;
public static async Task<T> GetItemAsync(string id)
{
try
{
var documentUri = UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id);
Document document = await client.ReadDocumentAsync(documentUri);
return (T)(dynamic)document;
}
catch (DocumentClientException e) when (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
}
public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
{
var collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
collectionUri,
new FeedOptions { MaxItemCount = -1 })
.Where(predicate)
.AsDocumentQuery();
List<T> results = new List<T>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
public static async Task<Document> CreateItemAsync(T item)
{
var collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
return await client.CreateDocumentAsync(collectionUri, item);
}
public static async Task<Document> UpdateItemAsync(string id, T item)
{
var documentUri = UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id);
return await client.ReplaceDocumentAsync(documentUri, item);
}
public static async Task DeleteItemAsync(string id)
{
var documentUri = UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id);
await client.DeleteDocumentAsync(documentUri);
}
public static void Initialize()
{
client = new DocumentClient(new Uri(Endpoint), AuthKey);
CreateDatabaseIfNotExistsAsync().Wait();
CreateCollectionIfNotExistsAsync().Wait();
}
private static async Task CreateDatabaseIfNotExistsAsync()
{
try
{
var databaseUri = UriFactory.CreateDatabaseUri(DatabaseId);
await client.ReadDatabaseAsync(databaseUri);
}
catch (DocumentClientException e) when (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
}
}
private static async Task CreateCollectionIfNotExistsAsync()
{
try
{
var collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
await client.ReadDocumentCollectionAsync(collectionUri);
}
catch (DocumentClientException e) when (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
var databaseUri = UriFactory.CreateDatabaseUri(DatabaseId);
await client.CreateDocumentCollectionAsync(
databaseUri,
new DocumentCollection { Id = CollectionId },
new RequestOptions { OfferThroughput = 400 });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment