save method

Future<T> save(
  1. T entity
)

Save entity (INSERT only) Skips primary key column if autoIncrementPrimaryKey is true and value is null

Implementation

Future<T> save(T entity) async {
  final row = toRow(entity);

  // Filter out auto-increment primary key if value is null
  final insertRow = Map<String, dynamic>.from(row);
  if (autoIncrementPrimaryKey && insertRow[primaryKeyColumn] == null) {
    insertRow.remove(primaryKeyColumn);
  }

  final columns = insertRow.keys.join(', ');
  final placeholders = insertRow.keys.map((k) => '@$k').join(', ');

  final sql =
      'INSERT INTO $tableName ($columns) VALUES ($placeholders) RETURNING *';

  final result = await connection.query(
    sql,
    parameters: insertRow,
  );
  if (result.isEmpty) throw Exception('Save failed');

  return fromRow(result.first);
}