Skip to content
🚀 Lightweight CosmosDb wrapper
C# PowerShell
Branch: master
Clone or download
isak-spinit Merge pull request #27 from Spinit-AB/feature/searchable-attribute
Add attribute to specify searchable properties
Latest commit 3029096 Nov 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Examples/TodoApi Support for custom analysis Apr 17, 2019
Spinit.CosmosDb.FunctionalTests
Spinit.CosmosDb.UnitTests
Spinit.CosmosDb
.gitignore
LICENSE Initial commit Jan 24, 2019
README.md
Spinit.CosmosDb.sln Uses DateParseHandling.None (eg keep string values) and uses IsoDateT… May 8, 2019
azure-pipelines.yml
set-version-variables.ps1
update-build-number.ps1

README.md

Spinit.CosmosDb

GitHub Build Status Nuget

Lightweight CosmosDb wrapper

Install

Package Manager:

PM> Install-Package Spinit.CosmosDb

.NET CLI:

> dotnet add package Spinit.CosmosDb

Usage

Define a database model:

public class MyEntity : ICosmosEntity
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

[DatabaseId("MyDatabase")] // name of the cosmos database, optional
public class MyDatabase : CosmosDatabase
{
    public MyDatabase(DatabaseOptions<MyDatabase> options)
        : base(options)
    { }

    [CollectionId("MyEntities")] // name of the cosmos collection, optional
    public ICosmosDbCollection<MyEntity> MyEntities { get; private set; }
}

Add to services (IoC):

var myConnectionString = configuration.GetConnectionString("CosmosDb");

services
    ...
    // This will register the database type and all it's collections
    .AddCosmosDatabase<MyDatabase>(options => options.UseConnectionString(myConnectionString))
    ...
    ;

Inject and use:

public class MyController : ControllerBase
{
    private readonly ICosmosDbCollection<MyEntity> _collection;

    public MyController(ICosmosDbCollection<MyEntity> collection)
    {
        _collection = collection;
    }

    [HttpGet]
    public Task<SearchResponse<MyEntity>> List(string query)
    {
        var searchRequest = new SearchRequest<MyEntity>
        {
            Query = query
        };
        return _collection.SearchAsync(searchRequest);
    }

    [HttpGet("{id}")]
    public Task<MyEntity> Get(string id)
    {
        return _collection.GetAsync(id);
    }

    [HttpPost]
    public Task Upsert(MyEntity input)
    {
        return _collection.UpsertAsync(input);
    }
}

License

MIT License

Copyright (c) 2019- Spinit AB

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You can’t perform that action at this time.