https://github.com/cryptoc1/litedbcontext
A light-weight async wrapper for LiteDB
https://github.com/cryptoc1/litedbcontext
dotnet litedb
Last synced: 8 days ago
JSON representation
A light-weight async wrapper for LiteDB
- Host: GitHub
- URL: https://github.com/cryptoc1/litedbcontext
- Owner: Cryptoc1
- License: mit
- Created: 2025-02-13T21:34:52.000Z (over 1 year ago)
- Default Branch: develop
- Last Pushed: 2026-03-29T20:30:53.000Z (3 months ago)
- Last Synced: 2026-03-29T20:54:22.370Z (3 months ago)
- Topics: dotnet, litedb
- Language: C#
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LiteDbContext

[](https://libraries.io/nuget/LiteDbContext)
[](https://github.com/Cryptoc1/LiteDbContext/actions/workflows/default.yml)
[](https://app.codecov.io/gh/Cryptoc1/LiteDbContext/)
[](https://www.nuget.org/packages/LiteDbContext)
A light-weight async wrapper around [LiteDB](https://github.com/litedb-org/LiteDB).
### Key Features:
- Modern C#
- Supports cancellations
## Usage
1. Define a `LiteDbContext`:
```csharp
using LiteDB;
// ...
public sealed class ExampleDbContext(LiteDbOptions options) : LiteDbContext(options)
{
// NOTE: if a `name` is not provided to `DbSet(string? name)`, `[CallerMemberName]` will provide the `PropertyInfo.Name` for you, e.g. `Examples`
public LiteDbSet Examples => DbSet();
}
public sealed record class ExampleEntity
{
public required string Key { get; init; }
public string Value { get; init; }
}
```
2. Add your `LiteDbContext` as a service:
```csharp
using LiteDB.Extensions.DependencyInjection;
// ...
IServiceCollection services;
// ...
services.AddLiteDbContext(options =>
{
options.ConnectionString = "...";
options.Mapper.ConfigureExamples();
});
// ...
internal static class ExampleEntityConfiguration
{
public static BsonMapper ConfigureExamples(this BsonMapper mapper)
{
mapper.Entity()
.Id(example => example.Key);
return mapper;
}
}
```
3. Use you `LiteDbContext`:
```csharp
public sealed class ExampleController
{
[HttpGet]
public async Task> Index(
[FromServices] ExampleDbContext context,
[FromQuery, Required] string query)
{
var examples = await context.Examples.Query()
.Where(example => example.Value.StartsWith( query ))
.ToArrayAsync(HttpContext.RequestAborted);
return Ok(examples);
}
}
```
## _How'd He Do It?_
Work against the database is synchronized via a queue, backed by an unbounded `Channel`.
See [`DbWorkQueue`](https://github.com/Cryptoc1/LiteDbContext/blob/develop/src/DbWorkQueue.cs).