@ordinatio/entities 1.6.0 → 1.7.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 +4 -0
- package/dist/{chunk-KPZRBM5J.mjs → chunk-EKHZAM3Z.mjs} +9 -0
- package/dist/companies/prisma/index.d.mts +1 -1
- package/dist/companies/server/index.d.mts +2 -2
- package/dist/tags/index.d.mts +10 -1
- package/dist/tags/index.mjs +5 -1
- package/dist/tags/server/index.d.mts +13 -2
- package/dist/tags/server/index.mjs +13 -6
- package/dist/{types-BJl_PZDf.d.mts → types-D-vgDFHg.d.mts} +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -203,6 +203,10 @@ Pure, zero-dependency helpers for companies, on their own subpath so a browser c
|
|
|
203
203
|
|
|
204
204
|
Pure, zero-dependency helpers for tags, on their own subpath so a browser component can import them without pulling in the rest of the package (no Prisma, zod or Node modules): `tagNameKey` / `cleanTagName` / `checkTagName` (the case-insensitive identity of a name; **pinned to its first shipped behaviour** because apps store the key in a unique column), `TAG_COLORS` / `isTagColor` / `resolveTagColor`, `DEFAULT_TAG_LIMITS` (name length 40, 50 changes per save, 15 pinned, 500 per workspace: defaults, a store takes them as arguments), and the picker helpers `describeTagChanges`, `tagSetKey`, `rebaseSelection`, `mergeTagOptions`. The `./schemas` and `./errors` subpaths exist only in the workspace (source) exports map and are not part of the published package yet. On the separate server entry `@ordinatio/entities/tags/server` (EXPERIMENTAL until a second product has used it): the `TagStore` interface (built per workspace, so the package never sees a tenant id; atomic, typed, limit-aware operations), `createTagService(store, limits?)` (the tag rules over any store: validate and clean names and colours, create and find-or-create, rename/recolour/pin, remove, apply only the changes a user made to one item's tags, and read tags back sorted; every outcome a user can cause is a typed `{ ok: false, reason, ... }` result with the context an app needs for its message, and a store result it does not know throws; a store that throws is not caught, so an app that must never fail a page, such as a sidebar of pinned tags, wraps that one call itself; `pinned()` asks the store for the first `maxPinned` pinned tags, so make `listPinned` return them in a stable order such as by name), `planTagChanges`, an in-memory reference store (`createMemoryTagWorld`, `createMemoryTagHarness`) and the store contract suite `defineTagStoreContract` that any storage adapter must pass (framework-agnostic: pass your runner's `describe`/`it`/`expect`). On a further entry, `@ordinatio/entities/tags/prisma` (also EXPERIMENTAL): `createPrismaTagStore(config)`, a ready-made `TagStore` for a Prisma client on Postgres, so an app writes configuration instead of storage code. It holds the generic storage rules: a per-workspace advisory lock around the two limits (the lock key prefix is configurable, so an app can keep an older key), lock waits that THROW on timeout in create, pin and update (a remove and a link write start no explicit lock of their own, so they wait only on locks other transactions hold, bounded only if those transactions are), `FOR UPDATE` on the tag row in an update (which briefly serialises with the key-share lock a concurrent link insert takes on the same tag), `createManyAndReturn` and `DELETE ... RETURNING` so `added` and `removed` come from rows really written, and foreign-key-abort handling (a link to a tag or item deleted a moment ago becomes `tagGone` or `targetGone`, decided by a fresh read after the aborted transaction). The configuration names your client (as a getter, called each time the store needs the client), the tag model and table, the workspace column, and per taggable kind the join model, table and columns and the item model; every name that reaches SQL is checked once and double-quoted, values are always bound. It needs Prisma 5.14 or later and unique and foreign-key constraints in your database (listed at the top of its `types.ts`) and does not import `@prisma/client`. **Prove it on YOUR database** by running `defineTagStoreContract` against it with a harness of your own (with at least ten connections available): the contract, not the unit tests of this package, is what shows the SQL, the locks and the foreign keys behave. Pin this package's exact version while the server entry is experimental: adding a required store method or a contract rule can break an adapter that passed before. The concurrency tests are meaningful only against a real store: run them against your database with a connection pool of at least 10. **Adapters and mappers must treat an unknown result `kind` or reason as an error**, never as success: adding a kind is a breaking change for an exhaustive switch.
|
|
205
205
|
|
|
206
|
+
### Own colours and a default colour, 1.7.0
|
|
207
|
+
|
|
208
|
+
`createTagService(store, limits, options?)` takes two optional choices. `options.colors` is your own list of allowed colour strings (default: the seven names in `TAG_COLORS`). `options.defaultColor` is the colour of a new tag made without one: a string, or a function (sync or async) called once for each create attempt that has a valid name and no colour, never when `findOrCreate` finds the tag (it runs BEFORE the store insert, so an attempt that ends as a duplicate or over the limit also uses one pick; `findOrCreate` picks again on its race retry). A function that throws makes the call throw. Old callers need no change.
|
|
209
|
+
|
|
206
210
|
### Soft delete (Trash), 1.6.0
|
|
207
211
|
|
|
208
212
|
Three OPT-IN options, all off by default: a consumer that sets none of them gets exactly the queries and SQL of 1.5.0 (pinned by the snapshot tests `src/companies/prisma/off-baseline.test.ts`, `src/tags/prisma/off-baseline.test.ts` and `src/entity-notes/prisma/off-baseline.test.ts`, which were recorded on 1.5.0 before any change). Every column name is checked like every other name that reaches SQL.
|
|
@@ -27,6 +27,13 @@ function resolveTagColor(color) {
|
|
|
27
27
|
if (color === void 0) return { ok: true, color: "neutral" };
|
|
28
28
|
return isTagColor(color) ? { ok: true, color } : { ok: false };
|
|
29
29
|
}
|
|
30
|
+
function isListedColor(value, allowed) {
|
|
31
|
+
return typeof value === "string" && allowed.includes(value);
|
|
32
|
+
}
|
|
33
|
+
function resolveListedColor(color, allowed, fallback = "neutral") {
|
|
34
|
+
const chosen = color === void 0 ? fallback : color;
|
|
35
|
+
return isListedColor(chosen, allowed) ? { ok: true, color: chosen } : { ok: false };
|
|
36
|
+
}
|
|
30
37
|
|
|
31
38
|
// src/tags/changes.ts
|
|
32
39
|
function describeTagChanges(before, after) {
|
|
@@ -60,6 +67,8 @@ export {
|
|
|
60
67
|
checkTagName,
|
|
61
68
|
isTagColor,
|
|
62
69
|
resolveTagColor,
|
|
70
|
+
isListedColor,
|
|
71
|
+
resolveListedColor,
|
|
63
72
|
describeTagChanges,
|
|
64
73
|
tagSetKey,
|
|
65
74
|
rebaseSelection,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as CompanyRecord, T as TrashableCompanyStore,
|
|
1
|
+
import { C as CompanyRecord, T as TrashableCompanyStore, b as CompanyStore } from '../../types-D-vgDFHg.mjs';
|
|
2
2
|
|
|
3
3
|
interface PrismaCompanyStoreConfig {
|
|
4
4
|
/** Called each time the store needs the client (never at import), so a test can swap in a client with a bigger pool. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CompanyRecord,
|
|
2
|
-
export { c as CompanyAddMemberResult, d as CompanyCreateLimits, e as CompanyCreateResult, f as CompanyMemberLimits, g as CompanyRemoveMemberResult, h as CompanyRemoveResult, i as CompanyRestoreResult, j as CompanySetRoleResult, k as CompanyTrashResult, l as CompanyUpdatePatch, m as CompanyUpdateResult, n as CompanyWebsite, T as TrashableCompanyStore } from '../../types-
|
|
1
|
+
import { C as CompanyRecord, a as CompanyMemberRecord, P as PersonCompany, b as CompanyStore } from '../../types-D-vgDFHg.mjs';
|
|
2
|
+
export { c as CompanyAddMemberResult, d as CompanyCreateLimits, e as CompanyCreateResult, f as CompanyMemberLimits, g as CompanyRemoveMemberResult, h as CompanyRemoveResult, i as CompanyRestoreResult, j as CompanySetRoleResult, k as CompanyTrashResult, l as CompanyUpdatePatch, m as CompanyUpdateResult, n as CompanyWebsite, T as TrashableCompanyStore } from '../../types-D-vgDFHg.mjs';
|
|
3
3
|
import { C as CompanyLimits } from '../../constants-DV-gn6u8.mjs';
|
|
4
4
|
|
|
5
5
|
/** Why a call did not do what was asked, with the context an app needs for its message. */
|
package/dist/tags/index.d.mts
CHANGED
|
@@ -24,6 +24,15 @@ declare function resolveTagColor(color: string | undefined): {
|
|
|
24
24
|
} | {
|
|
25
25
|
ok: false;
|
|
26
26
|
};
|
|
27
|
+
/** Whether `value` is one of the strings of `allowed` (a product's own palette). A SEPARATE function, so the one-parameter `isTagColor` keeps working as a callback (`colors.filter(isTagColor)`). Used by the tag service when it is given `options.colors`; not part of the package's public entry. */
|
|
28
|
+
declare function isListedColor(value: unknown, allowed: readonly string[]): value is string;
|
|
29
|
+
/** The colour to store: `color`, or `fallback` when none was given; `{ ok: false }` when the result is not in `allowed`. A SEPARATE function (see `isListedColor`). */
|
|
30
|
+
declare function resolveListedColor(color: string | undefined, allowed: readonly string[], fallback?: string): {
|
|
31
|
+
ok: true;
|
|
32
|
+
color: string;
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
};
|
|
27
36
|
|
|
28
37
|
interface TagSnapshot {
|
|
29
38
|
name: string;
|
|
@@ -64,4 +73,4 @@ interface TagChoice {
|
|
|
64
73
|
*/
|
|
65
74
|
declare function mergeTagOptions<T extends TagChoice>(fresh: readonly T[], createdHere: readonly T[]): T[];
|
|
66
75
|
|
|
67
|
-
export { type RebasedSelection, type TagChoice, TagColor, type TagNameCheck, type TagSnapshot, checkTagName, cleanTagName, describeTagChanges, isTagColor, mergeTagOptions, rebaseSelection, resolveTagColor, tagNameKey, tagSetKey };
|
|
76
|
+
export { type RebasedSelection, type TagChoice, TagColor, type TagNameCheck, type TagSnapshot, checkTagName, cleanTagName, describeTagChanges, isListedColor, isTagColor, mergeTagOptions, rebaseSelection, resolveListedColor, resolveTagColor, tagNameKey, tagSetKey };
|
package/dist/tags/index.mjs
CHANGED
|
@@ -4,22 +4,26 @@ import {
|
|
|
4
4
|
checkTagName,
|
|
5
5
|
cleanTagName,
|
|
6
6
|
describeTagChanges,
|
|
7
|
+
isListedColor,
|
|
7
8
|
isTagColor,
|
|
8
9
|
mergeTagOptions,
|
|
9
10
|
rebaseSelection,
|
|
11
|
+
resolveListedColor,
|
|
10
12
|
resolveTagColor,
|
|
11
13
|
tagNameKey,
|
|
12
14
|
tagSetKey
|
|
13
|
-
} from "../chunk-
|
|
15
|
+
} from "../chunk-EKHZAM3Z.mjs";
|
|
14
16
|
export {
|
|
15
17
|
DEFAULT_TAG_LIMITS,
|
|
16
18
|
TAG_COLORS,
|
|
17
19
|
checkTagName,
|
|
18
20
|
cleanTagName,
|
|
19
21
|
describeTagChanges,
|
|
22
|
+
isListedColor,
|
|
20
23
|
isTagColor,
|
|
21
24
|
mergeTagOptions,
|
|
22
25
|
rebaseSelection,
|
|
26
|
+
resolveListedColor,
|
|
23
27
|
resolveTagColor,
|
|
24
28
|
tagNameKey,
|
|
25
29
|
tagSetKey
|
|
@@ -50,6 +50,17 @@ interface TagOption {
|
|
|
50
50
|
name: string;
|
|
51
51
|
color: string;
|
|
52
52
|
}
|
|
53
|
+
/** Optional choices of a product. Without them the service behaves exactly as it always did (the seven default colour names, neutral by default). */
|
|
54
|
+
interface TagServiceOptions {
|
|
55
|
+
/** The colours this product allows: any non-empty strings. A copy is taken. Default: the seven names in `TAG_COLORS`. Every colour already stored by an older version must be listed, or an update that re-sends it is refused. */
|
|
56
|
+
colors?: readonly string[];
|
|
57
|
+
/**
|
|
58
|
+
* The colour of a NEW tag made without one. A fixed string (default `'neutral'`), or a function (sync or async) that is called once for each create attempt that has a valid name and no colour: `findOrCreate` calls it only when the tag
|
|
59
|
+
* does not exist yet, and again on its race retry, so each attempt takes a fresh pick. The value it returns must be in `colors`, otherwise the create fails with `invalid-color`. A function that THROWS makes the call throw: the service never hides it
|
|
60
|
+
* (a product that wants to fail open catches inside its own function).
|
|
61
|
+
*/
|
|
62
|
+
defaultColor?: string | (() => string | Promise<string>);
|
|
63
|
+
}
|
|
53
64
|
interface TagService<T extends TagRecord, Target extends string> {
|
|
54
65
|
/** Every tag, sorted by name. */
|
|
55
66
|
list(): Promise<T[]>;
|
|
@@ -97,7 +108,7 @@ interface TagService<T extends TagRecord, Target extends string> {
|
|
|
97
108
|
forTargets(target: Target, targetIds: string[]): Promise<Map<string, T[]>>;
|
|
98
109
|
counts(ids?: string[]): Promise<Map<string, Record<Target, number>>>;
|
|
99
110
|
}
|
|
100
|
-
declare function createTagService<T extends TagRecord, Target extends string>(store: TagStore<T, Target>, suppliedLimits?: TagLimits): TagService<T, Target>;
|
|
111
|
+
declare function createTagService<T extends TagRecord, Target extends string>(store: TagStore<T, Target>, suppliedLimits?: TagLimits, options?: TagServiceOptions): TagService<T, Target>;
|
|
101
112
|
|
|
102
113
|
interface TagContractRunner {
|
|
103
114
|
describe: (name: string, body: () => void) => void;
|
|
@@ -165,4 +176,4 @@ declare function createMemoryTagHarness<Target extends string>(targetTypes: read
|
|
|
165
176
|
|
|
166
177
|
declare function defineTagStoreContract<Target extends string>(api: TagContractRunner, harness: TagStoreHarness<Target>): void;
|
|
167
178
|
|
|
168
|
-
export { type MemoryTagWorld, type MemoryTagWorldOptions, type TagChangePlan, type TagContractRunner, type TagFailure, TagLinkChange, type TagOption, TagRecord, type TagResult, type TagService, TagStore, type TagStoreHarness, createMemoryTagHarness, createMemoryTagWorld, createTagService, defineTagStoreContract, planTagChanges };
|
|
179
|
+
export { type MemoryTagWorld, type MemoryTagWorldOptions, type TagChangePlan, type TagContractRunner, type TagFailure, TagLinkChange, type TagOption, TagRecord, type TagResult, type TagService, type TagServiceOptions, TagStore, type TagStoreHarness, createMemoryTagHarness, createMemoryTagWorld, createTagService, defineTagStoreContract, planTagChanges };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DEFAULT_TAG_LIMITS,
|
|
3
|
+
TAG_COLORS,
|
|
3
4
|
checkTagName,
|
|
4
|
-
|
|
5
|
-
} from "../../chunk-
|
|
5
|
+
resolveListedColor
|
|
6
|
+
} from "../../chunk-EKHZAM3Z.mjs";
|
|
6
7
|
|
|
7
8
|
// src/tags/server/plan.ts
|
|
8
9
|
function planTagChanges(change, maxChanges) {
|
|
@@ -21,8 +22,12 @@ function byName(a, b) {
|
|
|
21
22
|
function unknownKind(method, result) {
|
|
22
23
|
throw new Error(`tag store ${method} returned an unknown result kind "${result.kind}": the store and the service are out of step`);
|
|
23
24
|
}
|
|
24
|
-
function createTagService(store, suppliedLimits = DEFAULT_TAG_LIMITS) {
|
|
25
|
+
function createTagService(store, suppliedLimits = DEFAULT_TAG_LIMITS, options = {}) {
|
|
25
26
|
const limits = { ...suppliedLimits };
|
|
27
|
+
const colors = options.colors === void 0 ? TAG_COLORS : Object.freeze([...options.colors]);
|
|
28
|
+
if (colors.length === 0 || colors.some((c) => typeof c !== "string" || c.length === 0)) throw new Error("createTagService: options.colors must be a non-empty list of non-empty strings");
|
|
29
|
+
const defaultColor = options.defaultColor;
|
|
30
|
+
if (typeof defaultColor === "string" && !colors.includes(defaultColor)) throw new Error(`createTagService: options.defaultColor "${defaultColor}" is not in the allowed colours`);
|
|
26
31
|
function fail(failure) {
|
|
27
32
|
return { ok: false, ...failure };
|
|
28
33
|
}
|
|
@@ -34,8 +39,10 @@ function createTagService(store, suppliedLimits = DEFAULT_TAG_LIMITS) {
|
|
|
34
39
|
async function create(input) {
|
|
35
40
|
const name = checkName(input.name);
|
|
36
41
|
if (!name.ok) return fail(name.failure);
|
|
37
|
-
|
|
38
|
-
if (
|
|
42
|
+
let given = input.color;
|
|
43
|
+
if (given === void 0 && typeof defaultColor === "function") given = await defaultColor();
|
|
44
|
+
const color = resolveListedColor(given, colors, typeof defaultColor === "string" ? defaultColor : "neutral");
|
|
45
|
+
if (!color.ok) return fail({ reason: "invalid-color", color: String(given) });
|
|
39
46
|
const result = await store.create({ name: name.name, nameKey: name.key, color: color.color }, { maxTags: limits.maxPerWorkspace });
|
|
40
47
|
if (result.kind === "created") return { ok: true, tag: result.tag };
|
|
41
48
|
if (result.kind === "duplicate") return fail({ reason: "duplicate" });
|
|
@@ -65,7 +72,7 @@ function createTagService(store, suppliedLimits = DEFAULT_TAG_LIMITS) {
|
|
|
65
72
|
patch.rename = { name: name.name, nameKey: name.key };
|
|
66
73
|
}
|
|
67
74
|
if (input.color !== void 0) {
|
|
68
|
-
const color =
|
|
75
|
+
const color = resolveListedColor(input.color, colors);
|
|
69
76
|
if (!color.ok) return fail({ reason: "invalid-color", color: String(input.color) });
|
|
70
77
|
patch.color = color.color;
|
|
71
78
|
}
|
|
@@ -207,4 +207,4 @@ interface TrashableCompanyStore<T extends CompanyRecord = CompanyRecord> extends
|
|
|
207
207
|
getAny(id: string): Promise<T | null>;
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
export type { CompanyRecord as C, PersonCompany as P, TrashableCompanyStore as T,
|
|
210
|
+
export type { CompanyRecord as C, PersonCompany as P, TrashableCompanyStore as T, CompanyMemberRecord as a, CompanyStore 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 };
|