@ordinatio/entities 1.4.0 → 1.5.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.
@@ -0,0 +1,123 @@
1
+ /** The thing a note is about. `kind` is opaque to this package. */
2
+ interface NoteParent {
3
+ kind: string;
4
+ id: string;
5
+ }
6
+ /** The fields the rules need. An adapter may return a wider row: the store is generic over it. */
7
+ interface NoteRecord {
8
+ id: string;
9
+ parentKind: string;
10
+ parentId: string;
11
+ /** The cleaned body, stored exactly as given. */
12
+ body: string;
13
+ /** Starts at 1; +1 on every successful edit. Deleting and restoring do NOT change it. */
14
+ revision: number;
15
+ /** Who wrote it: opaque to this package (a user id, a name ...). */
16
+ authorId: string;
17
+ /** Stored and returned at MILLISECOND precision (what a JavaScript Date holds): list order and cursors compare it with `id`, so a store that keeps more precision must round it. */
18
+ createdAt: Date;
19
+ /** Set to the edit time on an edit only (not on delete or restore); within the same millisecond as the create it can equal `createdAt`. */
20
+ updatedAt: Date;
21
+ /** Null while the note is live. */
22
+ deletedAt: Date | null;
23
+ deletedBy: string | null;
24
+ }
25
+ type NoteStatus = 'live' | 'deleted';
26
+ /** `parentGone`: the parent does not exist in this workspace (unknown, another workspace's, or deleted a moment ago); nothing is stored. */
27
+ type NoteCreateResult<T extends NoteRecord> = {
28
+ kind: 'created';
29
+ note: T;
30
+ } | {
31
+ kind: 'parentGone';
32
+ };
33
+ /** Precedence: `notFound`, then `deleted` (a deleted note cannot be edited: restore it first), then `conflict`. `conflict` carries the CURRENT note. */
34
+ type NoteUpdateResult<T extends NoteRecord> = {
35
+ kind: 'updated';
36
+ before: T;
37
+ after: T;
38
+ } | {
39
+ kind: 'notFound';
40
+ } | {
41
+ kind: 'deleted';
42
+ } | {
43
+ kind: 'conflict';
44
+ current: T;
45
+ };
46
+ type NoteDeleteResult<T extends NoteRecord> = {
47
+ kind: 'deleted';
48
+ before: T;
49
+ after: T;
50
+ } | {
51
+ kind: 'notFound';
52
+ } | {
53
+ kind: 'alreadyDeleted';
54
+ };
55
+ type NoteRestoreResult<T extends NoteRecord> = {
56
+ kind: 'restored';
57
+ before: T;
58
+ after: T;
59
+ } | {
60
+ kind: 'notFound';
61
+ } | {
62
+ kind: 'notDeleted';
63
+ };
64
+ interface NoteListQuery {
65
+ parent: NoteParent;
66
+ status: NoteStatus;
67
+ /** At least 1 (the service clamps it). */
68
+ limit: number;
69
+ /** An opaque cursor from an earlier page of the SAME query, or null for the first page. */
70
+ cursor: string | null;
71
+ }
72
+ /**
73
+ * `badCursor`: the cursor is not one this store produced (a store never throws for it). A cursor is only a POSITION in the newest-first order (a time and an id): it is not bound to a
74
+ * parent, a status or a workspace, so a valid cursor used with another query simply starts after that position in the new listing; it never exposes another workspace's rows.
75
+ */
76
+ type NoteListResult<T extends NoteRecord> = {
77
+ kind: 'page';
78
+ notes: T[];
79
+ nextCursor: string | null;
80
+ } | {
81
+ kind: 'badCursor';
82
+ };
83
+ /**
84
+ * WHAT THE SERVICE GUARANTEES A STORE (so an adapter never needs to defend against these, and a contract test may rely on them): every id, kind, `authorId` and `actorId` it receives is
85
+ * a text of 1 to 255 characters without a NUL character or a lone UTF-16 surrogate; `body` is already cleaned, non-empty, within the length limit and without NUL; `expectedRevision` is a whole number from 1 to
86
+ * 2,147,483,647; `limit` is a whole number from 1 to the maximum page size; `status` is `live` or `deleted`; `cursor` is null or a string of at most 512 characters without NUL or a lone surrogate (it may still
87
+ * be one the store did not produce: answer `badCursor`, also for a well-formed cursor whose parts are out of range, such as a time no Date can hold, or one outside the store's own column range); `countByParent` gets a NON-EMPTY list
88
+ * of distinct valid ids (the service answers an empty list itself). It does NOT limit how many ids `countByParent` gets: the caller bounds that. A store used WITHOUT the service must
89
+ * apply the same rules.
90
+ */
91
+ interface NoteStore<T extends NoteRecord = NoteRecord> {
92
+ /**
93
+ * `body` is already cleaned (the service sends nothing else; an adapter may rely on it) and is stored EXACTLY. ATOMIC with the parent's existence: the parent must be share-locked
94
+ * while the note is inserted, so that deleting the parent waits or fails cleanly instead of leaving an orphan. `revision` is 1, `deletedAt` and `deletedBy` are null.
95
+ */
96
+ create(parent: NoteParent, body: string, authorId: string): Promise<NoteCreateResult<T>>;
97
+ /** The note, DELETED ONES INCLUDED; an id of another workspace or an unknown id is null (an adapter with uuid or integer id columns must answer such an id as null, not fail the cast). */
98
+ get(id: string): Promise<T | null>;
99
+ /**
100
+ * COMPARE-AND-SET on `revision`: the write happens only if the stored revision equals `expectedRevision`; then `revision` goes up by one and `updatedAt` moves. `before` is read in the
101
+ * same transaction as the write. Ten simultaneous edits from the same revision give exactly one `updated` and nine `conflict`. The precedence holds UNDER CONCURRENCY too: a note soft-deleted between a
102
+ * caller's read and its write is `deleted`, never `conflict` (do the read and the write under one row lock, or in one statement that distinguishes the cases).
103
+ */
104
+ update(id: string, body: string, expectedRevision: number): Promise<NoteUpdateResult<T>>;
105
+ /** Sets `deletedAt` and `deletedBy`; revision and `updatedAt` are untouched. Ten simultaneous deletes of one note give exactly one `deleted`. */
106
+ softDelete(id: string, actorId: string): Promise<NoteDeleteResult<T>>;
107
+ /** Clears `deletedAt` and `deletedBy`; the note returns to its original place by `createdAt`. */
108
+ restore(id: string): Promise<NoteRestoreResult<T>>;
109
+ /**
110
+ * One page of ONE parent's notes with the given status, NEWEST FIRST by `createdAt` (ties by `id`, descending, using ONE definition of the order: the SAME key `(createdAt, id)` and the same comparison for
111
+ * the ordering and for the cursor, so rows in the same millisecond are never repeated or skipped at a page boundary; a database whose collation orders ids differently from the cursor's comparison must compare in the database). Keyset pagination: a note added or
112
+ * deleted between two page fetches never makes an older note repeat or disappear. The page has at most `limit` notes and `nextCursor` is non-null only when more follow.
113
+ * A parent that does not exist (or is another workspace's) simply has no notes.
114
+ */
115
+ list(query: NoteListQuery): Promise<NoteListResult<T>>;
116
+ /**
117
+ * The number of notes with `status` per parent, for many parents of one kind in ONE round trip. EVERY asked id appears (0 when it has none or is not a parent of this
118
+ * workspace; the service fills in a missing id with 0, but a store must still do it); an EMPTY list asks for nothing and returns an empty map; a repeated id is one entry. Must work at tens of thousands of ids (in Postgres use `= ANY($1)`).
119
+ */
120
+ countByParent(kind: string, parentIds: string[], status: NoteStatus): Promise<Map<string, number>>;
121
+ }
122
+
123
+ export type { NoteStore as N, NoteRecord as a, NoteStatus as b, NoteParent as c, NoteCreateResult as d, NoteDeleteResult as e, NoteListQuery as f, NoteListResult as g, NoteRestoreResult as h, NoteUpdateResult as i };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordinatio/entities",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "Entity knowledge, agent intelligence, notes, and contacts",
@@ -30,6 +30,21 @@
30
30
  "import": "./dist/companies/prisma/index.mjs",
