Skip to content

Creating SqfEntity Table Model

Hüseyin Tokpınar edited this page Nov 14, 2019 · 7 revisions

add the sqfentity package to dependencies in pubscpec.yaml first.

dependencies:
  sqfentity: ^1.2.2+8  # please check the latest version on https://pub.dev/packages/sqfentity
  sqfentity_gen: ^1.2.0+13 # please check the latest version on https://pub.dev/packages/sqfentity_gen

dev_dependencies:
  build_runner: ^1.6.5
  build_verify: ^1.1.0

Create a new Table Model

First, create your model.dart file in lib/model/ folder to define your model and import sqfentity and other necessary packages

  import 'dart:convert';
  import 'dart:typed_data'; // for blob types. You can remove this if unnecessary
  import 'package:flutter/foundation.dart';
  import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  import 'package:http/http.dart' as http;
  import 'package:flutter/material.dart';
  import 'package:sqfentity/sqfentity.dart';
  import 'package:sqfentity_gen/sqfentity_gen.dart';

Write the following statement for the file to be created

part 'model.g.dart';

STEP 1: Our model file is ready to use. Define your tables as shown in the example Classes below. For example, we have created 3 tables constant for category, product, and todo that instanced from "SqfEntityTable" as follows:

Table 1: Category

const tableCategory = SqfEntityTable(
  tableName: 'category',
  primaryKeyName: 'id',
  primaryKeyType: PrimaryKeyType.integer_auto_incremental,
  useSoftDeleting: true,
  modelName: null,
  fields: [
    SqfEntityField('name', DbType.text),
    SqfEntityField('isActive', DbType.bool, defaultValue: true),
  ]
);

If useSoftDeleting is true then, The builder engine creates a field named "isDeleted" on the table. When an item was deleted then this field value is changed to "1" (does not hard delete) in this case, it is possible to recover a deleted item using the recover() method. If the modelName (class name) is null then EntityBase uses TableName instead of modelName

Table 2: Product

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)
]);

If this table (Product) is the child of a parent table (Category), you must declare the SqfEntityFieldRelationship column into fields for Object Relational Mapping. You can choose one of the following for DeleteRule: CASCADE, NO ACTION, SET NULL, SET DEFAULT VALUE For more information about the rules Click here

Relationships

For more information about the Relationships click here

Clone this wiki locally