Glesity.Wasm.Sqlite
0.2.0-alpha
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
<PackageReference Include="Glesity.Wasm.Sqlite" Version="0.2.0-alpha" />
<PackageVersion Include="Glesity.Wasm.Sqlite" Version="0.2.0-alpha" />
<PackageReference Include="Glesity.Wasm.Sqlite" />
paket add Glesity.Wasm.Sqlite --version 0.2.0-alpha
#r "nuget: Glesity.Wasm.Sqlite, 0.2.0-alpha"
#:package Glesity.Wasm.Sqlite@0.2.0-alpha
#addin nuget:?package=Glesity.Wasm.Sqlite&version=0.2.0-alpha&prerelease
#tool nuget:?package=Glesity.Wasm.Sqlite&version=0.2.0-alpha&prerelease
Glesity.Wasm.Sqlite
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]andIIncrementalGenerator, compatible with<PublishTrimmed>true</PublishTrimmed>and<TrimMode>full</TrimMode>. - Self-Contained WASI Native Library: Ships
runtimes/wasi-wasm/native/libsqlite3.aand MSBuildbuildTransitivetargets that automatically wireDirectPInvokeandNativeLibraryintoilcandwasm-ld. - Unified Data Abstractions: Uses
[DataEntity],[PrimaryKey],[Column],[Index], and[Ignore]fromGlesity.Wasm.Data.Abstractionsto generate strongly typed table wrappers implementingISqlRepository<TEntity, TKey>, plusInsert,InsertRange,FindBy<Pk>, andDeleteBy<Pk>. - Unified Exception Hierarchy: Maps SQLite result codes to
WasmDataConstraintException,WasmDataConnectionException, andSqliteException : 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.aforwasi-wasmis bundled inside the package - consumers do not need to install Bytecode Alliancewasi-sdkmanually. 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 generatesFind,Exists,Delete,FindBy<Property>, andDeleteBy<Property>.[Column("custom_name", IsUnique = false)]- overrides the defaultsnake_casecolumn name and adds aUNIQUEconstraint if requested.[Index("idx_name", IsUnique = false)]- generates aCREATE [UNIQUE] INDEX IF NOT EXISTSstatement executed duringEnsureCreated().[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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Glesity.Wasm.Data.Abstractions (>= 0.1.0-alpha)
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 |