hhtokpinar / sqfEntity

SqfEntity ORM for Flutter/Dart lets you build and execute SQL commands on SQLite database easily and quickly with the help of fluent methods similar to .Net Entity Framework. SqfEntity also generates add/edit forms with validations and special controls (DropDown List, DateTime pickers, Checkboxes.. etc) for your table.
378 stars 99 forks source link

Override CRUD method #222

Closed Grrravity closed 2 years ago

Grrravity commented 2 years ago

Hi there, I'm facing a need and I can't figure how to satisfy with the current setup Got my local DB, which is a copy of my remote DB, and in it there's an "update" table where I'd like to put CRUD actions done offline to push to remote when network's available.

This table is quite simple and record actions done on every tables : origin_table_name uuid action_done timestamp
users uuid v5 create DateTime.now
geolocalisation uuid v5 update DateTime.now

etc etc...

My question is: Is there a way to override CRUD methods from generated model.g.dart to add a line in that update table when an action is done on any other tables.

If I were the only dev working on this project I would make sure to add an extra Update(blablabla).save() after each crud but I'd like to make this extra action global so we're 100% sure it will be done :)

Thanks for your help !

Grrravity commented 2 years ago

found and feels like documentation is lacking explanations x) You have to use customCode like this :

const tableA = SqfEntityTable(
    tableName: 'table_a',
    modelName: 'TableA',
    primaryKeyName: 'uuid',
    primaryKeyType: PrimaryKeyType.text,
    fields: [
      SqfEntityField('long', DbType.real),
      SqfEntityField('lat', DbType.real),
      SqfEntityField('text', DbType.text, isNotNull: true),
      SqfEntityField('created_at', DbType.datetime),
      SqfEntityField('updated_at', DbType.datetime),
      SqfEntityField('deleted_at', DbType.datetime),
      SqfEntityFieldRelationship(
          parentTable: yyy,
          deleteRule: DeleteRule.CASCADE,
          fieldName: 'yyy_uuid',
          isNotNull: true),
      SqfEntityFieldRelationship(
          parentTable: zzz,
          deleteRule: DeleteRule.CASCADE,
          fieldName: 'zzz_uuid',
          isNotNull: true),
      SqfEntityFieldRelationship(
          parentTable: www,
          deleteRule: DeleteRule.CASCADE,
          fieldName: 'www_uuid'),
    ],
    customCode: '''
  createSync(TableA element) async {
    await element.save();
    var someThing = await TableY().select().top(1).toSingle();
    String uuid = someThing?.uuid ?? '';
    TableSync syncElement = TableSync(
        uuid: uuidGen.v4(),
        table_name: 'table_a',
        target_uuid: element.uuid,
        action: 'CREATE',
        created_at: element.created_at,
        updated_at: element.updated_at,
        other_uuid: uuid);
    await syncElement.save();
  }
  ''');
hhtokpinar commented 2 years ago

Also there are some features like implementing your class to models or adding a preSaveAction method that invented by @ReniDelonzek Just take a look here: https://pub.dev/packages/sqfentity_gen/changelog#2102