knex_dart_sqlite

SQLite driver for knex_dart — execute queries against a SQLite database using the knex_dart query builder.

Pub Version License: MIT

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

See also

Libraries

knex_dart_sqlite
SQLite driver for knex_dart.