touch method
Touches the model's timestamp column and persists the change.
When attribute is provided, that column is updated instead of the
default updated_at column.
Implementation
Future<bool> touch([String? attribute]) async {
if (attribute == null && !usesTimestamps) {
return false;
}
final def = expectDefinition();
final resolver = _resolveResolverFor(def);
final updatedAtField = attribute == null
? _updatedAtField(def as ModelDefinition<OrmEntity>)
: def.fieldByColumn(attribute) ?? def.fieldByName(attribute);
if (updatedAtField == null) {
return false;
}
if (attribute == null &&
!_timestampsEnabledForDefinition(def as ModelDefinition<OrmEntity>)) {
return false;
}
final timestamp = _timestampValue(updatedAtField);
if (_isTracked) {
_asAttributes.setAttribute(updatedAtField.columnName, timestamp);
}
final pk = def.primaryKeyField;
if (pk == null) {
throw StateError(
'Model ${def.modelName} must declare a primary key before touch.',
);
}
final key = _primaryKeyValue(def);
if (key == null) {
throw StateError(
'Model ${def.modelName} is missing primary key ${pk.name}.',
);
}
final encoded = resolver.codecRegistry.encodeField(
updatedAtField,
timestamp,
);
final plan = MutationPlan.update(
definition: def,
rows: [
MutationRow(
values: {updatedAtField.columnName: encoded},
keys: {pk.columnName: key},
),
],
driverName: resolver.driver.metadata.name,
);
await resolver.runMutation(plan);
return true;
}