Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Add usage example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Feb 20, 2024
1 parent d4f4b25 commit 90c6af9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
26 changes: 26 additions & 0 deletions packages/drift_sqlite_async/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ Supported functionality:
3. Table updates are propagated between sqlite_async and Drift - watching queries works using either API.
4. Select queries can run concurrently with writes and other select statements.


## Usage

Use `SqliteAsyncDriftConnection` to create a DatabaseConnection / QueryExecutor for Drift from the sqlite_async SqliteDatabase:

```dart
@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
@override
int get schemaVersion => 1;
}
Future<void> main() async {
// The sqlite_async db
final db = SqliteDatabase(path: 'example.db');
// The Drift db
final appdb = AppDatabase(db);
}
```

A full example is in the `examples/` folder.

For details on table definitions and using the database, see the [Drift documentation](https://drift.simonbinder.eu/).

## Transactions and concurrency

sqlite_async uses WAL mode and multiple read connections by default, and this
Expand Down
14 changes: 7 additions & 7 deletions packages/drift_sqlite_async/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class TodoItems extends Table {
}

@DriftDatabase(tables: [TodoItems])
class TodoDatabase extends _$TodoDatabase {
TodoDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
class AppDatabase extends _$AppDatabase {
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));

@override
int get schemaVersion => 1;
Expand All @@ -27,23 +27,23 @@ Future<void> main() async {
await db.execute(
'CREATE TABLE IF NOT EXISTS todos(id integer primary key, description text)');

final tdb = TodoDatabase(db);
final appdb = AppDatabase(db);

// Watch a query on the Drift database
tdb.select(tdb.todoItems).watch().listen((todos) {
appdb.select(appdb.todoItems).watch().listen((todos) {
print('Todos: $todos');
});

// Insert using the Drift database
await tdb
.into(tdb.todoItems)
await appdb
.into(appdb.todoItems)
.insert(TodoItemsCompanion.insert(description: 'Test Drift'));

// Insert using the sqlite_async database
await db.execute('INSERT INTO todos(description) VALUES(?)', ['Test Direct']);

await Future.delayed(const Duration(milliseconds: 100));

await tdb.close();
await appdb.close();
await db.close();
}
4 changes: 2 additions & 2 deletions packages/drift_sqlite_async/example/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions packages/drift_sqlite_async/example/with_migrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class TodoItems extends Table {
}

@DriftDatabase(tables: [TodoItems])
class TodoDatabase extends _$TodoDatabase {
TodoDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
class AppDatabase extends _$AppDatabase {
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));

@override
int get schemaVersion => 1;
Expand All @@ -36,23 +36,23 @@ Future<void> main() async {
await db.execute(
'CREATE TABLE IF NOT EXISTS todos(id integer primary key, description text)');

final tdb = TodoDatabase(db);
final appdb = AppDatabase(db);

// Watch a query on the Drift database
tdb.select(tdb.todoItems).watch().listen((todos) {
appdb.select(appdb.todoItems).watch().listen((todos) {
print('Todos: $todos');
});

// Insert using the Drift database
await tdb
.into(tdb.todoItems)
await appdb
.into(appdb.todoItems)
.insert(TodoItemsCompanion.insert(description: 'Test Drift'));

// Insert using the sqlite_async database
await db.execute('INSERT INTO todos(description) VALUES(?)', ['Test Direct']);

await Future.delayed(const Duration(milliseconds: 100));

await tdb.close();
await appdb.close();
await db.close();
}
4 changes: 2 additions & 2 deletions packages/drift_sqlite_async/example/with_migrations.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90c6af9

Please sign in to comment.