|
| 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  💬</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> |
0 commit comments