DataMagic.LiteDatabase 9.0.0

dotnet add package DataMagic.LiteDatabase --version 9.0.0
                    
NuGet\Install-Package DataMagic.LiteDatabase -Version 9.0.0
                    
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="DataMagic.LiteDatabase" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DataMagic.LiteDatabase" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DataMagic.LiteDatabase" />
                    
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 DataMagic.LiteDatabase --version 9.0.0
                    
#r "nuget: DataMagic.LiteDatabase, 9.0.0"
                    
#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 DataMagic.LiteDatabase@9.0.0
                    
#: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=DataMagic.LiteDatabase&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DataMagic.LiteDatabase&version=9.0.0
                    
Install as a Cake Tool

DataMagic.LiteDatabase

I. Overview

DataMagic.LiteDatabase is an extension package of the DataMagic ecosystem that provides common functions and utilities for LiteDB database queries. This library simplifies working with LiteDB by offering extension methods for filtering, searching, and pagination operations on ILiteQueryable<T>, making it easier to build robust and maintainable data access layers.

The package integrates with the DataMagic.Abstractions library to provide a type-safe, fluent API for LiteDB operations including numeric filtering, date and date-time comparisons, result pagination, and applying partial updates to entities.

II. Functions

Extensions

DateFilterExtensions

Extension methods for filtering LiteDB queries based on date values.

Methods:

  • WithDateSearch<T, TValueType>(ILiteQueryable<T>, Expression<Func<T, TValueType>>, DateFilter)

    • Applies date filtering to an ILiteQueryable collection
    • Supports comparison operators: SmallerThan, SmallerThanEqualTo, GreaterThanEqualTo, GreaterThan, Equal
    • Returns: Filtered ILiteQueryable<T>
    • Usage: Filter entities by date fields with specific comparison operators
  • WithDateRangeSearch<T, TValueType>(ILiteQueryable<T>, Expression<Func<T, TValueType>>, DateRangeFilter)

    • Applies date range filtering (From/To) to an ILiteQueryable collection
    • Combines multiple date filters for range queries
    • Returns: Filtered ILiteQueryable<T>
    • Usage: Filter entities within a specific date range
DateTimeFilterExtensions

Extension methods for filtering LiteDB queries based on date-time values.

Methods:

  • WithDateTimeSearch<T, TValueType>(ILiteQueryable<T>, Expression<Func<T, TValueType>>, DateTimeFilter)

    • Applies date-time filtering to an ILiteQueryable collection
    • Supports comparison operators: SmallerThan, SmallerThanEqualTo, GreaterThanEqualTo, GreaterThan, Equal
    • Returns: Filtered ILiteQueryable<T>
    • Usage: Filter entities by date-time fields with specific comparison operators
  • WithDateTimeRangeSearch<T, TValueType>(ILiteQueryable<T>, Expression<Func<T, TValueType>>, DateTimeRangeFilter)

    • Applies date-time range filtering (From/To) to an ILiteQueryable collection
    • Combines multiple date-time filters for range queries
    • Returns: Filtered ILiteQueryable<T>
    • Usage: Filter entities within a specific date-time range
NumericSearchExtensions

Extension methods for filtering LiteDB queries based on numeric values.

Methods:

  • WithNumericSearch<T, TValueType>(ILiteQueryable<T>, Expression<Func<T, TValueType>>, NumericFilter<TValueType>)

    • Applies numeric filtering to an ILiteQueryable collection
    • Supports comparison operators: Equal, SmallerThan, SmallerThanEqualTo, GreaterThanEqualTo, GreaterThan
    • Validates numeric types at runtime
    • Returns: Filtered ILiteQueryable<T>
    • Usage: Filter entities by numeric fields with specific comparison operators
  • WithNumericRangeSearch<T, TValueType>(ILiteQueryable<T>, Expression<Func<T, TValueType>>, NumericRangeFilter<TValueType, TValueType>)

    • Applies numeric range filtering (From/To) to an ILiteQueryable collection
    • Combines multiple numeric filters for range queries
    • Returns: Filtered ILiteQueryable<T>
    • Usage: Filter entities within a specific numeric range
