persister 1.0.3 copy "persister: ^1.0.3" to clipboard
persister: ^1.0.3 copied to clipboard

outdated

Persister allows you to increase your performance when using MySql on dart servers, providing an easy API.

example/persister_example.dart

import 'package:persister/persister.dart';

//This is a sample class using Persister.
//Though the order of the attributes of a class does not matter,
//It's very important to define the primary key fieldname at the first position of columns array, within super constructor
//Also, you have to define the primary key value at the first position of the values getter array.

//IMPORTANT
//values and columns need to have the same order. This mean they have to be related by position
//for example if I had:
// values: [1, 'Raul']
// columns: ['id', 'name']
// the first position (0) of both arrays corresponds to the fieldname id and the value 1.
// the second position (1) corresponds to fieldname 'name' and value 'raul'.
//You have to respect the order. Maybe in  the future the arrays're being replaced by a Map...

//So, the super construction will have metadata about the table: name, fieldnames and if the pk field is auto_incrementable
//you have to
class Test extends Persister<Test> {
  //id is the primary key of test table
  int id;
  String text;
  //IMPORTANT!!!!!
  //be sure to declare the id field in the first position of the columns array
  Test({this.id = -1, required this.text})
      : super(
            columns: ['id', 'text'],
            table: 'test',
            isIdAutoIncrementable: true);
  factory Test.fromMap(Map<String, dynamic> map) {
    return Test(id: map['id'], text: map['text']);
  }
  //fromMap and toMap methods, you maybe want to implement then!
  @override
  Test fromMap(Map<String, dynamic> map) => Test.fromMap(map);
  Map<String, dynamic> toMap() => {'id': id, 'text': text};

  @override
  String toString() => 'Test(id: $id, text: $text)';

  @override
  //IMPORTANT!!!!!!
  //be sure to declare de id value in the first position of the values array
  List<dynamic> get values => [id, text];
}

//Now we have prepared the model let's start with the use of this pacakge, but first there's one thing you have to do
//As this library uses mysql_manager dependency, written also by me, you need to configure the .env file at the root of your
//project. This .env file can contain the properties you desire. However the following are needed in order to connect to your
//mysql

// db=YOUR_DB_NAME
// host=YOUR_DB_HOST
// user=YOUR_MYSQL_USER
// password=YOUR_MYSQL_PASS
// port=THE_PORT_WHERE_DB_IS_RUNNING

//WHEN THIS .env file is well configured you can use Persister as shown within main function.
void main() async {
  //INSERT A ROW INSIDE test TABLE
  Test test = Test(text: 'this is a new test');
  //Save method will insert the data in a row within the database.
  //If you have passed the attribute  isAutoIncrementable = true, the autogenerated id will be returned, so the object will have this id.
  //In order words. If the test object below is inserted with an id of 1000, it will be returned and set into this same object. You don't have to worry about this auto incrementable ids.
  test = await test.save();

  test.text = 'updated the new test';
  //update method will update the object inside your table
  await test.update();

  //Deleting one element. Deletes by id.
  await test.delete();

  //selecting data

  //For selecting data we use the static methods from Persister

  //selecting all data
  //for selecting all the data you should use Persister
  // selectAll method, which will return a List<Map<String,dynamic>>. This raw data can be parsed
  // to a List<Model> (List<Test> in this case) using concatenating this selectAll with deserialize method.
  //You have to use a function / constructor in order to parse each Map of the list into a Object.
  //As you can see in this example I am using the same Test.fromMap factory constructor I've declared in the class construction.
  List<Test> tests = await Persister.selectAll(table: 'test')
      .deserialize((map) => Test.fromMap(map));

  //Using the nativeQuery
  //You can use your own sql queries with the folling static method from Persister
  //And again, deserialize allow you to easily parse the Results into a model you have defined.

  //this is a way to use prepared statements. When you use prepared statements you HAVE to pass the values array in the same order of ? aparition.
  List<Test> testsNative = await Persister.nativeQuery(
      sql: 'select * from test where id > ?',
      values: [5]).deserialize((map) => Test.fromMap(map));

  // if you do not wanna use prepared statements you can use nativeQuery in this way (same as former example)
  List<Test> testsNative2 =
      await Persister.nativeQuery(sql: 'select * from test where id > 5')
          .deserialize((map) => Test.fromMap(map));
}
0
likes
100
pub points
0%
popularity

Publisher

unverified uploader

Persister allows you to increase your performance when using MySql on dart servers, providing an easy API.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

mysql_manager

More

Packages that depend on persister