@marinoscar/platform-db 0.1.0-next.21 → 0.1.0-next.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @marinoscar/platform-db
2
2
 
3
+ ## 0.1.0-next.23
4
+
5
+ ### Minor Changes
6
+
7
+ - d8777d8: Add STATE and COUNTED inbox notifications (#1077). `NotificationsService.notifyState` keeps ONE live inbox row per user, event and scope, refreshed in place and dismissed when its count drains to zero; `notifyCounted` folds repeated occurrences into one row whose `data.count` grows, optionally inside a rolling window and partitioned by a match object. Both serialize on a transaction-scoped advisory lock, and the database backs them with the partial unique index `notifications_live_dedup_uniq_idx`. Platform migration `0041_notifications_state_counted` adds `data`, `scope_id`, `dismissed_at`, `updated_at` and `dedup_key` to `notifications` (all additive). The inbox gains a Dismiss action (`POST /api/notifications/:id/dismiss`), list and unread count exclude dismissed rows, the wire shape gains `count`, `scopeId`, `dismissedAt` and `updatedAt`, and the retention sweep removes read and dismissed rows by age while STATE and COUNTED rows that are unread are never purged by age. Plain `notify()` is unchanged.
8
+
9
+ ## 0.1.0-next.22
10
+
3
11
  ## 0.1.0-next.21
4
12
 
5
13
  ## 0.1.0-next.20
@@ -0,0 +1,19 @@
1
+ -- AlterTable
2
+ ALTER TABLE "notifications" ADD COLUMN "data" JSONB,
3
+ ADD COLUMN "dedup_key" TEXT,
4
+ ADD COLUMN "dismissed_at" TIMESTAMPTZ,
5
+ ADD COLUMN "scope_id" UUID,
6
+ ADD COLUMN "updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP;
7
+
8
+ -- CreateIndex
9
+ CREATE INDEX "notifications_dismissed_at_idx" ON "notifications"("dismissed_at");
10
+
11
+ -- Existing rows were last changed when they were created (or read); the new
12
+ -- column's default would otherwise stamp them all with the migration time.
13
+ UPDATE "notifications" SET "updated_at" = COALESCE("read_at", "created_at");
14
+
15
+ -- At most one LIVE foldable (STATE or COUNTED) row per (user, dedup key).
16
+ -- Raw SQL: Prisma cannot express a partial unique index. Listed in
17
+ -- packages/platform-db/raw-sql-indexes.json; never replace it with @@unique or
18
+ -- a find-then-insert.
19
+ CREATE UNIQUE INDEX "notifications_live_dedup_uniq_idx" ON "notifications"("user_id", "dedup_key") WHERE "dismissed_at" IS NULL AND "dedup_key" IS NOT NULL;
@@ -402,5 +402,13 @@
402
402
  "sha256": "ec4af7e5123ecd8bb6faf3505893cb2efab4d793dae84a98d8b7f26f2dff3776",
403
403
  "since": "1.0.0",
404
404
  "slice": "alerts"
405
+ },
406
+ {
407
+ "dir": "0041_notifications_state_counted",
408
+ "id": "0041_notifications_state_counted",
409
+ "requires": [],
410
+ "sha256": "c9285347971920da5310a279351d8de8acac3bba4b281e227cb025745dafc73c",
411
+ "since": "1.0.0",
412
+ "slice": "notifications"
405
413
  }
406
414
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marinoscar/platform-db",
3
- "version": "0.1.0-next.21",
3
+ "version": "0.1.0-next.23",
4
4
  "description": "Prisma schema fragments, SQL migrations and seed functions of the platform (CommonJS).",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -98,6 +98,15 @@
98
98
  "reason": "At most one OPEN (pending or firing) incident per alert rule and label set; a partial unique index Prisma cannot express. The evaluator creates incidents and treats the unique violation as 'already open', so two overlapping evaluations leave one incident.",
99
99
  "doc": "docs/specs/platform-packages.md",
100
100
  "createdIn": "0040_add_alerts"
