@molecule/api-resource-comment 1.0.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.
Files changed (50) hide show
  1. package/LICENSE +115 -0
  2. package/dist/authorizers/index.d.ts +10 -0
  3. package/dist/authorizers/index.d.ts.map +1 -0
  4. package/dist/authorizers/index.js +9 -0
  5. package/dist/browser-guard.d.ts +2 -0
  6. package/dist/browser-guard.d.ts.map +1 -0
  7. package/dist/browser-guard.js +18 -0
  8. package/dist/handlers/count.d.ts +14 -0
  9. package/dist/handlers/count.d.ts.map +1 -0
  10. package/dist/handlers/count.js +39 -0
  11. package/dist/handlers/create.d.ts +14 -0
  12. package/dist/handlers/create.d.ts.map +1 -0
  13. package/dist/handlers/create.js +54 -0
  14. package/dist/handlers/del.d.ts +14 -0
  15. package/dist/handlers/del.d.ts.map +1 -0
  16. package/dist/handlers/del.js +52 -0
  17. package/dist/handlers/index.d.ts +8 -0
  18. package/dist/handlers/index.d.ts.map +1 -0
  19. package/dist/handlers/index.js +7 -0
  20. package/dist/handlers/list.d.ts +14 -0
  21. package/dist/handlers/list.d.ts.map +1 -0
  22. package/dist/handlers/list.js +41 -0
  23. package/dist/handlers/read.d.ts +14 -0
  24. package/dist/handlers/read.d.ts.map +1 -0
  25. package/dist/handlers/read.js +42 -0
  26. package/dist/handlers/replies.d.ts +14 -0
  27. package/dist/handlers/replies.d.ts.map +1 -0
  28. package/dist/handlers/replies.js +39 -0
  29. package/dist/handlers/update.d.ts +14 -0
  30. package/dist/handlers/update.d.ts.map +1 -0
  31. package/dist/handlers/update.js +59 -0
  32. package/dist/index.d.ts +68 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +67 -0
  35. package/dist/requestHandlerMap.d.ts +25 -0
  36. package/dist/requestHandlerMap.d.ts.map +1 -0
  37. package/dist/requestHandlerMap.js +24 -0
  38. package/dist/routes.d.ts +43 -0
  39. package/dist/routes.d.ts.map +1 -0
  40. package/dist/routes.js +24 -0
  41. package/dist/service.d.ts +70 -0
  42. package/dist/service.d.ts.map +1 -0
  43. package/dist/service.js +135 -0
  44. package/dist/types.d.ts +70 -0
  45. package/dist/types.d.ts.map +1 -0
  46. package/dist/types.js +9 -0
  47. package/dist/validation.d.ts +20 -0
  48. package/dist/validation.d.ts.map +1 -0
  49. package/dist/validation.js +22 -0
  50. package/package.json +66 -0
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Threaded comments resource for molecule.dev.
3
+ *
4
+ * Polymorphic comments that attach to any resource type. Supports threaded
5
+ * replies, pagination, and ownership-based authorization.
6
+ *
7
+ * @module
8
+ * @example
9
+ * ```typescript
10
+ * import { routes, requestHandlerMap } from '@molecule/api-resource-comment'
11
+ *
12
+ * // Wire routes into your Express app via mlcl inject
13
+ * // POST /:resourceType/:resourceId/comments
14
+ * // GET /:resourceType/:resourceId/comments
15
+ * // GET /comments/:commentId
16
+ * // PUT /comments/:commentId
17
+ * // DELETE /comments/:commentId
18
+ * // GET /comments/:commentId/replies
19
+ * // GET /:resourceType/:resourceId/comments/count — cheap badge counts
20
+ * ```
21
+ *
22
+ * @remarks
23
+ * - **List endpoints return a PAGINATED envelope** `{ data, total, limit, offset }`, not a
24
+ * bare array — read the rows off `result.data` (server). On the client, `unwrapList(res)`
25
+ * from `@molecule/app-http` normalizes this envelope (pass it the whole HttpResponse), so
26
+ * the rows come back; reading the response as a bare array — or `res.data` alone (which is
27
+ * the envelope) — yields an EMPTY list.
28
+ * - **Migration required.** `src/__setup__/comments.sql` ships with this package
29
+ * and must exist in the target database before use (scaffolded apps apply it
30
+ * automatically; existing apps must apply it first).
31
+ * - **Reads are PUBLIC by default.** `list`, `read`, `replies`, and `count` ship
32
+ * with no auth middleware (comment threads on public content). If the
33
+ * commented resources are private in your app, add an authorizer that checks
34
+ * access to the PARENT resource before serving its comments.
35
+ * - **The author is always the session user.** Create/update/delete require
36
+ * `authenticate`; the create handler ignores any client-supplied author id,
37
+ * and update/delete verify ownership in the handler — keep those properties
38
+ * in any custom path.
39
+ * - **The parent is polymorphic and unverified** (no FK on
40
+ * `resourceType`/`resourceId`): validate that the target exists in your domain
41
+ * code if orphaned threads matter, and reuse the same canonical type slugs as
42
+ * your other polymorphic resources (bookmarks, activity feed).
43
+ *
44
+ * @e2e
45
+ * Integration checklist — drive the real UI (live preview, no mocks), adapt
46
+ * each item to this app's actual screens/flows, and check every box off one
47
+ * by one. A box you can't check is an integration bug to fix — not a skip:
48
+ * - [ ] Posting a comment on a commentable resource shows it in the thread
49
+ * immediately and it persists across a full reload.
50
+ * - [ ] Replying to a comment renders the reply nested under its parent.
51
+ * - [ ] The author can edit their own comment and the updated text persists;
52
+ * a DIFFERENT signed-in user gets no edit/delete controls on it and a
53
+ * direct attempt is denied.
54
+ * - [ ] Deleting an own comment removes it per the app's policy (gone or
55
+ * tombstone) and stays removed after reload.
56
+ * - [ ] A long thread paginates ("load more" fetches older comments) without
57
+ * duplicating or dropping entries.
58
+ * - [ ] A resource with no comments shows a readable empty state.
59
+ */
60
+ export * from './authorizers/index.js';
61
+ export * from './browser-guard.js';
62
+ export * from './handlers/index.js';
63
+ export * from './requestHandlerMap.js';
64
+ export * from './routes.js';
65
+ export * from './service.js';
66
+ export * from './types.js';
67
+ export * from './validation.js';
68
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,cAAc,wBAAwB,CAAA;AACtC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,wBAAwB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Threaded comments resource for molecule.dev.
3
+ *
4
+ * Polymorphic comments that attach to any resource type. Supports threaded
5
+ * replies, pagination, and ownership-based authorization.
6
+ *
7
+ * @module
8
+ * @example
9
+ * ```typescript
10
+ * import { routes, requestHandlerMap } from '@molecule/api-resource-comment'
11
+ *
12
+ * // Wire routes into your Express app via mlcl inject
13
+ * // POST /:resourceType/:resourceId/comments
14
+ * // GET /:resourceType/:resourceId/comments
15
+ * // GET /comments/:commentId
16
+ * // PUT /comments/:commentId
17
+ * // DELETE /comments/:commentId
18
+ * // GET /comments/:commentId/replies
19
+ * // GET /:resourceType/:resourceId/comments/count — cheap badge counts
20
+ * ```
21
+ *
22
+ * @remarks
23
+ * - **List endpoints return a PAGINATED envelope** `{ data, total, limit, offset }`, not a
24
+ * bare array — read the rows off `result.data` (server). On the client, `unwrapList(res)`
25
+ * from `@molecule/app-http` normalizes this envelope (pass it the whole HttpResponse), so
26
+ * the rows come back; reading the response as a bare array — or `res.data` alone (which is
27
+ * the envelope) — yields an EMPTY list.
28
+ * - **Migration required.** `src/__setup__/comments.sql` ships with this package
29
+ * and must exist in the target database before use (scaffolded apps apply it
30
+ * automatically; existing apps must apply it first).
31
+ * - **Reads are PUBLIC by default.** `list`, `read`, `replies`, and `count` ship
32
+ * with no auth middleware (comment threads on public content). If the
33
+ * commented resources are private in your app, add an authorizer that checks
34
+ * access to the PARENT resource before serving its comments.
35
+ * - **The author is always the session user.** Create/update/delete require
36
+ * `authenticate`; the create handler ignores any client-supplied author id,
37
+ * and update/delete verify ownership in the handler — keep those properties
38
+ * in any custom path.
39
+ * - **The parent is polymorphic and unverified** (no FK on
40
+ * `resourceType`/`resourceId`): validate that the target exists in your domain
41
+ * code if orphaned threads matter, and reuse the same canonical type slugs as
42
+ * your other polymorphic resources (bookmarks, activity feed).
43
+ *
44
+ * @e2e
45
+ * Integration checklist — drive the real UI (live preview, no mocks), adapt
46
+ * each item to this app's actual screens/flows, and check every box off one
47
+ * by one. A box you can't check is an integration bug to fix — not a skip:
48
+ * - [ ] Posting a comment on a commentable resource shows it in the thread
49
+ * immediately and it persists across a full reload.
50
+ * - [ ] Replying to a comment renders the reply nested under its parent.
51
+ * - [ ] The author can edit their own comment and the updated text persists;
52
+ * a DIFFERENT signed-in user gets no edit/delete controls on it and a
53
+ * direct attempt is denied.
54
+ * - [ ] Deleting an own comment removes it per the app's policy (gone or
55
+ * tombstone) and stays removed after reload.
56
+ * - [ ] A long thread paginates ("load more" fetches older comments) without
57
+ * duplicating or dropping entries.
58
+ * - [ ] A resource with no comments shows a readable empty state.
59
+ */
60
+ export * from './authorizers/index.js';
61
+ export * from './browser-guard.js';
62
+ export * from './handlers/index.js';
63
+ export * from './requestHandlerMap.js';
64
+ export * from './routes.js';
65
+ export * from './service.js';
66
+ export * from './types.js';
67
+ export * from './validation.js';
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Maps route handler names to their implementations.
3
+ *
4
+ * @module
5
+ */
6
+ import { commentCount } from './handlers/count.js';
7
+ import { create } from './handlers/create.js';
8
+ import { del } from './handlers/del.js';
9
+ import { list } from './handlers/list.js';
10
+ import { read } from './handlers/read.js';
11
+ import { replies } from './handlers/replies.js';
12
+ import { update } from './handlers/update.js';
13
+ /**
14
+ * Handler map for comment routes.
15
+ */
16
+ export declare const requestHandlerMap: {
17
+ readonly create: typeof create;
18
+ readonly list: typeof list;
19
+ readonly commentCount: typeof commentCount;
20
+ readonly read: typeof read;
21
+ readonly update: typeof update;
22
+ readonly del: typeof del;
23
+ readonly replies: typeof replies;
24
+ };
25
+ //# sourceMappingURL=requestHandlerMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"requestHandlerMap.d.ts","sourceRoot":"","sources":["../src/requestHandlerMap.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C;;GAEG;AACH,eAAO,MAAM,iBAAiB;;;;;;;;CAQpB,CAAA"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Maps route handler names to their implementations.
3
+ *
4
+ * @module
5
+ */
6
+ import { commentCount } from './handlers/count.js';
7
+ import { create } from './handlers/create.js';
8
+ import { del } from './handlers/del.js';
9
+ import { list } from './handlers/list.js';
10
+ import { read } from './handlers/read.js';
11
+ import { replies } from './handlers/replies.js';
12
+ import { update } from './handlers/update.js';
13
+ /**
14
+ * Handler map for comment routes.
15
+ */
16
+ export const requestHandlerMap = {
17
+ create,
18
+ list,
19
+ commentCount,
20
+ read,
21
+ update,
22
+ del,
23
+ replies,
24
+ };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Comment route definitions.
3
+ *
4
+ * Declarative routing — mlcl inject reads these to generate the Express router.
5
+ *
6
+ * @module
7
+ */
8
+ /**
9
+ * Routes for comment CRUD and threaded replies.
10
+ */
11
+ export declare const routes: readonly [{
12
+ readonly method: "post";
13
+ readonly path: "/:resourceType/:resourceId/comments";
14
+ readonly handler: "create";
15
+ readonly middlewares: readonly ["authenticate"];
16
+ }, {
17
+ readonly method: "get";
18
+ readonly path: "/:resourceType/:resourceId/comments";
19
+ readonly handler: "list";
20
+ }, {
21
+ readonly method: "get";
22
+ readonly path: "/:resourceType/:resourceId/comments/count";
23
+ readonly handler: "commentCount";
24
+ }, {
25
+ readonly method: "get";
26
+ readonly path: "/comments/:commentId";
27
+ readonly handler: "read";
28
+ }, {
29
+ readonly method: "put";
30
+ readonly path: "/comments/:commentId";
31
+ readonly handler: "update";
32
+ readonly middlewares: readonly ["authenticate"];
33
+ }, {
34
+ readonly method: "delete";
35
+ readonly path: "/comments/:commentId";
36
+ readonly handler: "del";
37
+ readonly middlewares: readonly ["authenticate"];
38
+ }, {
39
+ readonly method: "get";
40
+ readonly path: "/comments/:commentId/replies";
41
+ readonly handler: "replies";
42
+ }];
43
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAaT,CAAA"}
package/dist/routes.js ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Comment route definitions.
3
+ *
4
+ * Declarative routing — mlcl inject reads these to generate the Express router.
5
+ *
6
+ * @module
7
+ */
8
+ /**
9
+ * Routes for comment CRUD and threaded replies.
10
+ */
11
+ export const routes = [
12
+ {
13
+ method: 'post',
14
+ path: '/:resourceType/:resourceId/comments',
15
+ handler: 'create',
16
+ middlewares: ['authenticate'],
17
+ },
18
+ { method: 'get', path: '/:resourceType/:resourceId/comments', handler: 'list' },
19
+ { method: 'get', path: '/:resourceType/:resourceId/comments/count', handler: 'commentCount' },
20
+ { method: 'get', path: '/comments/:commentId', handler: 'read' },
21
+ { method: 'put', path: '/comments/:commentId', handler: 'update', middlewares: ['authenticate'] },
22
+ { method: 'delete', path: '/comments/:commentId', handler: 'del', middlewares: ['authenticate'] },
23
+ { method: 'get', path: '/comments/:commentId/replies', handler: 'replies' },
24
+ ];
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Comment business logic service.
3
+ *
4
+ * Uses the abstract DataStore from `@molecule/api-database` for all
5
+ * persistence — never raw SQL.
6
+ *
7
+ * @module
8
+ */
9
+ import type { Comment, CreateCommentInput, PaginatedResult, PaginationOptions, UpdateCommentInput } from './types.js';
10
+ /**
11
+ * Creates a new comment on a resource.
12
+ *
13
+ * @param resourceType - The type of resource being commented on.
14
+ * @param resourceId - The ID of the resource being commented on.
15
+ * @param userId - The ID of the commenting user.
16
+ * @param data - The comment creation input.
17
+ * @returns The created comment.
18
+ */
19
+ export declare function createComment(resourceType: string, resourceId: string, userId: string, data: CreateCommentInput): Promise<Comment>;
20
+ /**
21
+ * Retrieves paginated comments for a resource, ordered by creation date descending.
22
+ * Only returns top-level comments (no replies).
23
+ *
24
+ * @param resourceType - The type of resource.
25
+ * @param resourceId - The ID of the resource.
26
+ * @param options - Pagination options.
27
+ * @returns A paginated result of comments.
28
+ */
29
+ export declare function getCommentsByResource(resourceType: string, resourceId: string, options?: PaginationOptions): Promise<PaginatedResult<Comment>>;
30
+ /**
31
+ * Retrieves a single comment by ID.
32
+ *
33
+ * @param commentId - The comment ID to look up.
34
+ * @returns The comment or `null` if not found.
35
+ */
36
+ export declare function getCommentById(commentId: string): Promise<Comment | null>;
37
+ /**
38
+ * Updates a comment. Only the comment owner can update.
39
+ *
40
+ * @param commentId - The comment ID to update.
41
+ * @param userId - The requesting user's ID (must match comment owner).
42
+ * @param data - The update input.
43
+ * @returns The updated comment or `null` if not found or unauthorized.
44
+ */
45
+ export declare function updateComment(commentId: string, userId: string, data: UpdateCommentInput): Promise<Comment | null>;
46
+ /**
47
+ * Deletes a comment. Only the comment owner can delete.
48
+ *
49
+ * @param commentId - The comment ID to delete.
50
+ * @param userId - The requesting user's ID (must match comment owner).
51
+ * @returns `true` if deleted, `false` if not found or unauthorized.
52
+ */
53
+ export declare function deleteComment(commentId: string, userId: string): Promise<boolean>;
54
+ /**
55
+ * Retrieves paginated replies to a comment, ordered by creation date ascending.
56
+ *
57
+ * @param commentId - The parent comment ID.
58
+ * @param options - Pagination options.
59
+ * @returns A paginated result of reply comments.
60
+ */
61
+ export declare function getReplies(commentId: string, options?: PaginationOptions): Promise<PaginatedResult<Comment>>;
62
+ /**
63
+ * Returns the total number of comments on a resource (including replies).
64
+ *
65
+ * @param resourceType - The type of resource.
66
+ * @param resourceId - The ID of the resource.
67
+ * @returns The total comment count.
68
+ */
69
+ export declare function getCommentCount(resourceType: string, resourceId: string): Promise<number>;
70
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAWH,OAAO,KAAK,EACV,OAAO,EACP,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,YAAY,CAAA;AAInB;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CACjC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,OAAO,CAAC,CAUlB;AAED;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CACzC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAqBnC;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAE/E;AAED;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAUzB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAMvF;AAED;;;;;;GAMG;AACH,wBAAsB,UAAU,CAC9B,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAiBnC;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK/F"}
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Comment business logic service.
3
+ *
4
+ * Uses the abstract DataStore from `@molecule/api-database` for all
5
+ * persistence — never raw SQL.
6
+ *
7
+ * @module
8
+ */
9
+ import { count, create as dbCreate, deleteById, findById, findMany, updateById, } from '@molecule/api-database';
10
+ const TABLE = 'comments';
11
+ /**
12
+ * Creates a new comment on a resource.
13
+ *
14
+ * @param resourceType - The type of resource being commented on.
15
+ * @param resourceId - The ID of the resource being commented on.
16
+ * @param userId - The ID of the commenting user.
17
+ * @param data - The comment creation input.
18
+ * @returns The created comment.
19
+ */
20
+ export async function createComment(resourceType, resourceId, userId, data) {
21
+ const result = await dbCreate(TABLE, {
22
+ resourceType,
23
+ resourceId,
24
+ userId,
25
+ parentId: data.parentId ?? null,
26
+ body: data.body,
27
+ editedAt: null,
28
+ });
29
+ return result.data;
30
+ }
31
+ /**
32
+ * Retrieves paginated comments for a resource, ordered by creation date descending.
33
+ * Only returns top-level comments (no replies).
34
+ *
35
+ * @param resourceType - The type of resource.
36
+ * @param resourceId - The ID of the resource.
37
+ * @param options - Pagination options.
38
+ * @returns A paginated result of comments.
39
+ */
40
+ export async function getCommentsByResource(resourceType, resourceId, options = {}) {
41
+ const limit = options.limit ?? 20;
42
+ const offset = options.offset ?? 0;
43
+ const where = [
44
+ { field: 'resourceType', operator: '=', value: resourceType },
45
+ { field: 'resourceId', operator: '=', value: resourceId },
46
+ { field: 'parentId', operator: 'is_null' },
47
+ ];
48
+ const [data, total] = await Promise.all([
49
+ findMany(TABLE, {
50
+ where,
51
+ orderBy: [{ field: 'createdAt', direction: 'desc' }],
52
+ limit,
53
+ offset,
54
+ }),
55
+ count(TABLE, where),
56
+ ]);
57
+ return { data, total, limit, offset };
58
+ }
59
+ /**
60
+ * Retrieves a single comment by ID.
61
+ *
62
+ * @param commentId - The comment ID to look up.
63
+ * @returns The comment or `null` if not found.
64
+ */
65
+ export async function getCommentById(commentId) {
66
+ return findById(TABLE, commentId);
67
+ }
68
+ /**
69
+ * Updates a comment. Only the comment owner can update.
70
+ *
71
+ * @param commentId - The comment ID to update.
72
+ * @param userId - The requesting user's ID (must match comment owner).
73
+ * @param data - The update input.
74
+ * @returns The updated comment or `null` if not found or unauthorized.
75
+ */
76
+ export async function updateComment(commentId, userId, data) {
77
+ const existing = await findById(TABLE, commentId);
78
+ if (!existing || existing.userId !== userId)
79
+ return null;
80
+ const result = await updateById(TABLE, commentId, {
81
+ body: data.body,
82
+ editedAt: new Date().toISOString(),
83
+ updatedAt: new Date().toISOString(),
84
+ });
85
+ return result.data;
86
+ }
87
+ /**
88
+ * Deletes a comment. Only the comment owner can delete.
89
+ *
90
+ * @param commentId - The comment ID to delete.
91
+ * @param userId - The requesting user's ID (must match comment owner).
92
+ * @returns `true` if deleted, `false` if not found or unauthorized.
93
+ */
94
+ export async function deleteComment(commentId, userId) {
95
+ const existing = await findById(TABLE, commentId);
96
+ if (!existing || existing.userId !== userId)
97
+ return false;
98
+ await deleteById(TABLE, commentId);
99
+ return true;
100
+ }
101
+ /**
102
+ * Retrieves paginated replies to a comment, ordered by creation date ascending.
103
+ *
104
+ * @param commentId - The parent comment ID.
105
+ * @param options - Pagination options.
106
+ * @returns A paginated result of reply comments.
107
+ */
108
+ export async function getReplies(commentId, options = {}) {
109
+ const limit = options.limit ?? 20;
110
+ const offset = options.offset ?? 0;
111
+ const where = [{ field: 'parentId', operator: '=', value: commentId }];
112
+ const [data, total] = await Promise.all([
113
+ findMany(TABLE, {
114
+ where,
115
+ orderBy: [{ field: 'createdAt', direction: 'asc' }],
116
+ limit,
117
+ offset,
118
+ }),
119
+ count(TABLE, where),
120
+ ]);
121
+ return { data, total, limit, offset };
122
+ }
123
+ /**
124
+ * Returns the total number of comments on a resource (including replies).
125
+ *
126
+ * @param resourceType - The type of resource.
127
+ * @param resourceId - The ID of the resource.
128
+ * @returns The total comment count.
129
+ */
130
+ export async function getCommentCount(resourceType, resourceId) {
131
+ return count(TABLE, [
132
+ { field: 'resourceType', operator: '=', value: resourceType },
133
+ { field: 'resourceId', operator: '=', value: resourceId },
134
+ ]);
135
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Comment resource type definitions.
3
+ *
4
+ * Polymorphic comments that can attach to any resource type via
5
+ * `resourceType` and `resourceId`. Supports threaded replies via `parentId`.
6
+ *
7
+ * @module
8
+ */
9
+ /**
10
+ * A comment attached to a resource, with optional threading via `parentId`.
11
+ */
12
+ export interface Comment {
13
+ /** Unique comment identifier. */
14
+ id: string;
15
+ /** The type of resource this comment is attached to (e.g. 'project', 'post'). */
16
+ resourceType: string;
17
+ /** The ID of the resource this comment is attached to. */
18
+ resourceId: string;
19
+ /** The ID of the user who created this comment. */
20
+ userId: string;
21
+ /** The parent comment ID for threaded replies, or `null` for top-level comments. */
22
+ parentId: string | null;
23
+ /** The comment body text. */
24
+ body: string;
25
+ /** When the comment was last edited, or `null` if never edited. */
26
+ editedAt: string | null;
27
+ /** When the comment was created (ISO 8601). */
28
+ createdAt: string;
29
+ /** When the comment was last updated (ISO 8601). */
30
+ updatedAt: string;
31
+ }
32
+ /**
33
+ * Input for creating a new comment.
34
+ */
35
+ export interface CreateCommentInput {
36
+ /** The comment body text. */
37
+ body: string;
38
+ /** Optional parent comment ID for threaded replies. */
39
+ parentId?: string;
40
+ }
41
+ /**
42
+ * Input for updating an existing comment.
43
+ */
44
+ export interface UpdateCommentInput {
45
+ /** The updated comment body text. */
46
+ body: string;
47
+ }
48
+ /**
49
+ * Options for paginated queries.
50
+ */
51
+ export interface PaginationOptions {
52
+ /** Maximum number of results to return. */
53
+ limit?: number;
54
+ /** Number of results to skip. */
55
+ offset?: number;
56
+ }
57
+ /**
58
+ * A paginated result set.
59
+ */
60
+ export interface PaginatedResult<T> {
61
+ /** The result items for the current page. */
62
+ data: T[];
63
+ /** Total number of matching items across all pages. */
64
+ total: number;
65
+ /** Maximum number of results per page. */
66
+ limit: number;
67
+ /** Number of results skipped. */
68
+ offset: number;
69
+ }
70
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,iCAAiC;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,iFAAiF;IACjF,YAAY,EAAE,MAAM,CAAA;IACpB,0DAA0D;IAC1D,UAAU,EAAE,MAAM,CAAA;IAClB,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAA;IACd,oFAAoF;IACpF,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,mEAAmE;IACnE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAA;IACjB,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,6CAA6C;IAC7C,IAAI,EAAE,CAAC,EAAE,CAAA;IACT,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAA;IACb,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAA;IACb,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAA;CACf"}
package/dist/types.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Comment resource type definitions.
3
+ *
4
+ * Polymorphic comments that can attach to any resource type via
5
+ * `resourceType` and `resourceId`. Supports threaded replies via `parentId`.
6
+ *
7
+ * @module
8
+ */
9
+ export {};
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Comment input validation schemas.
3
+ *
4
+ * @module
5
+ */
6
+ import { z } from 'zod';
7
+ /**
8
+ * Schema for validating comment creation input.
9
+ */
10
+ export declare const createCommentSchema: z.ZodObject<{
11
+ body: z.ZodString;
12
+ parentId: z.ZodOptional<z.ZodString>;
13
+ }, z.core.$strip>;
14
+ /**
15
+ * Schema for validating comment update input.
16
+ */
17
+ export declare const updateCommentSchema: z.ZodObject<{
18
+ body: z.ZodString;
19
+ }, z.core.$strip>;
20
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB;;GAEG;AACH,eAAO,MAAM,mBAAmB;;;iBAK9B,CAAA;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB;;iBAG9B,CAAA"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Comment input validation schemas.
3
+ *
4
+ * @module
5
+ */
6
+ import { z } from 'zod';
7
+ /**
8
+ * Schema for validating comment creation input.
9
+ */
10
+ export const createCommentSchema = z.object({
11
+ /** The comment body text. Must be non-empty and at most 10,000 characters. */
12
+ body: z.string().min(1).max(10_000),
13
+ /** Optional parent comment ID for threaded replies. */
14
+ parentId: z.string().uuid().optional(),
15
+ });
16
+ /**
17
+ * Schema for validating comment update input.
18
+ */
19
+ export const updateCommentSchema = z.object({
20
+ /** The updated comment body text. Must be non-empty and at most 10,000 characters. */
21
+ body: z.string().min(1).max(10_000),
22
+ });