@20baht/definitions 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +97 -2
- package/dist/elysia.d.ts +363 -0
- package/dist/elysia.js +101 -0
- package/dist/files.d.ts +4 -0
- package/dist/files.js +9 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +17 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/memory.d.ts +3 -0
- package/dist/memory.js +79 -0
- package/dist/pg.d.ts +334 -0
- package/dist/pg.js +170 -0
- package/dist/seed.d.ts +23 -0
- package/dist/seed.js +15 -0
- package/dist/service.d.ts +2 -0
- package/dist/service.js +109 -0
- package/dist/types.d.ts +155 -0
- package/dist/types.js +1 -0
- package/dist/upgrade.d.ts +2 -0
- package/dist/upgrade.js +43 -0
- package/package.json +91 -4
package/dist/service.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { parseSchema } from '@20baht/validator';
|
|
2
|
+
import { contentHash } from './hash.js';
|
|
3
|
+
import { upgradeDefinition } from './upgrade.js';
|
|
4
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
5
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
6
|
+
function kindOf(definition) {
|
|
7
|
+
if (!isRecord(definition))
|
|
8
|
+
return null;
|
|
9
|
+
if (Array.isArray(definition.forms))
|
|
10
|
+
return 'workflow';
|
|
11
|
+
if (Array.isArray(definition.fields))
|
|
12
|
+
return 'form';
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const refused = (reason, message) => ({
|
|
16
|
+
ok: false,
|
|
17
|
+
reason,
|
|
18
|
+
message,
|
|
19
|
+
});
|
|
20
|
+
const badName = (name) => refused('bad-name', `${JSON.stringify(name)} is not a definition name (letters, digits, ".", "_", "-"; at most 128)`);
|
|
21
|
+
const kindChanged = (name, was, now) => refused('kind-changed', `${name} is a ${was}; this is a ${now}`);
|
|
22
|
+
export function createDefinitions(options) {
|
|
23
|
+
const { storage, registry } = options;
|
|
24
|
+
const now = () => (options.now ?? (() => new Date()))().toISOString();
|
|
25
|
+
const upgrade = (definition) => {
|
|
26
|
+
const ours = upgradeDefinition(definition);
|
|
27
|
+
return options.upgrade ? options.upgrade(ours) : ours;
|
|
28
|
+
};
|
|
29
|
+
const read = (version) => version && { ...version, definition: upgrade(version.definition) };
|
|
30
|
+
const check = (definition) => {
|
|
31
|
+
try {
|
|
32
|
+
const parsed = parseSchema(upgrade(definition), registry);
|
|
33
|
+
return { ok: true, kind: kindOf(parsed) ?? 'form', definition: parsed };
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return refused('invalid', error instanceof Error ? error.message : String(error));
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
list: () => storage.list(),
|
|
41
|
+
active: async (name) => read(await storage.active(name)),
|
|
42
|
+
version: async (name, version) => read(await storage.version(name, version)),
|
|
43
|
+
versions: (name) => storage.versions(name),
|
|
44
|
+
async draft(name) {
|
|
45
|
+
const draft = await storage.draft(name);
|
|
46
|
+
return draft && { ...draft, definition: upgrade(draft.definition) };
|
|
47
|
+
},
|
|
48
|
+
check,
|
|
49
|
+
async saveDraft(name, definition, { by }) {
|
|
50
|
+
if (!NAME.test(name))
|
|
51
|
+
return badName(name);
|
|
52
|
+
const kind = kindOf(definition);
|
|
53
|
+
const latest = await storage.latest(name);
|
|
54
|
+
if (latest && kind && kind !== latest.kind)
|
|
55
|
+
return kindChanged(name, latest.kind, kind);
|
|
56
|
+
const draft = {
|
|
57
|
+
name,
|
|
58
|
+
kind: kind ?? latest?.kind ?? null,
|
|
59
|
+
definition,
|
|
60
|
+
base: latest?.version ?? null,
|
|
61
|
+
saved_by: by,
|
|
62
|
+
saved_at: now(),
|
|
63
|
+
};
|
|
64
|
+
await storage.saveDraft(draft);
|
|
65
|
+
const checked = check(definition);
|
|
66
|
+
return { ok: true, draft, issue: checked.ok ? null : checked.message };
|
|
67
|
+
},
|
|
68
|
+
discardDraft: (name) => storage.dropDraft(name),
|
|
69
|
+
async publish(name, { by, activate = false, definition }) {
|
|
70
|
+
if (!NAME.test(name))
|
|
71
|
+
return badName(name);
|
|
72
|
+
const source = definition ?? (await storage.draft(name))?.definition;
|
|
73
|
+
if (source === undefined)
|
|
74
|
+
return refused('no-draft', `${name} has no draft to publish`);
|
|
75
|
+
const checked = check(source);
|
|
76
|
+
if (!checked.ok)
|
|
77
|
+
return checked;
|
|
78
|
+
const latest = await storage.latest(name);
|
|
79
|
+
if (latest && checked.kind !== latest.kind) {
|
|
80
|
+
return kindChanged(name, latest.kind, checked.kind);
|
|
81
|
+
}
|
|
82
|
+
const hash = await contentHash(checked.definition);
|
|
83
|
+
if (latest?.hash === hash) {
|
|
84
|
+
return refused('unchanged', `${name} version ${latest.version} is this definition already`);
|
|
85
|
+
}
|
|
86
|
+
const version = await storage.append({
|
|
87
|
+
name,
|
|
88
|
+
kind: checked.kind,
|
|
89
|
+
definition: checked.definition,
|
|
90
|
+
hash,
|
|
91
|
+
by,
|
|
92
|
+
at: now(),
|
|
93
|
+
activate,
|
|
94
|
+
dropDraft: definition === undefined,
|
|
95
|
+
});
|
|
96
|
+
return { ok: true, version };
|
|
97
|
+
},
|
|
98
|
+
async activate(name, version) {
|
|
99
|
+
const found = await storage.version(name, version);
|
|
100
|
+
if (!found || !(await storage.activate(name, version, now()))) {
|
|
101
|
+
return refused('not-found', `${name} has no version ${version}`);
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
ok: true,
|
|
105
|
+
version: { ...found, definition: upgrade(found.definition) },
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { FormDefinition, Registry, WorkflowDefinition } from '@20baht/validator';
|
|
2
|
+
/** What a stored definition is: one form, or a workflow of forms. A name keeps its kind. */
|
|
3
|
+
export type DefinitionKind = 'form' | 'workflow';
|
|
4
|
+
/** A definition that passed `parseSchema`: what a version holds. */
|
|
5
|
+
export type Definition = FormDefinition | WorkflowDefinition;
|
|
6
|
+
/**
|
|
7
|
+
* One published version: frozen when it was published, never changed after. `version` counts
|
|
8
|
+
* from 1 for each name. `hash` is the SHA-256 of the content, keys in order.
|
|
9
|
+
*/
|
|
10
|
+
export interface DefinitionVersion<D extends Definition = Definition> {
|
|
11
|
+
name: string;
|
|
12
|
+
kind: DefinitionKind;
|
|
13
|
+
version: number;
|
|
14
|
+
definition: D;
|
|
15
|
+
hash: string;
|
|
16
|
+
published_by: string;
|
|
17
|
+
/** ISO 8601 */
|
|
18
|
+
published_at: string;
|
|
19
|
+
}
|
|
20
|
+
/** A version without its content: a row of `versions(name)`. */
|
|
21
|
+
export type VersionInfo = Omit<DefinitionVersion, 'definition'> & {
|
|
22
|
+
active: boolean;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The edit in progress for a name. It may not pass `parseSchema` yet: a draft is saved as the
|
|
26
|
+
* builder holds it, and checked again when it is published. `base` is the version it started
|
|
27
|
+
* from (`null` for a name never published).
|
|
28
|
+
*/
|
|
29
|
+
export interface Draft {
|
|
30
|
+
name: string;
|
|
31
|
+
kind: DefinitionKind | null;
|
|
32
|
+
definition: unknown;
|
|
33
|
+
base: number | null;
|
|
34
|
+
saved_by: string;
|
|
35
|
+
/** ISO 8601 */
|
|
36
|
+
saved_at: string;
|
|
37
|
+
}
|
|
38
|
+
/** A name as `list()` shows it. */
|
|
39
|
+
export interface DefinitionSummary {
|
|
40
|
+
name: string;
|
|
41
|
+
kind: DefinitionKind | null;
|
|
42
|
+
/** The version new submissions use, `null` when none is. */
|
|
43
|
+
active: number | null;
|
|
44
|
+
/** The highest version published, `null` before the first. */
|
|
45
|
+
latest: number | null;
|
|
46
|
+
draft: boolean;
|
|
47
|
+
/** ISO 8601: the last publish, activation or draft save. */
|
|
48
|
+
updated_at: string;
|
|
49
|
+
}
|
|
50
|
+
/** A version to freeze, as the service hands it to the storage. */
|
|
51
|
+
export interface NewVersion {
|
|
52
|
+
name: string;
|
|
53
|
+
kind: DefinitionKind;
|
|
54
|
+
definition: Definition;
|
|
55
|
+
hash: string;
|
|
56
|
+
by: string;
|
|
57
|
+
at: string;
|
|
58
|
+
/** Make it the active version in the same step. */
|
|
59
|
+
activate: boolean;
|
|
60
|
+
/** Drop the name's draft in the same step (it is what was published). */
|
|
61
|
+
dropDraft: boolean;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Where definitions are kept. Each method is one atomic step: `append` gives the next number
|
|
65
|
+
* to one caller only, and `activate` leaves exactly one version active. Rows are returned as
|
|
66
|
+
* they were written; the service upgrades their spelling on the way out, never in storage.
|
|
67
|
+
* `@20baht/definitions/pg` has the Postgres adapter, `memoryStorage()` one in memory.
|
|
68
|
+
*/
|
|
69
|
+
export interface DefinitionStorage {
|
|
70
|
+
list(): Promise<DefinitionSummary[]>;
|
|
71
|
+
draft(name: string): Promise<Draft | undefined>;
|
|
72
|
+
saveDraft(draft: Draft): Promise<void>;
|
|
73
|
+
/** `false` when the name had no draft. */
|
|
74
|
+
dropDraft(name: string): Promise<boolean>;
|
|
75
|
+
version(name: string, version: number): Promise<DefinitionVersion | undefined>;
|
|
76
|
+
active(name: string): Promise<DefinitionVersion | undefined>;
|
|
77
|
+
latest(name: string): Promise<DefinitionVersion | undefined>;
|
|
78
|
+
/** Newest first. */
|
|
79
|
+
versions(name: string): Promise<VersionInfo[]>;
|
|
80
|
+
/** Freezes the name's next version (latest + 1) and returns it. */
|
|
81
|
+
append(entry: NewVersion): Promise<DefinitionVersion>;
|
|
82
|
+
/** `false` when the name has no such version. */
|
|
83
|
+
activate(name: string, version: number, at: string): Promise<boolean>;
|
|
84
|
+
}
|
|
85
|
+
/** A refusal, with the reason a program reads and a message a person reads. */
|
|
86
|
+
export interface Refused<Reason extends string> {
|
|
87
|
+
ok: false;
|
|
88
|
+
reason: Reason;
|
|
89
|
+
message: string;
|
|
90
|
+
}
|
|
91
|
+
/** What `check` says of a definition: its kind, or the first thing wrong with it. */
|
|
92
|
+
export type CheckResult = {
|
|
93
|
+
ok: true;
|
|
94
|
+
kind: DefinitionKind;
|
|
95
|
+
definition: Definition;
|
|
96
|
+
} | Refused<'invalid'>;
|
|
97
|
+
export type SaveDraftResult = {
|
|
98
|
+
ok: true;
|
|
99
|
+
draft: Draft;
|
|
100
|
+
issue: string | null;
|
|
101
|
+
} | Refused<'kind-changed' | 'bad-name'>;
|
|
102
|
+
export type PublishResult = {
|
|
103
|
+
ok: true;
|
|
104
|
+
version: DefinitionVersion;
|
|
105
|
+
} | Refused<'no-draft' | 'invalid' | 'kind-changed' | 'unchanged' | 'bad-name'>;
|
|
106
|
+
export type ActivateResult = {
|
|
107
|
+
ok: true;
|
|
108
|
+
version: DefinitionVersion;
|
|
109
|
+
} | Refused<'not-found'>;
|
|
110
|
+
/** Who is doing it, kept on the draft or the version. */
|
|
111
|
+
export interface By {
|
|
112
|
+
by: string;
|
|
113
|
+
}
|
|
114
|
+
export interface DefinitionsOptions {
|
|
115
|
+
storage: DefinitionStorage;
|
|
116
|
+
/**
|
|
117
|
+
* Read for `fieldTypes` only, as `parseSchema` reads it: a field type the app registers is
|
|
118
|
+
* valid in a definition, one it does not is refused at publish.
|
|
119
|
+
*/
|
|
120
|
+
registry?: Pick<Registry, 'fieldTypes'>;
|
|
121
|
+
/**
|
|
122
|
+
* The app's own upgrade of what it stored in an older spelling, run after the library's on
|
|
123
|
+
* every read. Must be idempotent: it sees today's spelling too.
|
|
124
|
+
*/
|
|
125
|
+
upgrade?: (definition: unknown) => unknown;
|
|
126
|
+
/** The clock, for tests. */
|
|
127
|
+
now?: () => Date;
|
|
128
|
+
}
|
|
129
|
+
/** The definitions of one app. Every read comes back in today's wire spelling. */
|
|
130
|
+
export interface Definitions {
|
|
131
|
+
list(): Promise<DefinitionSummary[]>;
|
|
132
|
+
/**
|
|
133
|
+
* The version new submissions use. `D` is what the host knows the name holds
|
|
134
|
+
* (`active<typeof contactForm>('contact')`): the service does not check it.
|
|
135
|
+
*/
|
|
136
|
+
active<D extends Definition = Definition>(name: string): Promise<DefinitionVersion<D> | undefined>;
|
|
137
|
+
version<D extends Definition = Definition>(name: string, version: number): Promise<DefinitionVersion<D> | undefined>;
|
|
138
|
+
versions(name: string): Promise<VersionInfo[]>;
|
|
139
|
+
draft(name: string): Promise<Draft | undefined>;
|
|
140
|
+
/** Does it pass `parseSchema` with this app's registry? Nothing is stored. */
|
|
141
|
+
check(definition: unknown): CheckResult;
|
|
142
|
+
/** Saves the edit in progress, valid or not, and says the first thing wrong with it. */
|
|
143
|
+
saveDraft(name: string, definition: unknown, who: By): Promise<SaveDraftResult>;
|
|
144
|
+
discardDraft(name: string): Promise<boolean>;
|
|
145
|
+
/**
|
|
146
|
+
* Freezes the draft (or `definition`, when given) as the next version. `activate` makes it
|
|
147
|
+
* the one new submissions use in the same step.
|
|
148
|
+
*/
|
|
149
|
+
publish(name: string, options: By & {
|
|
150
|
+
activate?: boolean;
|
|
151
|
+
definition?: unknown;
|
|
152
|
+
}): Promise<PublishResult>;
|
|
153
|
+
/** Makes a published version the active one: a rollback is activating an older version. */
|
|
154
|
+
activate(name: string, version: number): Promise<ActivateResult>;
|
|
155
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/upgrade.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { normalizeLegacySchema } from '@20baht/engine';
|
|
2
|
+
const ASSIGNMENT_KEYS = {
|
|
3
|
+
assignTo: 'assign_to',
|
|
4
|
+
dueInHours: 'due_in_hours',
|
|
5
|
+
dueInBusinessDays: 'due_in_business_days',
|
|
6
|
+
dueInHoursBy: 'due_in_hours_by',
|
|
7
|
+
skipWhenAlreadyApproved: 'skip_when_already_approved',
|
|
8
|
+
autoApproveWhenDue: 'auto_approve_when_due',
|
|
9
|
+
};
|
|
10
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
11
|
+
function renamed(record, keys) {
|
|
12
|
+
const out = {};
|
|
13
|
+
for (const [key, value] of Object.entries(record)) {
|
|
14
|
+
const next = keys[key];
|
|
15
|
+
if (next === undefined)
|
|
16
|
+
out[key] = value;
|
|
17
|
+
else if (!Object.hasOwn(record, next))
|
|
18
|
+
out[next] = value;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
const TARGET_KEYS = { managerOfAssignee: 'manager_of_assignee' };
|
|
23
|
+
function upgradeAssignment(assignment) {
|
|
24
|
+
if (!isRecord(assignment))
|
|
25
|
+
return assignment;
|
|
26
|
+
const next = renamed(assignment, ASSIGNMENT_KEYS);
|
|
27
|
+
const targets = next.assign_to;
|
|
28
|
+
if (Array.isArray(targets)) {
|
|
29
|
+
next.assign_to = targets.map((target) => isRecord(target) ? renamed(target, TARGET_KEYS) : target);
|
|
30
|
+
}
|
|
31
|
+
return next;
|
|
32
|
+
}
|
|
33
|
+
export function upgradeDefinition(definition) {
|
|
34
|
+
const { schema } = normalizeLegacySchema(definition);
|
|
35
|
+
if (!isRecord(schema) || !Array.isArray(schema.forms))
|
|
36
|
+
return schema;
|
|
37
|
+
return {
|
|
38
|
+
...schema,
|
|
39
|
+
forms: schema.forms.map((form) => isRecord(form) && 'assignment' in form
|
|
40
|
+
? { ...form, assignment: upgradeAssignment(form.assignment) }
|
|
41
|
+
: form),
|
|
42
|
+
};
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,93 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/definitions",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=20.19.0"
|
|
7
|
+
},
|
|
8
|
+
"description": "Form and workflow definitions kept in storage: edit a draft, publish it as a numbered version that never changes, activate one version, read old JSON in today's spelling. A storage port with a Postgres adapter (drizzle-orm), typed Elysia routes and a seed from JSON files.",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Nattapong Promthong",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/bank20baht/20baht.git",
|
|
14
|
+
"directory": "packages/core/definitions"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/bank20baht/20baht/tree/main/packages/core/definitions#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/bank20baht/20baht/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"forms",
|
|
22
|
+
"workflow",
|
|
23
|
+
"definitions",
|
|
24
|
+
"versioning",
|
|
25
|
+
"drizzle",
|
|
26
|
+
"postgres",
|
|
27
|
+
"bahtui"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./pg": {
|
|
42
|
+
"types": "./dist/pg.d.ts",
|
|
43
|
+
"import": "./dist/pg.js",
|
|
44
|
+
"default": "./dist/pg.js"
|
|
45
|
+
},
|
|
46
|
+
"./elysia": {
|
|
47
|
+
"types": "./dist/elysia.d.ts",
|
|
48
|
+
"import": "./dist/elysia.js",
|
|
49
|
+
"default": "./dist/elysia.js"
|
|
50
|
+
},
|
|
51
|
+
"./files": {
|
|
52
|
+
"types": "./dist/files.d.ts",
|
|
53
|
+
"import": "./dist/files.js",
|
|
54
|
+
"default": "./dist/files.js"
|
|
55
|
+
},
|
|
56
|
+
"./package.json": "./package.json"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "rm -rf dist && tsc -p tsconfig.json --removeComments && tsc -p tsconfig.json --emitDeclarationOnly",
|
|
60
|
+
"test": "vitest run",
|
|
61
|
+
"test:watch": "vitest",
|
|
62
|
+
"test:coverage": "vitest run --coverage"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@20baht/validator": ">=0.3.11 <0.4.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"@20baht/engine": ">=0.3.11 <0.4.0",
|
|
69
|
+
"drizzle-orm": ">=0.44.0 <1.0.0",
|
|
70
|
+
"elysia": ">=1.4.0 <2.0.0"
|
|
71
|
+
},
|
|
72
|
+
"peerDependenciesMeta": {
|
|
73
|
+
"drizzle-orm": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"elysia": {
|
|
77
|
+
"optional": true
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@20baht/engine": "workspace:*",
|
|
82
|
+
"@electric-sql/pglite": "^0.3.0",
|
|
83
|
+
"@elysiajs/openapi": "^1.4.16",
|
|
84
|
+
"drizzle-kit": "^0.31.11",
|
|
85
|
+
"drizzle-orm": "^0.44.0",
|
|
86
|
+
"elysia": "^1.4.0",
|
|
87
|
+
"typescript": "^5.9.3",
|
|
88
|
+
"vitest": "^3.2.4"
|
|
89
|
+
},
|
|
90
|
+
"main": "./dist/index.js",
|
|
91
|
+
"types": "./dist/index.d.ts",
|
|
92
|
+
"sideEffects": false
|
|
93
|
+
}
|