create method

  1. @override
FutureOr<void> create(
  1. IndividualModel entity, {
  2. bool createOpLog = true,
  3. DataOperation dataOperation = DataOperation.create,
})
override

The create method creates a new entity.

Implementation

@override
FutureOr<void> create(
  IndividualModel entity, {
  bool createOpLog = true,
  DataOperation dataOperation = DataOperation.create,
}) async {
  return retryLocalCallOperation(() async {
    final addresses = entity.address;
    final identifiers = entity.identifiers;

    final individualCompanion = entity.companion;
    final nameCompanion = entity.name?.companion;

    await sql.batch((batch) async {
      batch.insert(
        sql.individual,
        individualCompanion,
        mode: InsertMode.insertOrReplace,
      );
      if (nameCompanion != null) {
        batch.insert(
          sql.name,
          nameCompanion,
          mode: InsertMode.insertOrReplace,
        );
      }

      if (addresses != null) {
        final addressCompanions = addresses.map((e) {
          return e.companion;
        }).toList();

        batch.insertAll(
          sql.address,
          addressCompanions,
          mode: InsertMode.insertOrReplace,
        );
      }

      if (identifiers != null) {
        final identifierCompanions = identifiers.map((e) {
          return e.companion;
        }).toList();

        batch.insertAll(sql.identifier, identifierCompanions);
      }
    });

    await super.create(entity);
  });
}