bulkCreate method

  1. @override
FutureOr<void> bulkCreate(
  1. List<TaskModel> entities
)
override

Implementation

@override
FutureOr<void> bulkCreate(
  List<TaskModel> entities,
) async {
  final taskCompanions = entities.map((e) => e.companion).toList();

  List<AddressCompanion> addressCompanions = [];
  List<TaskResourceCompanion> resourceCompanions = [];

  for (TaskModel entity in entities) {
    final addressCompanion = entity.address
        ?.copyWith(
          relatedClientReferenceId: entity.clientReferenceId,
          auditDetails: entity.auditDetails,
          clientAuditDetails: entity.clientAuditDetails,
        )
        .companion;
    if (addressCompanion != null) {
      addressCompanions.add(addressCompanion);
    }

    final resources = entity.resources?.map((e) {
          return e
              .copyWith(
                taskclientReferenceId: entity.clientReferenceId,
              )
              .companion;
        }).toList() ??
        [];
    resourceCompanions.addAll(resources);
  }

  await sql.batch((batch) async {
    batch.insertAll(
      sql.task,
      taskCompanions,
      mode: InsertMode.insertOrReplace,
    );

    if (addressCompanions.isNotEmpty) {
      batch.insertAll(
        sql.address,
        addressCompanions.whereNotNull().toList(),
        mode: InsertMode.insertOrReplace,
      );
    }

    batch.insertAllOnConflictUpdate(sql.taskResource, resourceCompanions);
  });
}