Skip to content

Fix web support docs, add example/multiplatform #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
working-directory: sqlite3

- name: Analyze
run: dart analyze --fatal-infos
run: dart analyze --fatal-infos lib/ test/
working-directory: ${{ matrix.package }}

test:
Expand Down
13 changes: 7 additions & 6 deletions sqlite3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ import 'package:sqlite3/common.dart';
import 'package:sqlite3/wasm.dart';

Future<WasmSqlite3> loadSqlite() async {
final fs = await IndexedDbFileSystem.open('my_app');

return await WasmSqlite3.loadFromUrl(
Uri.parse('sqlite.wasm'),
environment: SqliteEnvironment(fileSystem: fs),
);
final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
final fileSystem = await IndexedDbFileSystem.open(dbName: 'my_app');
sqlite.registerVirtualFileSystem(fileSystem, makeDefault: true);
return sqlite;
}
```

Expand All @@ -127,6 +125,9 @@ An example for such web folder is in `example/web/` of this repo.
To view the example, copy a compiled `sqlite3.wasm` file to `web/sqlite3.wasm` in this directory.
Then, run `dart run build_runner serve example:8080` and visit `http://localhost:8080/web/` in a browser.

Another `example/multiplatform/` uses common interface to `sqlite3` on web and native platforms.
To run this example, merge its files into a Flutter app.

### Sharing code between web and a Dart VM

The `package:sqlite3/common.dart` library defines common interfaces that are implemented by both
Expand Down
2 changes: 2 additions & 0 deletions sqlite3/example/multiplatform/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://dart.dev/guides/language/analysis-options
include: package:lints/recommended.yaml
22 changes: 22 additions & 0 deletions sqlite3/example/multiplatform/db/db.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:sqlite3/common.dart' show CommonDatabase;
import 'sqlite3/sqlite3.dart' show openSqliteDb;

late CommonDatabase sqliteDb;

Future<void> openDb() async {
sqliteDb = await openSqliteDb();

final dbVersion =
sqliteDb.select('PRAGMA user_version').first['user_version'];

print('DB version: $dbVersion');

if (dbVersion == 0) {
sqliteDb.execute('''
BEGIN;
-- TODO
PRAGMA user_version = 1;
COMMIT;
''');
}
}
11 changes: 11 additions & 0 deletions sqlite3/example/multiplatform/db/sqlite3/native.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart'
show getApplicationDocumentsDirectory;
import 'package:sqlite3/common.dart' show CommonDatabase;
import 'package:sqlite3/sqlite3.dart' show sqlite3;

Future<CommonDatabase> openSqliteDb() async {
final docsDir = await getApplicationDocumentsDirectory();
final filename = path.join(docsDir.path, 'my_app.db');
return sqlite3.open(filename);
}
3 changes: 3 additions & 0 deletions sqlite3/example/multiplatform/db/sqlite3/sqlite3.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'unsupported.dart'
if (dart.library.js) 'web.dart'
if (dart.library.ffi) 'native.dart' show openSqliteDb;
5 changes: 5 additions & 0 deletions sqlite3/example/multiplatform/db/sqlite3/unsupported.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:sqlite3/common.dart' show CommonDatabase;

Future<CommonDatabase> openSqliteDb() async {
throw UnsupportedError('Sqlite3 is unsupported on this platform.');
}
12 changes: 12 additions & 0 deletions sqlite3/example/multiplatform/db/sqlite3/web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:sqlite3/common.dart' show CommonDatabase;
import 'package:sqlite3/wasm.dart' show IndexedDbFileSystem, WasmSqlite3;

Future<CommonDatabase> openSqliteDb() async {
const name = 'my_app';
// Please download `sqlite3.wasm` from https://github.com/simolus3/sqlite3.dart/releases
// into the `web/` dir of your Flutter app. See `README.md` for details.
final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
final fileSystem = await IndexedDbFileSystem.open(dbName: name);
sqlite.registerVirtualFileSystem(fileSystem, makeDefault: true);
return sqlite.open(name);
}
7 changes: 7 additions & 0 deletions sqlite3/example/multiplatform/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'db/db.dart' show openDb;

Future<void> main() async {
// WidgetsFlutterBinding.ensureInitialized();
await openDb();
// runApp(const App());
}
16 changes: 16 additions & 0 deletions sqlite3/example/multiplatform/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: sqlite3_multiplatform_example
description: Uses common interface to `sqlite3` on web and native platforms.
version: 0.1.0
publish_to: 'none'

environment:
sdk: ">=2.12.0 <4.0.0"

dependencies:
path: ^1.8.3
# path_provider: ^2.1.1
sqlite3: ^2.1.0
# sqlite3_flutter_libs: ^0.5.17

dev_dependencies:
lints: ^3.0.0