Skip to content

Commit ea80324

Browse files
stevensJourneyDominicGBauerDominicGBauersimolus3mugikhan
authored
[Feature] Web Support (#26)
* [Alpha WIP] Web Support (#25) Co-authored-by: Dominic Gunther Bauer <[email protected]> Co-authored-by: DominicGBauer <[email protected]> Co-authored-by: Simon Binder <[email protected]> Co-authored-by: Mughees Khan <[email protected]>
1 parent 26315e1 commit ea80324

38 files changed

+1556
-91
lines changed

.github/workflows/release.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This triggers whenever a tagged release is pushed
2+
name: Compile Assets and Create Draft Release
3+
4+
on:
5+
push:
6+
tags:
7+
# Trigger on tags beginning with 'v'
8+
- 'v*'
9+
10+
jobs:
11+
release:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: write # Needed to create releases
15+
16+
steps:
17+
- name: Checkout Repository
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0 # Need to fetch the tags
21+
22+
- uses: dart-lang/setup-dart@v1
23+
24+
- name: Install dependencies
25+
run: dart pub get
26+
27+
- name: Compile WebWorker
28+
run: dart compile js -o assets/db_worker.js -O0 lib/src/web/worker/worker.dart
29+
30+
- name: Set tag name
31+
id: tag
32+
run: |
33+
tag=$(basename "${{ github.ref }}")
34+
echo "tag=$tag" >> $GITHUB_OUTPUT
35+
36+
- name: Create Release
37+
env:
38+
GH_TOKEN: ${{ github.token }}
39+
GH_REPO: ${{ github.repository }}
40+
run: |
41+
tag="${{ steps.tag.outputs.tag }}"
42+
body="Release $tag"
43+
gh release create --draft "$tag" --title "$tag" --notes "$body"
44+
45+
- name: Upload Worker
46+
env:
47+
GH_TOKEN: ${{ github.token }}
48+
GH_REPO: ${{ github.repository }}
49+
run: |
50+
gh release upload "${{ steps.tag.outputs.tag }}" assets/db_worker.js

.github/workflows/test.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ jobs:
5555
run: dart pub get
5656

5757
- name: Install SQLite
58-
run: ./scripts/install_sqlite.sh ${{ matrix.sqlite_version }} ${{ matrix.sqlite_url }}
58+
run: |
59+
./scripts/install_sqlite.sh ${{ matrix.sqlite_version }} ${{ matrix.sqlite_url }}
60+
mkdir -p assets && curl -LJ https://github.com/simolus3/sqlite3.dart/releases/download/sqlite3-2.4.3/sqlite3.wasm -o assets/sqlite3.wasm
61+
62+
- name: Compile WebWorker
63+
run: dart compile js -o assets/db_worker.js -O0 lib/src/web/worker/worker.dart
5964

6065
- name: Run Tests
6166
run: |
6267
export LD_LIBRARY_PATH=./sqlite-autoconf-${{ matrix.sqlite_version }}/.libs
63-
dart test
68+
dart test -p vm,chrome

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
# https://dart.dev/guides/libraries/private-files#pubspeclock.
77
pubspec.lock
88

9+
# Test assets
10+
assets
11+
912
.idea
13+
.vscode
14+
.devcontainer
1015
*.db
1116
*.db-*
1217
test-db

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
## 0.8.0
2+
3+
- Added web support (web functionality is in beta)
4+
15
## 0.7.0
26

37
- BREAKING CHANGE: Update all Database types to use a `CommonDatabase` interface.
48
- Update `openDB` and `open` methods to be synchronous.
59
- Fix `ArgumentError (Invalid argument(s): argument value for 'return_value' is null)` in sqlite3 when closing the database connection by upgrading to version 2.4.4.
610

11+
## 0.7.0-alpha.5
12+
13+
- The dependency for the `Drift` package is now removed in favour of using the new `sqlite3_web` package.
14+
- A new implementation for WebDatabase is used for SQL database connections on web.
15+
- New exports are added for downstream consumers of this package to extended custom workers with custom SQLite function capabilities.
16+
- Update minimum Dart SDK to 3.4.0
17+
18+
## 0.7.0-alpha.4
19+
20+
- Add latest changes from master
21+
22+
## 0.7.0-alpha.3
23+
24+
- Add latest changes from master
25+
26+
## 0.7.0-alpha.2
27+
28+
- Fix re-using a shared Mutex from <https://github.com/powersync-ja/sqlite_async.dart/pull/31>
29+
30+
## 0.7.0-alpha.1
31+
32+
- Added initial support for web platform.
33+
734
## 0.6.1
835

936
- Fix errors when closing a `SqliteDatabase`.

DEVELOPING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Developing Instructions
2+
3+
## Testing
4+
5+
Running tests for the `web` platform requires some preparation to be executed. The `sqlite3.wasm` and `db_worker.js` files need to be available in the Git ignored `./assets` folder.
6+
7+
See the [test action](./.github/workflows/test.yaml) for the latest steps.
8+
9+
On your local machine run the commands from the `Install SQLite`, `Compile WebWorker` and `Run Tests` steps.
10+
11+
## Releases
12+
13+
Web worker files are compiled and uploaded to draft Github releases whenever tags matching `v*` are pushed. These tags are created when versioning. Releases should be manually finalized and published when releasing new package versions.

README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# sqlite_async
1+
# sqlite_async
22

33
High-performance asynchronous interface for SQLite on Dart & Flutter.
44

@@ -15,13 +15,13 @@ query access.
1515

1616
## Features
1717

18-
* All operations are asynchronous by default - does not block the main isolate.
19-
* Watch a query to automatically re-run on changes to the underlying data.
20-
* Concurrent transactions supported by default - one write transaction and many multiple read transactions.
21-
* Uses WAL mode for fast writes and running read transactions concurrently with a write transaction.
22-
* Direct synchronous access in an isolate is supported for performance-sensitive use cases.
23-
* Automatically convert query args to JSON where applicable, making JSON1 operations simple.
24-
* Direct SQL queries - no wrapper classes or code generation required.
18+
- All operations are asynchronous by default - does not block the main isolate.
19+
- Watch a query to automatically re-run on changes to the underlying data.
20+
- Concurrent transactions supported by default - one write transaction and many multiple read transactions.
21+
- Uses WAL mode for fast writes and running read transactions concurrently with a write transaction.
22+
- Direct synchronous access in an isolate is supported for performance-sensitive use cases.
23+
- Automatically convert query args to JSON where applicable, making JSON1 operations simple.
24+
- Direct SQL queries - no wrapper classes or code generation required.
2525

2626
See this [blog post](https://www.powersync.co/blog/sqlite-optimizations-for-ultra-high-performance),
2727
explaining why these features are important for using SQLite.
@@ -74,4 +74,28 @@ void main() async {
7474
7575
await db.close();
7676
}
77-
```
77+
```
78+
79+
# Web
80+
81+
Note: Web support is currently in Beta.
82+
83+
Web support requires Sqlite3 WASM and web worker Javascript files to be accessible via configurable URIs.
84+
85+
Default URIs are shown in the example below. URIs only need to be specified if they differ from default values.
86+
87+
The compiled web worker files can be found in our Github [releases](https://github.com/powersync-ja/sqlite_async.dart/releases)
88+
The `sqlite3.wasm` asset can be found [here](https://github.com/simolus3/sqlite3.dart/releases)
89+
90+
Setup
91+
92+
```Dart
93+
import 'package:sqlite_async/sqlite_async.dart';
94+
95+
final db = SqliteDatabase(
96+
path: 'test.db',
97+
options: SqliteOptions(
98+
webSqliteOptions: WebSqliteOptions(
99+
wasmUri: 'sqlite3.wasm', workerUri: 'db_worker.js')));
100+
101+
```

lib/sqlite3_wasm.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Re-exports [sqlite3 WASM](https://pub.dev/packages/sqlite3) to expose sqlite3 without
2+
/// adding it as a direct dependency.
3+
library;
4+
5+
export 'package:sqlite3/wasm.dart';

lib/sqlite3_web.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Re-exports [sqlite3_web](https://pub.dev/packages/sqlite3_web) to expose sqlite3_web without
2+
/// adding it as a direct dependency.
3+
library;
4+
5+
export 'package:sqlite3_web/sqlite3_web.dart';

lib/sqlite3_web_worker.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Items required for extending custom web workers
2+
export './src/web/worker/worker_utils.dart';
3+
export './src/web/protocol.dart';

lib/src/common/sqlite_database.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ mixin SqliteDatabaseMixin implements SqliteConnection, SqliteQueries {
2424
@override
2525
Stream<UpdateNotification> get updates;
2626

27-
final StreamController<UpdateNotification> updatesController =
28-
StreamController.broadcast();
29-
3027
@protected
3128
Future<void> get isInitialized;
3229

0 commit comments

Comments
 (0)