Glesity.Wasm.Sqlite 0.2.0-alpha

This is a prerelease version of Glesity.Wasm.Sqlite.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Glesity.Wasm.Sqlite --version 0.2.0-alpha
                    
NuGet\Install-Package Glesity.Wasm.Sqlite -Version 0.2.0-alpha
                    
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="Glesity.Wasm.Sqlite" Version="0.2.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Glesity.Wasm.Sqlite" Version="0.2.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="Glesity.Wasm.Sqlite" />
                    
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 Glesity.Wasm.Sqlite --version 0.2.0-alpha
                    
#r "nuget: Glesity.Wasm.Sqlite, 0.2.0-alpha"
                    
#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 Glesity.Wasm.Sqlite@0.2.0-alpha
                    
#: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=Glesity.Wasm.Sqlite&version=0.2.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Glesity.Wasm.Sqlite&version=0.2.0-alpha&prerelease
                    
Install as a Cake Tool

Glesity.Wasm.Sqlite

NuGet License: MIT

Zero-reflection, NativeAOT-LLVM SQLite engine for .NET WebAssembly (wasi-wasm), built on top of Glesity.Wasm.Data.Abstractions.

Bundles a statically compiled SQLite (libsqlite3.a v3.54.1), MSBuild targets for automatic DirectPInvoke linking, and a Roslyn Source Generator that emits AOT-safe ISqlRepository<TEntity, TKey> implementations without runtime reflection.

Features

  • Zero Reflection & Full Trimming Safe: Built with [LibraryImport] and IIncrementalGenerator, compatible with <PublishTrimmed>true</PublishTrimmed> and <TrimMode>full</TrimMode>.
  • Self-Contained WASI Native Library: Ships runtimes/wasi-wasm/native/libsqlite3.a and MSBuild buildTransitive targets that automatically wire DirectPInvoke and NativeLibrary into ilc and wasm-ld.
  • Unified Data Abstractions: Uses [DataEntity], [PrimaryKey], [Column], [Index], and [Ignore] from Glesity.Wasm.Data.Abstractions to generate strongly typed table wrappers implementing ISqlRepository<TEntity, TKey>, plus Insert, InsertRange, FindBy<Pk>, and DeleteBy<Pk>.
  • Unified Exception Hierarchy: Maps SQLite result codes to WasmDataConstraintException, WasmDataConnectionException, and SqliteException : WasmDataException.
  • WASI VFS & In-Memory Support: Works with both :memory: databases and persistent files inside WASI preopened directories (e.g., /data/app.db).

Installation

Add the NuGet package to your wasi-wasm project:

dotnet add package Glesity.Wasm.Sqlite --prerelease

Note: Prebuilt libsqlite3.a for wasi-wasm is bundled inside the package - consumers do not need to install Bytecode Alliance wasi-sdk manually. Your target project only needs .NET NativeAOT-LLVM (wasi-wasm) configured.

Your WASI project should target net11.0 with wasi-wasm and BytecodeAlliance.Componentize.DotNet.Wasm.SDK:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>

    <ImplicitUsings>enable</ImplicitUsings>

    <SelfContained>true</SelfContained>
    <UseAppHost>false</UseAppHost>

    <InvariantGlobalization>true</InvariantGlobalization>

    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>full</TrimMode>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Glesity.Wasm.Sqlite" Version="0.2.0-alpha" />

    <PackageReference
      Include="BytecodeAlliance.Componentize.DotNet.Wasm.SDK"
      Version="0.8.0-preview00011" />

    <PackageReference
      Condition="$([MSBuild]::IsOSPlatform('Linux'))"
      Include="runtime.linux-x64.Microsoft.DotNet.ILCompiler.LLVM"
      Version="10.0.0-rc.1.26306.1" />

    <PackageReference
      Condition="$([MSBuild]::IsOSPlatform('Windows'))"
      Include="runtime.win-x64.Microsoft.DotNet.ILCompiler.LLVM"
      Version="10.0.0-rc.1.26306.1" />

    <PackageReference
      Condition="$([MSBuild]::IsOSPlatform('OSX'))"
      Include="runtime.osx-x64.Microsoft.DotNet.ILCompiler.LLVM"
      Version="10.0.0-rc.1.26306.1" />
  </ItemGroup>

</Project>

Quick Start

