bulkCreate method

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

Implementation

@override
FutureOr<void> bulkCreate(
  List<IndividualModel> entities,
) async {
  return retryLocalCallOperation(() async {
    final individualCompanions = entities.map((e) => e.companion).toList();

    final identifiersList = entities
        .map((e) => e.identifiers?.map((a) {
              return a
                  .copyWith(
                    clientReferenceId: e.clientReferenceId,
                    clientAuditDetails: e.clientAuditDetails,
                    auditDetails: e.auditDetails,
                  )
                  .companion;
            }).toList())
        .toList();

    final identifierCompanions =
        identifiersList.expand((e) => [e?[0]]).toList();

    await sql.batch((batch) async {
      final addressList = entities
          .map((e) =>
              e.address?.map((a) {
                return a
                    .copyWith(
                      relatedClientReferenceId: e.clientReferenceId,
                      clientAuditDetails: e.clientAuditDetails,
                      auditDetails: e.auditDetails,
                    )
                    .companion;
              }).toList() ??
              [])
          .toList();
      final addressCompanions = addressList.expand((e) => [e[0]]).toList();
      final nameCompanions = entities.map((e) {
        if (e.name != null) {
          return e.name!
              .copyWith(
                individualClientReferenceId: e.clientReferenceId,
                clientAuditDetails: e.clientAuditDetails,
                auditDetails: e.auditDetails,
              )
              .companion;
        }
      }).toList();
      if (nameCompanions.isNotEmpty) {
        batch.insertAll(
          sql.name,
          nameCompanions.whereNotNull().toList(),
          mode: InsertMode.insertOrReplace,
        );
      }

      batch.insertAll(
        sql.address,
        addressCompanions,
        mode: InsertMode.insertOrReplace,
      );
      batch.insertAll(
        sql.identifier,
        identifierCompanions.whereNotNull().toList(),
        mode: InsertMode.insertOrReplace,
      );
      batch.insertAll(
        sql.individual,
        individualCompanions,
        mode: InsertMode.insertOrReplace,
      );
    });
  });
}