Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build status](https://ci.appveyor.com/api/projects/status/au32jna62iue4wut?svg=true)](https://ci.appveyor.com/project/Elfocrash/cosmonaut) [![NuGet Package](https://img.shields.io/nuget/v/Cosmonaut.svg)](https://www.nuget.org/packages/Cosmonaut)
[![Build Status](https://dev.azure.com/nickchapsas/Cosmonaut/_apis/build/status/Elfocrash.Cosmonaut)](https://dev.azure.com/nickchapsas/Cosmonaut/_build/latest?definitionId=2) [![NuGet Package](https://img.shields.io/nuget/v/Cosmonaut.svg)](https://www.nuget.org/packages/Cosmonaut) [![Licensed under the MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Elfocrash/Cosmonaut/blob/master/LICENSE)

# Cosmonaut

Expand All @@ -14,6 +14,12 @@ Cosmonaut is a supercharged SDK with object mapping capabilities that enables .N
- [(Video) Getting started with .NET Core and CosmosDB using Cosmonaut](http://chapsas.com/video-getting-started-with-net-core-and-cosmosdb-using-cosmonaut/)
- [(Video) How to save money in CosmosDB with Cosmonaut's Collection Sharing](http://chapsas.com/video-how-to-save-money-in-cosmosdb-with-cosmonauts-collection-sharing/)
- [CosmosDB Fluent Pagination with Cosmonaut](http://chapsas.com/cosmosdb-fluent-pagination-with-cosmonaut/)
- [Implementing server side CosmosDB pagination in a Blazor Web App (Part 1: Page Number and Page Size)
](https://chapsas.com/implementing-skiptake-server-side-cosmosdb-pagination-in-a-blazor-web-app/)

### Samples
- The `samples` folder in this project
- [Web app server-side pagination for CosmosDB](https://github.com/Elfocrash/CosmosDBPaginationSample)

### Usage
The idea is pretty simple. You can have one CosmosStore per entity (POCO/dtos etc).
Expand Down Expand Up @@ -267,15 +273,3 @@ Just initialise the AppInsightsTelemetryModule in your Startup or setup pipeline
Example: `AppInsightsTelemetryModule.Instance.Initialize(new TelemetryConfiguration("InstrumentationKey"))`

If you already have initialised `TelemetryConfiguration` for your application then use `TelemetryConfiguration.Active` instead of `new TelemetryConfiguration` because if you don't there will be no association between the dependency calls and the parent request.

#### Benchmarks

##### Averages of 1000 iterations for 1000 documents (1Kb each) per operation on collection with default indexing and Unlimited RU/s (POCO serialization)

| Operation used | Duration |
| ------------- |:-------------:|
| AddRangeAsync | 1152ms |
| ToListAsync |51ms|
| UpdateRangeAsync |1129ms|
| UpsertRangeAsync |1034ms|
| RemoveRangeAsync | 899ms |
20 changes: 20 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

pool:
vmImage: 'vs2017-win2016'

variables:
buildConfiguration: 'Release'

steps:
- powershell: .\installcosmosemu.ps1
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- script: dotnet test tests\Cosmonaut.Unit
displayName: 'Running unit tests'
- script: dotnet test tests\Cosmonaut.System
displayName: 'Running integration tests'

22 changes: 22 additions & 0 deletions installcosmosemu.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Invoke-WebRequest -Uri 'https://aka.ms/cosmosdb-emulator' -OutFile 'cosmos-db.msi'
cmd /c start /wait msiexec /i cosmos-db.msi /qn /quiet /norestart /log install.log
Set-Content -Value '"C:\Program Files\Azure Cosmos DB Emulator\CosmosDB.Emulator.exe" /NoUI /NoExplorer /NoFirewall' -Path .\startCosmosDb.cmd
Start-Process -FilePath .\startCosmosDb.cmd

$attempt = 0
$max = 3
while(!$client.Connected -and $attempt -lt $max) {
try {
$client = New-Object System.Net.Sockets.TcpClient([System.Net.Sockets.AddressFamily]::InterNetwork)
$attempt++; $client.Connect("127.0.0.1", 8081); write-host "CosmosDB started"
}
catch {
if($attempt -eq $max) {
write-host "CosmosDB was not started"; $client.Close(); return
}
[int]$sleepTime = 5*$attempt
write-host "CosmosDB is not started. Retry after $sleepTime seconds..."
sleep $sleepTime;
$client.Close()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Cosmonaut.NetCore2WebApi.Contract
{
public class EditPersonRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Cosmonaut.NetCore2WebApi.Contract
{
public class GetPersonResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cosmonaut.Extensions;
using Cosmonaut.NetCore2WebApi.Contract;
using Cosmonaut.NetCore2WebApi.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Documents.SystemFunctions;
using Microsoft.EntityFrameworkCore;

namespace Cosmonaut.NetCore2WebApi.Controllers
{
Expand Down
9 changes: 1 addition & 8 deletions samples/Cosmonaut.NetCore2WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Cosmonaut.NetCore2WebApi
{
Expand Down
8 changes: 1 addition & 7 deletions samples/Cosmonaut.NetCore2WebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Cosmonaut.NetCore2WebApi
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IServiceCollection AddCosmosStore<TEntity>(this IServiceCollection
settingsAction?.Invoke(settings);
return services.AddCosmosStore<TEntity>(settings, overriddenCollectionName);
}

public static IServiceCollection AddCosmosStore<TEntity>(this IServiceCollection services,
ICosmonautClient cosmonautClient,
string databaseName,
Expand Down
2 changes: 1 addition & 1 deletion src/Cosmonaut/Cosmonaut.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/Elfocrash/Cosmonaut</RepositoryUrl>
<PackageTags>azure entitystore entity db orm microsoft cosmos cosmosdb documentdb docdb nosql azureofficial dotnetcore netcore netstandard</PackageTags>
<PackageReleaseNotes>Please report any issues on Github.</PackageReleaseNotes>
<Version>2.4.1</Version>
<Version>2.5.0</Version>
<NeutralLanguage></NeutralLanguage>
<Company>Nick Chapsas</Company>
<PackageIconUrl>https://raw.githubusercontent.com/Elfocrash/Cosmonaut/develop/logo.png</PackageIconUrl>
Expand Down
11 changes: 10 additions & 1 deletion src/Cosmonaut/CosmonautClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ public async Task<Document> GetDocumentAsync(string databaseId, string collectio
return await this.InvokeCosmosOperationAsync(() => DocumentClient.ReadDocumentAsync(documentUri, requestOptions, cancellationToken), documentId)
.ExecuteCosmosQuery();
}


public async Task<T> GetDocumentAsync<T>(string databaseId, string collectionId, string documentId,
RequestOptions requestOptions = null, CancellationToken cancellationToken = default) where T : class
{
var documentUri = UriFactory.CreateDocumentUri(databaseId, collectionId, documentId);
return await this.InvokeCosmosOperationAsync(
() => DocumentClient.ReadDocumentAsync<T>(documentUri, requestOptions, cancellationToken), documentId)
.ExecuteCosmosQuery();
}

public async Task<IEnumerable<DocumentCollection>> QueryCollectionsAsync(string databaseId,
Expression<Func<DocumentCollection, bool>> predicate = null, FeedOptions feedOptions = null, CancellationToken cancellationToken = default)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Cosmonaut/CosmosStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Cosmonaut.Storage;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;

namespace Cosmonaut
{
Expand Down Expand Up @@ -228,10 +227,8 @@ public async Task<CosmosResponse<TEntity>> RemoveByIdAsync(string id, RequestOpt

public async Task<TEntity> FindAsync(string id, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)
{
var document = await CosmonautClient.GetDocumentAsync(DatabaseName, CollectionName, id,
return await CosmonautClient.GetDocumentAsync<TEntity>(DatabaseName, CollectionName, id,
GetRequestOptions(id, requestOptions), cancellationToken);

return document != null ? JsonConvert.DeserializeObject<TEntity>(document.ToString()) : null;
}

public async Task<TEntity> FindAsync(string id, object partitionKeyValue, CancellationToken cancellationToken = default)
Expand Down
12 changes: 6 additions & 6 deletions src/Cosmonaut/CosmosStoreSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public CosmosStoreSettings(string databaseName,
string authKey,
Action<CosmosStoreSettings> settings)
{
DatabaseName = databaseName;
EndpointUrl = endpointUrl;
AuthKey = authKey;
DatabaseName = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
EndpointUrl = endpointUrl ?? throw new ArgumentNullException(nameof(endpointUrl));
AuthKey = authKey ?? throw new ArgumentNullException(nameof(authKey));
settings?.Invoke(this);
}

Expand Down Expand Up @@ -77,9 +77,9 @@ public CosmosStoreSettings(
bool scaleCollectionRUsAutomatically = false,
int maximumUpscaleRequestUnits = CosmosConstants.DefaultMaximumUpscaleThroughput)
{
DatabaseName = databaseName;
AuthKey = authKey;
EndpointUrl = endpointUrl;
DatabaseName = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
EndpointUrl = endpointUrl ?? throw new ArgumentNullException(nameof(endpointUrl));
AuthKey = authKey ?? throw new ArgumentNullException(nameof(authKey));
ConnectionPolicy = connectionPolicy;
DefaultCollectionThroughput = defaultCollectionThroughput;
ScaleCollectionRUsAutomatically = scaleCollectionRUsAutomatically;
Expand Down
Loading