Coder Social home page Coder Social logo

universe's Introduction

Universe

A simpler way of querying a CosmosDb Namespace

How-to:

  1. Your models / cosmos entities should inherit from the interface
public class MyCosmosEntity : ICosmicEntity
{
  public string FirstName { get; set; }
  
  public string LastName { get; set; }
  
  // The properties below are implementations from ICosmicEntity
  public string id { get; set; }
  public DateTime AddedOn { get; set; }
  public DateTime ModifiedOn { get; set; }

  [JsonIgnore]
  public string PartitionKey => FirstName;
}
  1. Create a repository like so:
public class MyRepository : Galaxy<MyModel>
{
    public MyRepository(CosmosClient client, string database, string container, string partitionKey) : base(client, database, container, partitionKey)
    {
    }
}

// If you want to see debug information such as the full Query text executed, use the format below:
public class MyRepository : Galaxy<MyModel>
{
    public MyRepository(CosmosClient client, string database, string container, string partitionKey) : base(client, database, container, partitionKey, true)
    {
    }
}
  1. In your Startup.cs / Main method / Program.cs, configure the CosmosClient like so:
_ = services.AddScoped(_ => new CosmosClient(
    System.Environment.GetEnvironmentVariable("CosmosDbUri"),
    System.Environment.GetEnvironmentVariable("CosmosDbPrimaryKey"),
    clientOptions: new()
    {
        Serializer = new UniverseSerializer() // This is from Universe.Options
        AllowBulkExecution = true // This will tell the underlying code to allow async bulk operations
    }
));
  1. In your Startup.cs / Main method / Program.cs, configure your CosmosDb repository like so:
_ = services.AddScoped<IGalaxy<MyModel>, MyRepository>(service => new(
    client: service.GetRequiredService<CosmosClient>(),
    database: "database-name",
    container: "container-name",
    partitionKey: "/partitionKey"
));
  1. Inject your IGalaxy<MyModel> dependency into your classes and enjoy a simpler way to query CosmosDb

Example Query:

MyModel myModel = await IGalaxy<MyModel>.Get(new Catalyst("LastName", "last name value"));

universe's People

Contributors

kuromukira avatar

Stargazers

 avatar

Watchers

 avatar

universe's Issues

Add debugging capabilities by exposing a list of recently executed queries

Store the recently executed queries per instance of the IGalaxy<T> interface.

It should look like this:
IGalaxy<T>.RecentQueries

This is a RecentQuery array containing the query string and parameters used for the query.
There should also be a property during initialization to set if the instance of the interface will store recent queries in-memory.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.