updateById method

Future<R?> updateById(
  1. Object id,
  2. Map<String, Object?> values
)

Updates supplied fields within the key and filters, returning the changed row or null. Omitted fields remain untouched; nullable null clears a field.

Implementation

Future<R?> updateById(Object id, Map<String, Object?> values) async {
  _returning();
  _rejectPaging();
  if (values.isEmpty) {
    throw ArgumentError('Update requires at least one field');
  }
  final scoped = whereFields({_primary.field: eq<Object?>(id)});
  final bindings = _bindings();
  final assignments = <String>[];
  for (final e in values.entries) {
    final c = definition.column(e.key);
    if (c.primaryKey || c.identity) {
      throw ArgumentError('Primary key updates are unsupported');
    }
    _checkValue(c, e.value);
    assignments.add('${quoteIdentifier(c.name)} = ${bindings.bind(e.value)}');
  }
  final result = await session.run(
    'UPDATE $_onlyTable SET ${assignments.join(', ')}${scoped._where(bindings)} RETURNING $_columns',
    parameters: bindings.values,
  );
  if (result.rows.length > 1) {
    throw StateError('Primary key update returned multiple rows');
  }
  return result.rows.isEmpty ? null : decode(result.rows.single);
}