Skip to content

json-api-dotnet/JsonApiDotNetCore.MongoDb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB support for JsonApiDotNetCore

Plug-n-play implementation of IResourceRepository<TResource, TId> allowing you to use MongoDB with your JsonApiDotNetCore APIs.

Build Coverage NuGet

Installation and Usage

dotnet add package JsonApiDotNetCore.MongoDb

Models

#nullable enable

[Resource]
public class Book : HexStringMongoIdentifiable
{
    [Attr]
    public string Name { get; set; } = null!;
}

Middleware

// Program.cs

#nullable enable

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddSingleton<IMongoDatabase>(_ =>
{
    var client = new MongoClient("mongodb://localhost:27017");
    return client.GetDatabase("ExampleDbName");
});

builder.Services.AddJsonApi(resources: resourceGraphBuilder =>
{
    resourceGraphBuilder.Add<Book, string?>();
});

builder.Services.AddJsonApiMongoDb();

builder.Services.AddResourceRepository<MongoRepository<Book, string?>>();

// Configure the HTTP request pipeline.

app.UseRouting();
app.UseJsonApi();
app.MapControllers();

app.Run();

Note: If your API project uses MongoDB only (so not in combination with EF Core), then instead of registering all MongoDB resources and repositories individually, you can use:

builder.Services.AddJsonApi(facade => facade.AddCurrentAssembly());
builder.Services.AddJsonApiMongoDb();

builder.Services.AddScoped(typeof(IResourceReadRepository<,>), typeof(MongoRepository<,>));
builder.Services.AddScoped(typeof(IResourceWriteRepository<,>), typeof(MongoRepository<,>));
builder.Services.AddScoped(typeof(IResourceRepository<,>), typeof(MongoRepository<,>));

Using client-generated IDs

Resources that inherit from HexStringMongoIdentifiable use auto-generated (performant) 12-byte hexadecimal Object IDs. You can assign an ID manually, but it must match the 12-byte hexadecimal pattern.

To assign free-format string IDs manually, make your resources inherit from FreeStringMongoIdentifiable instead. When creating a resource without assigning an ID, a 12-byte hexadecimal ID will be auto-generated.

Set options.AllowClientGeneratedIds to true in Program.cs to allow API clients to assign IDs. This can be combined with both base classes, but FreeStringMongoIdentifiable probably makes the most sense.

Limitations

  • JSON:API relationships are currently not supported. You can use complex object graphs though, which are stored in a single document.

Contributing

Have a question, found a bug or want to submit code changes? See our contributing guidelines.

Trying out the latest build

After each commit to the master branch, a new prerelease NuGet package is automatically published to AppVeyor at https://ci.appveyor.com/nuget/jsonapidotnetcore-mongodb. To try it out, follow the next steps:

  • In Visual Studio: Tools, NuGet Package Manager, Package Manager Settings, Package Sources
  • Open the NuGet package manager console (Tools, NuGet Package Manager, Package Manager Console)
    • Select AppVeyor JADNC MongoDb as package source
    • Run command: Install-Package JonApiDotNetCore -pre

Development

To build the code from this repository locally, run:

dotnet build

You don't need to have a running instance of MongoDB on your machine to run tests. Just type the following command in your terminal:

dotnet test

If you want to run the examples and explore them on your own you are going to need that running instance of MongoDB. If you have docker installed you can launch it like this:

run-docker-mongodb.ps1

And then to run the API:

dotnet run --project src/Examples/GettingStarted

Alternatively, to build and validate the code, run all tests, generate code coverage and produce the NuGet package:

Build.ps1