enqueue method
Append a write intent. Returns the autoincrement row id.
clientUpdatedAt is the wall-clock at the time of the user action,
used by SyncWorker for last-write-wins conflict resolution.
Implementation
Future<int> enqueue({
required String localId,
required String tableName,
required String operation,
required Map<String, dynamic> payload,
DateTime? clientUpdatedAt,
}) async {
_requireReady();
final encoded = jsonEncode(payload);
if (encoded.length > _maxPayloadBytes) {
throw ArgumentError(
'Outbox payload for "$tableName" exceeds $_maxPayloadBytes bytes '
'(${encoded.length}). Truncate before enqueueing.',
);
}
final now = DateTime.now().toUtc();
final clientTs = (clientUpdatedAt ?? now).toUtc().toIso8601String();
final stmt = _db!.prepare('''
INSERT INTO outbox_events
(local_id, table_name, operation, payload,
state, created_at, client_updated_at)
VALUES (?, ?, ?, ?, 'pending', ?, ?)
''');
try {
stmt.execute([
localId,
tableName,
operation,
encoded,
now.toIso8601String(),
clientTs,
]);
return _db!.lastInsertRowId;
} finally {
stmt.close();
}
}