31
31
  "default": "./dist/companies/prisma/index.mjs"
32
32
  },
33
+ "./notes": {
34
+ "types": "./dist/entity-notes/index.d.mts",
35
+ "import": "./dist/entity-notes/index.mjs",
36
+ "default": "./dist/entity-notes/index.mjs"
37
+ },
38
+ "./notes/server": {
39
+ "types": "./dist/entity-notes/server/index.d.mts",
40
+ "import": "./dist/entity-notes/server/index.mjs",
41
+ "default": "./dist/entity-notes/server/index.mjs"
42
+ },
43
+ "./notes/prisma": {
44
+ "types": "./dist/entity-notes/prisma/index.d.mts",
45
+ "import": "./dist/entity-notes/prisma/index.mjs",
46
+ "default": "./dist/entity-notes/prisma/index.mjs"
47
+ },
33
48
  "./tags": {
34
49
  "types": "./dist/tags/index.d.mts",
35
50
  "import": "./dist/tags/index.mjs",
@@ -74,7 +89,7 @@
74
89
  }
75
90
  },
76
91
  "scripts": {
77
- "build": "tsup src/index.ts src/companies/index.ts src/companies/server/index.ts src/companies/prisma/index.ts src/tags/index.ts src/tags/server/index.ts src/tags/prisma/index.ts --format esm --dts --clean",
92
+ "build": "tsup src/index.ts src/companies/index.ts src/companies/server/index.ts src/companies/prisma/index.ts src/entity-notes/index.ts src/entity-notes/server/index.ts src/entity-notes/prisma/index.ts src/tags/index.ts src/tags/server/index.ts src/tags/prisma/index.ts --format esm --dts --clean",
78
93
  "test": "vitest",
79
94
  "test:run": "vitest run"
80
95
  }