Quds Db
Is an automated version of sqflite!
How to use
1. To create a model
Model classes should extend DbModel class. Define the schema of your model, Supported field type:
Integers | Strings | DateTimes | Others |
---|---|---|---|
IdField | StringField | DateTimeField (saved as num) | BlobField |
IntField | DateTimeStringField (Saved as string) | BoolField | |
NumField | JsonField ListField |
||
DoubleField | ColorField | ||
EnumField |
class Note extends DbModel {
var title = StringField(columnName: 'title');
var content = StringField(columnName: 'content');
var isImportant = BoolField(columnName: 'isImportant');
var color = ColorField(columnName: 'color');
@override
List<FieldWithValue>? getFields() => [title, content, isImportant, color];
}
Note that: Every model has default fields:
- id (Auto incremental integer field)
- creationTime (automatically set once when created)
- modificationTime (automatically set when created, and with every update operation)
2. To create a table manager
class NotesProvider extends DbTableProvider<Note> {
NotesProvider() : super(() => Note());
@override
String get tableName => 'Notes';
}
As shown, to set the name of the table:
@override
String get tableName => 'Notes';
Note that: In provider class constructor, you should provide it with model object creation function.
NotesProvider() : super(() => Note());
To create a provider instance,
NotesProvider notesProvider = NotesProvider();
3. Crud operations:
Creation: (Insertion)
single:
Note n = Note();
n.title.value = 'New note';
n.content.value = 'Note content, describe your self';
n.isImportant.value = ([true, false]..shuffle()).first;
n.color.value = ([Colors.red, Colors.blue, Colors.yellow, Colors.orange]
..shuffle())
.first;
await notesProvider.insertEntry(n);
multiple:
await notesProvider.insertCollection([n1,n2,n3,...]);
Reading (Query):
var allNotes = await notesProvider.select();
var importantNotes = await notesProvider.select(where:(n)=>n.isImportant.isTrue);
var imortantRed = await notesProvider.select(where:(n)=>n.isImportant.isTrue & n.color.equals(Color(0xffff0000)));
Updating:
n.title = 'new title';
await notesProvider.updateEntry(n);
Deletion:
await notesProvider.deleteEntry(n);
Monitoring changes:
To handle the changes in some table:
notesProvider.addEntryChangeListner((changeType, entry) {
switch (changeType) {
case EntryChangeType.Insertion:
//New Note added (entry)
break;
case EntryChangeType.Deletion:
//(entry) has been deleted
break;
case EntryChangeType.Modification:
//(entry) has been modified
break;
}
});
Database location:
Where are the data being stored? By default, your data will be saved in app data folder / 'data.db', and to change it:
DbHelper.mainDbPath = 'your db path';
And if you want to save a table in other file:
class NotesProvider extends DbTableProvider<Note> {
NotesProvider()
: super(() => Note(), specialDbFile: 'your specified file path');
@override
String get tableName => 'Notes';
}
Libraries
- quds_db
- A sqflite expansion package that simplifies creating databases and tables, crud operations, queries with modelization
- quds_db/database_operations
- quds_db/sqflite/sqflite_ffi
- quds_db/sqflite/src/constant
- quds_db/sqflite/src/database_factory_ffi
- quds_db/sqflite/src/database_tracker
- quds_db/sqflite/src/env_utils
- quds_db/sqflite/src/import
- quds_db/sqflite/src/isolate
- quds_db/sqflite/src/method_call
- quds_db/sqflite/src/mixin/handler_mixin
- Exported for implementation
- quds_db/sqflite/src/sqflite_ffi
- quds_db/sqflite/src/sqflite_ffi_exception
- quds_db/sqflite/src/sqflite_ffi_impl
- quds_db/sqflite/src/sqflite_ffi_io
- quds_db/sqflite/src/sqflite_ffi_stub
- quds_db/sqflite/src/sqflite_import
- quds_db/sqflite/src/windows/setup
- quds_db/sqflite/src/windows/setup_impl
- quds_db/support_functions/support_functions