101
+ },
102
+ {
103
+ "name": "notifications_live_dedup_uniq_idx",
104
+ "table": "notifications",
105
+ "unique": true,
106
+ "definition": "CREATE UNIQUE INDEX notifications_live_dedup_uniq_idx ON public.notifications USING btree (user_id, dedup_key) WHERE ((dismissed_at IS NULL) AND (dedup_key IS NOT NULL))",
107
+ "reason": "At most one LIVE foldable (STATE or COUNTED) notification per user and dedup key; a partial unique index Prisma cannot express. The writer serializes on an advisory lock and treats a unique violation as 'already live', so concurrent producers leave one row.",
108
+ "doc": "docs/specs/platform-packages.md",
109
+ "createdIn": "0041_notifications_state_counted"
101
110
  }
102
111
  ]
103
112
  }
@@ -210,10 +210,48 @@ model Notification {
210
210
 
211
211
  createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
212
212
 
213
- // NO `updatedAt`. The only mutation this row ever undergoes is being marked
214
- // read, and `readAt` already timestamps that with more precision than an
215
- // `updatedAt` would carry. A second, redundant answer to "when did this
216
- // change?" is a second thing to keep consistent for no reader.
213
+ // ---------------------------------------------------------------------------
214
+ // STATE and COUNTED rows (issue #1077) — all additive and nullable/defaulted,
215
+ // so every existing row and the plain `notify()` insert are unchanged.
216
+ // ---------------------------------------------------------------------------
217
+ //
218
+ // `notify()` appends one row per call. Two producers cannot live with that:
219
+ // a STATE notification ("N items await review") is ONE live row per
220
+ // (user, scope, event) refreshed in place, and a COUNTED event ("N uploads
221
+ // finished") folds repeated occurrences into ONE row whose `data.count`
222
+ // grows. These columns carry them:
223
+ //
224
+ // data the producer's payload; STATE and COUNTED rows always write
225
+ // it (`count`, and `countAtRead` once read). NULL for a plain
226
+ // `notify()` row.
227
+ // scopeId an optional scope the row belongs to (a group, an
228
+ // organization, a workspace), so one user can hold one live
229
+ // STATE row per scope. NOT authorization: the producer decides
230
+ // the recipients.
231
+ // dismissedAt "stop showing me this", distinct from `readAt` ("I saw it").
232
+ // A dismissed row leaves the list and the unread count; the
233
+ // retention sweep removes it by age.
234
+ // updatedAt the rolling-window anchor of a COUNTED row and the refresh
235
+ // time of a STATE row. Raw SQL does not run Prisma's
236
+ // `@updatedAt`, so every raw UPDATE sets it explicitly.
237
+ // dedupKey the fold key of a STATE or COUNTED row (NULL for a plain
238
+ // `notify()` row, which is how the retention sweep tells them
239
+ // apart); see the raw-SQL partial unique index below.
240
+ data Json?
241
+ scopeId String? @map("scope_id") @db.Uuid
242
+ dismissedAt DateTime? @map("dismissed_at") @db.Timestamptz
243
+ updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz
244
+ dedupKey String? @map("dedup_key")
245
+
246
+ // RAW-SQL INDEX, intentional schema drift (packages/platform-db/raw-sql-indexes.json):
247
+ // notifications_live_dedup_uniq_idx
248
+ // UNIQUE (user_id, dedup_key) WHERE dismissed_at IS NULL AND dedup_key IS NOT NULL
249
+ // At most one LIVE foldable row per (user, dedup key). The DATABASE decides a
250
+ // race, not a find-then-insert. A COUNTED row whose window closed is retired
251
+ // by rewriting its `dedup_key` to `retired:<id>` (it stays in the inbox, and
252
+ // still reads as a folded row to the retention sweep), which frees the key
253
+ // for the next row. Prisma cannot express a partial unique index: never
254
+ // replace it with `@@unique` or a `findFirst` pre-check.
217
255
 
218
256
  // Relations
219
257
  //
@@ -241,6 +279,8 @@ model Notification {
241
279
  // The retention sweep (`notifications.inbox.purge`, #681): "oldest first,
242
280
  // older than the cutoff" across every user, which neither index above serves.
243
281
  @@index([createdAt])
282
+ // The retention sweep's dismissed pass (issue #1077): "dismissed longer ago than the cutoff".
283
+ @@index([dismissedAt])
244
284
  @@map("notifications")
245
285
  }
246
286