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
2 changes: 1 addition & 1 deletion Cosmonaut.Tests/MockHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Mock<DocumentCollection> GetMockDocumentCollection()
{
resource.SetResourceTimestamp(DateTime.UtcNow);
var resourceResponse = new ResourceResponse<T>(resource);
var documentServiceResponseType = Type.GetType("Microsoft.Azure.Documents.DocumentServiceResponse, Microsoft.Azure.DocumentDB.Core, Version=1.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
var documentServiceResponseType = Type.GetType("Microsoft.Azure.Documents.DocumentServiceResponse, Microsoft.Azure.DocumentDB.Core, Version=1.9.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

var flags = BindingFlags.NonPublic | BindingFlags.Instance;

Expand Down
4 changes: 2 additions & 2 deletions 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>cosmosdb azure cosmos entitystore entity db orm</PackageTags>
<PackageReleaseNotes>Please report any issues on Github.</PackageReleaseNotes>
<Version>1.3.4</Version>
<Version>1.4.2</Version>
<NeutralLanguage></NeutralLanguage>
</PropertyGroup>

Expand All @@ -26,7 +26,7 @@

<ItemGroup>
<PackageReference Include="Humanizer.Core.uk" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="1.10.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="1.9.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
</ItemGroup>

Expand Down
16 changes: 8 additions & 8 deletions Cosmonaut/CosmosStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ public sealed class CosmosStore<TEntity> : ICosmosStore<TEntity> where TEntity :
private readonly CosmosScaler<TEntity> _cosmosScaler;

public CosmosStore(CosmosStoreSettings settings,
IDatabaseCreator databaseCreator,
ICollectionCreator collectionCreator)
IDatabaseCreator databaseCreator = null,
ICollectionCreator collectionCreator = null)
{
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
var endpointUrl = Settings.EndpointUrl ?? throw new ArgumentNullException(nameof(Settings.EndpointUrl));
var authKey = Settings.AuthKey ?? throw new ArgumentNullException(nameof(Settings.AuthKey));
DocumentClient = DocumentClientFactory.CreateDocumentClient(endpointUrl, authKey);
if (string.IsNullOrEmpty(Settings.DatabaseName)) throw new ArgumentNullException(nameof(Settings.DatabaseName));
_collectionCreator = collectionCreator ?? throw new ArgumentNullException(nameof(collectionCreator));
_databaseCreator = databaseCreator ?? throw new ArgumentNullException(nameof(databaseCreator));
_collectionCreator = collectionCreator ?? new CosmosCollectionCreator(DocumentClient);
_databaseCreator = databaseCreator ?? new CosmosDatabaseCreator(DocumentClient);
_cosmosScaler = new CosmosScaler<TEntity>(this);
InitialiseCosmosStore();
}

internal CosmosStore(IDocumentClient documentClient,
string databaseName,
IDatabaseCreator databaseCreator,
ICollectionCreator collectionCreator,
IDatabaseCreator databaseCreator = null,
ICollectionCreator collectionCreator = null,
bool scaleable = false)
{
DocumentClient = documentClient ?? throw new ArgumentNullException(nameof(documentClient));
Settings = new CosmosStoreSettings(databaseName, documentClient.ServiceEndpoint, documentClient.AuthKey.ToString(), documentClient.ConnectionPolicy,
scaleCollectionRUsAutomatically: scaleable);
if (string.IsNullOrEmpty(Settings.DatabaseName)) throw new ArgumentNullException(nameof(Settings.DatabaseName));
_databaseCreator = databaseCreator ?? throw new ArgumentNullException(nameof(databaseCreator));
_collectionCreator = collectionCreator ?? throw new ArgumentNullException(nameof(collectionCreator));
_collectionCreator = collectionCreator ?? new CosmosCollectionCreator(DocumentClient);
_databaseCreator = databaseCreator ?? new CosmosDatabaseCreator(DocumentClient);
_cosmosScaler = new CosmosScaler<TEntity>(this);
InitialiseCosmosStore();
}
Expand Down
9 changes: 3 additions & 6 deletions Cosmonaut/Extensions/CosmonautServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ public static class CosmonautServiceCollectionExtensions
{
public static IServiceCollection AddCosmosStore<TEntity>(this IServiceCollection services, CosmosStoreSettings settings) where TEntity : class
{
var documentClient = DocumentClientFactory.CreateDocumentClient(settings);
services.AddSingleton<ICosmosStore<TEntity>>(x=> new CosmosStore<TEntity>(settings,
new CosmosDatabaseCreator(documentClient),
new CosmosCollectionCreator(documentClient)));
services.AddSingleton<ICosmosStore<TEntity>>(x=> new CosmosStore<TEntity>(settings));
return services;
}

Expand All @@ -26,8 +23,8 @@ public static IServiceCollection AddCosmosStore<TEntity>(this IServiceCollection
public static IServiceCollection AddCosmosStore<TEntity>(this IServiceCollection services,
IDocumentClient documentClient,
string databaseName,
IDatabaseCreator databaseCreator,
ICollectionCreator collectionCreator) where TEntity : class
IDatabaseCreator databaseCreator = null,
ICollectionCreator collectionCreator = null) where TEntity : class
{
services.AddSingleton<ICosmosStore<TEntity>>(x => new CosmosStore<TEntity>(documentClient, databaseName, databaseCreator, collectionCreator));
return services;
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,34 @@ Registering the CosmosStores in ServiceCollection for DI support
"<<authkey>>");

serviceCollection.AddCosmosStore<Book>(cosmosSettings);

//or just by using the Action extension

serviceCollection.AddCosmosStore<Book>(options =>
{
options.DatabaseName = "<<databaseName>>";
options.AuthKey = "<<authkey>>";
options.EndpointUrl = new Uri("<<cosmosUri>>");
});

//or just initialise the object

ICosmosStore<Book> bookStore = new CosmosStore<Book>(cosmosSettings)
```

##### Quering for entities

In order to query for entities all you have to do is call the `.Query()` method and then use LINQ to create the query you want.
It is HIGHLY recommended that you use one of the `Async` methods to get the results back, such as `ToListAsync` or `FirstOrDefaultAsync` , when available.
It is HIGHLY recommended that you use one of the `Async` extension methods to get the results back, such as `ToListAsync` or `FirstOrDefaultAsync` , when available.

```csharp
var user = await cosmoStore.Query().FirstOrDefaultAsync(x => x.Username == "elfocrash");
var users = await cosmoStore.Query().ToListAsync(x => x.HairColor == HairColor.Black);
var otherUsers = await cosmosStore.Query().Where(x => x.Name.StartsWith("Smit")).ToListAsync(cancellationToken)

// or you can use SQL

var user = await cosmoStore.QueryMultipleAsync("select * from c w.Firstname = 'Smith'");
```

##### Adding an entity in the entity store
Expand Down