{"id":16558109,"url":"https://github.com/vicajilau/psqlite","last_synced_at":"2025-10-28T20:32:04.024Z","repository":{"id":63619675,"uuid":"569162773","full_name":"vicajilau/psqlite","owner":"vicajilau","description":"PSQlite is a wrapper to work with SQLite databases in a simpler, more intuitive and scalable way for any Flutter project.","archived":false,"fork":false,"pushed_at":"2024-11-11T08:58:13.000Z","size":209,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T18:11:12.409Z","etag":null,"topics":["dart","dart-package","database","flutter","flutter-plugin","orm","sqlite"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/psqlite","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vicajilau.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-22T08:11:30.000Z","updated_at":"2024-11-11T08:58:11.000Z","dependencies_parsed_at":"2023-02-05T17:16:44.995Z","dependency_job_id":"0ec2bcd8-a12e-4c5e-a269-a57e01268160","html_url":"https://github.com/vicajilau/psqlite","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicajilau%2Fpsqlite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicajilau%2Fpsqlite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicajilau%2Fpsqlite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicajilau%2Fpsqlite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vicajilau","download_url":"https://codeload.github.com/vicajilau/psqlite/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238719862,"owners_count":19519286,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","dart-package","database","flutter","flutter-plugin","orm","sqlite"],"created_at":"2024-10-11T20:09:37.152Z","updated_at":"2025-10-28T20:31:58.607Z","avatar_url":"https://github.com/vicajilau.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PSQLite\n\n[![pub package](https://img.shields.io/pub/v/sqflite.svg)](https://pub.dev/packages/psqlite)\n\nEasily manipulate sqlite databases in Dart using this package. The designed objects structure is as follows:\n* [TableDb][]: Defines a table in a database. It consists of a collection of ColumnDb objects and a table name.\n* [ColumnDb][]: Defines a column of a database table. A TableDb object will be made up of a collection of ColumnDb objects.\n* [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).\n* [ObjectStored][]: All objects that intend to be stored in SQLite databases should extend the ObjectStored class.\n* [PSQLite][]: Encapsulates a database that is made up of a TableDb and a database name.\n* [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.\n* [ConditionDb][]: Allows you to define the type of condition of a filter.\n\n## Usage example\n\nIn the following example you will see how to create a user database. \n\nImport `psqlite.dart`\n\n```dart\nimport 'package:psqlite/psqlite.dart';\n```\n\n### Create a User \nUser class that represents the object that we are going to store. \nAny object that is going to be ported to SQLite requires extending the ObjectStored class. \nThis will force us to override the toMap and getPrimaryKey methods.\n```dart\nimport 'package:psqlite/psqlite.dart';\n\nclass User extends ObjectStored {\n  final String _id;\n  String _name;\n  String _lastName;\n  int _age;\n\n  User(this._id, this._name, this._lastName, this._age);\n\n  User.fromJson(this._id, this._name, this._lastName, this._age);\n\n  String getId() =\u003e _id;\n  String getName() =\u003e _name;\n  String getLastName() =\u003e _lastName;\n  int getAge() =\u003e _age;\n\n  void setName(String name) =\u003e _name = name;\n  void setLastName(String lastName) =\u003e _lastName = lastName;\n  void setAge(int age) =\u003e _age = age;\n\n  @override\n  String toString() =\u003e\n      'User{_id: $_id, _name: $_name, _lastName: $_lastName, _age: $_age}';\n\n  // The keys must correspond to the names of the columns in the database.\n  @override\n  Map\u003cString, dynamic\u003e toMap() {\n    return {'id': _id, 'name': _name, 'lastName': _lastName, 'age': _age};\n  }\n\n  @override\n  String getPrimaryKey() =\u003e _id;\n\n  @override\n  bool operator ==(Object other) {\n    if (other is! User) return false;\n    if (_id != other._id) return false;\n    if (_name != other._name) return false;\n    if (_lastName != other._lastName) return false;\n    if (_age != other._age) return false;\n    return true;\n  }\n\n  @override\n  int get hashCode {\n    var result = 17;\n    result = 37 * result + _id.hashCode;\n    result = 37 * result + _name.hashCode;\n    result = 37 * result + _lastName.hashCode;\n    result = 37 * result + _age.hashCode;\n    return result;\n  }\n}\n```\n\n### Create a User Storage Service \n\nThe easiest way to encapsulate the data persistence of the User object that we have created is by creating a wrapper service.\nInside we will create the columns of the table and the name of the database.\nThe mockedDatabase parameter will allow us to perform unit tests on our service.\nFor simplicity and in order to create [FilterDb][] type objects, the parameterized use of the column names is recommended. \nIn this case, an enumerator has been used for this example called UserColumnName.\n\n```dart\nimport 'package:psqlite/psqlite.dart';\n\nimport 'user.dart';\n\nenum UserColumnName { id, name, lastName, age }\n\nclass UserStorageService {\n  static final shared = UserStorageService.init();\n  late PSQLite _database;\n  final _tableName = 'users';\n\n  UserStorageService.init({bool mockedDatabase = false}) {\n    List\u003cColumnDb\u003e columns = [\n      ColumnDb(\n          name: UserColumnName.id.name,\n          type: FieldTypeDb.text,\n          isPrimaryKey: true),\n      ColumnDb(name: UserColumnName.name.name, type: FieldTypeDb.text),\n      ColumnDb(name: UserColumnName.lastName.name, type: FieldTypeDb.text),\n      ColumnDb(name: UserColumnName.age.name, type: FieldTypeDb.integer)\n    ];\n    final table = TableDb.create(name: _tableName, columns: columns);\n    _database = PSQLite(table: table, isMocked: mockedDatabase);\n  }\n\n  PSQLite getDatabase() =\u003e _database;\n\n  Future\u003cvoid\u003e addUser(User user) async {\n    await _database.insertElement(user);\n  }\n\n  Future\u003cvoid\u003e updateUser(User user) async {\n    await _database.updateElement(user);\n  }\n\n  Future\u003cbool\u003e removeUser(User user) async {\n    return await _database.deleteElement(user);\n  }\n\n  Future\u003cUser?\u003e getUser(String id) async {\n    final response = await _database.getElementBy(id);\n    if (response != null) {\n      return User(\n          response[UserColumnName.id.name],\n          response[UserColumnName.name.name],\n          response[UserColumnName.lastName.name],\n          response[UserColumnName.age.name]);\n    }\n    return null;\n  }\n\n  Future\u003cList\u003cUser\u003e\u003e getListOfUsers({List\u003cFilterDb\u003e where = const []}) async {\n    final maps = await _database.getElements(where: where);\n    return List.generate(maps.length, (i) {\n      return User(\n          maps[i][UserColumnName.id.name],\n          maps[i][UserColumnName.name.name],\n          maps[i][UserColumnName.lastName.name],\n          maps[i][UserColumnName.age.name]);\n    });\n  }\n\n  Future\u003cvoid\u003e removeUsers(List\u003cFilterDb\u003e filters) async {\n    await _database.deleteElements(where: filters);\n  }\n\n  Future\u003cvoid\u003e removeAll() async {\n    await _database.clearTable();\n  }\n}\n```\n\n### Usages\nNow we can make requests to our User Storage Service from anywhere in the application. \n\n#### Create a User Storage Service\nYou can create a single object:\n```dart\nfinal storageService = UserStorageService.init();\n```\n\nOr you can get the shared instance with singleton pattern:\n```dart\nfinal storageService = UserStorageService.shared;\n```\n\n#### Add a new User in SQLite\nYou can add a new user using our user storage service:\n```dart\nfinal storageService = UserStorageService.init();\nfinal user = User(\"1\", \"Liam\", \"Neeson\", 18);\nawait storageService.addUser(user);\n```\n\n#### Update a stored User in SQLite\nYou can update a stored user, in this example first we add a new user and then we are going to update it:\n```dart\nfinal storageService = UserStorageService.init();\n// User add part\nUser user = User(\"1\", \"Liam\", \"Neeson\", 18);\nawait storageService.addUser(user);\n// Update user part\nuser.setLastName(finalLastName);\nawait storageService.updateUser(user);\n```\n\n#### Get the list of Users stored in SQLite\nYou can get the complete list of users stored:\n```dart\nfinal storageService = UserStorageService.init();\nawait storageService.getListOfUsers();\n```\n\n#### Get a list of filtered users\nWe can obtain a list of filtered elements. To do this we create a list of filters that we want to apply:\n```dart\nfinal storageService = UserStorageService.init();\nList\u003cFilterDb\u003e filters = [\n  FilterDb(UserColumnName.lastName.name, \"Neeson\", ConditionDb.equal),\n  FilterDb(UserColumnName.age.name, 18, ConditionDb.greaterOrEqual)\n];\nfinal filteredUsers = await storageService.getListOfUsers(where: filters);\n```\n\n#### Remove a User stored in SQLite\nYou can delete a user by passing an instance of it as a parameter:\n```dart\nfinal storageService = UserStorageService.init();\nfinal user = User(\"1\", \"Liam\", \"Neeson\", 18);\nawait storageService.removeUser(user);\n```\n\n#### Remove ALL Users stored in SQLite\nYou can delete ALL users in 2 different ways. Calling the removeAll method: \n```dart\nfinal storageService = UserStorageService.init();\nawait storageService.removeAll();\n```\nOr calling the removeUsers method without filters param:\n\n```dart\nfinal storageService = UserStorageService.init();\nawait storageService.removeUsers();\n```\n\n[TableDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/table_db.dart\n[ColumnDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/column_db.dart\n[FieldTypeDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/field_type_db.dart\n[ObjectStored]: https://github.com/vicajilau/psqlite/blob/master/lib/src/object_stored.dart\n[PSQLite]: https://github.com/vicajilau/psqlite/blob/master/lib/src/psqlite.dart\n[FilterDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/filter_db.dart\n[ConditionDb]: https://github.com/vicajilau/psqlite/blob/master/lib/src/condition_db.dart\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicajilau%2Fpsqlite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvicajilau%2Fpsqlite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicajilau%2Fpsqlite/lists"}