CrumbleDB 2.1.1

dotnet add package CrumbleDB --version 2.1.1
                    
NuGet\Install-Package CrumbleDB -Version 2.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CrumbleDB" Version="2.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CrumbleDB" Version="2.1.1" />
                    
Directory.Packages.props
<PackageReference Include="CrumbleDB" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CrumbleDB --version 2.1.1
                    
#r "nuget: CrumbleDB, 2.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CrumbleDB@2.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CrumbleDB&version=2.1.1
                    
Install as a Cake Addin
#tool nuget:?package=CrumbleDB&version=2.1.1
                    
Install as a Cake Tool

CrumbleDB - A Lightweight JSON-based Database for .NET

Overview

CrumbleDB is a simple, persistent, in-memory database solution for .NET applications that stores data in JSON files. It provides a lightweight alternative to full-fledged database systems when you need basic persistence with minimal overhead.

Key features
  • Type-safe collections with generic support
  • Asynchronous operations
  • Automatic JSON serialization/deserialization
  • Transaction support with rollback capability
  • File-based persistence
  • Backups

Quick Start

Add the CrumbleDB package to your project via NuGet (package name would be specified here in a real scenario).

1. Initialize the Database
using CrumbleDB.Core;

// Open or create a database in the specified folder
var db = CrumbleDb.Open("path/to/database/folder");
2. Define Your Entity
using CrumbleDB.Entities;

public class User : CrumbleEntity
{
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
}
3. Work with Collections
// Get or create a collection
var users = await db.GetCollectionAsync<User>();

// Add items
users.Add(new User { Name = "Alice", Email = "alice@example.com", Age = 30 });
await users.AddForcedAsync(new User { Name = "Bob", Email = "bob@example.com", Age = 25 });

// Query items
var youngUsers = users.Values.Where(u => u.Age < 30).ToList();

// Update items
users.UpdateById(userId, user => user.Age++);

// Remove items
users.RemoveById(userId);

// Execute transaction
await users.ExecuteTransactionAsync(() =>
{
	collection.AddRange(Generate(100));
	collection.RemoveAll(x => x.Age >= 50);
	collection.ForEach((user) => user.Name += "_Updated");
});

Core Concepts

CrumbleEntity

All entities must inherit from CrumbleEntity which provides:

  • Automatic GUID-based ID generation
  • Value-based equality comparison
  • Hash code implementation
Collections

Collections (CrumbleCollection<T>) are typed containers that:

  • Store entities of type T where T : CrumbleEntity
  • Persist data to JSON files
  • Provide in-memory operations with optional immediate persistence
Database Operations

The CrumbleDbCore class provides:

  • Collection management
  • Backup creation
  • Collection purging
  • File information retrieval

API Reference

CrumbleCollection<T>

Basic Operations

  • Add(T item) - Adds an item to the collection (in-memory only)
  • AddForcedAsync(T item) - Adds an item and persists immediately
  • AddRange(IEnumerable<T> items) - Adds multiple items to the collection
  • AddRangeForcedAsync(IEnumerable<T> items) - Adds multiple items to the collection and immediately writes them to file
  • Remove(T item) - Removes an item (in-memory only)
  • RemoveForcedAsync(T item) - Removes an item and persists immediately
  • Clear() - Clears the collection (in-memory only)
  • ClearForcedAsync() - Clears and persists immediately
  • WriteAsync() - Asynchronously writes the collection to disk using the specified file path

Query Operations

  • Values - Gets all items as a read-only list
  • Count - Gets the number of items
  • IsEmpty - Checks if the collection is empty
  • ToDictionary() - Converts to a dictionary keyed by entity IDs

Advanced Operations

  • ExecuteTransactionAsync(Action action) - Executes an action with rollback on failure
  • ForEach(Action<T> action) - Executes a specified action on each element in the collection
  • ForEachForcedAsync(Action<T> action) - Executes a specified action on each element in the collection and immediately writes the collection to file
  • UpdateById(Guid id, Action<T> patch) - Updates an entity by ID
  • UpdateByIdForcedAsync(Guid id, Action<T> patch) - Updates an existing item in the collection by its ID and immediately writes it to file
  • UpdateAll(Predicate<T> predicate, Action<T> patch) - Updates all items in the collection that match the specified predicate using the specified patch function
  • UpdateAllForcedAsync(Predicate<T> predicate, Action<T> patch) - Updates all items in the collection that match the specified predicate using the specified patch function and immediately writes the collection to file
  • RemoveById(Guid id) - Removes an entity by ID
  • RemoveByIdForcedAsync(Guid id) - Removes an item by its ID and immediately writes the collection to file
  • Rewrite(IEnumerable<T> items) - Replaces all items in the collection
  • RewriteForcedAsync(IEnumerable<T> items) - Replaces the entire collection with the specified items and immediately writes them to file.
CrumbleDbCore

Collection Management

  • GetCollectionAsync<T>() - Gets or creates a collection
  • GetCollectionInfo<T>() - Gets file information about a collection
  • DropCollection<T>() - Deletes a collection file
  • PurgeCollectionAsync<T>() - Clears a collection file

Maintenance

  • CreateBackup<T>() - Creates a timestamped backup
  • RestoreBackupAsync<T>(string backupPath) - Restores the collection file for the specified type from a backup file
  • PurgeCollectionsAsync() - Clears all collection files
  • GetCollectionNames() - Lists all collection names

  • Not designed for high-throughput scenarios
  • No built-in indexing or advanced query capabilities
  • Entire collections are loaded into memory
  • No relationship management between collections
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.1 165 6/26/2025
2.1.0 167 6/25/2025
2.0.0 164 6/24/2025
1.0.0 163 5/27/2025

minor improvements and additions