bulkCreate method

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

Implementation

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

    final staffList = entities
        .map((e) => e.staff?.map((a) {
              return StaffCompanion(
                id: Value(a.id),
                tenantId: Value(a.tenantId.toString()),
                userId: Value(a.userId.toString()),
                registerId: Value(a.registerId.toString()),
                enrollmentDate: Value(a.enrollmentDate),
                denrollmentDate: Value(a.denrollmentDate),
              );
            }).toList())
        .toList();

    final staffCompanions = staffList.expand((e) => [e?[0]]).toList();

    final attendeeList = entities
        .map((e) => e.attendees?.map((a) {
              return AttendeeCompanion(
                id: Value(a.id),
                individualId: Value(a.individualId.toString()),
                tenantId: Value(a.tenantId.toString()),
                registerId: Value(a.registerId.toString()),
                enrollmentDate: Value(a.enrollmentDate),
                denrollmentDate: Value(a.denrollmentDate),
              );
            }).toList())
        .toList();

    final attendeeCompanions = attendeeList
        .expand((e) => e ?? [])
        .cast<Insertable<AttendeeData>>()
        .toList();

    await sql.batch((batch) async {
      batch.insertAll(
        sql.attendanceRegister,
        registerCompanions,
        mode: InsertMode.insertOrReplace,
      );

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

      if (attendeeCompanions.isNotEmpty) {
        batch.insertAll(
          sql.attendee,
          attendeeCompanions,
          mode: InsertMode.insertOrReplace,
        );
      }
    });
  });
}