@aria-framework/notify 0.2.0 → 0.3.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/package.json +30 -30
- package/store.js +82 -45
package/package.json
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@aria-framework/notify",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Aria App Framework — notify module. In-app notifications: ownership-scoped rows, a per-process SSE hub for live delivery, and a fire-and-forget emitter that needs no request and no actor, so a scheduler can raise one.",
|
|
5
|
-
"license": "UNLICENSED",
|
|
6
|
-
"private": false,
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
|
-
"main": "index.js",
|
|
11
|
-
"files": [
|
|
12
|
-
"index.js",
|
|
13
|
-
"store.js",
|
|
14
|
-
"hub.js",
|
|
15
|
-
"emit.js",
|
|
16
|
-
"assets/",
|
|
17
|
-
"views/"
|
|
18
|
-
],
|
|
19
|
-
"peerDependencies": {
|
|
20
|
-
"better-sqlite3": ">=11"
|
|
21
|
-
},
|
|
22
|
-
"peerDependenciesMeta": {
|
|
23
|
-
"better-sqlite3": {
|
|
24
|
-
"optional": true
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"scripts": {
|
|
28
|
-
"test": "node test/store.js && node test/hub.js && node test/emit.js && node test/bell.js"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@aria-framework/notify",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Aria App Framework — notify module. In-app notifications: ownership-scoped rows, a per-process SSE hub for live delivery, and a fire-and-forget emitter that needs no request and no actor, so a scheduler can raise one.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"store.js",
|
|
14
|
+
"hub.js",
|
|
15
|
+
"emit.js",
|
|
16
|
+
"assets/",
|
|
17
|
+
"views/"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"better-sqlite3": ">=11"
|
|
21
|
+
},
|
|
22
|
+
"peerDependenciesMeta": {
|
|
23
|
+
"better-sqlite3": {
|
|
24
|
+
"optional": true
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "node test/store.js && node test/hub.js && node test/emit.js && node test/bell.js"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/store.js
CHANGED
|
@@ -18,8 +18,19 @@
|
|
|
18
18
|
'use strict';
|
|
19
19
|
|
|
20
20
|
/** The columns this module reads and writes. An app's migration must provide at least these. */
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Column list for the app's CREATE TABLE. A FUNCTION of the dialect since 0.3.0: the id column
|
|
23
|
+
* and the timestamp default are the two things engines genuinely differ on, and an app on
|
|
24
|
+
* Postgres cannot use `INTEGER PRIMARY KEY AUTOINCREMENT`.
|
|
25
|
+
*
|
|
26
|
+
* `is_read` stays INTEGER 0/1 rather than becoming BOOLEAN on Postgres — deliberately. A real
|
|
27
|
+
* boolean would make every `is_read = 0` in this file dialect-dependent for no gain, and
|
|
28
|
+
* INTEGER is valid on both.
|
|
29
|
+
*/
|
|
30
|
+
function schemaFor(dialect) {
|
|
31
|
+
const d = dialect || require('@aria-framework/db-worker').dialectFor('sqlite');
|
|
32
|
+
return `
|
|
33
|
+
id ${d.autoIncrementPk()},
|
|
23
34
|
user_id INTEGER NOT NULL, -- recipient; FK to the app's users table
|
|
24
35
|
type TEXT NOT NULL, -- app-defined, e.g. 'assigned' | 'invoice.overdue'
|
|
25
36
|
title TEXT NOT NULL, -- short line
|
|
@@ -30,27 +41,62 @@ const SCHEMA = `
|
|
|
30
41
|
entity_type TEXT, -- app's record type, e.g. 'ticket' | 'invoice'
|
|
31
42
|
entity_id INTEGER,
|
|
32
43
|
is_read INTEGER NOT NULL DEFAULT 0,
|
|
33
|
-
created_at TEXT NOT NULL DEFAULT (
|
|
44
|
+
created_at TEXT NOT NULL DEFAULT (${d.now()}),
|
|
34
45
|
read_at TEXT
|
|
35
46
|
`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The SQLite schema, unchanged, for consumers that predate schemaFor(). */
|
|
50
|
+
const SCHEMA = schemaFor(null);
|
|
36
51
|
|
|
37
52
|
/**
|
|
38
53
|
* @param {Function} getDb returns the better-sqlite3 handle. A function, not a handle, because
|
|
39
54
|
* the worker opens the database after modules are required.
|
|
40
55
|
* @param {string} table defaults to `notifications`
|
|
41
56
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
57
|
+
/**
|
|
58
|
+
* @param {object} opts
|
|
59
|
+
* driver the @aria-framework/db-worker driver (preferred since 0.3.0) — engine-agnostic
|
|
60
|
+
* getDb LEGACY: a function returning a better-sqlite3 handle. Still accepted so a consumer
|
|
61
|
+
* can upgrade this package before it upgrades its wiring; wrapped in a driver here.
|
|
62
|
+
* table defaults to 'notifications'
|
|
63
|
+
*
|
|
64
|
+
* EVERY METHOD IS ASYNC as of 0.3.0. There is no synchronous Postgres driver, so a store that
|
|
65
|
+
* runs on both engines has no choice — and the DB worker already awaits whatever a model method
|
|
66
|
+
* returns, so the cost lands on the ~6 call sites inside a consumer's model, not on its routes.
|
|
67
|
+
*/
|
|
68
|
+
function createStore(opts = {}) {
|
|
69
|
+
const { table = 'notifications' } = opts;
|
|
70
|
+
let driver = opts.driver || null;
|
|
71
|
+
if (!driver) {
|
|
72
|
+
if (typeof opts.getDb !== 'function') {
|
|
73
|
+
throw new Error('notify: pass { driver } (preferred) or { getDb } (legacy)');
|
|
74
|
+
}
|
|
75
|
+
// Adopt the raw handle rather than opening anything: the app owns the connection.
|
|
76
|
+
const { createSqliteDriver } = require('@aria-framework/db-worker');
|
|
77
|
+
let adopted = null;
|
|
78
|
+
driver = new Proxy({}, {
|
|
79
|
+
get(_t, prop) {
|
|
80
|
+
if (!adopted) adopted = createSqliteDriver({ adopt: opts.getDb() });
|
|
81
|
+
return adopted[prop];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
44
86
|
// Interpolated once, at construction, from a value the APP supplies — never from a request.
|
|
45
87
|
const T = String(table).replace(/[^A-Za-z0-9_]/g, '');
|
|
46
88
|
if (!T) throw new Error('notify: table name must be a plain identifier');
|
|
47
89
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
90
|
+
const d = () => driver.dialect;
|
|
91
|
+
|
|
92
|
+
async function unreadCount(userId) {
|
|
93
|
+
const r = await driver.get(`SELECT COUNT(*) AS c FROM ${T} WHERE user_id = ? AND is_read = 0`, [userId]);
|
|
94
|
+
return Number(r.c);
|
|
95
|
+
}
|
|
51
96
|
|
|
52
97
|
return {
|
|
53
98
|
SCHEMA,
|
|
99
|
+
schemaFor,
|
|
54
100
|
table: T,
|
|
55
101
|
|
|
56
102
|
/**
|
|
@@ -59,76 +105,67 @@ function createStore({ getDb, table = 'notifications' } = {}) {
|
|
|
59
105
|
* Returns the ids actually written so the caller can hand them to the hub. De-duplicated,
|
|
60
106
|
* because "the assignee" and "a collaborator" are frequently the same person and two bells
|
|
61
107
|
* for one event reads as a bug.
|
|
62
|
-
*
|
|
63
|
-
* @param {number[]} userIds
|
|
64
|
-
* @param {{type, title, body?, link?, actor_name?, entity_type?, entity_id?}} p
|
|
65
|
-
* @returns {{userIds: number[]}}
|
|
66
108
|
*/
|
|
67
|
-
createMany(userIds, p = {}) {
|
|
109
|
+
async createMany(userIds, p = {}) {
|
|
68
110
|
const ids = [...new Set((userIds || []).filter((v) => v != null).map(Number))]
|
|
69
111
|
.filter((n) => Number.isInteger(n));
|
|
70
112
|
// A notification with no type or no title has nothing to show; refusing quietly is right
|
|
71
113
|
// because the caller is fire-and-forget and must not learn about this through a throw.
|
|
72
114
|
if (!ids.length || !p.type || !p.title) return { userIds: [] };
|
|
73
115
|
|
|
74
|
-
|
|
75
|
-
const ins = db.prepare(`
|
|
76
|
-
INSERT INTO ${T} (user_id, type, title, body, link, actor_name, entity_type, entity_id)
|
|
77
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
78
|
-
`);
|
|
79
|
-
db.transaction(() => {
|
|
116
|
+
await driver.transaction(async (tx) => {
|
|
80
117
|
for (const uid of ids) {
|
|
81
|
-
|
|
82
|
-
|
|
118
|
+
await tx.run(
|
|
119
|
+
`INSERT INTO ${T} (user_id, type, title, body, link, actor_name, entity_type, entity_id)
|
|
120
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
121
|
+
[uid, p.type, p.title, p.body || null, p.link || null,
|
|
122
|
+
p.actor_name || null, p.entity_type || null, p.entity_id || null]
|
|
123
|
+
);
|
|
83
124
|
}
|
|
84
|
-
})
|
|
125
|
+
});
|
|
85
126
|
return { userIds: ids };
|
|
86
127
|
},
|
|
87
128
|
|
|
88
129
|
unreadCount,
|
|
89
130
|
|
|
90
131
|
/** Latest N (read and unread) for the bell, plus the unread count. */
|
|
91
|
-
recent(userId, limit = 15) {
|
|
132
|
+
async recent(userId, limit = 15) {
|
|
92
133
|
const lim = Math.min(50, Math.max(1, parseInt(limit, 10) || 15));
|
|
93
|
-
const items =
|
|
94
|
-
`SELECT * FROM ${T} WHERE user_id = ? ORDER BY created_at DESC, id DESC LIMIT
|
|
95
|
-
|
|
96
|
-
return { unread: unreadCount(userId), items };
|
|
134
|
+
const items = await driver.all(
|
|
135
|
+
`SELECT * FROM ${T} WHERE user_id = ? ORDER BY created_at DESC, id DESC LIMIT ?`, [userId, lim]);
|
|
136
|
+
return { unread: await unreadCount(userId), items };
|
|
97
137
|
},
|
|
98
138
|
|
|
99
139
|
/** Paginated history for a full notifications page. */
|
|
100
|
-
listPage(userId, page = 1, pageSize = 25) {
|
|
101
|
-
const db = getDb();
|
|
140
|
+
async listPage(userId, page = 1, pageSize = 25) {
|
|
102
141
|
const p = Math.max(1, parseInt(page, 10) || 1);
|
|
103
142
|
const size = Math.min(100, Math.max(5, parseInt(pageSize, 10) || 25));
|
|
104
|
-
const total =
|
|
105
|
-
const rows =
|
|
106
|
-
`SELECT * FROM ${T} WHERE user_id = ? ORDER BY created_at DESC, id DESC LIMIT ? OFFSET
|
|
107
|
-
|
|
143
|
+
const total = Number((await driver.get(`SELECT COUNT(*) AS c FROM ${T} WHERE user_id = ?`, [userId])).c);
|
|
144
|
+
const rows = await driver.all(
|
|
145
|
+
`SELECT * FROM ${T} WHERE user_id = ? ORDER BY created_at DESC, id DESC LIMIT ? OFFSET ?`,
|
|
146
|
+
[userId, size, (p - 1) * size]);
|
|
108
147
|
return {
|
|
109
148
|
rows, total, page: p, pageSize: size,
|
|
110
149
|
pages: Math.max(1, Math.ceil(total / size)),
|
|
111
|
-
unread: unreadCount(userId)
|
|
150
|
+
unread: await unreadCount(userId)
|
|
112
151
|
};
|
|
113
152
|
},
|
|
114
153
|
|
|
115
154
|
/** Mark one read — ONLY if it belongs to userId. The scope is the authorisation. */
|
|
116
|
-
markRead(id, userId) {
|
|
117
|
-
const r =
|
|
118
|
-
`UPDATE ${T} SET is_read = 1, read_at =
|
|
119
|
-
WHERE id = ? AND user_id = ? AND is_read = 0
|
|
120
|
-
).run(id, userId);
|
|
155
|
+
async markRead(id, userId) {
|
|
156
|
+
const r = await driver.run(
|
|
157
|
+
`UPDATE ${T} SET is_read = 1, read_at = ${d().now()}
|
|
158
|
+
WHERE id = ? AND user_id = ? AND is_read = 0`, [id, userId]);
|
|
121
159
|
return { changed: r.changes };
|
|
122
160
|
},
|
|
123
161
|
|
|
124
|
-
markAllRead(userId) {
|
|
125
|
-
const r =
|
|
126
|
-
`UPDATE ${T} SET is_read = 1, read_at =
|
|
127
|
-
WHERE user_id = ? AND is_read = 0
|
|
128
|
-
).run(userId);
|
|
162
|
+
async markAllRead(userId) {
|
|
163
|
+
const r = await driver.run(
|
|
164
|
+
`UPDATE ${T} SET is_read = 1, read_at = ${d().now()}
|
|
165
|
+
WHERE user_id = ? AND is_read = 0`, [userId]);
|
|
129
166
|
return { changed: r.changes };
|
|
130
167
|
}
|
|
131
168
|
};
|
|
132
169
|
}
|
|
133
170
|
|
|
134
|
-
module.exports = { createStore, SCHEMA };
|
|
171
|
+
module.exports = { createStore, SCHEMA, schemaFor };
|