sqlbrite 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

SQL Brite alt text

Pub Build Status License: MIT

sqlbrite for flutter inspired by sqlbrite - 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
dependencies:
  ...
  sqlbrite: ^1.1.0
  1. Install it: You can install packages from the command line with Flutter:
$ flutter packages get
  1. Import it: Now in your Dart code, you can use:
import 'package:sqlbrite/sqlbrite.dart';

Usage #

Wrap your database in a BriteDatabase:

final Database db;
final briteDb = BriteDatabase(db);

The BriteDatabase.createQuery method is similar to Database.query. Listen to the returned Observable<Query> which will immediately notify with a Query to run.


class Entity {
  factory Entity.fromJson(Map<String, dynamic> map) {
    return Entity(...);
  }
}

// Emits a single row, doesn't emit if the row dosen't exist, emit error if more than 1 row in result set
final Observable<Entity> singleQuery$ = briteDb.createQuery(
  'table',
  where: 'id = ? LIMIT 1',
  whereArgs: [id],
).mapToOne((row) => Entity.fromJson(row));

// 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 Observable<Entity> singleOrDefaultQuery$ = briteDb.createQuery(
  'table',
  where: 'id = ? LIMIT 1',
  whereArgs: [id],
).mapToOneOrDefault((row) => Entity.fromJson(row));

// Emits a list of rows.
final Observable<List<Entity>> listQuery$ = briteDb.createQuery(
  'table',
  where: 'name LIKE ?',
  whereArgs: [queryName],
).mapToList((row) => Entity.fromJson(row));

Since queries are just regular RxDart Observable objects, operators can also be used 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.

briteDb
    .createQuery(
      'table',
      where: 'name LIKE ?',
      whereArgs: [queryName],
    )
    .debounceTime(const Duration(milliseconds: 500))
    .where((Query query) => /*filtering query*/) // query is lazy, this lets you not even execute it if you don't need to
    .transform(QueryToListStreamTransformer((Map<String, dynamic> row) => Entity.fromJson(row)))
    .listen((List<Entity> entities) => /*do something*/);

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 Petrus Nguyễn Thái Học

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.

1.1.0 - 04 May, 2019 #

  • Add document for public API.

  • Fix error when call method mapToOne and mapToList.

  • Minor changes.

1.0.1 - 03 May, 2019 #

  • Update rxdart dependency, fix error when call method mapToOne and mapToList.

1.0.0 - 03 May, 2019 #

  • Initial release.

example/README.md

Examples #

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  sqlbrite: ^1.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:sqlbrite/sqlbrite.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
32
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Oct 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
flutter 0.0.0
rxdart ^0.22.0 0.22.4
sqflite ^1.1.5 1.1.7+1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
path 1.6.4
sky_engine 0.0.99
synchronized 2.1.0+1
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito ^4.0.0
test_api ^0.2.4