Skip to content

Commit d434ad3

Browse files
move drift package
1 parent fe85a70 commit d434ad3

20 files changed

+1595
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ To configure the monorepo for development run `melos prepare` after cloning
1010

1111
## Packages
1212

13-
See [./packages/sqlite_async](./packages/sqlite_async) for details.
13+
- [./packages/sqlite_async](./packages/sqlite_async) Creates asynchronous SQLite connections
14+
- [./packages/drify_sqlite_async](./packages/drift_sqlite_async/README.md) A Drift wrapper for `sqlite_async`
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
## 0.1.0-alpha.1
3+
4+
Initial release.

packages/drift_sqlite_async/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Journey Mobile, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/drift_sqlite_async/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# drift_sqlite_async
2+
3+
`drift_sqlite_async` allows using drift on an sqlite_async database - the APIs from both can be seamlessly used together in the same application.
4+
5+
Supported functionality:
6+
1. All queries including select, insert, update, delete.
7+
2. Transactions and nested transactions.
8+
3. Table updates are propagated between sqlite_async and Drift - watching queries works using either API.
9+
4. Select queries can run concurrently with writes and other select statements.
10+
11+
12+
## Usage
13+
14+
Use `SqliteAsyncDriftConnection` to create a DatabaseConnection / QueryExecutor for Drift from the sqlite_async SqliteDatabase:
15+
16+
```dart
17+
@DriftDatabase(tables: [TodoItems])
18+
class AppDatabase extends _$AppDatabase {
19+
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
20+
21+
@override
22+
int get schemaVersion => 1;
23+
}
24+
25+
Future<void> main() async {
26+
// The sqlite_async db
27+
final db = SqliteDatabase(path: 'example.db');
28+
// The Drift db
29+
final appdb = AppDatabase(db);
30+
}
31+
```
32+
33+
A full example is in the `examples/` folder.
34+
35+
For details on table definitions and using the database, see the [Drift documentation](https://drift.simonbinder.eu/).
36+
37+
## Transactions and concurrency
38+
39+
sqlite_async uses WAL mode and multiple read connections by default, and this
40+
is also exposed when using the database with Drift.
41+
42+
Drift's transactions use sqlite_async's `writeTransaction`. The same locks are used
43+
for both, preventing conflicts.
44+
45+
Read-only transactions are not currently supported in Drift.
46+
47+
Drift's nested transactions are supported, implemented using SAVEPOINT.
48+
49+
Select statements in Drift use read operations (`getAll()`) in sqlite_async,
50+
and can run concurrently with writes.
51+
52+
## Update notifications
53+
54+
sqlite_async uses SQLite's update_hook to detect changes for watching queries,
55+
and will automatically pick up changes made using Drift. This also includes any updates from custom queries in Drift.
56+
57+
Changes from sqlite_async are automatically propagated to Drift when using SqliteAsyncDriftConnection.
58+
These events are only sent while no write transaction is active.
59+
60+
Within Drift's transactions, Drift's own update notifications will still apply for watching queries within that transaction.
61+
62+
Note: There is a possibility of events being duplicated. This should not have a significant impact on most applications.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
targets:
2+
$default:
3+
builders:
4+
drift_dev:
5+
options:
6+
fatal_warnings: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:drift/drift.dart';
2+
import 'package:drift_sqlite_async/drift_sqlite_async.dart';
3+
import 'package:sqlite_async/sqlite_async.dart';
4+
5+
part 'main.g.dart';
6+
7+
class TodoItems extends Table {
8+
@override
9+
String get tableName => 'todos';
10+
11+
IntColumn get id => integer().autoIncrement()();
12+
TextColumn get description => text()();
13+
}
14+
15+
@DriftDatabase(tables: [TodoItems])
16+
class AppDatabase extends _$AppDatabase {
17+
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
18+
19+
@override
20+
int get schemaVersion => 1;
21+
}
22+
23+
Future<void> main() async {
24+
final db = SqliteDatabase(path: 'example.db');
25+
26+
// Example where the schema is managed manually
27+
await db.execute(
28+
'CREATE TABLE IF NOT EXISTS todos(id integer primary key, description text)');
29+
30+
final appdb = AppDatabase(db);
31+
32+
// Watch a query on the Drift database
33+
appdb.select(appdb.todoItems).watch().listen((todos) {
34+
print('Todos: $todos');
35+
});
36+
37+
// Insert using the Drift database
38+
await appdb
39+
.into(appdb.todoItems)
40+
.insert(TodoItemsCompanion.insert(description: 'Test Drift'));
41+
42+
// Insert using the sqlite_async database
43+
await db.execute('INSERT INTO todos(description) VALUES(?)', ['Test Direct']);
44+
45+
await Future.delayed(const Duration(milliseconds: 100));
46+
47+
await appdb.close();
48+
await db.close();
49+
}

packages/drift_sqlite_async/example/main.g.dart

+189
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)