sirius 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

logo

Sirius is an abstraction to SQFlite. The main goal is to avoid the need to write basic CRUD queries and abstract the process of creating and maintaining the SQLite tables by yourself.

How it works #

  1. Let your model extend DPAEntity
class Fruit extends DPAEntity {}

This makes your model consumable by the Sirius engine. Now, you can register the fields of the model class to be stored in the underlaying table. To do that, you have to override the registerFields() method.

  1. Register fields which have to be stored
int id;
String name; 

@override
void registerFields() {
  registerField("id", DataType.Integer, primaryKey: true);
  registerField("name", DataType.String);
}

In the example above, a table would be created, which holds 2 values, id and name, where id is the primary key.

  1. Define a Serialization

Sirius will use the serialized version of your model class to store it in the resulting table. To achieve this, the toMap() method has to be implemented

@override
Map<String, dynamic> toMap() {
  return {
    "id": id,
    "name": name,
  };
}
  1. Create a Factory

The factory is responsible for instantiating your model objects. It's main responsibility is to provide the repository a reference entity to fetch the field-data from.

class FruitFactory extends DPAFactory<Fruit> {
  @override
  Fruit fromMap(Map<String, dynamic> data) {
    if (!(data.containsKey("id") &&
        data.containsKey("name")))
      throw Exception("not all fields are present in the map");

    return construct(data["id"], data["name"]);
  }

  @override
  Fruit get entity => Fruit();
  
  Fruit construct(int id, String name) {
    Fruit f = entity;

    f.id = id;
    f.name = name;
    
    return f;
  }
}

The fromMap(..) method is used to deserialize the model class every time it is retrieved from the table. entity should instanciate a reference Object. It does not have to contain data, as it is used by the repository to determine the table structure.

In the example above, I added a construct(..) method to avoid the need of passing a Map each time i want to instantiate a Fruit. This step is optional.

  1. Create the Repository
class FruitRepository extends DPARepository<Fruit, FruitFactory> {
  FruitRepository() : super(FruitFactory());
}

The repository ties all ends together. The factory defined above gets used to create a table in the background, where the Fruit objects will be stored. All basic CRUD operations are called from the repository.

Use examples #

FruitRepository fruitRepository = FruitRepository();

Fruit apple = fruitRepository.entityFactory.construct(
  3,
  "Strawberry",
  "Red, small fruit which has 'Berry' in its name but is really a 'Nut'.",
);

fruitRepository.add(apple);

Fruit fruit = await fruitRepository.one({"id": 3});

List<Fruit> allFruits = await fruitRepository.all();

await fruitRepository.delete({"id": 3});

Here, the repository gets instantiated once. It is then used to:

  • get a reference to the FruitFactory
  • add a new Fruit object to the table
  • query all stored Fruit objects
  • get a specific Fruit object from the table
  • delete a specific Fruit object from the table

[0.0.3] - 10.03.2020 #

Added accessors to repositories to avoid have the user implement singletons in each repository.

[0.0.2] - 10.03.2020 #

Provided getter for the table name in the repository to build custom queries.

Bugfixes. Empty masks for restrictive queries (delete, update) can be empty to target all rows.

Moved example into one file to be prettier on pub.dev.

[0.0.1] - 13.12.2019 #

First release of the package with core functionality.

example/lib/main.dart

import 'package:example/add_fruit_dialog.dart';
import 'package:example/model/fruit.dart';
import 'package:example/model/fruit_repository.dart';
import 'package:flutter/material.dart';
import 'package:sirius/dpa.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  List<Fruit> fruits = [];

  Future<void> _initData() async {
    FruitRepository repo = DPA.repository(FruitRepository);
    fruits = await repo.all();

    setState(() {});
  }

  @override
  void initState() {
    FruitRepository();
    super.initState();
    _initData();
  }

  ///
  /// callback to the dialog to add the new defined fruit to the database and
  /// update the view
  ///
  void _addFruit(String name, String description, int weight) async {
    print("Adding Fruit($name, $description, $weight)");

    FruitRepository repo = DPA.repository(FruitRepository);

    Fruit fNew =
        repo.entityFactory.construct(name, description, weight: weight);
    await repo.add(fNew);

    fruits = await repo.all();

    setState(() {});
  }

  ///
  /// deletes all fruits stored by passing an empty mask to the delete(..)
  /// method in the repository
  ///
  void _deleteAll() async {
    FruitRepository repo = DPA.repository(FruitRepository);
    await repo.delete({});
    fruits = await repo.all();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'DPA Demo Application',
      home: Builder(
        builder: (BuildContext context) => Scaffold(
          appBar: AppBar(
            title: Text("DPA Test"),
          ),
          floatingActionButton: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              FloatingActionButton(
                child: Icon(Icons.delete),
                onPressed: _deleteAll,
              ),
              SizedBox(width: 8),
              FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) =>
                        AddFruitDialog(_addFruit),
                  );
                },
              ),
            ],
          ),
          body: DataTable(
            columns: [
              DataColumn(
                label: Text("Id"),
                numeric: true,
              ),
              DataColumn(label: Text("Name")),
              DataColumn(label: Text("Description")),
              DataColumn(
                label: Text("Weight"),
                numeric: true,
              ),
            ],
            rows: fruits
                .map(
                  (Fruit f) => DataRow(cells: [
                    DataCell(Text("${f.id}")),
                    DataCell(Text("${f.name}")),
                    DataCell(Text("${f.description}")),
                    DataCell(Text("${f.weight}")),
                  ]),
                )
                .toList(),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  sirius: ^0.0.3

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:sirius/configuration.dart';
import 'package:sirius/dpa.dart';
import 'package:sirius/folder/i_folder_provider.dart';
import 'package:sirius/folder/path_folder_provider.dart';
import 'package:sirius/folder/test_folder_provider.dart';
import 'package:sirius/i_dpa_entity.dart';
import 'package:sirius/i_dpa_factory.dart';
import 'package:sirius/i_dpa_repository.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
23
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:sirius/configuration.dart that imports:
  • package:sirius/folder/path_folder_provider.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform ios

Because:

  • package:sirius/configuration.dart that imports:
  • package:sirius/folder/path_folder_provider.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform linux

Because:

  • package:sirius/dpa.dart that imports:
  • package:sqflite/sqflite.dart that declares support for platforms: android, ios, macos

Package does not support Flutter platform macos

Because:

  • package:sirius/configuration.dart that imports:
  • package:sirius/folder/path_folder_provider.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform web

Because:

  • package:sirius/configuration.dart that imports:
  • package:sirius/folder/path_folder_provider.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: android, ios, linux, macos

Package does not support Flutter platform windows

Because:

  • package:sirius/configuration.dart that imports:
  • package:sirius/folder/path_folder_provider.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: android, ios, linux, macos

Package not compatible with SDK dart

Because:

  • sirius that is a package requiring null.

Maintenance suggestions

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.1.0 <3.0.0
flutter 0.0.0
path_provider ^1.5.1 1.6.11
sqflite ^1.1.8 1.3.1
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
intl 0.16.1
meta 1.1.8 1.2.2
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
sqflite_common 1.0.2+1
synchronized 2.2.0+1
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test