put method

  1. @override
Future<int?> put(
  1. String key,
  2. AtNotification value, {
  3. bool skipCommit = false,
})
override

Associates value with key. If a mapping already exists, the old value is replaced.

Returns the commit-log sequence number assigned to this write, or null if no sequence number was produced (when skipCommit is true, or when this backend does not maintain a commit log).

Throws a DataStoreException if the underlying store fails.

Implementation

@override
Future<int?> put(String key, AtNotification value,
    {bool skipCommit = false}) async {
  final existed = await exists(key);
  _db.runInTransaction(() {
    _db.raw.execute(
      'INSERT INTO notifications (id, payload, notification_datetime, type, status, expires_at) '
      'VALUES (?, ?, ?, ?, ?, ?) '
      'ON CONFLICT(id) DO UPDATE SET payload = excluded.payload, '
      'notification_datetime = excluded.notification_datetime, '
      'type = excluded.type, status = excluded.status, '
      'expires_at = excluded.expires_at;',
      [
        key,
        SqliteNotificationCodec.encode(value),
        SqliteNotificationCodec.notificationDateTimeMillis(value) ?? 0,
        SqliteNotificationCodec.typeExtract(value),
        SqliteNotificationCodec.statusExtract(value),
        SqliteNotificationCodec.expiresAtMillis(value),
      ],
    );
  });
  _changes.add(existed ? KeyUpdated(key) : KeyAdded(key));
  return null; // notifications carry no commit id
}