https://github.com/hoc081098/sqlbrite
🌼 RxDart Reactive stream sqflite(sqlite) for Flutter - Sqlbrite for flutter - A lightweight wrapper around sqflite which introduces reactive stream semantics to SQL operations. https://pub.dev/packages/sqlbrite
https://github.com/hoc081098/sqlbrite
flutter flutter-reactive-sqflite flutter-reactive-storage flutter-sqflite flutter-sqlbrite flutter-sqlite rxdart rxdart-bloc rxdart-epic rxdart-ext rxdart-extensions rxdart-flutter rxdart-helper rxdart-redux sqlbrite sqlbrite-dbflow sqlbrite-rxjava sqlbrite-sample
Last synced: 4 months ago
JSON representation
🌼 RxDart Reactive stream sqflite(sqlite) for Flutter - Sqlbrite for flutter - A lightweight wrapper around sqflite which introduces reactive stream semantics to SQL operations. https://pub.dev/packages/sqlbrite
- Host: GitHub
- URL: https://github.com/hoc081098/sqlbrite
- Owner: hoc081098
- License: mit
- Created: 2019-04-13T09:19:53.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-02T20:13:02.000Z (5 months ago)
- Last Synced: 2024-12-09T20:22:09.968Z (5 months ago)
- Topics: flutter, flutter-reactive-sqflite, flutter-reactive-storage, flutter-sqflite, flutter-sqlbrite, flutter-sqlite, rxdart, rxdart-bloc, rxdart-epic, rxdart-ext, rxdart-extensions, rxdart-flutter, rxdart-helper, rxdart-redux, sqlbrite, sqlbrite-dbflow, sqlbrite-rxjava, sqlbrite-sample
- Language: Dart
- Homepage: https://pub.dev/packages/sqlbrite
- Size: 371 KB
- Stars: 30
- Watchers: 2
- Forks: 5
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
# SQL Brite 
## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098)
[](https://github.com/hoc081098/sqlbrite/actions/workflows/flutter.yml)
[](https://github.com/hoc081098/sqlbrite/actions/workflows/build-example.yml)
[](https://pub.dev/packages/sqlbrite)
[](https://pub.dev/packages/sqlbrite)
[](https://travis-ci.com/hoc081098/sqlbrite)
[](https://codecov.io/gh/hoc081098/sqlbrite)
[](https://opensource.org/licenses/MIT)
[](https://github.com/dart-lang/pedantic)
[](https://hits.seeyoufarm.com)- Reactive stream wrapper around `sqflite` for Flutter inspired by [sqlbrite](https://github.com/square/sqlbrite)
- Streaming sqflite
- RxDart reactive stream sqflite for Flutter
- A lightweight wrapper around sqflite which introduces reactive stream semantics to SQL operations.## Getting Started
1. Depend on it: In your flutter project, add the dependency to your `pubspec.yaml`
```yaml
dependencies:
...
sqlbrite:
```2. Install it: You can install packages from the command line with Flutter:
```shell script
$ flutter packages get
```3. Import it: Now in your Dart code, you can use:
```dart
import 'package:sqlbrite/sqlbrite.dart';
```## Usage
### 1. Wrap your database in a `BriteDatabase`:
```dart
final Database db = await openDb();
final briteDb = BriteDatabase(db);
final briteDb = BriteDatabase(db, logger: null); // disable logging.
```### 2. Using
- The `BriteDatabase.createQuery` method is similar to `Database.query`. Listen to the returned `Stream` which will immediately notify with a `Query` to run.
- These queries will run once to get the current data, then again whenever the given table is modified though the `BriteDatabase`.#### Create entity model
```dart
class Entity {
factory Entity.fromJson(Map map) { ... }
factory Entity.empty() { ... }Map toJson() { ... }
}
```#### Use `mapToOne` extension method on `Stream`
```dart
// Emits a single row, emit error if the row doesn't exist or more than 1 row in result set.
final Stream singleQuery$ = briteDb.createQuery(
'table',
where: 'id = ?',
whereArgs: [id],
limit: 1,
).mapToOne((row) => Entity.fromJson(row));
```#### Use `mapToOneOrDefault` extension method on `Stream`
```dart
// Emits a single row, or the given default value if the row doesn't exist, or emit error if more than 1 row in result set
final Stream singleOrDefaultQuery$ = briteDb.createQuery(
'table',
where: 'id = ?',
whereArgs: [id],
limit: 1,
).mapToOneOrDefault(
(row) => Entity.fromJson(row),
defaultValue: Entity.empty()
);
```#### Use `mapToList` extension method on `Stream`
```dart
// Emits a list of rows.
final Stream> listQuery$ = briteDb.createQuery(
'table',
where: 'name LIKE ?',
whereArgs: [queryName],
).mapToList((row) => Entity.fromJson(row));
```#### Same API like `Database`
```dart
// will trigger query stream again
briteDb.insert(
'table',
Entity(...).toJson()
);// will trigger query stream again
briteDb.update(
'table',
Entity(...).toJson(),
where: 'id = ?',
whereArgs: [id],
);// will trigger query stream again
briteDb.update(
'table',
where: 'id = ?',
whereArgs: [id],
);```
#### Full power of `RxDart` operators
- You can use RxDart operators to control the frequency of notifications to subscribers.
- The full power of RxDart's operators are available for combining, filtering, and triggering any number of queries and data changes.```dart
briteDb
.createQuery(
'table',
where: 'name LIKE ?',
whereArgs: [queryName],
)
.debounceTime(const Duration(milliseconds: 500))
.where(filterQuery) // query is lazy, this lets you not even execute it if you don't need to
.mapToList((row) => Entity.fromJson(row))
.listen(updateUI);
```## Philosophy
SQL Brite's only responsibility is to be a mechanism for coordinating and composing the notification
of updates to tables such that you can update queries as soon as data changes.This library is not an ORM. It is not a type-safe query mechanism. It's not going to perform database migrations for you.
## License
MIT License
Copyright (c) 2019 - 2022 Petrus Nguyễn Thái Học