@molecule/api-resource-comment 1.0.5 → 1.1.1
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/README.md +174 -99
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/numbers.d.ts +49 -0
- package/dist/numbers.d.ts.map +1 -0
- package/dist/numbers.js +117 -0
- package/package.json +4 -2
- package/src/__setup__/comment_numbers.sql +8 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-10-16T00:32:13.746Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-comment
|
|
@@ -86,20 +86,14 @@ interface CreateCommentInput {
|
|
|
86
86
|
}
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
#### `
|
|
89
|
+
#### `UpdateCommentInput`
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
Input for updating an existing comment.
|
|
92
92
|
|
|
93
93
|
```typescript
|
|
94
|
-
interface
|
|
95
|
-
/** The
|
|
96
|
-
|
|
97
|
-
/** Total number of matching items across all pages. */
|
|
98
|
-
total: number
|
|
99
|
-
/** Maximum number of results per page. */
|
|
100
|
-
limit: number
|
|
101
|
-
/** Number of results skipped. */
|
|
102
|
-
offset: number
|
|
94
|
+
interface UpdateCommentInput {
|
|
95
|
+
/** The updated comment body text. */
|
|
96
|
+
body: string
|
|
103
97
|
}
|
|
104
98
|
```
|
|
105
99
|
|
|
@@ -116,14 +110,33 @@ interface PaginationOptions {
|
|
|
116
110
|
}
|
|
117
111
|
```
|
|
118
112
|
|
|
119
|
-
#### `
|
|
113
|
+
#### `PaginatedResult`
|
|
120
114
|
|
|
121
|
-
|
|
115
|
+
A paginated result set.
|
|
122
116
|
|
|
123
117
|
```typescript
|
|
124
|
-
interface
|
|
125
|
-
/** The
|
|
126
|
-
|
|
118
|
+
interface PaginatedResult<T> {
|
|
119
|
+
/** The result items for the current page. */
|
|
120
|
+
data: T[]
|
|
121
|
+
/** Total number of matching items across all pages. */
|
|
122
|
+
total: number
|
|
123
|
+
/** Maximum number of results per page. */
|
|
124
|
+
limit: number
|
|
125
|
+
/** Number of results skipped. */
|
|
126
|
+
offset: number
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Types
|
|
131
|
+
|
|
132
|
+
#### `NumberedComment`
|
|
133
|
+
|
|
134
|
+
A comment together with its sequential number.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
type NumberedComment = Comment & {
|
|
138
|
+
/** The comment's sequential integer number (1, 2, 3, …). */
|
|
139
|
+
number: number
|
|
127
140
|
}
|
|
128
141
|
```
|
|
129
142
|
|
|
@@ -151,153 +164,169 @@ function create(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
|
151
164
|
- `req` — The request with `resourceType` and `resourceId` params and comment body.
|
|
152
165
|
- `res` — The response object.
|
|
153
166
|
|
|
154
|
-
#### `
|
|
167
|
+
#### `del(req, res)`
|
|
155
168
|
|
|
156
|
-
|
|
169
|
+
Deletes a comment. Only the comment owner can delete.
|
|
157
170
|
|
|
158
171
|
```typescript
|
|
159
|
-
function
|
|
160
|
-
resourceType: string,
|
|
161
|
-
resourceId: string,
|
|
162
|
-
userId: string,
|
|
163
|
-
data: CreateCommentInput,
|
|
164
|
-
): Promise<Comment>
|
|
172
|
+
function del(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
165
173
|
```
|
|
166
174
|
|
|
167
|
-
- `
|
|
168
|
-
- `
|
|
169
|
-
- `userId` — The ID of the commenting user.
|
|
170
|
-
- `data` — The comment creation input.
|
|
175
|
+
- `req` — The request with `commentId` param.
|
|
176
|
+
- `res` — The response object.
|
|
171
177
|
|
|
172
|
-
|
|
178
|
+
#### `list(req, res)`
|
|
173
179
|
|
|
174
|
-
|
|
180
|
+
Lists paginated top-level comments for a resource.
|
|
175
181
|
|
|
176
|
-
|
|
182
|
+
```typescript
|
|
183
|
+
function list(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
- `req` — The request with `resourceType` and `resourceId` params.
|
|
187
|
+
- `res` — The response object.
|
|
188
|
+
|
|
189
|
+
#### `read(req, res)`
|
|
190
|
+
|
|
191
|
+
Retrieves a single comment by ID.
|
|
177
192
|
|
|
178
193
|
```typescript
|
|
179
|
-
function
|
|
194
|
+
function read(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
180
195
|
```
|
|
181
196
|
|
|
182
197
|
- `req` — The request with `commentId` param.
|
|
183
198
|
- `res` — The response object.
|
|
184
199
|
|
|
185
|
-
#### `
|
|
200
|
+
#### `replies(req, res)`
|
|
186
201
|
|
|
187
|
-
|
|
202
|
+
Lists paginated replies to a comment.
|
|
188
203
|
|
|
189
204
|
```typescript
|
|
190
|
-
function
|
|
205
|
+
function replies(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
191
206
|
```
|
|
192
207
|
|
|
193
|
-
- `
|
|
194
|
-
- `
|
|
195
|
-
|
|
196
|
-
**Returns:** `true` if deleted, `false` if not found or unauthorized.
|
|
208
|
+
- `req` — The request with `commentId` param.
|
|
209
|
+
- `res` — The response object.
|
|
197
210
|
|
|
198
|
-
#### `
|
|
211
|
+
#### `update(req, res)`
|
|
199
212
|
|
|
200
|
-
|
|
213
|
+
Updates an existing comment. Only the comment owner can update.
|
|
201
214
|
|
|
202
215
|
```typescript
|
|
203
|
-
function
|
|
216
|
+
function update(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
204
217
|
```
|
|
205
218
|
|
|
206
|
-
- `
|
|
207
|
-
|
|
208
|
-
**Returns:** The comment or `null` if not found.
|
|
219
|
+
- `req` — The request with `commentId` param and update body.
|
|
220
|
+
- `res` — The response object.
|
|
209
221
|
|
|
210
|
-
#### `
|
|
222
|
+
#### `numberComment(commentId)`
|
|
211
223
|
|
|
212
|
-
|
|
224
|
+
The comment's number, allocating the next one when it has none. Numbers are
|
|
225
|
+
unique and increase.
|
|
213
226
|
|
|
214
227
|
```typescript
|
|
215
|
-
function
|
|
228
|
+
function numberComment(commentId: string): Promise<number>
|
|
216
229
|
```
|
|
217
230
|
|
|
218
|
-
- `
|
|
219
|
-
- `resourceId` — The ID of the resource.
|
|
231
|
+
- `commentId` — The comment's UUID.
|
|
220
232
|
|
|
221
|
-
**Returns:**
|
|
233
|
+
**Returns:** Its sequential number.
|
|
222
234
|
|
|
223
|
-
#### `
|
|
235
|
+
#### `createNumberedComment(resourceType, resourceId, userId, data)`
|
|
224
236
|
|
|
225
|
-
|
|
226
|
-
Only returns top-level comments (no replies).
|
|
237
|
+
Creates a comment and its number together: both are written, or neither.
|
|
227
238
|
|
|
228
239
|
```typescript
|
|
229
|
-
function
|
|
240
|
+
function createNumberedComment(
|
|
230
241
|
resourceType: string,
|
|
231
242
|
resourceId: string,
|
|
232
|
-
|
|
233
|
-
|
|
243
|
+
userId: string,
|
|
244
|
+
data: CreateCommentInput,
|
|
245
|
+
): Promise<NumberedComment>
|
|
234
246
|
```
|
|
235
247
|
|
|
236
|
-
- `resourceType` — The type of resource.
|
|
237
|
-
- `resourceId` — The ID of the resource.
|
|
238
|
-
- `
|
|
248
|
+
- `resourceType` — The type of resource being commented on.
|
|
249
|
+
- `resourceId` — The ID of the resource being commented on.
|
|
250
|
+
- `userId` — The ID of the commenting user.
|
|
251
|
+
- `data` — The comment creation input.
|
|
239
252
|
|
|
240
|
-
**Returns:**
|
|
253
|
+
**Returns:** The created comment with its number.
|
|
241
254
|
|
|
242
|
-
#### `
|
|
255
|
+
#### `getCommentByNumber(number)`
|
|
243
256
|
|
|
244
|
-
|
|
257
|
+
The comment a number belongs to.
|
|
245
258
|
|
|
246
259
|
```typescript
|
|
247
|
-
function
|
|
248
|
-
commentId: string,
|
|
249
|
-
options?: PaginationOptions,
|
|
250
|
-
): Promise<PaginatedResult<Comment>>
|
|
260
|
+
function getCommentByNumber(number: number): Promise<NumberedComment | null>
|
|
251
261
|
```
|
|
252
262
|
|
|
253
|
-
- `
|
|
254
|
-
- `options` — Pagination options.
|
|
263
|
+
- `number` — The comment's sequential number.
|
|
255
264
|
|
|
256
|
-
**Returns:**
|
|
265
|
+
**Returns:** The comment with its number, or `null` when no comment has it.
|
|
257
266
|
|
|
258
|
-
#### `
|
|
267
|
+
#### `commentNumbers(commentIds)`
|
|
259
268
|
|
|
260
|
-
|
|
269
|
+
The numbers of the given comments, for listing them with their numbers.
|
|
270
|
+
Comments without a number are left out of the map.
|
|
261
271
|
|
|
262
272
|
```typescript
|
|
263
|
-
function
|
|
273
|
+
function commentNumbers(commentIds: readonly string[]): Promise<Map<string, number>>
|
|
264
274
|
```
|
|
265
275
|
|
|
266
|
-
- `
|
|
267
|
-
- `res` — The response object.
|
|
276
|
+
- `commentIds` — The comments' UUIDs.
|
|
268
277
|
|
|
269
|
-
|
|
278
|
+
**Returns:** Each numbered comment's UUID mapped to its number.
|
|
270
279
|
|
|
271
|
-
|
|
280
|
+
#### `createComment(resourceType, resourceId, userId, data)`
|
|
281
|
+
|
|
282
|
+
Creates a new comment on a resource.
|
|
272
283
|
|
|
273
284
|
```typescript
|
|
274
|
-
function
|
|
285
|
+
function createComment(
|
|
286
|
+
resourceType: string,
|
|
287
|
+
resourceId: string,
|
|
288
|
+
userId: string,
|
|
289
|
+
data: CreateCommentInput,
|
|
290
|
+
): Promise<Comment>
|
|
275
291
|
```
|
|
276
292
|
|
|
277
|
-
- `
|
|
278
|
-
- `
|
|
293
|
+
- `resourceType` — The type of resource being commented on.
|
|
294
|
+
- `resourceId` — The ID of the resource being commented on.
|
|
295
|
+
- `userId` — The ID of the commenting user.
|
|
296
|
+
- `data` — The comment creation input.
|
|
279
297
|
|
|
280
|
-
|
|
298
|
+
**Returns:** The created comment.
|
|
281
299
|
|
|
282
|
-
|
|
300
|
+
#### `getCommentsByResource(resourceType, resourceId, options)`
|
|
301
|
+
|
|
302
|
+
Retrieves paginated comments for a resource, ordered by creation date descending.
|
|
303
|
+
Only returns top-level comments (no replies).
|
|
283
304
|
|
|
284
305
|
```typescript
|
|
285
|
-
function
|
|
306
|
+
function getCommentsByResource(
|
|
307
|
+
resourceType: string,
|
|
308
|
+
resourceId: string,
|
|
309
|
+
options?: PaginationOptions,
|
|
310
|
+
): Promise<PaginatedResult<Comment>>
|
|
286
311
|
```
|
|
287
312
|
|
|
288
|
-
- `
|
|
289
|
-
- `
|
|
313
|
+
- `resourceType` — The type of resource.
|
|
314
|
+
- `resourceId` — The ID of the resource.
|
|
315
|
+
- `options` — Pagination options.
|
|
290
316
|
|
|
291
|
-
|
|
317
|
+
**Returns:** A paginated result of comments.
|
|
292
318
|
|
|
293
|
-
|
|
319
|
+
#### `getCommentById(commentId)`
|
|
320
|
+
|
|
321
|
+
Retrieves a single comment by ID.
|
|
294
322
|
|
|
295
323
|
```typescript
|
|
296
|
-
function
|
|
324
|
+
function getCommentById(commentId: string): Promise<Comment | null>
|
|
297
325
|
```
|
|
298
326
|
|
|
299
|
-
- `
|
|
300
|
-
|
|
327
|
+
- `commentId` — The comment ID to look up.
|
|
328
|
+
|
|
329
|
+
**Returns:** The comment or `null` if not found.
|
|
301
330
|
|
|
302
331
|
#### `updateComment(commentId, userId, data)`
|
|
303
332
|
|
|
@@ -317,19 +346,50 @@ function updateComment(
|
|
|
317
346
|
|
|
318
347
|
**Returns:** The updated comment or `null` if not found or unauthorized.
|
|
319
348
|
|
|
320
|
-
|
|
349
|
+
#### `deleteComment(commentId, userId)`
|
|
321
350
|
|
|
322
|
-
|
|
351
|
+
Deletes a comment. Only the comment owner can delete.
|
|
323
352
|
|
|
324
|
-
|
|
353
|
+
```typescript
|
|
354
|
+
function deleteComment(commentId: string, userId: string): Promise<boolean>
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
- `commentId` — The comment ID to delete.
|
|
358
|
+
- `userId` — The requesting user's ID (must match comment owner).
|
|
359
|
+
|
|
360
|
+
**Returns:** `true` if deleted, `false` if not found or unauthorized.
|
|
361
|
+
|
|
362
|
+
#### `getReplies(commentId, options)`
|
|
363
|
+
|
|
364
|
+
Retrieves paginated replies to a comment, ordered by creation date ascending.
|
|
325
365
|
|
|
326
366
|
```typescript
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
367
|
+
function getReplies(
|
|
368
|
+
commentId: string,
|
|
369
|
+
options?: PaginationOptions,
|
|
370
|
+
): Promise<PaginatedResult<Comment>>
|
|
331
371
|
```
|
|
332
372
|
|
|
373
|
+
- `commentId` — The parent comment ID.
|
|
374
|
+
- `options` — Pagination options.
|
|
375
|
+
|
|
376
|
+
**Returns:** A paginated result of reply comments.
|
|
377
|
+
|
|
378
|
+
#### `getCommentCount(resourceType, resourceId)`
|
|
379
|
+
|
|
380
|
+
Returns the total number of comments on a resource (including replies).
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
function getCommentCount(resourceType: string, resourceId: string): Promise<number>
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
- `resourceType` — The type of resource.
|
|
387
|
+
- `resourceId` — The ID of the resource.
|
|
388
|
+
|
|
389
|
+
**Returns:** The total comment count.
|
|
390
|
+
|
|
391
|
+
### Constants
|
|
392
|
+
|
|
333
393
|
#### `requestHandlerMap`
|
|
334
394
|
|
|
335
395
|
Handler map for comment routes.
|
|
@@ -389,6 +449,17 @@ const routes: readonly [
|
|
|
389
449
|
]
|
|
390
450
|
```
|
|
391
451
|
|
|
452
|
+
#### `createCommentSchema`
|
|
453
|
+
|
|
454
|
+
Schema for validating comment creation input.
|
|
455
|
+
|
|
456
|
+
```typescript
|
|
457
|
+
const createCommentSchema: z.ZodObject<
|
|
458
|
+
{ body: z.ZodString; parentId: z.ZodOptional<z.ZodString> },
|
|
459
|
+
z.core.$strip
|
|
460
|
+
>
|
|
461
|
+
```
|
|
462
|
+
|
|
392
463
|
#### `updateCommentSchema`
|
|
393
464
|
|
|
394
465
|
Schema for validating comment update input.
|
|
@@ -403,7 +474,7 @@ const updateCommentSchema: z.ZodObject<{ body: z.ZodString }, z.core.$strip>
|
|
|
403
474
|
|
|
404
475
|
Peer dependencies:
|
|
405
476
|
|
|
406
|
-
- `@molecule/api-database` ^1.0
|
|
477
|
+
- `@molecule/api-database` ^1.1.0
|
|
407
478
|
- `@molecule/api-i18n` ^1.0.1
|
|
408
479
|
- `@molecule/api-logger` ^1.0.1
|
|
409
480
|
- `@molecule/api-resource` ^1.0.1
|
|
@@ -425,6 +496,10 @@ Peer dependencies:
|
|
|
425
496
|
- **Migration required.** `src/__setup__/comments.sql` ships with this package
|
|
426
497
|
and must exist in the target database before use (scaffolded apps apply it
|
|
427
498
|
automatically; existing apps must apply it first).
|
|
499
|
+
- **Integer comment ids (RealWorld's `comment.id`, most public API specs):** keep the
|
|
500
|
+
UUID and give each comment a sequential number — `createNumberedComment()` creates both
|
|
501
|
+
in one transaction, `getCommentByNumber(n)` finds it, `commentNumbers(ids)` maps a list.
|
|
502
|
+
Apply `src/__setup__/comment_numbers.sql` too. Never build your own id-mapping table.
|
|
428
503
|
- **Reads are PUBLIC by default.** `list`, `read`, `replies`, and `count` ship
|
|
429
504
|
with no auth middleware (comment threads on public content). If the
|
|
430
505
|
commented resources are private in your app, add an authorizer that checks
|
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
* - **Migration required.** `src/__setup__/comments.sql` ships with this package
|
|
29
29
|
* and must exist in the target database before use (scaffolded apps apply it
|
|
30
30
|
* automatically; existing apps must apply it first).
|
|
31
|
+
* - **Integer comment ids (RealWorld's `comment.id`, most public API specs):** keep the
|
|
32
|
+
* UUID and give each comment a sequential number — `createNumberedComment()` creates both
|
|
33
|
+
* in one transaction, `getCommentByNumber(n)` finds it, `commentNumbers(ids)` maps a list.
|
|
34
|
+
* Apply `src/__setup__/comment_numbers.sql` too. Never build your own id-mapping table.
|
|
31
35
|
* - **Reads are PUBLIC by default.** `list`, `read`, `replies`, and `count` ship
|
|
32
36
|
* with no auth middleware (comment threads on public content). If the
|
|
33
37
|
* commented resources are private in your app, add an authorizer that checks
|
|
@@ -60,6 +64,7 @@
|
|
|
60
64
|
export * from './authorizers/index.js';
|
|
61
65
|
export * from './browser-guard.js';
|
|
62
66
|
export * from './handlers/index.js';
|
|
67
|
+
export * from './numbers.js';
|
|
63
68
|
export * from './requestHandlerMap.js';
|
|
64
69
|
export * from './routes.js';
|
|
65
70
|
export * from './service.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AAEH,cAAc,wBAAwB,CAAA;AACtC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,cAAc,CAAA;AAC5B,cAAc,wBAAwB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
* - **Migration required.** `src/__setup__/comments.sql` ships with this package
|
|
29
29
|
* and must exist in the target database before use (scaffolded apps apply it
|
|
30
30
|
* automatically; existing apps must apply it first).
|
|
31
|
+
* - **Integer comment ids (RealWorld's `comment.id`, most public API specs):** keep the
|
|
32
|
+
* UUID and give each comment a sequential number — `createNumberedComment()` creates both
|
|
33
|
+
* in one transaction, `getCommentByNumber(n)` finds it, `commentNumbers(ids)` maps a list.
|
|
34
|
+
* Apply `src/__setup__/comment_numbers.sql` too. Never build your own id-mapping table.
|
|
31
35
|
* - **Reads are PUBLIC by default.** `list`, `read`, `replies`, and `count` ship
|
|
32
36
|
* with no auth middleware (comment threads on public content). If the
|
|
33
37
|
* commented resources are private in your app, add an authorizer that checks
|
|
@@ -60,6 +64,7 @@
|
|
|
60
64
|
export * from './authorizers/index.js';
|
|
61
65
|
export * from './browser-guard.js';
|
|
62
66
|
export * from './handlers/index.js';
|
|
67
|
+
export * from './numbers.js';
|
|
63
68
|
export * from './requestHandlerMap.js';
|
|
64
69
|
export * from './routes.js';
|
|
65
70
|
export * from './service.js';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequential integer numbers for comments, for APIs whose clients address a
|
|
3
|
+
* comment by an integer (RealWorld's `comment.id` is one; so are most public
|
|
4
|
+
* API specs). The comment keeps its UUID; the number lives in
|
|
5
|
+
* `comment_numbers` and is allocated once, at most one per comment.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { Comment, CreateCommentInput } from './types.js';
|
|
10
|
+
/** A comment together with its sequential number. */
|
|
11
|
+
export type NumberedComment = Comment & {
|
|
12
|
+
/** The comment's sequential integer number (1, 2, 3, …). */
|
|
13
|
+
number: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The comment's number, allocating the next one when it has none. Numbers are
|
|
17
|
+
* unique and increase.
|
|
18
|
+
*
|
|
19
|
+
* @param commentId - The comment's UUID.
|
|
20
|
+
* @returns Its sequential number.
|
|
21
|
+
* @throws {Error} When no number could be allocated after several attempts.
|
|
22
|
+
*/
|
|
23
|
+
export declare function numberComment(commentId: string): Promise<number>;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a comment and its number together: both are written, or neither.
|
|
26
|
+
*
|
|
27
|
+
* @param resourceType - The type of resource being commented on.
|
|
28
|
+
* @param resourceId - The ID of the resource being commented on.
|
|
29
|
+
* @param userId - The ID of the commenting user.
|
|
30
|
+
* @param data - The comment creation input.
|
|
31
|
+
* @returns The created comment with its number.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createNumberedComment(resourceType: string, resourceId: string, userId: string, data: CreateCommentInput): Promise<NumberedComment>;
|
|
34
|
+
/**
|
|
35
|
+
* The comment a number belongs to.
|
|
36
|
+
*
|
|
37
|
+
* @param number - The comment's sequential number.
|
|
38
|
+
* @returns The comment with its number, or `null` when no comment has it.
|
|
39
|
+
*/
|
|
40
|
+
export declare function getCommentByNumber(number: number): Promise<NumberedComment | null>;
|
|
41
|
+
/**
|
|
42
|
+
* The numbers of the given comments, for listing them with their numbers.
|
|
43
|
+
* Comments without a number are left out of the map.
|
|
44
|
+
*
|
|
45
|
+
* @param commentIds - The comments' UUIDs.
|
|
46
|
+
* @returns Each numbered comment's UUID mapped to its number.
|
|
47
|
+
*/
|
|
48
|
+
export declare function commentNumbers(commentIds: readonly string[]): Promise<Map<string, number>>;
|
|
49
|
+
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../src/numbers.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH,OAAO,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAI7D,qDAAqD;AACrD,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG;IACtC,4DAA4D;IAC5D,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAuDD;;;;;;;GAOG;AACH,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEtE;AAED;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CACzC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,eAAe,CAAC,CAK1B;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAQxF;AAED;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAMhG"}
|
package/dist/numbers.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequential integer numbers for comments, for APIs whose clients address a
|
|
3
|
+
* comment by an integer (RealWorld's `comment.id` is one; so are most public
|
|
4
|
+
* API specs). The comment keeps its UUID; the number lives in
|
|
5
|
+
* `comment_numbers` and is allocated once, at most one per comment.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { create as dbCreate, findMany, findOne, getActiveTransaction, withTransaction, } from '@molecule/api-database';
|
|
10
|
+
import { createComment, getCommentById } from './service.js';
|
|
11
|
+
const NUMBERS = 'comment_numbers';
|
|
12
|
+
/** How many times the work is redone when a concurrent writer took the number first. */
|
|
13
|
+
const MAX_ATTEMPTS = 5;
|
|
14
|
+
/**
|
|
15
|
+
* Runs `work` in its own transaction, again when it fails: a concurrent
|
|
16
|
+
* writer that took the same number fails the unique key, and the next
|
|
17
|
+
* attempt allocates the next number. Inside a caller's transaction a failed
|
|
18
|
+
* statement can poison it (Postgres), so there the work runs once and the
|
|
19
|
+
* caller decides.
|
|
20
|
+
*
|
|
21
|
+
* @param what - What is being numbered, for the error.
|
|
22
|
+
* @param work - The allocation.
|
|
23
|
+
* @returns What `work` resolves to.
|
|
24
|
+
*/
|
|
25
|
+
async function withNumberRetry(what, work) {
|
|
26
|
+
if (getActiveTransaction())
|
|
27
|
+
return work();
|
|
28
|
+
for (let attempt = 1;; attempt++) {
|
|
29
|
+
try {
|
|
30
|
+
return await withTransaction(work);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (attempt >= MAX_ATTEMPTS)
|
|
34
|
+
throw new Error(`Could not number ${what}`, { cause: error });
|
|
35
|
+
// Rolled back; a concurrent writer most likely took the number (its
|
|
36
|
+
// unique key failed). The next attempt reads the new highest number.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Allocates the next number for a comment that has none (no transaction of its own).
|
|
42
|
+
*
|
|
43
|
+
* @param commentId - The comment's UUID.
|
|
44
|
+
* @returns Its number.
|
|
45
|
+
*/
|
|
46
|
+
async function allocate(commentId) {
|
|
47
|
+
const existing = await findOne(NUMBERS, [
|
|
48
|
+
{ field: 'commentId', operator: '=', value: commentId },
|
|
49
|
+
]);
|
|
50
|
+
if (existing)
|
|
51
|
+
return Number(existing.number);
|
|
52
|
+
const [last] = await findMany(NUMBERS, {
|
|
53
|
+
orderBy: [{ field: 'number', direction: 'desc' }],
|
|
54
|
+
limit: 1,
|
|
55
|
+
});
|
|
56
|
+
const number = (last ? Number(last.number) : 0) + 1;
|
|
57
|
+
await dbCreate(NUMBERS, { number, commentId });
|
|
58
|
+
return number;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The comment's number, allocating the next one when it has none. Numbers are
|
|
62
|
+
* unique and increase.
|
|
63
|
+
*
|
|
64
|
+
* @param commentId - The comment's UUID.
|
|
65
|
+
* @returns Its sequential number.
|
|
66
|
+
* @throws {Error} When no number could be allocated after several attempts.
|
|
67
|
+
*/
|
|
68
|
+
export async function numberComment(commentId) {
|
|
69
|
+
return withNumberRetry(`comment ${commentId}`, () => allocate(commentId));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Creates a comment and its number together: both are written, or neither.
|
|
73
|
+
*
|
|
74
|
+
* @param resourceType - The type of resource being commented on.
|
|
75
|
+
* @param resourceId - The ID of the resource being commented on.
|
|
76
|
+
* @param userId - The ID of the commenting user.
|
|
77
|
+
* @param data - The comment creation input.
|
|
78
|
+
* @returns The created comment with its number.
|
|
79
|
+
*/
|
|
80
|
+
export async function createNumberedComment(resourceType, resourceId, userId, data) {
|
|
81
|
+
return withNumberRetry('a new comment', async () => {
|
|
82
|
+
const comment = await createComment(resourceType, resourceId, userId, data);
|
|
83
|
+
return { ...comment, number: await allocate(comment.id) };
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The comment a number belongs to.
|
|
88
|
+
*
|
|
89
|
+
* @param number - The comment's sequential number.
|
|
90
|
+
* @returns The comment with its number, or `null` when no comment has it.
|
|
91
|
+
*/
|
|
92
|
+
export async function getCommentByNumber(number) {
|
|
93
|
+
if (!Number.isInteger(number) || number < 1)
|
|
94
|
+
return null;
|
|
95
|
+
const row = await findOne(NUMBERS, [
|
|
96
|
+
{ field: 'number', operator: '=', value: number },
|
|
97
|
+
]);
|
|
98
|
+
if (!row)
|
|
99
|
+
return null;
|
|
100
|
+
const comment = await getCommentById(row.commentId);
|
|
101
|
+
return comment ? { ...comment, number: Number(row.number) } : null;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The numbers of the given comments, for listing them with their numbers.
|
|
105
|
+
* Comments without a number are left out of the map.
|
|
106
|
+
*
|
|
107
|
+
* @param commentIds - The comments' UUIDs.
|
|
108
|
+
* @returns Each numbered comment's UUID mapped to its number.
|
|
109
|
+
*/
|
|
110
|
+
export async function commentNumbers(commentIds) {
|
|
111
|
+
if (commentIds.length === 0)
|
|
112
|
+
return new Map();
|
|
113
|
+
const rows = await findMany(NUMBERS, {
|
|
114
|
+
where: [{ field: 'commentId', operator: 'in', value: [...commentIds] }],
|
|
115
|
+
});
|
|
116
|
+
return new Map(rows.map((r) => [r.commentId, Number(r.number)]));
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-resource-comment",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Threaded comments on any resource with polymorphic attachment and pagination",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -29,13 +29,15 @@
|
|
|
29
29
|
],
|
|
30
30
|
"license": "Apache-2.0",
|
|
31
31
|
"devDependencies": {
|
|
32
|
+
"@molecule/api-database": "1.1.1",
|
|
33
|
+
"@molecule/api-database-sqlite": "1.2.1",
|
|
32
34
|
"@types/node": "26.1.2",
|
|
33
35
|
"typescript": "6.0.3",
|
|
34
36
|
"vitest": "4.1.11",
|
|
35
37
|
"zod": "4.4.3"
|
|
36
38
|
},
|
|
37
39
|
"peerDependencies": {
|
|
38
|
-
"@molecule/api-database": "^1.0
|
|
40
|
+
"@molecule/api-database": "^1.1.0",
|
|
39
41
|
"@molecule/api-i18n": "^1.0.1",
|
|
40
42
|
"@molecule/api-logger": "^1.0.1",
|
|
41
43
|
"@molecule/api-resource": "^1.0.1",
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
-- Sequential integer numbers for comments, for APIs whose clients address a
|
|
2
|
+
-- comment by an integer id (RealWorld's `comment.id` is an integer). Opt-in:
|
|
3
|
+
-- written by numberComment() / createNumberedComment(); comments without a
|
|
4
|
+
-- number keep working by their UUID.
|
|
5
|
+
CREATE TABLE IF NOT EXISTS "comment_numbers" (
|
|
6
|
+
"number" INTEGER PRIMARY KEY,
|
|
7
|
+
"commentId" UUID NOT NULL UNIQUE REFERENCES "comments"("id") ON DELETE CASCADE
|
|
8
|
+
);
|