knex_dart_sqlite 0.4.0
knex_dart_sqlite: ^0.4.0 copied to clipboard
SQLite driver for knex_dart — connect and execute queries against a SQLite database using the knex_dart query builder.
knex_dart_sqlite #
SQLite driver for knex_dart — execute queries against a SQLite database using the knex_dart query builder.
Installation #
dependencies:
knex_dart_sqlite: ^0.4.0
Usage #
import 'package:knex_dart_sqlite/knex_dart_sqlite.dart';
// Pick one connection style for your app:
final db = await KnexSQLite.connect(filename: 'app.db');
// final db = await KnexSQLite.connect(filename: ':memory:');
// final db = await KnexSQLite.connect(
// filename: 'app.db',
// webStorageMode: 'auto', // auto | opfs | indexedDb | memory
// );
// Schema
await db.executeSchema(
(schema) {
schema.createTable('users', (t) {
t.increments('id');
t.string('name').notNullable();
t.string('email').unique();
t.timestamps();
});
},
);
// INSERT
await db.insert(
db('users').insert({'name': 'Alice', 'email': 'alice@example.com'}),
);
// SELECT
final users = await db.select(
db('users').where('active', '=', true).orderBy('name'),
);
// UPDATE
await db.update(
db('users').where('id', '=', 1).update({'name': 'Bob'}),
);
// DELETE
await db.delete(
db('users').where('id', '=', 1).delete(),
);
// Transactions
await db.trx((trx) async {
await trx.insert(trx('accounts').insert({'balance': 100}));
await trx.update(trx('accounts').where('id', '=', 1).update({'balance': 0}));
});
// Reactive query watching
final sub = db.watch(
db('users').where('active', '=', true).orderBy('name'),
debounce: const Duration(milliseconds: 100),
).listen((rows) {
print('Active users changed: ${rows.length}');
});
await sub.cancel();
await db.close();
SQLite-specific features #
?positional placeholders- Double-quoted identifier quoting
- In-memory database support (
:memory:) - Browser/WASM storage modes:
memory,indexedDb,opfs,auto - JSON operators via
json_extract() - Nested transactions via savepoints
- Top-level transaction serialization on SQLite's single connection
- Reactive
watch()query streams
webStorageMode is only supported on web/WASM. On native SQLite, passing it to
connect() throws UnsupportedError.
Documentation #
- Docs home: https://docs.knex.mahawarkartikey.in/
- Transactions: https://docs.knex.mahawarkartikey.in/query-building/transactions
- Migrations: https://docs.knex.mahawarkartikey.in/migration/migrations
- Schema Builder: https://docs.knex.mahawarkartikey.in/query-building/schema-builder
See also #
- knex_dart — core package
- knex_dart_postgres
- knex_dart_mysql