From 6dc2e376b82a34ec5531b9cd596eb0559c68b546 Mon Sep 17 00:00:00 2001 From: Victor Carreras Date: Mon, 28 Nov 2022 18:12:08 +0100 Subject: [PATCH] Update Readme --- README.md | 99 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0531a8b..1b71e6b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# PSQlite +# PSQLite [![pub package](https://img.shields.io/pub/v/sqflite.svg)](https://pub.dev/packages/psqlite) @@ -8,7 +8,8 @@ Easily manipulate sqlite databases in Dart using this package. The designed obje * [FieldTypeDb][]: Defines the type of the value of a column of a table in a database. SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true). * [ObjectStored][]: All objects that intend to be stored in SQLite databases should extend the ObjectStored class. * [PSQLite][]: Encapsulates a database that is made up of a TableDb and a database name. -* [PSQLite][]: Encapsulates a database that is made up of a TableDb and a database name. +* [FilterDb][]: Defining a filter to make requests to PSQLite will allow us not to have to bring you all the SQLite fields and filter manually, optimizing SQLite queries through the use of filters. +* [ConditionDb][]: Allows you to define the type of condition of a filter. ## Usage example @@ -31,29 +32,29 @@ class User extends ObjectStored { final String _id; String _name; String _lastName; + int _age; - User(this._id, this._name, this._lastName); + User(this._id, this._name, this._lastName, this._age); - User.fromJson(this._id, this._name, this._lastName); + User.fromJson(this._id, this._name, this._lastName, this._age); String getId() => _id; String getName() => _name; String getLastName() => _lastName; - + int getAge() => _age; + void setName(String name) => _name = name; void setLastName(String lastName) => _lastName = lastName; + void setAge(int age) => _age = age; @override - String toString() => 'User{_id: $_id, name: $_name, _lastName: $_lastName}'; + String toString() => + 'User{_id: $_id, _name: $_name, _lastName: $_lastName, _age: $_age}'; // The keys must correspond to the names of the columns in the database. @override Map toMap() { - return { - 'id': _id, - 'name': _name, - 'lastName': _lastName, - }; + return {'id': _id, 'name': _name, 'lastName': _lastName, 'age': _age}; } @override @@ -65,6 +66,7 @@ class User extends ObjectStored { if (_id != other._id) return false; if (_name != other._name) return false; if (_lastName != other._lastName) return false; + if (_age != other._age) return false; return true; } @@ -74,6 +76,7 @@ class User extends ObjectStored { result = 37 * result + _id.hashCode; result = 37 * result + _name.hashCode; result = 37 * result + _lastName.hashCode; + result = 37 * result + _age.hashCode; return result; } } @@ -84,35 +87,45 @@ class User extends ObjectStored { The easiest way to encapsulate the data persistence of the User object that we have created is by creating a wrapper service. Inside we will create the columns of the table and the name of the database. The mockedDatabase parameter will allow us to perform unit tests on our service. +For simplicity and in order to create [FilterDb][] type objects, the parameterized use of the column names is recommended. +In this case, an enumerator has been used for this example called UserColumnName. ```dart import 'package:psqlite/psqlite.dart'; import 'user.dart'; +enum UserColumnName { id, name, lastName, age } + class UserStorageService { static final shared = UserStorageService.init(); late PSQLite _database; + final _tableName = 'users'; UserStorageService.init({bool mockedDatabase = false}) { List columns = [ ColumnDb( - fieldName: 'id', fieldType: FieldTypeDb.text, isPrimaryKey: true), - ColumnDb(fieldName: 'name', fieldType: FieldTypeDb.text), - ColumnDb(fieldName: 'lastName', fieldType: FieldTypeDb.text) + name: UserColumnName.id.name, + type: FieldTypeDb.text, + isPrimaryKey: true), + ColumnDb(name: UserColumnName.name.name, type: FieldTypeDb.text), + ColumnDb(name: UserColumnName.lastName.name, type: FieldTypeDb.text), + ColumnDb(name: UserColumnName.age.name, type: FieldTypeDb.integer) ]; - final table = TableDb.create(name: 'users', columns: columns); + final table = TableDb.create(name: _tableName, columns: columns); _database = PSQLite(table: table, isMocked: mockedDatabase); } + PSQLite getDatabase() => _database; + Future addUser(User user) async { return await _database.insertElement(user); } - + Future updateUser(User user) async { return await _database.updateElement(user); } - + Future removeUser(User user) async { return await _database.deleteElement(user); } @@ -120,7 +133,11 @@ class UserStorageService { Future getUser(String id) async { final response = await _database.getElementBy(id); if (response != null) { - return User(response['id'], response['name'], response['lastName']); + return User( + response[UserColumnName.id.name], + response[UserColumnName.name.name], + response[UserColumnName.lastName.name], + response[UserColumnName.age.name]); } return null; } @@ -128,7 +145,22 @@ class UserStorageService { Future> getListOfUsers() async { final maps = await _database.getElements(); return List.generate(maps.length, (i) { - return User(maps[i]['id'], maps[i]['name'], maps[i]['lastName']); + return User( + maps[i][UserColumnName.id.name], + maps[i][UserColumnName.name.name], + maps[i][UserColumnName.lastName.name], + maps[i][UserColumnName.age.name]); + }); + } + + Future> getListOfUsersBy(List? filters) async { + final maps = await _database.getElementsWhere(filters); + return List.generate(maps.length, (i) { + return User( + maps[i][UserColumnName.id.name], + maps[i][UserColumnName.name.name], + maps[i][UserColumnName.lastName.name], + maps[i][UserColumnName.age.name]); }); } @@ -156,7 +188,7 @@ final storageService = UserStorageService.shared; You can add a new user using our user storage service: ```dart final storageService = UserStorageService.init(); -final user = User("1", "Liam", "Neeson"); +final user = User("1", "Liam", "Neeson", 18); await storageService.addUser(user); ``` @@ -165,7 +197,7 @@ You can update a stored user, in this example first we add a new user and then w ```dart final storageService = UserStorageService.init(); // User add part -User user = User("1", "Liam", "Neeson"); +User user = User("1", "Liam", "Neeson", 18); await storageService.addUser(user); // Update user part user.setLastName(finalLastName); @@ -179,11 +211,22 @@ final storageService = UserStorageService.init(); await storageService.getListOfUsers(); ``` +#### Get a list of filtered users +We can obtain a list of filtered elements. To do this we create a list of filters that we want to apply: +```dart +final storageService = UserStorageService.init(); +List filters = [ + FilterDb(UserColumnName.lastName.name, "Neeson", ConditionDb.equal), + FilterDb(UserColumnName.age.name, 18, ConditionDb.greaterOrEqual) +]; +final filteredUsers = await storageService.getListOfUsersBy(filters); +``` + #### Remove a User stored in SQLite You can delete a user by passing an instance of it as a parameter: ```dart final storageService = UserStorageService.init(); -final user = User("1", "Liam", "Neeson"); +final user = User("1", "Liam", "Neeson", 18); await storageService.removeUser(user); ``` @@ -194,8 +237,10 @@ final storageService = UserStorageService.init(); await storageService.removeAll(); ``` -[TableDb]: https://github.com/vicajilau/psqlite/blob/main/lib/src/table_db.dart -[ColumnDb]: https://github.com/vicajilau/psqlite/blob/main/lib/src/column_db.dart -[FieldTypeDb]: https://github.com/vicajilau/psqlite/blob/main/lib/src/field_type_db.dart -[ObjectStored]: https://github.com/vicajilau/psqlite/blob/main/lib/src/object_stored.dart -[PSQLite]: https://github.com/vicajilau/psqlite/blob/main/lib/src/psqlite.dart +[TableDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/table_db.dart +[ColumnDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/column_db.dart +[FieldTypeDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/field_type_db.dart +[ObjectStored]: https://github.com/vicajilau/psqlite/blob/master/lib/src/object_stored.dart +[PSQLite]: https://github.com/vicajilau/psqlite/blob/master/lib/src/psqlite.dart +[FilterDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/filter_db.dart +[ConditionDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/condition_db.dart