Skip to content

Commit c0c058d

Browse files
committed
Prepare drift_sqlite for publishing
1 parent e46d597 commit c0c058d

9 files changed

Lines changed: 364 additions & 3 deletions

File tree

‎future/drift_sqlite/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0-alpha.0
2+
3+
- Initial version.

‎future/drift_sqlite/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Simon Binder
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.

‎future/drift_sqlite/README.md‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
This package adds SQLite support for [drift](https://pub.dev/packages/drift), a
2+
reactive persistence library for Flutter and Dart.
3+
4+
## Getting started
5+
6+
> [!TIP]
7+
> For Flutter apps, consider using the [drift_flutter](https://pub.dev/packages/drift_flutter) package.
8+
> It is based on `drift_sqlite` and adds defaults to make the cross-platform setup easier.
9+
10+
Begin by [setting up a drift database](https://drift.simonbinder.eu/setup/). Add
11+
a dependency on `drift_sqlite` to use it to open database connections:
12+
13+
```dart
14+
import 'package:drift/drift.dart';
15+
import 'package:drift_sqlite/drift_sqlite.dart';
16+
17+
void main() {
18+
final db = ExampleDatabase(
19+
// TODO: Platform-specific connection setup.
20+
);
21+
}
22+
23+
@DriftDatabase(tables: [Notes])
24+
final class ExampleDatabase extends _$ExampleDatabase {
25+
ExampleDatabase(super.implementation);
26+
// ...
27+
}
28+
```
29+
30+
On native platforms, use `sqliteConnectionPool` to open a high-performance pool of SQLite
31+
connections:
32+
33+
```dart
34+
// For native platforms
35+
final db = ExampleDatabase(sqliteConnectionPool(
36+
file: File('test.db')
37+
));
38+
```
39+
40+
On the web, download `sqlite3.wasm` and `drift_worker.js` from a [drift release](https://github.com/simolus3/drift/releases)
41+
into the `web/` directory of your app. Then, use this snippet to open databases:
42+
43+
```dart
44+
// For the web
45+
final db = ExampleDatabase(DriftConnection(
46+
dialect: SqliteDialect.new,
47+
openConnection: () async {
48+
final web = WebSqlite.open(
49+
wasmModule: '/sqlite3.wasm',
50+
workers: .defaultWorkers('/drift_worker.js'),
51+
controller: WasmDatabase.driftDatabaseController(),
52+
);
53+
final db = await web.connectToRecommended('my_database_name');
54+
return WasmDatabase.wrapDatabase(rawDb);
55+
}
56+
));
57+
```
58+
59+
## Sponsors
60+
61+
Drift is proudly Sponsored by [Stream 💙](https://getstream.io/chat/sdk/flutter/?utm_source=Moor&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=Moor_July2022_FlutterChatSDK_klmh22) and [PowerSync](https://powersync.com/?utm_source=drift&utm_campaign=drift_sponsorship).
62+
63+
<p align="center">
64+
<table>
65+
<tbody>
66+
<tr>
67+
<td align="center">
68+
<a href="https://getstream.io/chat/sdk/flutter/?utm_source=Moor&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=Moor_July2022_FlutterChatSDK_klmh22" target="_blank"><img width="250px" src="https://stream-blog.s3.amazonaws.com/blog/wp-content/uploads/fc148f0fc75d02841d017bb36e14e388/Stream-logo-with-background-.png"/></a><br/><span><a href="https://getstream.io/chat/sdk/flutter/?utm_source=Moor&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=Moor_July2022_FlutterChatSDK_klmh22" target="_blank">Try the Flutter Chat Tutorial &nbsp💬</a></span>
69+
</td>
70+
<td align="center">
71+
<a href="https://powersync.com/?utm_source=drift&utm_campaign=drift_sponsorship" target="_blank"><img width="250px" src="https://powersync.com/images/brand/powersync-logo-light-bg.png"/></a><br/>
72+
<span><a href="https://docs.powersync.com/integrations/supabase/guide?&utm_campaign=drift_sponsorship">Try PowerSync with Supabase</a></span>
73+
</td>
74+
</tr>
75+
</tbody>
76+
</table>
77+
</p>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:drift3_preview/drift.dart';
2+
import 'package:drift_sqlite/drift_sqlite.dart';
3+
import 'package:sqlite3/sqlite3.dart';
4+
5+
Future<DriftSession> openConnection() async {
6+
return SqliteConnection(sqlite3.openInMemory());
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'package:drift3_preview/drift.dart';
2+
3+
Future<DriftSession> openConnection() async {
4+
throw UnsupportedError('Example not supported on this platform');
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:drift3_preview/drift.dart';
2+
import 'package:drift_sqlite/web.dart';
3+
import 'package:sqlite3_web/sqlite3_web.dart';
4+
5+
Future<DriftSession> openConnection() async {
6+
final web = WebSqlite.open(
7+
wasmModule: 'https://cdn.jsdelivr.net/npm/sqlite3-web/assets/sqlite3.wasm',
8+
// For a real setup, consider using a database with web workers for better
9+
// performance and multi-tab support: https://drift.simonbinder.eu/platforms/web/
10+
workers: .defaultWorkers(''),
11+
controller: WasmDatabase.driftDatabaseController(),
12+
);
13+
14+
final rawDb = await web.connect('memory', .inMemoryLocal);
15+
return WasmDatabase.wrapDatabase(rawDb);
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:drift3_preview/drift.dart';
2+
import 'package:drift_sqlite/drift_sqlite.dart';
3+
4+
import 'connection_unsupported.dart'
5+
if (dart.library.js_interop) 'connection_web.dart'
6+
if (dart.library.ffi) 'connection_native.dart';
7+
8+
part 'main.g.dart';
9+
10+
void main() async {
11+
final db = ExampleDatabase(
12+
DriftConnection(dialect: SqliteDialect.new, openConnection: openConnection),
13+
);
14+
15+
final note = await db.createNote('Drift example!');
16+
print('Created note: $note');
17+
18+
final query = db.notesQueries.all();
19+
await for (final snapshot in query.watch()) {
20+
print('Notes in database: $snapshot');
21+
}
22+
}
23+
24+
class Notes extends Table {
25+
IntColumn get id => integer().autoIncrement();
26+
TextColumn get contents => text();
27+
}
28+
29+
@DriftDatabase(tables: [Notes])
30+
final class ExampleDatabase extends _$ExampleDatabase {
31+
ExampleDatabase(super.implementation);
32+
33+
@override
34+
int get schemaVersion => 1;
35+
36+
Future<Note> createNote(String contents) async {
37+
return await into(
38+
notes,
39+
).insertReturning(NotesCompanion.insert(contents: contents));
40+
}
41+
}

‎future/drift_sqlite/example/main.g.dart‎

Lines changed: 191 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎future/drift_sqlite/pubspec.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: drift_sqlite
22
description: SQLite dialect for drift.
3-
version: 1.0.0-alpha.0
3+
version: 0.1.0-alpha.0
44
repository: https://github.com/simolus3/drift
55
homepage: https://drift.simonbinder.eu/
66
issue_tracker: https://github.com/simolus3/drift/issues
@@ -10,8 +10,8 @@ environment:
1010
sdk: ^3.10.0
1111

1212
dependencies:
13-
drift:
14-
drift3_preview:
13+
drift: ^2.35.0
14+
drift3_preview: ^3.0.0-alpha.0
1515
meta: ^1.16.0
1616
collection: ^1.18.0
1717
sqlite3: ^3.1.6

0 commit comments

Comments
 (0)