Path: redb/redb.SQLite/Extensions/SqliteOptionsExtensions.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.SQLite/Extensions/SqliteOptionsExtensions.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using redb.Core;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models.Configuration;
using redb.Core.Models.Contracts;
using redb.Core.Models.Security;
using redb.Core.Providers;
using redb.Core.Query;
using redb.Core.Serialization;
using redb.Core.Services;
using redb.SQLite.Data;
using redb.SQLite.Providers;
using redb.SQLite.Sql;
namespace redb.SQLite.Extensions;
///
/// SQLite provider extensions for RedbOptionsBuilder.
///
public static class SqliteOptionsExtensions
{
///
/// Configures REDB to use SQLite as the database provider.
/// For Pro version, use AddRedbPro() from redb.SQLite.Pro package.
///
/// The options builder.
/// SQLite connection string. If null, must be set in Configure().
/// The options builder for chaining.
///
/// services.AddRedb(options => options
/// .UseSqlite("Host=localhost;Database=mydb;Username=user;Password=pass")
/// .Configure(c => c.EnablePropsCache = true));
///
public static RedbOptionsBuilder UseSqlite(
this RedbOptionsBuilder builder,
string? connectionString = null)
{
var infrastructure = (IRedbOptionsBuilderInfrastructure)builder;
if (!string.IsNullOrEmpty(connectionString))
infrastructure.Configuration.ConnectionString = connectionString;
infrastructure.SetProviderRegistration((services, config, isPro) =>
{
if (isPro)
throw new InvalidOperationException(
"Pro version requires redb.SQLite.Pro package. Use AddRedbPro() from redb.SQLite.Pro.Extensions.");
RegisterOpenSourceServices(services, config);
});
return builder;
}
///
/// Registers OpenSource SQLite services.
/// Called by UseSqlite() and can be used by Pro version as fallback.
///
public static void RegisterOpenSourceServices(IServiceCollection services, RedbServiceConfiguration config)
{
// Free tier loads the native SQLite extension that hosts the server-side SQL
// functions (get_object_json, pvt_build_*_sql, ...). Honour an explicit override
// (REDB_SQLITE_EXTENSION env / manual NativeExtensionPath) first; otherwise
// auto-locate the binary shipped in this package next to the running app.
// Pro locates it too: it reads objects in C#, but the JSON of an object is get_object_json.
Data.SqliteDataSource.NativeExtensionPath ??= Data.SqliteDataSource.LocatePackagedExtension();
// DataSource and Context
if (!string.IsNullOrEmpty(config.ConnectionString))
{
// Factory registration: the container OWNS the data source it creates and disposes it
// on shutdown, releasing the connection pool (an instance registration is never
// disposed by MS DI - the pool would outlive the container and keep the file locked).
services.AddSingleton(_ => Data.SqliteDataSource.Create(config.ConnectionString, config.StringCollation != null, config.EnableLazyReferences));
services.AddScoped(sp =>
new SqliteRedbContext(sp.GetRequiredService()));
}
// Security
services.AddScoped(sp =>
AmbientSecurityContext.GetOrCreateDefault());
// SQL Dialect
services.AddSingleton();
// Core providers
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
// Query providers
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped(sp => sp.GetRequiredService().ListProvider); // one per scope: the service's own
services.AddScoped();
// Background deletion service (singleton + hosted service)
services.AddSingleton();
services.AddSingleton(sp => sp.GetRequiredService());
services.AddHostedService(sp => sp.GetRequiredService());
// Main service
services.AddScoped();
}
}