phorm_sqlite 1.3.0
phorm_sqlite: ^1.3.0 copied to clipboard
The SQLite driver and connection manager implementation for the PHORM.
PHORM SQLite ๐ #
SQLite driver for PHORM.
phorm_sqlite is the official SQLite driver and connection manager implementation for the PHORM ORM.
๐ก Tip: the PHORM Code Generator VS Code extension turns a plain Dart class into a fully annotated PHORM model in one click.
It implements the database interfaces from phorm (PhormDatabase, DatabaseExecutor) and handles connection lifecycles, background isolate execution, custom SQL functions, and smart schema migrations.
๐ฆ Role in PHORM Ecosystem #
The PHORM ORM is split into modular packages:
phorm_annotationsโ Database-agnostic annotations (@Schema,@Column,@ID) and logical type definitions.phormโ Driver-agnostic runtime engine containing CRUD APIs,WhereBuilderquery builder, soft deletes, and eager loading via JSON Aggregation.phorm_generatorโ Code generator (build_runner) that automates mixin, JSON, and runtime table configuration.phorm_sqlite(This Package) โ The SQLite driver. Implements connection pooling, background isolates (Native), WebAssembly persistence (Web), and smart migrations.
โก Key Features #
- ๐งต Non-Blocking Native Architecture โ Runs
sqlite3operations in a dedicated background DartIsolate. The Main/UI thread never stalls, even during heavy operations or massive query maps. - ๐ Seamless Web Support โ Out-of-the-box support for Flutter Web via WebAssembly (
sqlite3_web) and IndexedDB virtual filesystem persistence. Zero platform checks needed in your application. - ๐ก๏ธ Smart Migrations โ Idempotent, hash-tracked schema migration tracking via the internal
__phorm_migrationstable. - ๐ Custom SQL Functions โ Easy registration of Dart functions inside SQLite (e.g. native
REGEXPsupport). - ๐ SQLCipher Support โ Option to provide a
passwordparameter to encrypt native database files with SQLCipher. - ๐ Slow Query Profiling โ Custom logger hooks to trace and alert when queries exceed a specified duration.
โ๏ธ Installation #
Add phorm_sqlite to your pubspec.yaml:
dependencies:
phorm_sqlite: ^latest
# phorm and phorm_annotations are pulled in automatically
๐ Quick Start #
1. Initialize the DB Manager #
Create an instance of DB or DB.autoVersion to manage your SQLite connection:
import 'package:phorm_sqlite/phorm_sqlite.dart';
// Declare your tables (normally generated in .sql.g.dart)
final usersTable = Table<User>(...);
// Create the DB instance
final appDb = DB.autoVersion(
databaseName: 'my_app.db', // Will be placed in default OS app support directory
tables: [usersTable],
logQueries: true, // Print SQL queries to console
slowQueryThreshold: Duration(milliseconds: 150),
);
2. Configure Service Access #
Resolve services directly from your database manager:
// Resolves the PhormCore<User> CRUD service
final userService = appDb.service<User>();
// CRUD operations
await userService.insert(User(id: 'u1', name: 'Alice'));
final user = await userService.readOne('u1');
๐ ๏ธ DB Configuration API #
The DB constructor offers several tuning parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
databaseName |
String |
'app_database.db' |
The name of the file or ':memory:' for in-memory databases. |
version |
int |
(Required) | The targeted database schema version. |
tables |
List<Table> |
(Required) | The list of all registered Table schemas and migrations. |
customFunctions |
List<SqlFunction> |
[] |
List of custom Dart-implemented functions to register in SQLite. |
password |
String? |
null |
Password string for SQLCipher database encryption (Native only). |
logger |
PhormLogger? |
PhormConsoleLogger() |
Custom logger implementation for tracing db events. |
logQueries |
bool |
false |
Enables print logging of executed SQL queries and arguments. |
slowQueryThreshold |
Duration |
200ms |
Threshold duration after which a query is flagged as "slow" in logs. |
singleInstance |
bool |
true |
Caches and reuses connection instances for the same path. |
isolateThreshold |
int |
50 |
Row count threshold at which mapping is processed inside background isolates. |
DB.autoVersion #
A recommended factory constructor that automatically calculates the database schema version as the maximum targetVersion across all registered table migrations.
final db = DB.autoVersion(
databaseName: 'app.db',
tables: [usersTable, ordersTable],
);
๐งต Isolate-Based Architecture #
In standard Flutter database setups, queries and subsequent data mapping (fromJson) run on the Main/UI thread. Under high load, this causes UI "jank" (dropped frames).
phorm_sqlite resolves this by using an Isolate-based proxy router:
- Native Platforms: Spawns a background
Isolatethat owns the synchronoussqlite3connection. Commands are sent across ports, executing database writes/reads safely off the UI thread. - Flutter Web: Relies on WebAssembly (
WasmSqlite3) on the main thread. Since Dart isolates are simulated on Web, it utilizes direct async bindings to IndexedDB viaIndexedDbFileSystemto persist files across reloads under thephorm_prefix.
๐ Custom SQL Functions #
SQLite allows executing custom logic inside SQL queries by registering Dart functions.
import 'package:phorm_sqlite/phorm_sqlite.dart';
// Create a custom reverse function
final reverseFn = SqlFunction(
name: 'REVERSE_TEXT',
argumentCount: 1,
function: (args) {
if (args[0] == null) return null;
return args[0].toString().split('').reversed.join();
},
);
// Instantiate DB with functions registered
final db = DB(
version: 1,
tables: [usersTable],
customFunctions: [
reverseFn,
SqlFunction.regexp(), // Register native-like REGEXP support
],
);
You can now call these functions directly inside your SQL or queries:
SELECT REVERSE_TEXT(name) FROM users WHERE email REGEXP '.*@gmail\.com'
๐ SQLCipher Encryption #
To secure your application's offline data on native devices, pass a password parameter:
final db = DB.autoVersion(
databaseName: 'secured.db',
password: 'my-super-secret-passphrase',
tables: [usersTable],
);
Note: SQLCipher encryption is only available on Native IO platforms (iOS, Android, Desktop). It is silently ignored on Flutter Web due to browser-level WASM limitations.
๐ License #
MIT License
