@ordinatio/entities 1.4.0 → 1.6.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/README.md +20 -0
- package/dist/chunk-3VGJ4NFX.mjs +29 -0
- package/dist/companies/prisma/index.d.mts +19 -1
- package/dist/companies/prisma/index.mjs +123 -25
- package/dist/companies/server/index.d.mts +48 -2
- package/dist/companies/server/index.mjs +234 -22
- package/dist/constants-BwInbMWC.d.mts +11 -0
- package/dist/entity-notes/index.d.mts +19 -0
- package/dist/entity-notes/index.mjs +10 -0
- package/dist/entity-notes/prisma/index.d.mts +57 -0
- package/dist/entity-notes/prisma/index.mjs +284 -0
- package/dist/entity-notes/server/index.d.mts +153 -0
- package/dist/entity-notes/server/index.mjs +998 -0
- package/dist/tags/prisma/index.d.mts +5 -0
- package/dist/tags/prisma/index.mjs +4 -2
- package/dist/types-B4DY5xj3.d.mts +123 -0
- package/dist/{types-CbfseUAS.d.mts → types-BJl_PZDf.d.mts} +42 -1
- package/package.json +17 -2
|
@@ -12,6 +12,11 @@ interface PrismaTagTarget {
|
|
|
12
12
|
targetModel: string;
|
|
13
13
|
/** The item model's workspace column. Defaults to the tag's. */
|
|
14
14
|
targetWorkspaceColumn?: string;
|
|
15
|
+
/**
|
|
16
|
+
* OPTIONAL. The item model's nullable "trashed at" column (e.g. `deletedAt`). When set, an item whose column is not NULL counts as gone for tagging and untagging
|
|
17
|
+
* (`targetGone`). Counts and the tag lists of items are NOT filtered by it. Off by default: without it the queries are exactly those of 1.5.0.
|
|
18
|
+
*/
|
|
19
|
+
softDeleteColumn?: string;
|
|
15
20
|
}
|
|
16
21
|
interface PrismaTagStoreConfig<Target extends string> {
|
|
17
22
|
/** Called each time the store needs the client (never at import), so a test can swap in a client with a bigger pool. */
|
|
@@ -14,13 +14,15 @@ function quote(identifier) {
|
|
|
14
14
|
// src/tags/prisma/context.ts
|
|
15
15
|
function resolveTarget(kind, target, defaultWorkspaceColumn) {
|
|
16
16
|
const where = `targets.${kind}`;
|
|
17
|
-
|
|
17
|
+
const resolved = {
|
|
18
18
|
linkModel: assertIdentifier(target.linkModel, `${where}.linkModel`),
|
|
19
19
|
linkTable: assertIdentifier(target.linkTable, `${where}.linkTable`),
|
|
20
20
|
targetColumn: assertIdentifier(target.targetColumn, `${where}.targetColumn`),
|
|
21
21
|
targetModel: assertIdentifier(target.targetModel, `${where}.targetModel`),
|
|
22
22
|
targetWorkspaceColumn: assertIdentifier(target.targetWorkspaceColumn ?? defaultWorkspaceColumn, `${where}.targetWorkspaceColumn`)
|
|
23
23
|
};
|
|
24
|
+
if (target.softDeleteColumn !== void 0) resolved.softDeleteColumn = assertIdentifier(target.softDeleteColumn, `${where}.softDeleteColumn`);
|
|
25
|
+
return resolved;
|
|
24
26
|
}
|
|
25
27
|
function createContext(config) {
|
|
26
28
|
if (typeof config.client !== "function") throw new Error("createPrismaTagStore: client must be a function that returns the Prisma client");
|
|
@@ -167,7 +169,7 @@ function createReads(context) {
|
|
|
167
169
|
},
|
|
168
170
|
async targetExists(kind, targetId) {
|
|
169
171
|
const target = context.target(kind);
|
|
170
|
-
const row = await context.model(db(), target.targetModel).findFirst({ where: { id: targetId, [target.targetWorkspaceColumn]: context.workspaceId }, select: { id: true } });
|
|
172
|
+
const row = await context.model(db(), target.targetModel).findFirst({ where: { id: targetId, [target.targetWorkspaceColumn]: context.workspaceId, ...target.softDeleteColumn ? { [target.softDeleteColumn]: null } : {} }, select: { id: true } });
|
|
171
173
|
return row !== null && row !== void 0;
|
|
172
174
|
},
|
|
173
175
|
async counts(ids) {
|
|
@@ -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 };
|
|
@@ -21,11 +21,15 @@ type CompanyWebsite = {
|
|
|
21
21
|
interface CompanyCreateLimits {
|
|
22
22
|
maxCompanies: number;
|
|
23
23
|
}
|
|
24
|
+
/** `inTrash`: the name belongs to a company that is in the Trash (only a store with soft-delete configured reports it), with that company's id. */
|
|
24
25
|
type CompanyCreateResult<T extends CompanyRecord> = {
|
|
25
26
|
kind: 'created';
|
|
26
27
|
company: T;
|
|
27
28
|
} | {
|
|
28
29
|
kind: 'duplicate';
|
|
30
|
+
} | {
|
|
31
|
+
kind: 'inTrash';
|
|
32
|
+
id: string;
|
|
29
33
|
} | {
|
|
30
34
|
kind: 'limit';
|
|
31
35
|
};
|
|
@@ -46,6 +50,29 @@ type CompanyUpdateResult<T extends CompanyRecord> = {
|
|
|
46
50
|
kind: 'notFound';
|
|
47
51
|
} | {
|
|
48
52
|
kind: 'duplicate';
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'inTrash';
|
|
55
|
+
id: string;
|
|
56
|
+
};
|
|
57
|
+
/** Moving a company to the Trash (`TrashableCompanyStore` only). `before` and `after` are the rows around the change. */
|
|
58
|
+
type CompanyTrashResult<T extends CompanyRecord> = {
|
|
59
|
+
kind: 'trashed';
|
|
60
|
+
before: T;
|
|
61
|
+
after: T;
|
|
62
|
+
} | {
|
|
63
|
+
kind: 'notFound';
|
|
64
|
+
} | {
|
|
65
|
+
kind: 'alreadyTrashed';
|
|
66
|
+
};
|
|
67
|
+
/** Taking a company out of the Trash. */
|
|
68
|
+
type CompanyRestoreResult<T extends CompanyRecord> = {
|
|
69
|
+
kind: 'restored';
|
|
70
|
+
before: T;
|
|
71
|
+
after: T;
|
|
72
|
+
} | {
|
|
73
|
+
kind: 'notFound';
|
|
74
|
+
} | {
|
|
75
|
+
kind: 'notTrashed';
|
|
49
76
|
};
|
|
50
77
|
/** `memberCount` is how many people were detached with it, counted in the same transaction as the delete (an app's Activity entry quotes it). */
|
|
51
78
|
type CompanyRemoveResult<T extends CompanyRecord> = {
|
|
@@ -165,5 +192,19 @@ interface CompanyStore<T extends CompanyRecord = CompanyRecord> {
|
|
|
165
192
|
*/
|
|
166
193
|
companiesForPeople(personIds: string[]): Promise<Map<string, Array<PersonCompany<T>>>>;
|
|
167
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* A `CompanyStore` that can also move companies to the Trash and back. A SEPARATE interface so existing implementers of `CompanyStore` are unaffected.
|
|
197
|
+
* On a trashable store the plain reads (`list`, `get`, ...) and writes see LIVE companies only; `remove` stays a hard delete of a company in ANY state.
|
|
198
|
+
*/
|
|
199
|
+
interface TrashableCompanyStore<T extends CompanyRecord = CompanyRecord> extends CompanyStore<T> {
|
|
200
|
+
/** Atomic and idempotent under races: ten simultaneous trashes give one `trashed` and nine `alreadyTrashed`. `actorId` is stored with the trash (opaque to this package). */
|
|
201
|
+
trash(id: string, actorId: string): Promise<CompanyTrashResult<T>>;
|
|
202
|
+
/** Atomic: ten simultaneous restores give one `restored`. */
|
|
203
|
+
restore(id: string): Promise<CompanyRestoreResult<T>>;
|
|
204
|
+
/** Only the trashed companies of the workspace, the most recently trashed first. */
|
|
205
|
+
listTrashed(): Promise<T[]>;
|
|
206
|
+
/** The company of this workspace in ANY state (live or trashed), or null. */
|
|
207
|
+
getAny(id: string): Promise<T | null>;
|
|
208
|
+
}
|
|
168
209
|
|
|
169
|
-
export type { CompanyRecord as C, PersonCompany as P, CompanyStore as a, CompanyMemberRecord as b, CompanyAddMemberResult as c, CompanyCreateLimits as d, CompanyCreateResult as e, CompanyMemberLimits as f, CompanyRemoveMemberResult as g, CompanyRemoveResult as h,
|
|
210
|
+
export type { CompanyRecord as C, PersonCompany as P, TrashableCompanyStore as T, CompanyStore as a, CompanyMemberRecord as b, CompanyAddMemberResult as c, CompanyCreateLimits as d, CompanyCreateResult as e, CompanyMemberLimits as f, CompanyRemoveMemberResult as g, CompanyRemoveResult as h, CompanyRestoreResult as i, CompanySetRoleResult as j, CompanyTrashResult as k, CompanyUpdatePatch as l, CompanyUpdateResult as m, CompanyWebsite as n };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ordinatio/entities",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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
|
}
|