@ordinatio/entities 1.2.0 → 1.4.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 +5 -1
- package/dist/chunk-ZWID3JLL.mjs +123 -0
- package/dist/companies/index.d.mts +60 -0
- package/dist/companies/index.mjs +24 -0
- package/dist/companies/prisma/index.d.mts +58 -0
- package/dist/companies/prisma/index.mjs +276 -0
- package/dist/companies/server/index.d.mts +167 -0
- package/dist/companies/server/index.mjs +1826 -0
- package/dist/constants-DV-gn6u8.d.mts +22 -0
- package/dist/tags/prisma/index.d.mts +60 -0
- package/dist/tags/prisma/index.mjs +268 -0
- package/dist/tags/server/index.d.mts +3 -117
- package/dist/tags/server/index.mjs +11 -0
- package/dist/types-C81I1Teg.d.mts +117 -0
- package/dist/types-CbfseUAS.d.mts +169 -0
- package/package.json +22 -2
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/** The fields the rules need. An adapter may return a wider row (createdAt ...): the store is generic over it. */
|
|
2
|
+
interface CompanyRecord {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
/** The case-insensitive identity of the name (unique per workspace): see `companyNameKey` in `@ordinatio/entities/companies`. */
|
|
6
|
+
nameKey: string;
|
|
7
|
+
/** The normalised website, or null. Derived by `checkCompanyWebsite`: the store never builds it. */
|
|
8
|
+
website: string | null;
|
|
9
|
+
/** The domain derived from the website (null exactly when the website is null). */
|
|
10
|
+
domain: string | null;
|
|
11
|
+
}
|
|
12
|
+
/** A website and its domain travel TOGETHER so they cannot disagree: both set, or both null (no website). */
|
|
13
|
+
type CompanyWebsite = {
|
|
14
|
+
website: string;
|
|
15
|
+
domain: string;
|
|
16
|
+
} | {
|
|
17
|
+
website: null;
|
|
18
|
+
domain: null;
|
|
19
|
+
};
|
|
20
|
+
/** Named object types so a future limit is an additive field, not a signature change. */
|
|
21
|
+
interface CompanyCreateLimits {
|
|
22
|
+
maxCompanies: number;
|
|
23
|
+
}
|
|
24
|
+
type CompanyCreateResult<T extends CompanyRecord> = {
|
|
25
|
+
kind: 'created';
|
|
26
|
+
company: T;
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'duplicate';
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'limit';
|
|
31
|
+
};
|
|
32
|
+
interface CompanyUpdatePatch {
|
|
33
|
+
/** Name and key travel TOGETHER so they cannot disagree. */
|
|
34
|
+
rename?: {
|
|
35
|
+
name: string;
|
|
36
|
+
nameKey: string;
|
|
37
|
+
};
|
|
38
|
+
/** Sets the website and its domain together; `{ website: null, domain: null }` clears both. */
|
|
39
|
+
website?: CompanyWebsite;
|
|
40
|
+
}
|
|
41
|
+
type CompanyUpdateResult<T extends CompanyRecord> = {
|
|
42
|
+
kind: 'updated';
|
|
43
|
+
before: T;
|
|
44
|
+
after: T;
|
|
45
|
+
} | {
|
|
46
|
+
kind: 'notFound';
|
|
47
|
+
} | {
|
|
48
|
+
kind: 'duplicate';
|
|
49
|
+
};
|
|
50
|
+
/** `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
|
+
type CompanyRemoveResult<T extends CompanyRecord> = {
|
|
52
|
+
kind: 'removed';
|
|
53
|
+
company: T;
|
|
54
|
+
memberCount: number;
|
|
55
|
+
} | {
|
|
56
|
+
kind: 'notFound';
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* One person's membership of one company. `personId` is opaque to this package (a contact, a user ...); `role` is optional text ("Partner").
|
|
60
|
+
* A store hands back EXACTLY these three fields (map your row to them: the contract compares whole records), unlike companies, whose records
|
|
61
|
+
* an app may extend.
|
|
62
|
+
*/
|
|
63
|
+
interface CompanyMemberRecord {
|
|
64
|
+
companyId: string;
|
|
65
|
+
personId: string;
|
|
66
|
+
role: string | null;
|
|
67
|
+
}
|
|
68
|
+
/** A company with the role one person has in it (what a chip next to a person shows). */
|
|
69
|
+
interface PersonCompany<T extends CompanyRecord> {
|
|
70
|
+
company: T;
|
|
71
|
+
role: string | null;
|
|
72
|
+
}
|
|
73
|
+
interface CompanyMemberLimits {
|
|
74
|
+
maxMembers: number;
|
|
75
|
+
}
|
|
76
|
+
type CompanyAddMemberResult = {
|
|
77
|
+
kind: 'added';
|
|
78
|
+
member: CompanyMemberRecord;
|
|
79
|
+
} | {
|
|
80
|
+
kind: 'already';
|
|
81
|
+
member: CompanyMemberRecord;
|
|
82
|
+
} | {
|
|
83
|
+
kind: 'companyGone';
|
|
84
|
+
} | {
|
|
85
|
+
kind: 'personGone';
|
|
86
|
+
} | {
|
|
87
|
+
kind: 'memberLimit';
|
|
88
|
+
};
|
|
89
|
+
type CompanySetRoleResult = {
|
|
90
|
+
kind: 'updated';
|
|
91
|
+
before: CompanyMemberRecord;
|
|
92
|
+
after: CompanyMemberRecord;
|
|
93
|
+
} | {
|
|
94
|
+
kind: 'companyGone';
|
|
95
|
+
} | {
|
|
96
|
+
kind: 'notMember';
|
|
97
|
+
};
|
|
98
|
+
type CompanyRemoveMemberResult = {
|
|
99
|
+
kind: 'removed';
|
|
100
|
+
member: CompanyMemberRecord;
|
|
101
|
+
} | {
|
|
102
|
+
kind: 'companyGone';
|
|
103
|
+
} | {
|
|
104
|
+
kind: 'notMember';
|
|
105
|
+
};
|
|
106
|
+
interface CompanyStore<T extends CompanyRecord = CompanyRecord> {
|
|
107
|
+
/** Every company of the workspace. ORDER IS UNSPECIFIED: the service sorts. */
|
|
108
|
+
list(): Promise<T[]>;
|
|
109
|
+
/** The companies of THIS workspace among `ids`; foreign or deleted ids are simply absent. */
|
|
110
|
+
getMany(ids: string[]): Promise<T[]>;
|
|
111
|
+
findByKey(nameKey: string): Promise<T | null>;
|
|
112
|
+
get(id: string): Promise<T | null>;
|
|
113
|
+
/**
|
|
114
|
+
* Keys are OPAQUE and compared EXACTLY: two keys that differ only by case are two keys (the service lower-cases before it calls the store). Name,
|
|
115
|
+
* key, website and domain are stored exactly as given. A new company never gets the id of a removed one, and nothing of a removed company
|
|
116
|
+
* comes back when its key is taken again. Every id list (`getMany`, `memberCounts`) must work at tens of thousands of ids (in Postgres use
|
|
117
|
+
* `= ANY($1)` with one array parameter, not one bind parameter per id).
|
|
118
|
+
* ATOMIC: the limit is enforced together with the insert, so simultaneous creates can never overshoot it. Precedence: `limit` wins over
|
|
119
|
+
* `duplicate` (a duplicate name when the workspace is full is `limit`).
|
|
120
|
+
*/
|
|
121
|
+
create(input: {
|
|
122
|
+
name: string;
|
|
123
|
+
nameKey: string;
|
|
124
|
+
} & CompanyWebsite, limits: CompanyCreateLimits): Promise<CompanyCreateResult<T>>;
|
|
125
|
+
/**
|
|
126
|
+
* ATOMIC. Precedence: `notFound`, then `duplicate`. Renaming a company to the same key it already has (a case change) is not a duplicate.
|
|
127
|
+
* An empty patch is a no-op that returns `updated` with `before` equal to `after`. `before` is read in the same transaction as the write,
|
|
128
|
+
* so `before`/`after` are consistent under concurrent edits.
|
|
129
|
+
*/
|
|
130
|
+
update(id: string, patch: CompanyUpdatePatch): Promise<CompanyUpdateResult<T>>;
|
|
131
|
+
/** Its memberships go with it. Ten parallel removes of one company give exactly one `removed`. */
|
|
132
|
+
remove(id: string): Promise<CompanyRemoveResult<T>>;
|
|
133
|
+
/**
|
|
134
|
+
* Companies whose stored `domain` is EXACTLY `domain` (the caller has already normalised it), for the suggestion lookup. ORDER UNSPECIFIED.
|
|
135
|
+
* A company without a domain never matches. ADAPTER NOTE: an index on (workspace, domain) keeps this one cheap query.
|
|
136
|
+
*/
|
|
137
|
+
findByDomain(domain: string): Promise<T[]>;
|
|
138
|
+
/**
|
|
139
|
+
* Members per company in ONE round trip, for EVERY company of the workspace (or only `ids`): a company with no members appears with 0, an id
|
|
140
|
+
* that is not a company of this workspace is absent. An EMPTY `ids` asks for nothing: it returns an empty map.
|
|
141
|
+
*/
|
|
142
|
+
memberCounts(ids?: string[]): Promise<Map<string, number>>;
|
|
143
|
+
/** Is this a person of THIS workspace? (A person of another workspace, or a deleted one, is not.) */
|
|
144
|
+
personExists(personId: string): Promise<boolean>;
|
|
145
|
+
/**
|
|
146
|
+
* `role` is already cleaned: null or non-empty trimmed text (the service sends nothing else; an adapter may rely on it).
|
|
147
|
+
* ADAPTER NOTE: the member count and the insert must be atomic (a lock per company; a shared row lock on the company is NOT enough, because two
|
|
148
|
+
* shared locks coexist), and the person row must be share-locked while the membership is inserted, so that deleting the person waits or fails
|
|
149
|
+
* cleanly instead of leaving a dangling membership.
|
|
150
|
+
* ATOMIC: the member limit is enforced together with the insert. Precedence: `companyGone`, then `personGone`, then `already`, then
|
|
151
|
+
* `memberLimit`: a person who is ALREADY a member is reported as `already` (with the membership as it is, role untouched) and never counts
|
|
152
|
+
* against the limit. A company or person deleted between the check and the write is `companyGone` / `personGone` and nothing is written.
|
|
153
|
+
* `actorId` is stored with a new membership (who added it); it is opaque to this package.
|
|
154
|
+
*/
|
|
155
|
+
addMember(companyId: string, personId: string, role: string | null, actorId: string, limits: CompanyMemberLimits): Promise<CompanyAddMemberResult>;
|
|
156
|
+
/** `role` is null or cleaned non-empty text, as for `addMember`. ATOMIC; `before` is read in the same transaction as the write. Precedence: `companyGone`, then `notMember`. Setting the role it already has is `updated` with `before` equal to `after`. */
|
|
157
|
+
setRole(companyId: string, personId: string, role: string | null): Promise<CompanySetRoleResult>;
|
|
158
|
+
/** Precedence: `companyGone`, then `notMember`. Ten parallel removes of one membership give exactly one `removed`. */
|
|
159
|
+
removeMember(companyId: string, personId: string): Promise<CompanyRemoveMemberResult>;
|
|
160
|
+
/** The memberships of one company. ORDER IS UNSPECIFIED (the app sorts by the people's names). An unknown company has none. */
|
|
161
|
+
members(companyId: string): Promise<CompanyMemberRecord[]>;
|
|
162
|
+
/**
|
|
163
|
+
* The companies of many people in ONE round trip, each with the person's role. Only people with at least one company appear in the map;
|
|
164
|
+
* a company of another workspace never appears. ORDER of each list is unspecified: the service sorts.
|
|
165
|
+
*/
|
|
166
|
+
companiesForPeople(personIds: string[]): Promise<Map<string, Array<PersonCompany<T>>>>;
|
|
167
|
+
}
|
|
168
|
+
|
|
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, CompanySetRoleResult as i, CompanyUpdatePatch as j, CompanyUpdateResult as k, CompanyWebsite as l };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ordinatio/entities",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Entity knowledge, agent intelligence, notes, and contacts",
|
|
@@ -15,6 +15,21 @@
|
|
|
15
15
|
"import": "./dist/index.mjs",
|
|
16
16
|
"default": "./dist/index.mjs"
|
|
17
17
|
},
|
|
18
|
+
"./companies": {
|
|
19
|
+
"types": "./dist/companies/index.d.mts",
|
|
20
|
+
"import": "./dist/companies/index.mjs",
|
|
21
|
+
"default": "./dist/companies/index.mjs"
|
|
22
|
+
},
|
|
23
|
+
"./companies/server": {
|
|
24
|
+
"types": "./dist/companies/server/index.d.mts",
|
|
25
|
+
"import": "./dist/companies/server/index.mjs",
|
|
26
|
+
"default": "./dist/companies/server/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./companies/prisma": {
|
|
29
|
+
"types": "./dist/companies/prisma/index.d.mts",
|
|
30
|
+
"import": "./dist/companies/prisma/index.mjs",
|
|
31
|
+
"default": "./dist/companies/prisma/index.mjs"
|
|
32
|
+
},
|
|
18
33
|
"./tags": {
|
|
19
34
|
"types": "./dist/tags/index.d.mts",
|
|
20
35
|
"import": "./dist/tags/index.mjs",
|
|
@@ -24,6 +39,11 @@
|
|
|
24
39
|
"types": "./dist/tags/server/index.d.mts",
|
|
25
40
|
"import": "./dist/tags/server/index.mjs",
|
|
26
41
|
"default": "./dist/tags/server/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"./tags/prisma": {
|
|
44
|
+
"types": "./dist/tags/prisma/index.d.mts",
|
|
45
|
+
"import": "./dist/tags/prisma/index.mjs",
|
|
46
|
+
"default": "./dist/tags/prisma/index.mjs"
|
|
27
47
|
}
|
|
28
48
|
},
|
|
29
49
|
"publishConfig": {
|
|
@@ -54,7 +74,7 @@
|
|
|
54
74
|
}
|
|
55
75
|
},
|
|
56
76
|
"scripts": {
|
|
57
|
-
"build": "tsup src/index.ts src/tags/index.ts src/tags/server/index.ts --format esm --dts --clean",
|
|
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",
|
|
58
78
|
"test": "vitest",
|
|
59
79
|
"test:run": "vitest run"
|
|
60
80
|
}
|