Restoque CRUD Builder

Restoque annotation/code generation for CRUD operations and json serialization.

Getting Started

This project intends to help in the creation of classes with the CRUD functions and json serialization.

Example

Customer.dart

import 'package:restoque_build/annotations.dart';
import 'package:livestore/database/appDatabase.dart';
import 'package:sembast/sembast.dart';

part 'Customer.g.dart';

@RestoqueEntity("Customer")
class Customer extends _Customer {
  @RestoqueEntityKey()
  @RestoquePropertyName(name: 'id_customer')
  int idCustomer;
  @RestoquePropertyName(name: 'customer_name')
  String customerName;

  Customer({this.idCustomer, this.customerName}) {
    _$Customer(this);
  }

  factory Customer.fromJson(Map<String, dynamic> json) =>
      _$CustomerFromJson(json);
}

After create the class execute the following command.

flutter packages run build_runner build

The following class will be created automatically.

Customer.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'Customer.dart';

// **************************************************************************
// RestoqueEntityGenerator
// **************************************************************************

var _Store = StoreRef<String, dynamic>("Customer");
Customer instance;
_$Customer(Customer _instance) {
  instance = _instance;
}

Future<DatabaseClient> get _db async => await AppDatabase.instance.database;

class _Customer {
  Map<String, dynamic> toJson() => <String, dynamic>{
        'id_customer': instance.idCustomer,
        'customer_name': instance.customerName,
      };

  Future Upsert() async {
    final db = await _db;
    await _Store.record(instance.idCustomer.toString()).put(db, toJson());
  }

  Future Delete() async {
    final finder = Finder(filter: Filter.byKey(instance.idCustomer.toString()));
    await _Store.delete(
      await _db,
      finder: finder,
    );
  }

  Future<List<Customer>> GetAll() async {
    final db = await _db;
    final recordSnapshots = await _Store.find(db);
    var list = recordSnapshots.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<Customer> GetByKey(String key) async {
    final db = await _db;
    var result = await _Store.record(key).get(db);
    return Customer.fromJson(result);
  }

  Future<List<Customer>> idCustomerEquals(dynamic value) async {
    final finder = Finder(filter: Filter.equals('id_customer', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerNotEquals(dynamic value) async {
    final finder = Finder(filter: Filter.notEquals('id_customer', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerGreaterThan(dynamic value) async {
    final finder = Finder(filter: Filter.greaterThan('id_customer', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerGreaterThanOrEquals(dynamic value) async {
    final finder =
        Finder(filter: Filter.greaterThanOrEquals('id_customer', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerLessThan(dynamic value) async {
    final finder = Finder(filter: Filter.lessThan('id_customer', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerLessThanOrEquals(dynamic value) async {
    final finder =
        Finder(filter: Filter.lessThanOrEquals('id_customer', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameEquals(dynamic value) async {
    final finder = Finder(filter: Filter.equals('customer_name', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameNotEquals(dynamic value) async {
    final finder = Finder(filter: Filter.notEquals('customer_name', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameGreaterThan(dynamic value) async {
    final finder = Finder(filter: Filter.greaterThan('customer_name', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameGreaterThanOrEquals(dynamic value) async {
    final finder =
        Finder(filter: Filter.greaterThanOrEquals('customer_name', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameLessThan(dynamic value) async {
    final finder = Finder(filter: Filter.lessThan('customer_name', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameLessThanOrEquals(dynamic value) async {
    final finder =
        Finder(filter: Filter.lessThanOrEquals('customer_name', value));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerIsNull() async {
    final finder = Finder(filter: Filter.isNull('id_customer'));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> idCustomerNotNull() async {
    final finder = Finder(filter: Filter.notNull('id_customer'));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameIsNull() async {
    final finder = Finder(filter: Filter.isNull('customer_name'));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }

  Future<List<Customer>> customerNameNotNull() async {
    final finder = Finder(filter: Filter.notNull('customer_name'));
    final db = await _db;
    var result = await _Store.find(db, finder: finder);

    var list = result.map((snapshot) {
      final object = Customer.fromJson(snapshot.value);
      return object;
    }).toList();
    return list;
  }
}

Customer _$CustomerFromJson(Map<String, dynamic> json) {
  return Customer(
    idCustomer: json['id_customer'] as int,
    customerName: json['customer_name'] as String,
  );
}

Upsert

To insert rows into a database entity if they do not already exist, or update them if they do.

Customer customer = new Customer(idCustomer:1, customerName:"Paulo Modanez");
await customer.Upsert();

Delete

To delete row from database.

Customer customer = new Customer(idCustomer:1, customerName:"Paulo Modanez");
await customer.Delete();

GetByKey

Retrive data from database by key.

Customer customer = new Customer();
Customer result = await customer.GetByKey("1");

GetAll

Retrive all rows of the current entity type Ex:Customer.

Customer customer = new Customer();
List<Customer> results = await customer.GetAll();

All properties in the class will have the following search functions.

equals

propertyEquals("value");

notEquals

propertyNotEquals("value");

greaterThan

propertyGreaterThan("value");

greaterThanOrEquals

propertyGreaterThanOrEquals("value");

lessThan

propertyLessThan("value");

lessThanOrEquals

propertyLessThanOrEquals("value");

isNull

propertyIsNull();

notNull

propertyNotNull();

Json Serialization

Two functions will be created.

fromJson

  _$ClassFromJson(Map json);

toJson

  classInstance.toJson();

Libraries

annotations
restoque_builder.builder