@warlock.js/notifications 4.2.0
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 +21 -0
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/cjs/index.cjs +1034 -0
- package/cjs/index.cjs.map +1 -0
- package/esm/channels/database-channel.d.mts +10 -0
- package/esm/channels/database-channel.d.mts.map +1 -0
- package/esm/channels/database-channel.mjs +19 -0
- package/esm/channels/database-channel.mjs.map +1 -0
- package/esm/channels/index.d.mts +2 -0
- package/esm/channels/index.mjs +4 -0
- package/esm/channels/mail-channel.d.mts +11 -0
- package/esm/channels/mail-channel.d.mts.map +1 -0
- package/esm/channels/mail-channel.mjs +43 -0
- package/esm/channels/mail-channel.mjs.map +1 -0
- package/esm/config.d.mts +46 -0
- package/esm/config.d.mts.map +1 -0
- package/esm/config.mjs +32 -0
- package/esm/config.mjs.map +1 -0
- package/esm/contracts/channel.contract.d.mts +38 -0
- package/esm/contracts/channel.contract.d.mts.map +1 -0
- package/esm/contracts/index.d.mts +5 -0
- package/esm/contracts/notification.contract.d.mts +18 -0
- package/esm/contracts/notification.contract.d.mts.map +1 -0
- package/esm/contracts/preference-provider.contract.d.mts +20 -0
- package/esm/contracts/preference-provider.contract.d.mts.map +1 -0
- package/esm/contracts/queue-dispatcher.contract.d.mts +23 -0
- package/esm/contracts/queue-dispatcher.contract.d.mts.map +1 -0
- package/esm/contracts/rate-limiter.contract.d.mts +23 -0
- package/esm/contracts/rate-limiter.contract.d.mts.map +1 -0
- package/esm/dispatch/define-channel.d.mts +37 -0
- package/esm/dispatch/define-channel.d.mts.map +1 -0
- package/esm/dispatch/define-channel.mjs +39 -0
- package/esm/dispatch/define-channel.mjs.map +1 -0
- package/esm/dispatch/define-notification.d.mts +22 -0
- package/esm/dispatch/define-notification.d.mts.map +1 -0
- package/esm/dispatch/define-notification.mjs +125 -0
- package/esm/dispatch/define-notification.mjs.map +1 -0
- package/esm/dispatch/dispatch-id.mjs +15 -0
- package/esm/dispatch/dispatch-id.mjs.map +1 -0
- package/esm/dispatch/dispatcher.mjs +120 -0
- package/esm/dispatch/dispatcher.mjs.map +1 -0
- package/esm/dispatch/index.d.mts +4 -0
- package/esm/dispatch/index.mjs +6 -0
- package/esm/dispatch/notifications-event-bus.d.mts +18 -0
- package/esm/dispatch/notifications-event-bus.d.mts.map +1 -0
- package/esm/dispatch/notifications-event-bus.mjs +58 -0
- package/esm/dispatch/notifications-event-bus.mjs.map +1 -0
- package/esm/dispatch/notify.d.mts +20 -0
- package/esm/dispatch/notify.d.mts.map +1 -0
- package/esm/dispatch/notify.mjs +80 -0
- package/esm/dispatch/notify.mjs.map +1 -0
- package/esm/errors.d.mts +32 -0
- package/esm/errors.d.mts.map +1 -0
- package/esm/errors.mjs +56 -0
- package/esm/errors.mjs.map +1 -0
- package/esm/in-app/base-notifications-repository.d.mts +82 -0
- package/esm/in-app/base-notifications-repository.d.mts.map +1 -0
- package/esm/in-app/base-notifications-repository.mjs +187 -0
- package/esm/in-app/base-notifications-repository.mjs.map +1 -0
- package/esm/in-app/column-map.d.mts +65 -0
- package/esm/in-app/column-map.d.mts.map +1 -0
- package/esm/in-app/column-map.mjs +28 -0
- package/esm/in-app/column-map.mjs.map +1 -0
- package/esm/in-app/database-notification.d.mts +30 -0
- package/esm/in-app/database-notification.d.mts.map +1 -0
- package/esm/in-app/database-notification.mjs +70 -0
- package/esm/in-app/database-notification.mjs.map +1 -0
- package/esm/in-app/in-app.d.mts +56 -0
- package/esm/in-app/in-app.d.mts.map +1 -0
- package/esm/in-app/in-app.mjs +67 -0
- package/esm/in-app/in-app.mjs.map +1 -0
- package/esm/in-app/index.d.mts +4 -0
- package/esm/in-app/index.mjs +6 -0
- package/esm/index.d.mts +25 -0
- package/esm/index.mjs +23 -0
- package/esm/migration/index.d.mts +1 -0
- package/esm/migration/index.mjs +3 -0
- package/esm/migration/notification-columns.d.mts +13 -0
- package/esm/migration/notification-columns.d.mts.map +1 -0
- package/esm/migration/notification-columns.mjs +53 -0
- package/esm/migration/notification-columns.mjs.map +1 -0
- package/esm/queue/herald-queue.d.mts +10 -0
- package/esm/queue/herald-queue.d.mts.map +1 -0
- package/esm/queue/herald-queue.mjs +14 -0
- package/esm/queue/herald-queue.mjs.map +1 -0
- package/esm/queue/index.d.mts +3 -0
- package/esm/queue/index.mjs +5 -0
- package/esm/queue/load-herald.d.mts +6 -0
- package/esm/queue/load-herald.d.mts.map +1 -0
- package/esm/queue/load-herald.mjs +35 -0
- package/esm/queue/load-herald.mjs.map +1 -0
- package/esm/queue/notifications-worker.d.mts +9 -0
- package/esm/queue/notifications-worker.d.mts.map +1 -0
- package/esm/queue/notifications-worker.mjs +46 -0
- package/esm/queue/notifications-worker.mjs.map +1 -0
- package/esm/types.d.mts +130 -0
- package/esm/types.d.mts.map +1 -0
- package/llms-full.txt +994 -0
- package/llms.txt +17 -0
- package/package.json +40 -0
- package/skills/README.md +23 -0
- package/skills/configure-notifications/SKILL.md +138 -0
- package/skills/define-channel/SKILL.md +107 -0
- package/skills/define-notification/SKILL.md +133 -0
- package/skills/notifications-basics/SKILL.md +94 -0
- package/skills/observe-notifications/SKILL.md +66 -0
- package/skills/queue-notifications/SKILL.md +81 -0
- package/skills/send-ad-hoc/SKILL.md +95 -0
- package/skills/use-in-app/SKILL.md +168 -0
- package/skills/write-notification-migration/SKILL.md +70 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: queue-notifications
|
|
3
|
+
description: 'Send notifications asynchronously via the herald-backed queue. `heraldQueue({ channel?, broker? })` is a `QueueDispatcher` you add to the `queue` slot of the declarative `config/notifications.ts` (the connector registers it at boot); then `notif.queue(to, data, options?)` / `notify` publish a rendered job instead of sending inline. `startNotificationsWorker({ channel?, broker? })` runs the consumer (in a worker or the web process) that pulls jobs and runs `channel.send`. Both lazy-import `@warlock.js/herald` (optional peer) — a missing package throws a curated install message at use time. `defineNotification` renders payloads + resolves routes BEFORE queuing, so the job (`{ channel, route, payload, options }`) is fully serializable — no model re-hydration. Without a queue configured, `.queue()` rejects `NoQueueDispatcherError`. Triggers: `heraldQueue`, `startNotificationsWorker`, `QueueDispatcher`, `.queue(`, "async notifications", "queue notifications", "notification worker", "background notifications", `notifications.dispatch`; typical import `import { heraldQueue, startNotificationsWorker } from "@warlock.js/notifications"`. Skip: synchronous sends — `@warlock.js/notifications/define-notification/SKILL.md`; herald itself — `@warlock.js/herald/*`.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Queue notifications (async)
|
|
7
|
+
|
|
8
|
+
Move slow channels (SMTP, HTTP) off the request path. `.queue()` publishes a
|
|
9
|
+
rendered job to herald; a worker delivers it.
|
|
10
|
+
|
|
11
|
+
## Wire the dispatcher
|
|
12
|
+
|
|
13
|
+
```ts title="src/config/notifications.ts"
|
|
14
|
+
import { type NotificationConfig, heraldQueue, inApp, mailChannel } from "@warlock.js/notifications";
|
|
15
|
+
import { Notification } from "app/notifications/notification.model";
|
|
16
|
+
|
|
17
|
+
const config: NotificationConfig = {
|
|
18
|
+
channels: { mail: mailChannel(), database: inApp.configure({ model: Notification }) },
|
|
19
|
+
queue: heraldQueue(), // → `.queue()` now works
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default config; // the notifications connector registers it at boot
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`heraldQueue({ channel?, broker? })` publishes to `"notifications.dispatch"` on
|
|
26
|
+
the default broker unless overridden. It lazy-imports `@warlock.js/herald`; if
|
|
27
|
+
that package isn't installed, the first `.queue()` throws an install message.
|
|
28
|
+
|
|
29
|
+
Without `queue` configured, `.queue()` rejects `NoQueueDispatcherError` — a loud
|
|
30
|
+
config error, not a silent no-op.
|
|
31
|
+
|
|
32
|
+
## Run the worker
|
|
33
|
+
|
|
34
|
+
In a worker entrypoint (or the web process), after the config + the herald
|
|
35
|
+
broker are up:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { startNotificationsWorker } from "@warlock.js/notifications";
|
|
39
|
+
|
|
40
|
+
await startNotificationsWorker(); // subscribes to "notifications.dispatch"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The worker pulls each job, looks the channel up by name, and runs
|
|
44
|
+
`channel.send({ payload, route, options })`. The job carries an
|
|
45
|
+
ALREADY-RENDERED payload + resolved route (rendering happens at enqueue), so the
|
|
46
|
+
worker needs no recipient model.
|
|
47
|
+
|
|
48
|
+
## Send
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
await orderShipped.queue(user, { order }); // async
|
|
52
|
+
await orderShipped.queue(buyers, { order }, { delay: "10m" });
|
|
53
|
+
await notify.mail(user, payload, { /* sync — notify.* doesn't queue */ });
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`.queue()` renders synchronously then enqueues. Fan-out renders per recipient
|
|
57
|
+
and enqueues one job each.
|
|
58
|
+
|
|
59
|
+
## Idempotency
|
|
60
|
+
|
|
61
|
+
Pass `idempotencyKey` so a redelivered/retried job doesn't create a duplicate
|
|
62
|
+
in-app row — `createFor` find-or-creates on the key.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
await orderShipped.queue(user, { order }, { idempotencyKey: `ship:${order.id}` });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Phase notes / current limits
|
|
69
|
+
|
|
70
|
+
- **`delay` is carried on the job but not yet honored** — the worker delivers
|
|
71
|
+
immediately. Delay-aware delivery lands with a scheduled worker.
|
|
72
|
+
- **No retry / dead-letter yet** — a failed `channel.send` is logged + acked
|
|
73
|
+
(no poison-message loop). DLQ handling is a follow-up.
|
|
74
|
+
- Rate-limit budget is consumed at ENQUEUE time for queued sends (see
|
|
75
|
+
`RateLimiter` docstring).
|
|
76
|
+
|
|
77
|
+
## See also
|
|
78
|
+
|
|
79
|
+
- [`define-notification/SKILL.md`](../define-notification/SKILL.md) — `.queue()` on a defined notification.
|
|
80
|
+
- [`configure-notifications/SKILL.md`](../configure-notifications/SKILL.md) — the `queue` config slot.
|
|
81
|
+
- [`@warlock.js/herald/herald-basics/SKILL.md`](../../../herald/skills/herald-basics/SKILL.md) — connecting the broker.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: send-ad-hoc
|
|
3
|
+
description: 'Ad-hoc per-channel send via the `notify.<channel>(to, payload, options?)` Proxy facade. Works for any channel registered in `NotificationChannels` (built-in or custom). `to` accepts a `Notifiable` model OR a raw route string (`"x@y.com"`); `payload` is typed per channel via the registry; `options` is the standard `SendOptions`. `notify.channel(name).send(...)` is the runtime escape when the channel name is not a compile-time literal. Type-based preference gating applies when a type is known — explicit via `options.type` OR auto-detected from a database payload''s `type` field. For multi-channel sends use `defineNotification` instead — there is no inline `notify(to, { via, ... })` form on purpose. Triggers: `notify.<channel>`, `notify.channel(name)`, `notify.mail`, `notify.database`, "send a one-off notification", "ad-hoc notification", "send an email without defining a notification", "raw email target", "dynamic channel name"; typical import `import { notify } from "@warlock.js/notifications"`. Skip: reusable multi-channel notifications — `@warlock.js/notifications/define-notification/SKILL.md`; configuring channels — `@warlock.js/notifications/configure-notifications/SKILL.md`.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `notify.<channel>` — ad-hoc, per-channel sends
|
|
7
|
+
|
|
8
|
+
When you don't need a reusable definition — fire one channel, one recipient.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { notify } from "@warlock.js/notifications";
|
|
12
|
+
|
|
13
|
+
await notify.mail(user, { subject: "Welcome", html: "<p>Hi!</p>" });
|
|
14
|
+
await notify.database(user, { type: "welcome", title: "Welcome!" });
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
For **multi-channel** sends, use [`defineNotification`](../define-notification/SKILL.md) — that's the reusable pattern and the only mental model for "send through several channels at once" (no inline multi-channel overload).
|
|
18
|
+
|
|
19
|
+
## Shape
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
notify.<channel>(
|
|
23
|
+
to: Notifiable | string, // model OR raw route
|
|
24
|
+
payload: NotificationChannels[channel], // typed per channel
|
|
25
|
+
options?: SendOptions,
|
|
26
|
+
): Promise<void>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Raw target — no model
|
|
30
|
+
|
|
31
|
+
Pass a string instead of a model to use the string as the route directly:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
await notify.mail("guest@example.com", { subject: "Receipt", html: "<p>…</p>" });
|
|
35
|
+
await notify.whatsapp("+201234567890", { body: "Promo: 20% off!" }); // Phase 2
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
When the recipient isn't a model, `PreferenceProvider` and `RateLimiter` are skipped — there's no `Notifiable` to consult.
|
|
39
|
+
|
|
40
|
+
## Runtime-dynamic channel name
|
|
41
|
+
|
|
42
|
+
When the channel isn't a literal, use `notify.channel(name).send(...)`:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const channelName = await chooseChannelForUser(user);
|
|
46
|
+
await notify.channel(channelName).send(user, payload);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This is the only path with `unknown`-typed payloads — prefer the literal `notify.<channel>` when you can.
|
|
50
|
+
|
|
51
|
+
## Opt into preference/rate-limit gating
|
|
52
|
+
|
|
53
|
+
Ad-hoc sends bypass the preference gate unless a notification `type` is known. Two ways to provide it:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
// 1. Explicit — wins regardless of payload.
|
|
57
|
+
await notify.mail(user, payload, { type: "marketing.weekly" });
|
|
58
|
+
|
|
59
|
+
// 2. Auto-detected — database payloads carry their own `type` field.
|
|
60
|
+
await notify.database(user, { type: "welcome", title: "Welcome!" });
|
|
61
|
+
// ^^^^^^^^^^^^^^^ used for gating
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`SendOptions.force === true` bypasses preferences. `RateLimiter` is consulted whenever a type is known (regardless of `force`).
|
|
65
|
+
|
|
66
|
+
## Custom channels
|
|
67
|
+
|
|
68
|
+
After registering a custom channel (see [`define-channel`](../define-channel/SKILL.md)), it's first-class:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
await notify.discord(user, { content: "Build finished 🎉" });
|
|
72
|
+
await notify.slack(staff, { text: "Deploy starting" });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## SendOptions (3rd arg)
|
|
76
|
+
|
|
77
|
+
| Field | Effect |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `delay` | Reserved for `.queue` paths; not used by `notify.<channel>` (sync). |
|
|
80
|
+
| `locale` | Forwarded to the channel's `send({ options })`. |
|
|
81
|
+
| `meta` | Forwarded to the channel + included in every observability event. |
|
|
82
|
+
| `idempotencyKey` | Injected into the database payload (dedupe). |
|
|
83
|
+
| `force` | Bypass `PreferenceProvider`. Does NOT bypass `RateLimiter`. |
|
|
84
|
+
| `type` | Notification type for gating (ad-hoc only). |
|
|
85
|
+
|
|
86
|
+
## Failure surfaces
|
|
87
|
+
|
|
88
|
+
`notify.<channel>` rejects on configuration errors (`ChannelNotFoundError`) — those are loud, immediate, and don't fire a `failed` event. Channel-level send failures rethrow AND fire a `failed` event with the channel + error.
|
|
89
|
+
|
|
90
|
+
## See also
|
|
91
|
+
|
|
92
|
+
- [`notifications-basics/SKILL.md`](../notifications-basics/SKILL.md) — front door + mental model.
|
|
93
|
+
- [`define-notification/SKILL.md`](../define-notification/SKILL.md) — reusable multi-channel.
|
|
94
|
+
- [`define-channel/SKILL.md`](../define-channel/SKILL.md) — register a custom channel.
|
|
95
|
+
- [`observe-notifications/SKILL.md`](../observe-notifications/SKILL.md) — `sending` / `sent` / `failed` / `skipped` events.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: use-in-app
|
|
3
|
+
description: 'Use the `inApp` facade for the in-app/database channel''s READ side: `inApp.configure({ model: Notification })` or `{ repository: myRepo }` binds the store AND returns the channel; `inApp.list / listUnread / countUnread / markAsRead / markAsUnread` are RECIPIENT-SCOPED by construction (no IDOR — `markAsRead(user, id)` cannot flip another user''s row even with a foreign id). `countUnread` uses `RepositoryManager.countCached` for the badge fast-path. Triggers: `inApp.configure`, `inApp.list`, `inApp.listUnread`, `inApp.countUnread`, `inApp.markAsRead`, `inApp.markAsUnread`, `BaseNotificationsRepository`, `DatabaseNotification`, `NotificationsFilter`, "in-app notifications", "mark notification as read", "unread count", "notification badge", "IDOR-safe markAsRead"; typical import `import { inApp } from "@warlock.js/notifications"`. Skip: writing notifications (use `defineNotification` or `notify.database`) — `@warlock.js/notifications/define-notification/SKILL.md`; the migration columns — `@warlock.js/notifications/write-notification-migration/SKILL.md`; wiring it into config — `@warlock.js/notifications/configure-notifications/SKILL.md`.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `inApp` — in-app notification read API
|
|
7
|
+
|
|
8
|
+
The in-app database channel has two sides:
|
|
9
|
+
|
|
10
|
+
- **Write side** — `notify.database(user, {...})` and the database channel inside `defineNotification` create rows.
|
|
11
|
+
- **Read side** — `inApp.list` / `listUnread` / `countUnread` / `markAsRead` / `markAsUnread` query and mutate.
|
|
12
|
+
|
|
13
|
+
`inApp.configure({ model | repository })` binds the same repository for both sides, so cache invalidation just works.
|
|
14
|
+
|
|
15
|
+
## Configure (once, in `config/notifications.ts`)
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// 90% case — pass the model class; default repo built internally.
|
|
19
|
+
database: inApp.configure({ model: Notification }),
|
|
20
|
+
|
|
21
|
+
// 10% case — pass a custom repo.
|
|
22
|
+
database: inApp.configure({ repository: notificationsRepository }),
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Reads
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { inApp } from "@warlock.js/notifications";
|
|
29
|
+
|
|
30
|
+
// all rows for this recipient (paginated via the repo's standard `list`)
|
|
31
|
+
const all = await inApp.list(user);
|
|
32
|
+
|
|
33
|
+
// unread-only — pass `filter` to add type/etc. constraints
|
|
34
|
+
const unread = await inApp.listUnread(user, { type: "order.shipped" });
|
|
35
|
+
|
|
36
|
+
// cached unread count — backs the badge
|
|
37
|
+
const badge = await inApp.countUnread(user);
|
|
38
|
+
|
|
39
|
+
// one notification — for a detail view (recipient-scoped)
|
|
40
|
+
const one = await inApp.find(user, "ntf_123");
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
All read methods accept a cascade `Notifiable` model OR a raw `id` (string | number) — convenient when you only have an id without fetching the user.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
await inApp.list("user_42");
|
|
47
|
+
await inApp.countUnread(123);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Writes (recipient-scoped → no IDOR)
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// mark ALL unread rows for this recipient as read
|
|
54
|
+
await inApp.markAsRead(user);
|
|
55
|
+
|
|
56
|
+
// mark ONE specific row — STILL scoped to this recipient
|
|
57
|
+
await inApp.markAsRead(user, "ntf_123");
|
|
58
|
+
|
|
59
|
+
// inverse
|
|
60
|
+
await inApp.markAsUnread(user, "ntf_123");
|
|
61
|
+
|
|
62
|
+
// delete / dismiss — one row, or clear all for this recipient
|
|
63
|
+
await inApp.dismiss(user, "ntf_123");
|
|
64
|
+
await inApp.dismiss(user);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The recipient-id is forced into the `where` clause. A controller that receives an `id` from the request and naively calls `inApp.markAsRead(currentUser, id)` cannot accidentally flip another user's row — even if the request contains a forged id. The mutation will simply update 0 rows.
|
|
68
|
+
|
|
69
|
+
## Behind the scenes — `columnMap` drives everything
|
|
70
|
+
|
|
71
|
+
The model declares its physical columns ONCE via `static columnMap`. The
|
|
72
|
+
shipped `DatabaseNotification` accessors and `BaseNotificationsRepository`
|
|
73
|
+
(read filter + write mapping) both derive from it, so they can never drift:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
abstract class DatabaseNotification extends Model implements NotificationContract {
|
|
77
|
+
public static columnMap: NotificationColumnMap = {};
|
|
78
|
+
// recipientId / tenantId / isRead / readAt / markRead all read `columnMap`.
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class BaseNotificationsRepository extends RepositoryManager {
|
|
82
|
+
// constructor resolves the model's columnMap → builds `filterBy` +
|
|
83
|
+
// createFor / markRead / markUnread / deleteFor from it.
|
|
84
|
+
public createFor(recipientId, input, tenantId?) { /* one insert, starts unread */ }
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `columnMap` — map logical roles → your columns
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
export type NotificationColumnMap = {
|
|
92
|
+
recipient?: string; // recipient FK. Default "user_id"
|
|
93
|
+
tenant?: string; // multi-tenant scope, written from the recipient. Omit → single-tenant
|
|
94
|
+
readAt?: string; // read-timestamp column
|
|
95
|
+
isRead?: string; // read-flag column (indexed)
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Read-state is chosen by which keys are PRESENT:**
|
|
100
|
+
|
|
101
|
+
- `readAt` only → unread = `read_at IS NULL`; marking read stamps it.
|
|
102
|
+
- `isRead` only → unread = `is_read = false`; no timestamp.
|
|
103
|
+
- both → `is_read` is the indexed filter flag, `read_at` records WHEN.
|
|
104
|
+
|
|
105
|
+
Declare neither and you get the `readAt`-only default — so a model can omit
|
|
106
|
+
`columnMap` entirely. `unread` is the mode-agnostic read filter on `inApp` /
|
|
107
|
+
the repo; there is no `isRead` filter key to remember.
|
|
108
|
+
|
|
109
|
+
## Your model — declare `table`, `schema`, and `columnMap`
|
|
110
|
+
|
|
111
|
+
```ts title="src/app/notifications/notification.model.ts"
|
|
112
|
+
import { RegisterModel } from "@warlock.js/cascade";
|
|
113
|
+
import { v } from "@warlock.js/seal";
|
|
114
|
+
import { DatabaseNotification, type NotificationColumnMap } from "@warlock.js/notifications";
|
|
115
|
+
|
|
116
|
+
// Mirrors the migration columns; cascade validates + casts every write.
|
|
117
|
+
const notificationSchema = v.object({
|
|
118
|
+
user_id: v.string(),
|
|
119
|
+
type: v.string(),
|
|
120
|
+
title: v.string(),
|
|
121
|
+
body: v.string().nullish(),
|
|
122
|
+
payload: v.record(v.any()).nullish(),
|
|
123
|
+
read_at: v.date().nullish(),
|
|
124
|
+
idempotency_key: v.string().nullish(),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
@RegisterModel()
|
|
128
|
+
export class Notification extends DatabaseNotification {
|
|
129
|
+
public static table = "notifications";
|
|
130
|
+
public static schema = notificationSchema;
|
|
131
|
+
// read_at-only, single-tenant. Add `tenant: "organization_id"` for multi-tenant,
|
|
132
|
+
// swap to `isRead: "is_read"` (or add it) to change the read-state representation.
|
|
133
|
+
public static columnMap: NotificationColumnMap = { readAt: "read_at" };
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
To change columns, edit `columnMap` — the migration (`notificationColumns`), the
|
|
138
|
+
repo filter, and the accessors all follow. For a different driver's naming
|
|
139
|
+
(e.g. MongoDB camelCase), name the columns in `columnMap`
|
|
140
|
+
(`{ recipient: "userId", readAt: "readAt" }`).
|
|
141
|
+
|
|
142
|
+
## Custom repository (advanced, optional)
|
|
143
|
+
|
|
144
|
+
`columnMap` already covers column naming. Subclass only for EXTRA query methods:
|
|
145
|
+
|
|
146
|
+
```ts title="src/app/notifications/notifications.repository.ts"
|
|
147
|
+
import { BaseNotificationsRepository } from "@warlock.js/notifications";
|
|
148
|
+
import { Notification } from "./notification.model";
|
|
149
|
+
|
|
150
|
+
export class NotificationsRepository extends BaseNotificationsRepository<Notification> {
|
|
151
|
+
public source = Notification; // its columnMap still drives filter + write mapping
|
|
152
|
+
// …extra methods specific to your app
|
|
153
|
+
}
|
|
154
|
+
export const notificationsRepository = new NotificationsRepository();
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Then swap config:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
database: inApp.configure({ repository: notificationsRepository }),
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## See also
|
|
164
|
+
|
|
165
|
+
- [`notifications-basics/SKILL.md`](../notifications-basics/SKILL.md) — package front door.
|
|
166
|
+
- [`configure-notifications/SKILL.md`](../configure-notifications/SKILL.md) — wire `inApp.configure` into the config.
|
|
167
|
+
- [`write-notification-migration/SKILL.md`](../write-notification-migration/SKILL.md) — `notificationColumns(Notification)`, named from `columnMap`.
|
|
168
|
+
- [`define-notification/SKILL.md`](../define-notification/SKILL.md) — create rows via the database channel.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: write-notification-migration
|
|
3
|
+
description: 'Create the notifications table with the `notificationColumns(Notification)` factory + cascade `Migration.create`. Column NAMES come from the model''s `columnMap` (recipient / tenant / readAt / isRead); the fixed columns are `type` / `title` / `body` / `payload` / `idempotency_key`. Read-state follows columnMap presence: `readAt` → nullable timestamp, `isRead` → indexed boolean, both → both. Defaults to `user_id` + `read_at` when no columnMap. Spread + extend for extras (FK references, composite indexes). Triggers: `notificationColumns`, `Migration.create` for notifications, "create the notifications table", "notification migration", "add organization_id to notifications", `is_read` column, `read_at` column, `idempotency_key` column; typical import `import { Migration } from "@warlock.js/cascade"; import { notificationColumns } from "@warlock.js/notifications"`. Skip: generic cascade migration writing — `@warlock.js/cascade/write-migration/SKILL.md`; the model itself — `@warlock.js/notifications/use-in-app/SKILL.md`.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Write the notifications migration
|
|
7
|
+
|
|
8
|
+
The package ships no table or migration (thin eject). You own the migration; the package gives you a column factory that names columns from your model's `columnMap`, so it's one line.
|
|
9
|
+
|
|
10
|
+
> `npx warlock add notifications` scaffolds this migration (+ the model) for you. Reach for this skill when you need to customize columns or add extras.
|
|
11
|
+
|
|
12
|
+
## Common case — one line
|
|
13
|
+
|
|
14
|
+
```ts title="src/app/notifications/migrations/01-01-2026_00-00-00-notifications.migration.ts"
|
|
15
|
+
import { Migration } from "@warlock.js/cascade";
|
|
16
|
+
import { notificationColumns } from "@warlock.js/notifications";
|
|
17
|
+
import { Notification } from "../notification.model";
|
|
18
|
+
|
|
19
|
+
export default Migration.create(Notification, notificationColumns(Notification));
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`notificationColumns(Notification)` reads the model's `columnMap` and returns the
|
|
23
|
+
matching columns:
|
|
24
|
+
|
|
25
|
+
| columnMap | Columns |
|
|
26
|
+
|---|---|
|
|
27
|
+
| _(none / default)_ | `user_id`, `type`, `title`, `body`, `payload`, `read_at`, `idempotency_key` |
|
|
28
|
+
| `{ tenant: "organization_id", readAt: "read_at" }` | adds `organization_id`; read_at-only |
|
|
29
|
+
| `{ isRead: "is_read" }` | swaps `read_at` for `is_read` (indexed boolean) |
|
|
30
|
+
| `{ isRead: "is_read", readAt: "read_at" }` | both read-state columns |
|
|
31
|
+
|
|
32
|
+
The recipient / tenant / read-state column NAMES come from `columnMap`; `type` /
|
|
33
|
+
`title` / `body` / `payload` / `idempotency_key` are fixed. `id` / `createdAt` /
|
|
34
|
+
`updatedAt` are added automatically by cascade.
|
|
35
|
+
|
|
36
|
+
## With extras — spread + extend
|
|
37
|
+
|
|
38
|
+
Add your own columns — a category, a source channel, a tenant id, anything:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { Migration, string } from "@warlock.js/cascade";
|
|
42
|
+
import { notificationColumns } from "@warlock.js/notifications";
|
|
43
|
+
import { Notification } from "../notification.model";
|
|
44
|
+
|
|
45
|
+
export default Migration.create(Notification, {
|
|
46
|
+
...notificationColumns(Notification),
|
|
47
|
+
category: string().index().nullable(),
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Mirror any extra column in the model `schema`. To make it filterable, add a method to a custom repository (see `use-in-app/SKILL.md`).
|
|
52
|
+
|
|
53
|
+
## Column naming follows `columnMap`
|
|
54
|
+
|
|
55
|
+
The factory does NOT guess names from the driver — it reads the model's `columnMap`. To match a different convention (e.g. MongoDB camelCase), name the columns there and the migration follows:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
class Notification extends DatabaseNotification {
|
|
59
|
+
public static columnMap = { recipient: "userId", readAt: "readAt" };
|
|
60
|
+
}
|
|
61
|
+
// → notificationColumns(Notification) emits `userId` + `readAt`
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Without a model passed, defaults to `user_id` + `read_at` (read_at-only). See [`use-in-app/SKILL.md`](../use-in-app/SKILL.md) for the full `columnMap` shape.
|
|
65
|
+
|
|
66
|
+
## See also
|
|
67
|
+
|
|
68
|
+
- [`use-in-app/SKILL.md`](../use-in-app/SKILL.md) — the model + repository the migration backs.
|
|
69
|
+
- [`@warlock.js/cascade/write-migration/SKILL.md`](../../../cascade/skills/write-migration/SKILL.md) — generic Cascade migration form, `Migration.alter`, etc.
|
|
70
|
+
- [`@warlock.js/cascade/manage-data-sources/SKILL.md`](../../../cascade/skills/manage-data-sources/SKILL.md) — multi-data-source setup.
|