1. Define a Model

Annotate your record or class with attributes from Glesity.Wasm.Data.Abstractions (no partial modifier required):

using Glesity.Wasm.Data.Abstractions;

namespace MyWasiApp;

[DataEntity("player_profiles")]
public record PlayerProfile(
    [PrimaryKey] Guid Uuid,
    [Column("player_name"), Index(IsUnique = true)] string Nickname,
    int Level,
    string? Role,
    DateTimeOffset LastSeen,
    [Ignore] string? SessionToken = null
);

Supported attributes (from Glesity.Wasm.Data.Abstractions):

  • [DataEntity("table_name")] - marks a type for table & ISqlRepository<TEntity, TKey> code generation.
  • [PrimaryKey(AutoIncrement = false)] - marks the primary key column and generates Find, Exists, Delete, FindBy<Property>, and DeleteBy<Property>.
  • [Column("custom_name", IsUnique = false)] - overrides the default snake_case column name and adds a UNIQUE constraint if requested.
  • [Index("idx_name", IsUnique = false)] - generates a CREATE [UNIQUE] INDEX IF NOT EXISTS statement executed during EnsureCreated().
  • [Ignore] - excludes a property from database mapping.

2. Query and Persist Data

using Glesity.Wasm.Data.Abstractions;
using Glesity.Wasm.Sqlite;
using MyWasiApp;

// Open a persistent database inside a WASI preopened directory (or ":memory:")
using var db = SqliteDatabase.Open("/data/game.db");

var profiles = db.PlayerProfiles();
profiles.EnsureCreated();

var id = Guid.NewGuid();
profiles.Insert(new PlayerProfile(id, "Steve", 10, null, DateTimeOffset.UtcNow));
profiles.Upsert(new PlayerProfile(id, "Steve", 42, "Builder", DateTimeOffset.UtcNow));

// Use driver-agnostic ISqlRepository<PlayerProfile, Guid> or typed helpers
ISqlRepository<PlayerProfile, Guid> repo = profiles;
PlayerProfile? found = repo.Find(id);
long total = repo.Count();

foreach (var profile in repo.All())
{
    Console.WriteLine($"{profile.Nickname}: lvl {profile.Level}");
}

3. Raw SQL, Transactions & Error Handling

For custom queries or multi-statement scripts, use SqliteDatabase and SqliteStatement directly:

using Glesity.Wasm.Data.Abstractions;
using Glesity.Wasm.Sqlite;

using var db = SqliteDatabase.Open(":memory:");

try
{
    db.InTransaction(() =>
    {
        db.Execute("CREATE TABLE kv (k TEXT PRIMARY KEY, v INTEGER);");

        using var stmt = db.Prepare("INSERT INTO kv (k, v) VALUES (?1, ?2);");
        stmt.Bind(1, "apples").Bind(2, 64).ExecuteNonQuery();
    });
}
catch (WasmDataConstraintException ex)
{
    Console.WriteLine($"Constraint violation ({ex.ErrorCode}): {ex.Message}");
}
catch (SqliteException ex)
{
    Console.WriteLine($"SQLite error {ex.ResultCode}: {ex.Message}");
}

Supported Types

C# Type SQLite Storage Class Notes
int, long, bool INTEGER bool stored as 0 / 1
double REAL
string TEXT UTF-8
Guid TEXT Formatted as "D" (xxxxxxxx-xxxx-...)
DateTimeOffset INTEGER Stored as Unix time milliseconds
byte[] BLOB
Nullable<T> / T? Nullable column Mapped to SQL NULL

Running with Wasmtime

When using file-backed databases, grant directory access to your WASI runtime:

wasmtime run --dir ./data::/data MyWasiApp.wasm

Building from Source

Native SQLite (libsqlite3.a) is compiled targeting WASI using the Bytecode Alliance WASI SDK (managed automatically via Nix in this repository):

nix develop
just test        # Run Source Generator unit tests
just test-wasi   # Compile to WASI WASM and run integration tests in wasmtime
just pack        # Build the self-contained .nupkg package

License

MIT

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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
0.4.2-alpha 65 11/30/2026
0.4.0-alpha 61 11/30/2026
0.3.0-alpha 58 11/29/2026
0.2.0-alpha 62 11/29/2026
0.1.0-alpha 63 11/28/2026