save method

Future<void> save({
  1. Batch? batch,
  2. Transaction? transaction,
})

Implementation

Future<void> save({
  Batch? batch,
  Transaction? transaction,
}) async {
  final schema = getSchema();
  final factory = getFactory();
  final json = factory.toJson(this as T);

  bool exists = false;

  if (json['id'] != null) {
    final count = await Query.table(schema.name).where(schema.key, '=', json['id']).count();

    if (count >= 1) {
      exists = true;
    }
  }

  if (exists) {
    await Query.table(schema.name, batch, transaction)
        .where(schema.key, '=', json[schema.key])
        .update(json);
  } else {
    await Query.table(schema.name, batch, transaction).insert(json);
  }
}