batchInsert method

Future<List> batchInsert(
  1. {@required String table,
  2. @required List<Map<String, String>> rows,
  3. ConflictAlgorithm conflictAlgorithm = ConflictAlgorithm.rollback,
  4. bool verbose = false}
)

Insert rows in a table

Implementation

Future<List<dynamic>> batchInsert(
    {@required String table,
    @required List<Map<String, String>> rows,
    ConflictAlgorithm conflictAlgorithm = ConflictAlgorithm.rollback,
    bool verbose = false}) async {
  var res = <dynamic>[];
  await _mutex.synchronized(() async {
    try {
      if (!_isReady) {
        throw DatabaseNotReady();
      }
      final timer = Stopwatch()..start();

      await _db.transaction((txn) async {
        final batch = txn.batch();
        rows.forEach((row) {
          batch.insert(table, row, conflictAlgorithm: conflictAlgorithm);
          _changeFeedController.sink.add(DatabaseChangeEvent(
              type: DatabaseChange.insert,
              value: 1,
              query: "",
              table: table,
              data: row,
              executionTime: timer.elapsedMicroseconds));
        });
        res = await batch.commit();
      });
      timer.stop();
      if (verbose) {
        final msg = "Inserted ${rows.length} records "
            "in ${timer.elapsedMilliseconds} ms";
        print(msg);
      }
    } catch (e) {
      rethrow;
    }
  });
  return res;
}