sqfentity_gen 1.0.0+3 sqfentity_gen: ^1.0.0+3 copied to clipboard
SqfnEntity Model Generator. SqfEntity ORM for Flutter/Dart lets you build and execute SQL commands easily and quickly with the help of fluent methods similar to .Net Entity Framework.
// STEP 1: import sqfentity package.
import 'package:sqfentity_base/sqfentity_base.dart';
import 'model/model.dart';
// STEP 1: define your tables as shown in the example Classes below.
// Define the 'TableCategory' constant as SqfEntityTable.
const tableCategory = SqfEntityTable(
tableName: 'category',
primaryKeyName: 'id',
primaryKeyType: PrimaryKeyType.integer_auto_incremental,
useSoftDeleting: true,
// when useSoftDeleting is true, creates a field named 'isDeleted' on the table, and set to '1' this field when item deleted (does not hard delete)
modelName:
null, // SqfEntity will set it to TableName automatically when the modelName (class name) is null
// declare fields
fields: [
SqfEntityField('name', DbType.text),
SqfEntityField('isActive', DbType.bool, defaultValue: true),
]);
// Define the 'TableProduct' constant as SqfEntityTable.
const tableProduct = SqfEntityTable(
tableName: 'product',
primaryKeyName: 'id',
primaryKeyType: PrimaryKeyType.integer_auto_incremental,
useSoftDeleting: true,
fields: [
SqfEntityField('name', DbType.text),
SqfEntityField('description', DbType.text),
SqfEntityField('price', DbType.real, defaultValue: 0),
SqfEntityField('isActive', DbType.bool, defaultValue: true),
SqfEntityFieldRelationship(
parentTable: tableCategory,
deleteRule: DeleteRule.CASCADE,
defaultValue: '0'), // Relationship column for CategoryId of Product
SqfEntityField('rownum', DbType.integer,
sequencedBy:
seqIdentity /*Example of linking a column to a sequence */),
SqfEntityField('imageUrl', DbType.text)
]);
// Define the 'Todo' constant as SqfEntityTable.
const tableTodo = SqfEntityTable(
tableName: 'todos',
primaryKeyName: 'id',
useSoftDeleting:
false, // when useSoftDeleting is true, creates a field named 'isDeleted' on the table, and set to '1' this field when item deleted (does not hard delete)
primaryKeyType: PrimaryKeyType.integer_unique,
defaultJsonUrl:
'https://jsonplaceholder.typicode.com/todos', // optional: to synchronize your table with json data from webUrl
// declare fields
fields: [
SqfEntityField('userId', DbType.integer),
SqfEntityField('title', DbType.text),
SqfEntityField('completed', DbType.bool, defaultValue: false)
]);
// Define the 'identity' constant as SqfEntitySequence.
const seqIdentity = SqfEntitySequence(
sequenceName: 'identity',
maxValue: 10000, /* optional. default is max int (9.223.372.036.854.775.807) */
//modelName: 'SQEidentity',
/* optional. SqfEntity will set it to sequenceName automatically when the modelName is null*/
//cycle : false, /* optional. default is false; */
//minValue = 0; /* optional. default is 0 */
//incrementBy = 1; /* optional. default is 1 */
// startWith = 0; /* optional. default is 0 */
);
// STEP 2: Create your Database Model constant instanced from SqfEntityModel
// Note: SqfEntity provides support for the use of multiple databases. So you can create many Database Models and use them in the application.
@SqfEntityBuilder(myDbModel)
const myDbModel = SqfEntityModel(
modelName: null,
databaseName: 'sampleORM.db',
// put defined tables into the tables list.
databaseTables: [tableCategory, tableProduct, tableTodo],
// put defined sequences into the sequences list.
sequences: [seqIdentity],
bundledDatabasePath:
null // 'assets/sample.db' // This value is optional. When bundledDatabasePath is empty then EntityBase creats a new database when initializing the database
);
/* STEP 3: That's All..
--> Go Terminal Window and run command below
flutter pub run build_runner build --delete-conflicting-outputs
Note: After running the command Please check lib/model/model.g.dart
Enjoy.. Huseyin TOKPINAR
*/
void main(List<String> args) async {
// Run Entity Model samples
// ATTENTION! when the software/app is started, you must check the database was it initialized.
// If needed, initilizeDb method runs CREATE / ALTER TABLE query for you.
final bool isInitialized = await MyDbModel().initializeDB();
if (isInitialized == false) {
// If the database is not initialized, something went wrong. Check DEBUG CONSOLE for alerts
// TO DO
return;
}
}