@geonosis/document 0.5.0 → 0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @geonosis/document
2
2
 
3
+ ## 0.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - db89b0b: The core entry (`@geonosis/document`) reaches no floor: `documentStore`, `documentMigrations`,
8
+ `documentTable`, `appendOnlyTables`, `documentRoleStatements`, `HEAD_FIELDS`, `physicalColumnsOf`,
9
+ the `DEFAULT_*` names and `documentSurface` move to `@geonosis/document/store`. A consumer's DOMAIN
10
+ package imports the core for `Document.extend`/`documentEntities` and is bundled for browsers and
11
+ edge runtimes, where `@geonosis/db`'s `async_hooks`, `fs/promises` and `pg` do not exist — measured
12
+ on during.day, whose dashboard stopped compiling its sign-in page (#463). Migration: a server
13
+ package that builds the store or renders the DDL imports those names from `@geonosis/document/store`;
14
+ everything else is unchanged.
15
+
16
+ ### Patch Changes
17
+
18
+ - db89b0b: The core entry carries no node builtin: `hashOf` (sha256 over `node:crypto`) moves to
19
+ `@geonosis/document/store` beside the store that writes the chain; `canonicalOf` stays on the core.
20
+ The pre-release review of 0.6.0 measured the core still failing a browser resolve on `crypto`;
21
+ `@geonosis/document` now sits in the repo's `BROWSER_SAFE` guard, which bundle-resolves the built
22
+ core entry for a browser on every run.
23
+
3
24
  ## 0.5.0
4
25
 
5
26
  ### Minor Changes
package/README.md CHANGED
@@ -92,6 +92,10 @@ nothing.
92
92
 
93
93
  ## Store
94
94
 
95
+ `documentStore`, the DDL and the surface are the `@geonosis/document/store` entry — a server's
96
+ import. The main entry is the core alone and reaches no floor, so a domain package bundled for a
97
+ browser or an edge runtime carries neither drizzle nor pg (#463).
98
+
95
99
  `documentStore({ registry, seam, table?, schema?, columns?, revisions?, archive? })` is ONE table
96
100
  for every document entity the registry holds — never one table per entity — plus the append-only
97
101
  log beside it, and it spells no SQL: the log is `@geonosis/db`'s `defineLog` keyed by `(entity,
@@ -0,0 +1,180 @@
1
+ // src/core/body-version.ts
2
+ var BODY_VERSION_KEY = "_v";
3
+ var versionOf = (body) => {
4
+ const stamped = body[BODY_VERSION_KEY];
5
+ return typeof stamped === "number" && Number.isInteger(stamped) ? stamped : 1;
6
+ };
7
+ var migrateBody = (definition, stored) => {
8
+ if (stored === null || typeof stored !== "object" || Array.isArray(stored)) return stored;
9
+ const target = definition.bodyVersion ?? 1;
10
+ const steps = definition.migrations ?? {};
11
+ let body = stored;
12
+ for (let version = versionOf(body); version < target; version += 1) {
13
+ const step = steps[version];
14
+ if (step === void 0) break;
15
+ body = step(body);
16
+ }
17
+ const { [BODY_VERSION_KEY]: _stamp, ...withoutStamp } = body;
18
+ return withoutStamp;
19
+ };
20
+ var stampBody = (definition, body) => ({
21
+ ...body,
22
+ [BODY_VERSION_KEY]: definition.bodyVersion ?? 1
23
+ });
24
+ var documentBodyVersioning = (definition) => ({
25
+ migrate: (stored) => migrateBody(definition, stored),
26
+ stamp: (body) => stampBody(definition, body)
27
+ });
28
+
29
+ // src/core/document.ts
30
+ var fieldPatterns = {
31
+ calendarDate: /^\d{4}-\d{2}-\d{2}$/,
32
+ decimal: /^-?\d+(?:\.\d+)?$/
33
+ };
34
+ var FIELD_CHECKS = {
35
+ calendarDate: {
36
+ message: "a calendar date is `YYYY-MM-DD`, never prose",
37
+ test: (value) => typeof value === "string" && fieldPatterns.calendarDate.test(value)
38
+ },
39
+ decimal: {
40
+ message: "an amount is a decimal string, never a float",
41
+ test: (value) => typeof value === "string" && fieldPatterns.decimal.test(value)
42
+ },
43
+ int: {
44
+ message: "an integer",
45
+ test: (value) => typeof value === "number" && Number.isInteger(value)
46
+ },
47
+ positiveInt: {
48
+ message: "a positive integer",
49
+ test: (value) => typeof value === "number" && Number.isInteger(value) && value > 0
50
+ },
51
+ text: { message: "a string", test: (value) => typeof value === "string" }
52
+ };
53
+ var validatorOf = (fields, options) => {
54
+ const nullable = options?.nullable ?? false;
55
+ return {
56
+ "~standard": {
57
+ validate: (input) => {
58
+ if (input === null || typeof input !== "object") {
59
+ return { issues: [{ message: "expected an object" }] };
60
+ }
61
+ const record = input;
62
+ const issues = [];
63
+ const value = {};
64
+ for (const [key, kind] of Object.entries(fields)) {
65
+ const held = record[key];
66
+ value[key] = held;
67
+ if (nullable && held === null) continue;
68
+ if (!FIELD_CHECKS[kind].test(held)) {
69
+ issues.push({ message: FIELD_CHECKS[kind].message, path: [{ key }] });
70
+ }
71
+ }
72
+ return issues.length > 0 ? { issues } : { value };
73
+ },
74
+ vendor: "geonosis-document",
75
+ version: 1
76
+ }
77
+ };
78
+ };
79
+ var parseWith = (schema, input) => {
80
+ const result = schema["~standard"].validate(input);
81
+ if (result instanceof Promise) {
82
+ throw new Error(
83
+ "Document.head and Document.record parse synchronously; an async Standard Schema cannot back them"
84
+ );
85
+ }
86
+ if (result.issues !== void 0) {
87
+ throw new Error(result.issues.map((issue) => issue.message).join("; "));
88
+ }
89
+ return result.value;
90
+ };
91
+ var headFields = {
92
+ status: "text",
93
+ issueDate: "calendarDate",
94
+ dueDate: "calendarDate",
95
+ counterparty: "text",
96
+ externalRef: "text",
97
+ revision: "text",
98
+ total: "decimal"
99
+ };
100
+ var recordFields = {
101
+ version: "positiveInt",
102
+ createdAt: "int",
103
+ updatedAt: "int"
104
+ };
105
+ var headValidator = validatorOf(headFields, { nullable: true });
106
+ var recordValidator = validatorOf(recordFields);
107
+ var Document = {
108
+ head: headValidator,
109
+ headFields,
110
+ record: recordValidator,
111
+ recordFields,
112
+ extend: (definition) => ({
113
+ ...definition,
114
+ headOf: (body) => parseWith(headValidator, definition.headOf(body)),
115
+ is: (document) => document.entity === definition.name,
116
+ view: definition.view ?? definition.body
117
+ })
118
+ };
119
+
120
+ // src/core/entities.ts
121
+ var documentEntities = (map) => {
122
+ for (const [key, entity] of Object.entries(map)) {
123
+ if (entity.name !== key) {
124
+ throw new Error(
125
+ `documentEntities was handed "${key}" for an entity whose own name is "${entity.name}": the map's key is what a row's \`entity\` column carries, and a mismatch here is a row nothing can find again`
126
+ );
127
+ }
128
+ }
129
+ const names = Object.keys(map);
130
+ const values = Object.values(map);
131
+ const fold = (pick) => values.map(pick);
132
+ const distinctBy = (pick) => [
133
+ ...new Set(fold(pick))
134
+ ];
135
+ return {
136
+ bodies: distinctBy((entity) => entity.body),
137
+ cards: distinctBy((entity) => entity.card.schema),
138
+ entities: map,
139
+ fold,
140
+ instance: (name, parts) => ({ ...parts, entity: name, body: parts.body }),
141
+ named: (name) => map[name],
142
+ names,
143
+ views: distinctBy((entity) => entity.view)
144
+ };
145
+ };
146
+
147
+ // src/core/actor.ts
148
+ var SYSTEM_PREFIX = "system:";
149
+ var systemActor = (name) => `${SYSTEM_PREFIX}${name}`;
150
+ var isSystemActor = (actor) => actor.startsWith(SYSTEM_PREFIX);
151
+
152
+ // src/core/canonical.ts
153
+ var canonicalOf = (input) => {
154
+ if (input === null || typeof input !== "object") return input;
155
+ if (Array.isArray(input)) return input.map(canonicalOf);
156
+ return Object.fromEntries(
157
+ Object.entries(input).toSorted(([a], [b]) => a.localeCompare(b)).map(([key, held]) => [key, canonicalOf(held)])
158
+ );
159
+ };
160
+
161
+ // src/core/status.ts
162
+ var canTransition = (machine, from, to) => machine.transitions[from].includes(to);
163
+ var isTerminalStatus = (machine, status) => machine.transitions[status].length === 0;
164
+
165
+ export {
166
+ BODY_VERSION_KEY,
167
+ migrateBody,
168
+ stampBody,
169
+ documentBodyVersioning,
170
+ fieldPatterns,
171
+ validatorOf,
172
+ headFields,
173
+ Document,
174
+ documentEntities,
175
+ systemActor,
176
+ isSystemActor,
177
+ canonicalOf,
178
+ canTransition,
179
+ isTerminalStatus
180
+ };