SearchResultExtensions

Extension methods for paginating and converting LiteDB queries to search results.

Methods:

  • ToSearchResultAsync<T>(ILiteQueryable<T>, IPager, CancellationToken)
    • Converts an ILiteQueryable to a paginated ISearchResult<T>
    • Handles total record counting and pagination using LiteDB Offset / Limit
    • Returns all items when no pager is supplied
    • Returns: Task<ISearchResult<T>>
    • Usage: Paginate LiteDB queries

Models

EntityUpdateBuilder<T>

A builder class that collects field updates in a fluent manner and applies them to an entity instance. Since LiteDB updates whole documents, the builder sets the modified properties on the entity, which can then be saved with ILiteCollection<T>.Update.

Methods:

  • Update<TField>(Expression<Func<T, TField>>, IUpdatableProperty<TField>)

    • Adds an update operation using a lambda expression
    • Only updates if the property has been modified
    • Returns: EntityUpdateBuilder<T> (for method chaining)
    • Usage: Build update operations with strongly-typed property selectors
  • Build(T)

    • Applies all collected updates to the given entity
    • Does nothing if no updates were added
    • Usage: Set the modified values on an entity before saving it
  • CountUpdatedFields()

    • Returns the number of fields that will be updated
    • Returns: int
    • Usage: Validate or log the number of fields being updated

III. Requirements

Dependencies

The DataMagic.LiteDatabase package requires the following dependencies:

  • DataMagic.Abstractions (Version 1.2.0)

    • Provides core abstractions, interfaces, and models
    • Includes filter models, operators, and pagination interfaces
  • LiteDB (Version 5.0.21)

    • Embedded NoSQL document database for .NET
    • Provides LiteDatabase, ILiteCollection<T> and ILiteQueryable<T>

Target Framework

  • .NET 9.0 (net9.0)

Installation

To install the package, add it to your project using:

dotnet add package DataMagic.LiteDatabase

Or add the following to your .csproj file:

<PackageReference Include="DataMagic.LiteDatabase" Version="9.0.0" />

Usage Examples

Numeric Filtering

using var database = new LiteDatabase("data.db");
var users = database.GetCollection<User>("users");

var numericFilter = new NumericFilter<int>(18, NumericComparisonOperators.GreaterThanEqualTo);
var query = users.Query()
    .WithNumericSearch(x => x.Age, numericFilter);

Date-Time Range Filtering

var dateRangeFilter = new DateTimeRangeFilter
{
    From = new DateTimeFilter(DateTime.Now.AddDays(-30), DateTimeComparisonOperators.GreaterThanEqualTo),
    To = new DateTimeFilter(DateTime.Now, DateTimeComparisonOperators.SmallerThanEqualTo)
};
var query = users.Query()
    .WithDateTimeRangeSearch(x => x.CreatedTime, dateRangeFilter);

Pagination

var pager = new DefaultPager(1, 20);
var result = await users.Query()
    .OrderBy(x => x.Name)
    .ToSearchResultAsync(pager);
// result.Items contains the paginated items
// result.TotalRecords contains the total count

Update Builder

var user = users.FindById(id);

var updateBuilder = new EntityUpdateBuilder<User>();
updateBuilder
    .Update(x => x.Name, updatableNameProperty)
    .Update(x => x.Email, updatableEmailProperty);

if (updateBuilder.CountUpdatedFields() > 0)
{
    updateBuilder.Build(user);
    users.Update(user);
}

License

Please refer to the repository license file for licensing information.

Contributing

Contributions are welcome! Please ensure all code follows the existing patterns and includes appropriate documentation.

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
9.0.0 97 11/2/2026