Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Initial Drift QueryExecutor #1

Merged
merged 9 commits into from
Feb 20, 2024
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
48 changes: 48 additions & 0 deletions .github/workflows/packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Packages check

on:
push:
branches:
- "**"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: "3.x"
channel: "stable"

- name: Install Melos
run: flutter pub global activate melos
- name: Install dependencies
run: melos bootstrap
- name: Check formatting
run: melos format:check:packages
- name: Lint
run: melos analyze:packages
- name: Publish dry-run
run: melos publish --dry-run --yes
- name: Check publish score
run: |
flutter pub global activate pana
./.github/workflows/scripts/run-pana.sh

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: "3.x"
channel: "stable"
- name: Install melos
run: flutter pub global activate melos
- name: Install dependencies
run: melos bootstrap
- name: Run tests
run: melos test
25 changes: 25 additions & 0 deletions .github/workflows/scripts/run-pana.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
set -e

# Get the root directory of your project
ROOT_DIR=$(pwd)

# Specify the path to the packages folder
PACKAGES_DIR="$ROOT_DIR/packages"

# Iterate over each package folder
for PACKAGE in "$PACKAGES_DIR"/*; do
# Check if it's a directory
if [ -d "$PACKAGE" ]; then
echo "Analyzing package in: $PACKAGE"

# Change into the package directory
cd "$PACKAGE" || exit

# Run the pana command
flutter pub global run pana --no-warning --exit-code-threshold 10

# Return to the root directory
cd "$ROOT_DIR" || exit
fi
done
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
.dart_tool/
.packages
build/
# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock

# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
Expand All @@ -25,3 +23,10 @@ doc/api/

.flutter-plugins
.flutter-plugins-dependencies

# IDE files
*.iml
.idea

# Test databases
*.db
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 PowerSync
Copyright (c) 2024 Journey Mobile, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# drift_sqlite_async
sqlite_async executor for drift

`drift_sqlite_async` allows using drift on an sqlite_async database - the APIs from both can be seamlessly used together in the same application.

See [./packages/drift_sqlite_async](./packages/drift_sqlite_async) for details.
31 changes: 31 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: drift_sqlite_async_monorepo

packages:
- demos/**
- packages/**

scripts:
format:
description: Format Dart code.
run: dart format .

format:check:packages:
description: Check formatting of Dart code in packages.
run: dart format --output none --set-exit-if-changed packages

analyze:packages:
description: Analyze Dart code in packages.
run: dart analyze packages --fatal-infos

test:
description: Run tests in a specific package.
run: dart test
exec:
concurrency: 1
packageFilters:
dirExists:
- test
# This tells Melos tests to ignore env variables passed to tests from `melos run test`
# as they could change the behaviour of how tests filter packages.
env:
MELOS_TEST: true
4 changes: 4 additions & 0 deletions packages/drift_sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

## 0.1.0-alpha.1

Initial release.
21 changes: 21 additions & 0 deletions packages/drift_sqlite_async/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Journey Mobile, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions packages/drift_sqlite_async/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# drift_sqlite_async

`drift_sqlite_async` allows using drift on an sqlite_async database - the APIs from both can be seamlessly used together in the same application.

Supported functionality:
1. All queries including select, insert, update, delete.
2. Transactions and nested transactions.
3. Table updates are propagated between sqlite_async and Drift - watching queries works using either API.
4. Select queries can run concurrently with writes and other select statements.


## Usage

Use `SqliteAsyncDriftConnection` to create a DatabaseConnection / QueryExecutor for Drift from the sqlite_async SqliteDatabase:

```dart
@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));

@override
int get schemaVersion => 1;
}

Future<void> main() async {
// The sqlite_async db
final db = SqliteDatabase(path: 'example.db');
// The Drift db
final appdb = AppDatabase(db);
}
```

A full example is in the `examples/` folder.

For details on table definitions and using the database, see the [Drift documentation](https://drift.simonbinder.eu/).

## Transactions and concurrency

sqlite_async uses WAL mode and multiple read connections by default, and this
is also exposed when using the database with Drift.

Drift's transactions use sqlite_async's `writeTransaction`. The same locks are used
for both, preventing conflicts.

Read-only transactions are not currently supported in Drift.

Drift's nested transactions are supported, implemented using SAVEPOINT.

Select statements in Drift use read operations (`getAll()`) in sqlite_async,
and can run concurrently with writes.

## Update notifications

sqlite_async uses SQLite's update_hook to detect changes for watching queries,
and will automatically pick up changes made using Drift. This also includes any updates from custom queries in Drift.

Changes from sqlite_async are automatically propagated to Drift when using SqliteAsyncDriftConnection.
These events are only sent while no write transaction is active.

Within Drift's transactions, Drift's own update notifications will still apply for watching queries within that transaction.

Note: There is a possibility of events being duplicated. This should not have a significant impact on most applications.
6 changes: 6 additions & 0 deletions packages/drift_sqlite_async/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
targets:
$default:
builders:
drift_dev:
options:
fatal_warnings: true
49 changes: 49 additions & 0 deletions packages/drift_sqlite_async/example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:drift/drift.dart';
import 'package:drift_sqlite_async/drift_sqlite_async.dart';
import 'package:sqlite_async/sqlite_async.dart';

part 'main.g.dart';

class TodoItems extends Table {
@override
String get tableName => 'todos';

IntColumn get id => integer().autoIncrement()();
TextColumn get description => text()();
}

@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));

@override
int get schemaVersion => 1;
}

Future<void> main() async {
final db = SqliteDatabase(path: 'example.db');

// Example where the schema is managed manually
await db.execute(
'CREATE TABLE IF NOT EXISTS todos(id integer primary key, description text)');

final appdb = AppDatabase(db);

// Watch a query on the Drift database
appdb.select(appdb.todoItems).watch().listen((todos) {
print('Todos: $todos');
});

// Insert using the Drift database
await appdb
.into(appdb.todoItems)
.insert(TodoItemsCompanion.insert(description: 'Test Drift'));

// Insert using the sqlite_async database
await db.execute('INSERT INTO todos(description) VALUES(?)', ['Test Direct']);

await Future.delayed(const Duration(milliseconds: 100));

await appdb.close();
await db.close();
}
Loading