create method

Future<T> create(
  1. T model
)

Creates a new record from the model and returns the model with id populated.

Implementation

Future<T> create(T model) async {
  final data = <String, dynamic>{...model.toMap()};

  if (model.timestamps) {
    final now = DateTime.now().toUtc();
    data['created_at'] = now;
    data['updated_at'] = now;
  }

  // Use RETURNING to get the full inserted row back
  final insertBuilder = QueryBuilder(_tableName, _executor);
  await insertBuilder.insert(data);

  // Fetch the created record (for databases that support RETURNING, this could be optimized)
  // For now, get the last inserted record
  final row = await QueryBuilder(_tableName, _executor)
      .orderBy('id', 'DESC')
      .first();

  if (row != null) {
    final created = _factory(row);
    created.setRawData(_executor, row);
    return created;
  }
  return model;
}