brick_sqlite 0.0.2

  • Readme
  • Changelog
  • Installing
  • new66

Brick SQLite Provider #

Local storage for Flutter apps using Brick.

Supported Query Configuration

params #

The following map exactly to their SQLite keywords. The values will be inserted into a SQLite statement without being prepared.

  • collate
  • having
  • groupBy
  • limit
  • offset
  • orderBy

As the values are directly inserted, this creates a rare exception where params uses the column name instead of the field name:

//given this field in my model
@Sqlite(name: 'last_name')
final String lastName;

// my query would use the column name in `params:` but not in `where:`
Query(
  where: [Where('lastName', 'Mustermann')],
  params: {'orderBy': 'last_name ASC'},
)

where: #

All fields and associations are supported. All Compare values are also supported without additional configuration.

Models #

Intelligent Migrations #

Whenever a new field(s) is added or removed from a connected model, Brick can automatically generate a migration for SQLite. For even less friction, running the watcher while actively developing will create the migration on model save:

flutter pub run build_runner watch

💡 While Brick guesses right most of the time, the migration should still be reviewed after it's created (for example, a DropTable might just be a RenameTable).

Fields #

Maps can be serialized, but they must be digestible by jsonEncode.

@Sqlite(name:) #

SQLite column names can be named per field except with associations. Using name: is strongly discouraged as Brick's naming consistency is reliable and easily managed through migrations.

@Sqlite(name: "full_name")
final String lastName;

Unsupported Field Types #

The following are not serialized to SQLite. However, unsupported types can still be accessed in the model as non-final fields.

  • Nested List<> e.g. <List<List<int>>>
  • Many-to-many associations

Testing #

Responses can be stubbed from a SqliteProvider with actual data using StubSqlite:

import 'package:brick_sqlite/testing.dart';
import 'package:my_app/app/repository.dart';

void main() {
  group("MySqliteProvider", () {
    setUpAll(() {
      StubSqlite(MyRepository().sqliteProvider, responses: {
        MyModel: [
          {'name': 'Thomas' }
          {'name': 'John' }
        ],
      });
    });
  });
}

If a model has an association via a primary key, the association must be manually made with the responses:

StubSqlite(MyRepository().sqliteProvider, responses: {
  User: [
    {
      'name': 'Thomas',
      // for clarity's sake, the table names are directly used here, but
      // they should always be accessed via adapters:
      // MyRepository().sqliteProvider.modelDictionary.adapterFor[User]
      InsertForeignKey.foreignKeyColumnName('User', 'Hat', foreignKeyColumn: 'hat'): 8
    },
  ],
  Hat: [
    {
      InsertTable.PRIMARY_KEY_COLUMN: 8,
      'color': Color.brown.index,
    }
  ],
});

⚠️ Stubbed data does not support Query#where or Query#param like native Sqlite. Stubbing should be employed in simple use cases such as simple association lookups or generating models (e.g. for testing a computed getter).

FAQ #

Can I specify a different table name? #

Table names, association column names, and primary key column names are managed by the package. They are currently unchangeable.

Memory Cache Provider #

The Memory Cache Provider is a key-value store that functions based on the SQLite primary keys to optimize the SQLite provider queries. This is especially effective for low-level associations. The provider only caches models it's aware of:

// app models: `User`, `Hat`
MemoryCacheProvider([Hat])
// `User` is never stored in memory

It is not recommended to use this provider with parent models that have child associations, as those children may be updated in the future without notifying the parent.

FAQ #

Why can't I declare a model argument? #

Due to an open analyzer bug, a custom model cannot be passed to the repository as a type argument.

Unreleased #

0.0.2 #

  • Fix linter hints

Use this package as a library

1. Depend on it

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


dependencies:
  brick_sqlite: ^0.0.2

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:brick_sqlite/memory_cache_provider.dart';
import 'package:brick_sqlite/sqlite.dart';
import 'package:brick_sqlite/testing.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

Format lib/memory_cache_provider.dart.

Run flutter format to format lib/memory_cache_provider.dart.

Format lib/sqlite.dart.

Run flutter format to format lib/sqlite.dart.

Format lib/src/sqlite/alter_column_helper.dart.

Run flutter format to format lib/src/sqlite/alter_column_helper.dart.

Format lib/src/sqlite/query_sql_transformer.dart.

Run flutter format to format lib/src/sqlite/query_sql_transformer.dart.

Format lib/src/testing/stub_sqlite.dart.

Run flutter format to format lib/src/testing/stub_sqlite.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and brick_sqlite.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
brick_core >=0.0.2 <1.0.0 0.0.3+1
brick_sqlite_abstract >=0.0.2 <1.0.0 0.0.2
flutter 0.0.0
logging ^0.11.3+2 0.11.4
meta >=1.1.6 <2.0.0 1.1.8
path >=1.6.3 <2.0.0 1.6.4
sqflite >=1.1.6 <2.0.0 1.2.0
synchronized ^2.1.0+1 2.1.1
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito ^4.0.0
test >=1.9.4 <2.0.0