Documentation / @zerotal/notifications / DatabaseChannel
Class: DatabaseChannel
Defined in: notifications/src/DatabaseChannel.ts:63
Stores notifications in the database. The table and its indexes are created automatically on first use.
Schema: id TEXT PRIMARY KEY — UUID notifiable_type TEXT NOT NULL — recipient's model class name notifiable_id TEXT NOT NULL — stringified notifiable id type TEXT NOT NULL — notification class name data TEXT NOT NULL — JSON-encoded toDatabase() result read_at TEXT — ISO timestamp when read; NULL = unread created_at TEXT NOT NULL — ISO timestamp
Every read is scoped by (notifiable_type, notifiable_id), not by id alone:
ids are only unique within a model, so a User#1 and a Team#1 would
otherwise share one inbox.
Constructors
Constructor
new DatabaseChannel(
table,_sql):DatabaseChannel
Defined in: notifications/src/DatabaseChannel.ts:67
Parameters
table
string
_sql
Returns
DatabaseChannel
Methods
send()
send(
notifiable,notification):Promise<void>
Defined in: notifications/src/DatabaseChannel.ts:103
Parameters
notifiable
Notifiable
notification
Returns
Promise<void>
markAsRead()
markAsRead(
id):Promise<void>
Defined in: notifications/src/DatabaseChannel.ts:134
Mark a notification as read by its id.
Parameters
id
string
Returns
Promise<void>
markAsUnread()
markAsUnread(
id):Promise<void>
Defined in: notifications/src/DatabaseChannel.ts:141
Mark a notification as unread by its id.
Parameters
id
string
Returns
Promise<void>
unread()
unread(
notifiable,query?):Promise<NotificationRecord[]>
Defined in: notifications/src/DatabaseChannel.ts:147
Return unread notifications for a notifiable, newest first.
Parameters
notifiable
Notifiable
query?
InboxQuery = {}
Returns
Promise<NotificationRecord[]>
all()
all(
notifiable,query?):Promise<NotificationRecord[]>
Defined in: notifications/src/DatabaseChannel.ts:162
Return notifications for a notifiable, newest first.
Parameters
notifiable
Notifiable
query?
InboxQuery = {}
Returns
Promise<NotificationRecord[]>
unreadCount()
unreadCount(
notifiable):Promise<number>
Defined in: notifications/src/DatabaseChannel.ts:177
How many unread notifications a notifiable has — for a badge, without loading rows.
Parameters
notifiable
Notifiable
Returns
Promise<number>
markAllAsRead()
markAllAsRead(
notifiable):Promise<void>
Defined in: notifications/src/DatabaseChannel.ts:193
Mark every unread notification for a notifiable as read.
Parameters
notifiable
Notifiable
Returns
Promise<void>
recent()
recent(
limit?):Promise<NotificationRecord[]>
Defined in: notifications/src/DatabaseChannel.ts:214
The most recent stored notifications across every notifiable, newest first.
Backs the admin console; use all() for one recipient's inbox.
Parameters
limit?
number = 100
Returns
Promise<NotificationRecord[]>
delete()
delete(
id):Promise<void>
Defined in: notifications/src/DatabaseChannel.ts:223
Delete one stored notification by id.
Parameters
id
string
Returns
Promise<void>
clear()
clear(
notifiable):Promise<void>
Defined in: notifications/src/DatabaseChannel.ts:229
Delete every stored notification for a notifiable.
Parameters
notifiable
Notifiable
Returns
Promise<void>
prune()
prune(
days,includeUnread?):Promise<number>
Defined in: notifications/src/DatabaseChannel.ts:247
Delete read notifications older than days, across every notifiable.
Backs notifications:prune — an inbox table grows without bound otherwise.
Parameters
days
number
Age threshold in days.
includeUnread?
boolean = false
Also prune notifications never read. Default: false.
Returns
Promise<number>
How many rows were deleted.
_count()
_count():
Promise<number>
Defined in: notifications/src/DatabaseChannel.ts:265
Total stored notifications, across every notifiable.
Returns
Promise<number>