LowDb.Net
1.0.4
See the version list below for details.
dotnet add package LowDb.Net --version 1.0.4
NuGet\Install-Package LowDb.Net -Version 1.0.4
<PackageReference Include="LowDb.Net" Version="1.0.4" />
paket add LowDb.Net --version 1.0.4
#r "nuget: LowDb.Net, 1.0.4"
// Install LowDb.Net as a Cake Addin #addin nuget:?package=LowDb.Net&version=1.0.4 // Install LowDb.Net as a Cake Tool #tool nuget:?package=LowDb.Net&version=1.0.4
LowDb.Net
A .NET implementation of the LowDB file-based database for use in applications and experiments that only require a minimal database implementation. Supports type-safe JSON-based database file formats.
This library is inspired by the lowdb Javascript package, but rebuilt for the .NET platform.
Installation
This library is a NuGet package so it is easy to add to your project. To install the package into your solution, you can use the NuGet Package Manager. In PM, please use the following command:
PM > Install-Package LowDb.Net -Version 1.0.3
To install in the Visual Studio UI, go to the Tools menu > "Manage NuGet Packages". Then search for LowDb.Net, and install the package from there.
Usage
LowDb is a .NET 8 package that works seamlessly with any .NET application type (console, web api, Blazor, and Windows applications).
The following code shows how to consume the LowDb library an a simple console application that just creates and reads tasks:
using D20Tek.LowDb;
namespace Sample.Cli;
class Program
{
private static LowDb<TasksDocument> db = LowDbFactory.CreateLowDb<TasksDocument>("my-tasks.json");
static void Main(string[] args)
{
while (true)
{
Console.Write("Enter command (create, read, exit): ");
var command = Console.ReadLine()?.ToLower();
switch (command)
{
case "create":
CreateTask();
break;
case "read":
ReadTasks();
break;
case "exit":
return;
default:
Console.WriteLine("Invalid command. Please try again.");
break;
}
}
}
static void InitializeDatabase(string[] args)
{
db = LowDbFactory.CreateLowDb<TasksDocument>(b =>
{
if (args.Any(x => x.Equals("--in-memory", StringComparison.InvariantCultureIgnoreCase)))
{
Console.WriteLine("Running in-memory database mode.");
b.UseInMemoryDatabase();
}
else
{
b.UseFileDatabase("my-tasks.json");
}
});
}
static void CreateTask()
{
Console.Write("Enter task name: ");
var name = Console.ReadLine();
if (string.IsNullOrWhiteSpace(name))
{
Console.WriteLine("No task name was entered.");
return;
}
db.Update(x => x.Tasks.Add(
new TaskEntity
{
Id = x.GetNextId(),
Name = name,
IsCompleted = false
}));
Console.WriteLine("Task created successfully.");
}
}
For the full implementation of this console application, please see the Sample.Cli project in this repository.
Features
- Support for synchronous (LowDb) and async (LowDbAsync) database operations.
- Minimal, lightweight database implementation. Perfect for small databases with low write contention.
- Customizable layer of IStorageAdapter that can be used to target physcial files vs in-memory vs web storage (in the future).
- Supports type-safe JSON file database format.
- Supports in-memory adapter that is useful for writing unit tests.
- Ability to build custom storage adapters for additional data formats.
- Factory methods (with builders) to simplify the database creation in your projects.
- Dependency injection extensions to easily add LowDb to applications that support dependency injection.
Samples
For more detailed examples on how to use LowDb.Net, please review the following samples:
- Sample.AsyncCli - Console application for managing a task list and persisting a local file. Uses the LowDbAsync class and reads/writes files asynchronously.
- Sample.Cli - Console application for managing a task list and persisting a local file. Include the ability to run the app with an in-memory database instead.
- Sample.WebApi - Minimal WebApi project that exposes a TaskList web service with CRUD operations on a shared data file. All users have access to the same database file.
Limits
This database is intended for local usage for console and Windows applications and local storage in Blazor. It can be used on ASP.NET WebApi and Blazor Server projects, but only for projects with low database usage and low write contention.
When a single JSON database file becomes larger than 50-100MB, you may start to see performance degradation. The Write operations serialize the full object tree into JSON and saves it all to the file.
The LowDb does expose the Read and Write operations to enable batch multiple updates together to help with performance concerns. With those methods, you could implement the UnitOfWork pattern to make changes in batches.
You can also break up the data into separate files, if they can logically be separated... just as if you were using other Document DBs.
If you require large scale Document DBs, then other tools are more appropriate like Azure CosmosDB and MongoDB.
Feedback
If you use this library and have any feedback, bugs, or suggestions, please file them in the Issues section of this repository. I'm still in the process of building it, so any suggestions that would make it more useable are welcome.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LowDb.Net:
Package | Downloads |
---|---|
LowDb.Net.Browser
A .NET implementation of the LowDB file-based database for use in applications and experiments that only require a minimal database implementation. Supports JSON-based database in web storage. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Please find the detailed release notes at https://github.com/d20Tek/lowdb.net/ReleaseNotes.md