sqflite_hooks 1.4.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

sqflite_hooks #

An extension to sqflite for adding hooks to database actions.

Getting Started #

In your flutter project add the dependency:

dependencies:
  ...
  sqflite_hooks: ^1.0.0

sqflite is included as part of this package but if you require a specific version, make sure this is added to your pubspec.yaml.

For help getting started with Flutter, view the online documentation.

Usage example #

The usage is no different to sqflite. Just be sure to import sqflite_hooks instead of sqflite:

import 'package:sqflite_hooks/sqflite_hooks.dart';

Then you can open and create your database like so:

var databasesPath = await getDatabasesPath();
var path = join(databasesPath, 'demo.db');
var database = await openDatabase(path, version: 1,
    onCreate: (database, version) async {
    await database.execute(
        '''CREATE TABLE Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL);''');
});

Using hooks #

The main difference when using sqflite_hooks is that you can add hooks to a database object. This allows you to easily run Dart code when actions occur on your database.

database.addHook(
    (event) => event.table == 'Users' && event.operation == DatabaseOperation.insert,
    (event) {
        // do something when a new record is inserted on the Users table
    },
    'NewUserHook');

The addHook method is as follows:

void addHook(bool Function(DatabaseEvent) predicate, Function(DatabaseEvent) hook, String key)
  • The predicate parameter is a Function which should return true if the hook should run. A predicate should not be marked as async.
  • The hook parameter is a Function which will run should the predicate return true. A hook can be async.
  • The key parameter is a String used for keeping track of hooks and removing them later.

The DatabaseEvent class contains the following properties:

final DatabaseOperation operation;
final String table;
final Map<String, dynamic> values;
final String where;
final List whereArgs;

The operation property is a DatabaseOperation enum which has the following values:

enum DatabaseOperation { insert, update, delete }

Hooks can be removed like so:

database.removeHook('NewUserHook');

Please note: all hooks for a predicate returning true will run and be await-ed before the database operation completes.

Transactions #

Hooks on a HookedDatabase will also be fired when using a HookedTransaction. You can get a HookedTransaction object as you would usually get a Transaction:

await database.transaction((transaction) async {
    // this gives you a HookedTransaction object but the type exposed is a Transaction

    // use the transaction object as normal
});

Hooks get fired for each operation performed on a HookedTransaction

If a transaction fails and hooks have already fired, you can provide a function to the named parameter onRollBack when creating a hooked transaction. onRollBack expects a Function(List<DatabaseEvent>) argument:

await database.transaction((transaction) async {
    ...
}, onRollBack: (events) {
    for (var event in events) {
        // undo anything that might have happened from fired hooks before the transaction failed
    }
});

Batches #

Hooks on a HookedDatabase will also be fired when using a HookedBatch. You can get a HookedBatch object as you would usually get a Batch:

var batch = database.batch(); // this returns a HookedBatch object but the type exposed is a Batch

Hooks get fired once the commit method has been called on the HookedBatch, but after all database operations for the batch has completed.

A HookedBatch is also provided when using transaction.batch() and has its hooks fired once the commit method is called.

1.4.0 #

Adding on rollback callback for failed transactions

1.3.0 #

Adding in hooks functionality to transactions's batch.

1.2.0 #

Adding in hooks functionality to transactions.

1.1.0 #

Adding in hooks functionality to batches.

1.0.0 #

Initial release

Use predicates to trigger Dart methods when actions happen on an Sqflite database.

You can use the following in your database predicates:

  • Table name
  • Database operation (insert, update, delete)
  • Values used (insert and update)
  • Where and whereargs

example/README.md

sqflite_hooks_example #

Demonstrates how to use the sqflite_hooks plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  sqflite_hooks: ^1.4.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:sqflite_hooks/sqflite_hooks.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
50
Learn more about scoring.

We analyzed this package on Jan 21, 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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
sqflite ^1.1.7+2 1.2.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
path 1.6.4
sky_engine 0.0.99
synchronized 2.1.1
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito any