Skip to main content
TursoDb provides a powerful, type-safe wrapper around libSQL for Blazor WebAssembly applications. It enables local-first development with SQLite stored in the browser, with optional synchronization to Turso Cloud.

Local-First

Data is stored locally in the browser using SQLite, ensuring your app works offline.

Cloud Sync

Optionally sync with Turso Cloud for multi-device and real-time collaboration.

Type-Safe

Entity attributes and strongly-typed DbSets provide compile-time safety.

Auto Schema

Tables and indexes are automatically created from your entity definitions.

Installation

1

Install the NuGet package

2

Install the Server package (required)

TursoDb requires Cross-Origin Isolation headers for SharedArrayBuffer support.
3

Configure middleware

Add the Cross-Origin Isolation middleware to your server’s Program.cs:
Program.cs
Cross-Origin Isolation may break third-party scripts, iframes, and CDN resources that aren’t configured for isolated contexts. Test thoroughly before deploying.

Quick Start

Define Your Entities

Use attributes to define how your C# classes map to SQLite tables:
Models/User.cs
Use [NotMapped] on properties you don’t want persisted to the database.

Create Your Database

Inherit from TursoDatabase and add TursoDbSet<T> properties for each entity:
Data/AppDatabase.cs

Register and Use

Entity Attributes

Maps a class to a specific table name in SQLite.
If omitted, the class name is used as the table name.
Marks a property as the primary key. Use AutoIncrement = true for auto-generated IDs.
Maps a property to a specific column name. Use snake_case for SQLite conventions.
Creates an index on the column for faster queries. Use Unique = true for unique constraints.
Excludes a property from database persistence.

CRUD Operations

Create

Read

Update

Delete

Query Builder

Build complex queries with the fluent query API:

Available Methods

Use ? placeholders for parameters to prevent SQL injection. Parameters are passed in order after the SQL clause.

Transactions

Use transactions for atomic operations that should succeed or fail together:
If you dispose the transaction without calling CommitAsync(), it automatically rolls back.

Prepared Statements

For frequently executed queries, use prepared statements for better performance:

Raw SQL

Execute raw SQL when you need full control:

Turso Cloud Sync

For multi-device sync and cloud backup, use TursoSyncDatabase:

Setup

Data/SyncedAppDatabase.cs
Never hardcode auth tokens in production code. Use configuration or secure storage.

Sync Operations

Sync Events

Subscribe to sync lifecycle events:

Sync Options

Cross-Origin Isolation

TursoDb requires Cross-Origin Isolation headers for SharedArrayBuffer support. The BlazorEssentials.Server package provides middleware to add these headers.

Basic Configuration

Advanced Configuration

Headers Added

Type Mapping

TursoDb automatically maps C# types to SQLite types:

Best Practices

Register your database as a scoped service for proper lifecycle management:
Connect to the database early in your app lifecycle, such as in App.razor or a layout component:
When using cloud sync, implement conflict resolution strategies for your use case. Consider using timestamps or version numbers to detect conflicts.

Troubleshooting

This error indicates Cross-Origin Isolation is not configured. Ensure:
  1. BlazorEssentials.Server is installed
  2. app.UseCrossOriginIsolation() is called before other middleware
  3. Headers are not being stripped by a proxy or CDN
Cross-Origin Isolation may break external scripts. Options:
  1. Use options.CoepPolicy = "credentialless" for less strict isolation
  2. Add script paths to options.ExcludePaths
  3. Ensure external resources have Cross-Origin-Resource-Policy headers
Verify your Turso Cloud credentials:
  1. Check SyncUrl format: libsql://[db-name]-[org].turso.io
  2. Ensure SyncAuthToken is valid and not expired
  3. Verify the database exists in your Turso dashboard

API Reference

TursoDatabase

Base class for local databases

TursoSyncDatabase

Sync-enabled database with Turso Cloud

TursoDbSet

Typed collection for entity CRUD operations

TursoQueryBuilder

Fluent query builder API