@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 +21 -0
- package/README.md +4 -0
- package/dist/chunk-WWP7MNI7.js +180 -0
- package/dist/index.cjs +3 -1191
- package/dist/index.d.cts +2 -512
- package/dist/index.d.ts +2 -512
- package/dist/index.js +15 -1350
- package/dist/ports-D0NtPwAO.d.cts +233 -0
- package/dist/ports-D0NtPwAO.d.ts +233 -0
- package/dist/store.cjs +1352 -0
- package/dist/store.d.cts +285 -0
- package/dist/store.d.ts +285 -0
- package/dist/store.js +1200 -0
- package/package.json +16 -2
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
|
|
3
|
+
declare const BODY_VERSION_KEY = "_v";
|
|
4
|
+
type StoredBody = Record<string, unknown>;
|
|
5
|
+
/** A step declared for ONE entity's stored shape, keyed by the version it reads FROM. */
|
|
6
|
+
type DocumentBodyMigrations<Body extends StoredBody = StoredBody> = Readonly<Record<number, (body: Body) => Body>>;
|
|
7
|
+
/**
|
|
8
|
+
* What an entity declares about its own body's history: the version it is at today, and the steps
|
|
9
|
+
* that bring an older stored body up to it. Absent, an entity is at version 1 with no steps —
|
|
10
|
+
* every field here is per-entity, so a step declared on one entity's `migrations` can never run on
|
|
11
|
+
* another's body, which is the gap a single shared step table left open.
|
|
12
|
+
*/
|
|
13
|
+
type DocumentBodyVersion<Body extends StoredBody = StoredBody> = {
|
|
14
|
+
bodyVersion?: number;
|
|
15
|
+
migrations?: DocumentBodyMigrations<Body>;
|
|
16
|
+
};
|
|
17
|
+
/** Runs BEFORE the entity's schema parses: an older stored shape does not satisfy today's schema. */
|
|
18
|
+
declare const migrateBody: <Body extends StoredBody>(definition: DocumentBodyVersion<Body>, stored: unknown) => unknown;
|
|
19
|
+
declare const stampBody: <Body>(definition: {
|
|
20
|
+
bodyVersion?: number;
|
|
21
|
+
}, body: Body) => unknown;
|
|
22
|
+
/** The migrate/stamp pair, bound to ONE entity's own declared version and steps. */
|
|
23
|
+
declare const documentBodyVersioning: <Body extends StoredBody>(definition: DocumentBodyVersion<Body>) => {
|
|
24
|
+
migrate: (stored: unknown) => unknown;
|
|
25
|
+
stamp: (body: Body) => unknown;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Given by the caller, or minted from a counter. The series is read off the draft BEFORE the
|
|
30
|
+
* counter moves, so a draft that cannot be numbered burns nothing.
|
|
31
|
+
*/
|
|
32
|
+
type DocumentNumbering<Draft> = {
|
|
33
|
+
mode: 'given';
|
|
34
|
+
} | {
|
|
35
|
+
mode: 'counter';
|
|
36
|
+
counter: string;
|
|
37
|
+
series: (draft: Draft) => string;
|
|
38
|
+
format: (series: string, sequence: number) => string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** A named status machine: every status it declares, and what each may move to. */
|
|
42
|
+
type DocumentStatusMachine<Status extends string> = {
|
|
43
|
+
statuses: readonly Status[];
|
|
44
|
+
initial: Status;
|
|
45
|
+
transitions: Readonly<Record<Status, readonly Status[]>>;
|
|
46
|
+
};
|
|
47
|
+
declare const canTransition: <Status extends string>(machine: DocumentStatusMachine<Status>, from: Status, to: Status) => boolean;
|
|
48
|
+
declare const isTerminalStatus: <Status extends string>(machine: DocumentStatusMachine<Status>, status: Status) => boolean;
|
|
49
|
+
|
|
50
|
+
/** The five checks `Document.head` and `Document.record` are built from — never a sixth kind. */
|
|
51
|
+
type FieldKind = 'calendarDate' | 'decimal' | 'int' | 'positiveInt' | 'text';
|
|
52
|
+
/** The two `FieldKind`s a consumer's own schema library validates by pattern — `validatorOf`'s own regexes, on the door, so a fold names one pattern once (#409). */
|
|
53
|
+
declare const fieldPatterns: Record<'calendarDate' | 'decimal', RegExp>;
|
|
54
|
+
/**
|
|
55
|
+
* A `StandardSchemaV1` derived from a declarative field spec — one small checker per `FieldKind`,
|
|
56
|
+
* never a second listing of the fields `Document.head`/`Document.record` already carry.
|
|
57
|
+
*/
|
|
58
|
+
declare const validatorOf: <Output>(fields: Readonly<Record<string, FieldKind>>, options?: {
|
|
59
|
+
nullable?: boolean;
|
|
60
|
+
}) => StandardSchemaV1<unknown, Output>;
|
|
61
|
+
type DocumentHead = {
|
|
62
|
+
status: string | null;
|
|
63
|
+
issueDate: string | null;
|
|
64
|
+
dueDate: string | null;
|
|
65
|
+
counterparty: string | null;
|
|
66
|
+
externalRef: string | null;
|
|
67
|
+
revision: string | null;
|
|
68
|
+
total: string | null;
|
|
69
|
+
};
|
|
70
|
+
type DocumentRecord = {
|
|
71
|
+
version: number;
|
|
72
|
+
createdAt: number;
|
|
73
|
+
updatedAt: number;
|
|
74
|
+
};
|
|
75
|
+
/** What an entity decides when it extends `Document`; every side effect is written once over these. */
|
|
76
|
+
type DocumentDefinition<Name extends string, BodySchema extends StandardSchemaV1, DraftSchema extends StandardSchemaV1, IncomingSchema extends StandardSchemaV1, Status extends string, CardSchema extends StandardSchemaV1, Path extends string, ViewSchema extends StandardSchemaV1 = BodySchema> = {
|
|
77
|
+
name: Name;
|
|
78
|
+
noun: string;
|
|
79
|
+
basePath: Path;
|
|
80
|
+
body: BodySchema;
|
|
81
|
+
draft: DraftSchema;
|
|
82
|
+
incoming: IncomingSchema;
|
|
83
|
+
statuses: DocumentStatusMachine<Status>;
|
|
84
|
+
numbering: DocumentNumbering<StandardSchemaV1.InferOutput<DraftSchema>>;
|
|
85
|
+
/** The version this entity's stored body is at today — absent, it is 1. */
|
|
86
|
+
bodyVersion?: number;
|
|
87
|
+
/** Steps that bring an older stored body up to `bodyVersion`, keyed by the version they read. */
|
|
88
|
+
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
89
|
+
/** `number` is the MINTED number — read off the numbering before the counter moves, never a series. */
|
|
90
|
+
fromDraft: (draft: StandardSchemaV1.InferOutput<DraftSchema>, number: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
91
|
+
headOf: (body: StandardSchemaV1.InferOutput<BodySchema>) => DocumentHead;
|
|
92
|
+
searchText: (body: StandardSchemaV1.InferOutput<BodySchema>) => {
|
|
93
|
+
body: string;
|
|
94
|
+
};
|
|
95
|
+
statusOf: (body: StandardSchemaV1.InferOutput<BodySchema>) => Status;
|
|
96
|
+
withStatus: (body: StandardSchemaV1.InferOutput<BodySchema>, status: Status, at: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
97
|
+
prepare: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
98
|
+
merge: (stored: StandardSchemaV1.InferOutput<BodySchema>, incoming: StandardSchemaV1.InferOutput<IncomingSchema>, at: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
99
|
+
onSent: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
100
|
+
copy: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
101
|
+
removable: boolean;
|
|
102
|
+
card: {
|
|
103
|
+
schema: CardSchema;
|
|
104
|
+
of: (document: DocumentInstance<Name, StandardSchemaV1.InferOutput<BodySchema>>, names: {
|
|
105
|
+
counterpartyName: string;
|
|
106
|
+
}) => StandardSchemaV1.InferOutput<CardSchema>;
|
|
107
|
+
};
|
|
108
|
+
/** What a single-document read RENDERS — absent, the body a consumer already declared. */
|
|
109
|
+
view?: ViewSchema;
|
|
110
|
+
};
|
|
111
|
+
/** A stored document of one entity: the head every document has, the record's clocks, its body. */
|
|
112
|
+
type DocumentInstance<Name extends string, Body> = DocumentHead & DocumentRecord & {
|
|
113
|
+
entity: Name;
|
|
114
|
+
number: string;
|
|
115
|
+
body: Body;
|
|
116
|
+
};
|
|
117
|
+
type DocumentEntity<Name extends string, BodySchema extends StandardSchemaV1, DraftSchema extends StandardSchemaV1, IncomingSchema extends StandardSchemaV1, Status extends string, CardSchema extends StandardSchemaV1, Path extends string, ViewSchema extends StandardSchemaV1 = BodySchema> = DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, CardSchema, Path, ViewSchema> & {
|
|
118
|
+
is: (document: {
|
|
119
|
+
entity: string;
|
|
120
|
+
}) => document is DocumentInstance<Name, StandardSchemaV1.InferOutput<BodySchema>>;
|
|
121
|
+
view: ViewSchema;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* The abstract Document. Never stored as itself — a consumer's entities extend it, and everything
|
|
125
|
+
* written against `Document` runs for every one of them.
|
|
126
|
+
*/
|
|
127
|
+
declare const Document: {
|
|
128
|
+
head: StandardSchemaV1<unknown, DocumentHead>;
|
|
129
|
+
headFields: {
|
|
130
|
+
readonly status: "text";
|
|
131
|
+
readonly issueDate: "calendarDate";
|
|
132
|
+
readonly dueDate: "calendarDate";
|
|
133
|
+
readonly counterparty: "text";
|
|
134
|
+
readonly externalRef: "text";
|
|
135
|
+
readonly revision: "text";
|
|
136
|
+
readonly total: "decimal";
|
|
137
|
+
};
|
|
138
|
+
record: StandardSchemaV1<unknown, DocumentRecord>;
|
|
139
|
+
recordFields: {
|
|
140
|
+
readonly version: "positiveInt";
|
|
141
|
+
readonly createdAt: "int";
|
|
142
|
+
readonly updatedAt: "int";
|
|
143
|
+
};
|
|
144
|
+
extend: <Name extends string, BodySchema extends StandardSchemaV1, DraftSchema extends StandardSchemaV1, IncomingSchema extends StandardSchemaV1, Status extends string, CardSchema extends StandardSchemaV1, Path extends string, ViewSchema extends StandardSchemaV1 = BodySchema>(definition: DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, CardSchema, Path, ViewSchema>) => DocumentEntity<Name, BodySchema, DraftSchema, IncomingSchema, Status, CardSchema, Path, ViewSchema>;
|
|
145
|
+
};
|
|
146
|
+
type Parts<Entity> = Entity extends DocumentDefinition<infer Name, infer BodySchema, infer DraftSchema, infer IncomingSchema, infer Status, infer CardSchema, infer _Path, infer ViewSchema> ? {
|
|
147
|
+
name: Name;
|
|
148
|
+
body: StandardSchemaV1.InferOutput<BodySchema>;
|
|
149
|
+
draft: StandardSchemaV1.InferOutput<DraftSchema>;
|
|
150
|
+
incoming: StandardSchemaV1.InferOutput<IncomingSchema>;
|
|
151
|
+
status: Status;
|
|
152
|
+
card: StandardSchemaV1.InferOutput<CardSchema>;
|
|
153
|
+
view: StandardSchemaV1.InferOutput<ViewSchema>;
|
|
154
|
+
} : never;
|
|
155
|
+
type NameOf<Entity> = Parts<Entity>['name'];
|
|
156
|
+
type BodyOf<Entity> = Parts<Entity>['body'];
|
|
157
|
+
type DraftOf<Entity> = Parts<Entity>['draft'];
|
|
158
|
+
type IncomingOf<Entity> = Parts<Entity>['incoming'];
|
|
159
|
+
type StatusOf<Entity> = Parts<Entity>['status'];
|
|
160
|
+
type CardOf<Entity> = Parts<Entity>['card'];
|
|
161
|
+
type ViewOf<Entity> = Parts<Entity>['view'];
|
|
162
|
+
type InstanceOf<Entity> = DocumentInstance<NameOf<Entity>, BodyOf<Entity>>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The least a map's value has to be for `documentEntities` to do its own work: a name to check
|
|
166
|
+
* against the key, a body and a card schema to fold over. The FULL entity type stays whatever the
|
|
167
|
+
* caller passed — this is a constraint bound, not the type inference reads back — because a status
|
|
168
|
+
* machine typed to its own narrow union is not assignable to one held at `string`, and widening it
|
|
169
|
+
* here would break every entity's own `withStatus` the moment two of them met in one map.
|
|
170
|
+
*
|
|
171
|
+
* Exported for a layer that composes over the registry's OWN values generically (the store): the
|
|
172
|
+
* same minimal bound, never a second one.
|
|
173
|
+
*/
|
|
174
|
+
type AnyDocumentEntity = {
|
|
175
|
+
body: StandardSchemaV1;
|
|
176
|
+
card: {
|
|
177
|
+
schema: StandardSchemaV1;
|
|
178
|
+
};
|
|
179
|
+
name: string;
|
|
180
|
+
view: StandardSchemaV1;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* `documentEntities(map)`: every entity that extends `Document`, by the name its rows carry.
|
|
184
|
+
*
|
|
185
|
+
* A per-entity fact is never read off a map a caller fills in per entity — `fold` reads it off
|
|
186
|
+
* every entity the registry holds, in one place, typed over the union of the map's own values.
|
|
187
|
+
* Adding an entity is one `Document.extend` call and one line in the map handed here; the returned
|
|
188
|
+
* `names` union is DERIVED from the map's own keys.
|
|
189
|
+
*/
|
|
190
|
+
declare const documentEntities: <Map extends Readonly<Record<string, AnyDocumentEntity>>>(map: Map) => {
|
|
191
|
+
bodies: StandardSchemaV1[];
|
|
192
|
+
cards: StandardSchemaV1[];
|
|
193
|
+
entities: Map;
|
|
194
|
+
fold: <Result>(pick: (entity: Map[keyof Map]) => Result) => Result[];
|
|
195
|
+
instance: <Name extends keyof Map & string>(name: Name, parts: Omit<InstanceOf<Map[Name]>, "entity" | "body"> & {
|
|
196
|
+
body: unknown;
|
|
197
|
+
}) => DocumentInstance<Name, InstanceOf<Map[Name]>["body"]>;
|
|
198
|
+
named: <Name extends keyof Map & string>(name: Name) => Map[Name];
|
|
199
|
+
names: readonly (keyof Map & string)[];
|
|
200
|
+
views: StandardSchemaV1[];
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The archive's own key convention: `<tenant>/<entity>/<lineage>/<version>.json` — one object per
|
|
205
|
+
* revision, named so a lineage's own history sits together and a new lineage (`succeed`) never
|
|
206
|
+
* collides with the one it replaced.
|
|
207
|
+
*/
|
|
208
|
+
type ArchivePort = {
|
|
209
|
+
head: (key: string) => Promise<{
|
|
210
|
+
hash: string;
|
|
211
|
+
} | null>;
|
|
212
|
+
put: (key: string, bytes: Uint8Array, meta: {
|
|
213
|
+
hash: string;
|
|
214
|
+
immutable: true;
|
|
215
|
+
}) => Promise<void>;
|
|
216
|
+
};
|
|
217
|
+
/** A trusted third party's timestamp over a hash — never implemented here, only declared. */
|
|
218
|
+
type TimestampPort = {
|
|
219
|
+
stamp: (hash: string) => Promise<{
|
|
220
|
+
at: number;
|
|
221
|
+
token: Uint8Array;
|
|
222
|
+
}>;
|
|
223
|
+
};
|
|
224
|
+
type LinkVerifierPort = {
|
|
225
|
+
verify: (token: string) => Promise<{
|
|
226
|
+
entity: string;
|
|
227
|
+
exp: number;
|
|
228
|
+
org: string;
|
|
229
|
+
ref: string;
|
|
230
|
+
} | null>;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export { type AnyDocumentEntity as A, BODY_VERSION_KEY as B, type CardOf as C, Document as D, type FieldKind as F, type IncomingOf as I, type LinkVerifierPort as L, type NameOf as N, type StatusOf as S, type TimestampPort as T, type ViewOf as V, type ArchivePort as a, type BodyOf as b, type DocumentBodyMigrations as c, type DocumentBodyVersion as d, type DocumentDefinition as e, type DocumentEntity as f, type DocumentHead as g, type DocumentInstance as h, type DocumentNumbering as i, type DocumentRecord as j, type DocumentStatusMachine as k, type DraftOf as l, type InstanceOf as m, canTransition as n, documentBodyVersioning as o, documentEntities as p, fieldPatterns as q, isTerminalStatus as r, migrateBody as s, stampBody as t, validatorOf as v };
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
|
|
3
|
+
declare const BODY_VERSION_KEY = "_v";
|
|
4
|
+
type StoredBody = Record<string, unknown>;
|
|
5
|
+
/** A step declared for ONE entity's stored shape, keyed by the version it reads FROM. */
|
|
6
|
+
type DocumentBodyMigrations<Body extends StoredBody = StoredBody> = Readonly<Record<number, (body: Body) => Body>>;
|
|
7
|
+
/**
|
|
8
|
+
* What an entity declares about its own body's history: the version it is at today, and the steps
|
|
9
|
+
* that bring an older stored body up to it. Absent, an entity is at version 1 with no steps —
|
|
10
|
+
* every field here is per-entity, so a step declared on one entity's `migrations` can never run on
|
|
11
|
+
* another's body, which is the gap a single shared step table left open.
|
|
12
|
+
*/
|
|
13
|
+
type DocumentBodyVersion<Body extends StoredBody = StoredBody> = {
|
|
14
|
+
bodyVersion?: number;
|
|
15
|
+
migrations?: DocumentBodyMigrations<Body>;
|
|
16
|
+
};
|
|
17
|
+
/** Runs BEFORE the entity's schema parses: an older stored shape does not satisfy today's schema. */
|
|
18
|
+
declare const migrateBody: <Body extends StoredBody>(definition: DocumentBodyVersion<Body>, stored: unknown) => unknown;
|
|
19
|
+
declare const stampBody: <Body>(definition: {
|
|
20
|
+
bodyVersion?: number;
|
|
21
|
+
}, body: Body) => unknown;
|
|
22
|
+
/** The migrate/stamp pair, bound to ONE entity's own declared version and steps. */
|
|
23
|
+
declare const documentBodyVersioning: <Body extends StoredBody>(definition: DocumentBodyVersion<Body>) => {
|
|
24
|
+
migrate: (stored: unknown) => unknown;
|
|
25
|
+
stamp: (body: Body) => unknown;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Given by the caller, or minted from a counter. The series is read off the draft BEFORE the
|
|
30
|
+
* counter moves, so a draft that cannot be numbered burns nothing.
|
|
31
|
+
*/
|
|
32
|
+
type DocumentNumbering<Draft> = {
|
|
33
|
+
mode: 'given';
|
|
34
|
+
} | {
|
|
35
|
+
mode: 'counter';
|
|
36
|
+
counter: string;
|
|
37
|
+
series: (draft: Draft) => string;
|
|
38
|
+
format: (series: string, sequence: number) => string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** A named status machine: every status it declares, and what each may move to. */
|
|
42
|
+
type DocumentStatusMachine<Status extends string> = {
|
|
43
|
+
statuses: readonly Status[];
|
|
44
|
+
initial: Status;
|
|
45
|
+
transitions: Readonly<Record<Status, readonly Status[]>>;
|
|
46
|
+
};
|
|
47
|
+
declare const canTransition: <Status extends string>(machine: DocumentStatusMachine<Status>, from: Status, to: Status) => boolean;
|
|
48
|
+
declare const isTerminalStatus: <Status extends string>(machine: DocumentStatusMachine<Status>, status: Status) => boolean;
|
|
49
|
+
|
|
50
|
+
/** The five checks `Document.head` and `Document.record` are built from — never a sixth kind. */
|
|
51
|
+
type FieldKind = 'calendarDate' | 'decimal' | 'int' | 'positiveInt' | 'text';
|
|
52
|
+
/** The two `FieldKind`s a consumer's own schema library validates by pattern — `validatorOf`'s own regexes, on the door, so a fold names one pattern once (#409). */
|
|
53
|
+
declare const fieldPatterns: Record<'calendarDate' | 'decimal', RegExp>;
|
|
54
|
+
/**
|
|
55
|
+
* A `StandardSchemaV1` derived from a declarative field spec — one small checker per `FieldKind`,
|
|
56
|
+
* never a second listing of the fields `Document.head`/`Document.record` already carry.
|
|
57
|
+
*/
|
|
58
|
+
declare const validatorOf: <Output>(fields: Readonly<Record<string, FieldKind>>, options?: {
|
|
59
|
+
nullable?: boolean;
|
|
60
|
+
}) => StandardSchemaV1<unknown, Output>;
|
|
61
|
+
type DocumentHead = {
|
|
62
|
+
status: string | null;
|
|
63
|
+
issueDate: string | null;
|
|
64
|
+
dueDate: string | null;
|
|
65
|
+
counterparty: string | null;
|
|
66
|
+
externalRef: string | null;
|
|
67
|
+
revision: string | null;
|
|
68
|
+
total: string | null;
|
|
69
|
+
};
|
|
70
|
+
type DocumentRecord = {
|
|
71
|
+
version: number;
|
|
72
|
+
createdAt: number;
|
|
73
|
+
updatedAt: number;
|
|
74
|
+
};
|
|
75
|
+
/** What an entity decides when it extends `Document`; every side effect is written once over these. */
|
|
76
|
+
type DocumentDefinition<Name extends string, BodySchema extends StandardSchemaV1, DraftSchema extends StandardSchemaV1, IncomingSchema extends StandardSchemaV1, Status extends string, CardSchema extends StandardSchemaV1, Path extends string, ViewSchema extends StandardSchemaV1 = BodySchema> = {
|
|
77
|
+
name: Name;
|
|
78
|
+
noun: string;
|
|
79
|
+
basePath: Path;
|
|
80
|
+
body: BodySchema;
|
|
81
|
+
draft: DraftSchema;
|
|
82
|
+
incoming: IncomingSchema;
|
|
83
|
+
statuses: DocumentStatusMachine<Status>;
|
|
84
|
+
numbering: DocumentNumbering<StandardSchemaV1.InferOutput<DraftSchema>>;
|
|
85
|
+
/** The version this entity's stored body is at today — absent, it is 1. */
|
|
86
|
+
bodyVersion?: number;
|
|
87
|
+
/** Steps that bring an older stored body up to `bodyVersion`, keyed by the version they read. */
|
|
88
|
+
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
89
|
+
/** `number` is the MINTED number — read off the numbering before the counter moves, never a series. */
|
|
90
|
+
fromDraft: (draft: StandardSchemaV1.InferOutput<DraftSchema>, number: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
91
|
+
headOf: (body: StandardSchemaV1.InferOutput<BodySchema>) => DocumentHead;
|
|
92
|
+
searchText: (body: StandardSchemaV1.InferOutput<BodySchema>) => {
|
|
93
|
+
body: string;
|
|
94
|
+
};
|
|
95
|
+
statusOf: (body: StandardSchemaV1.InferOutput<BodySchema>) => Status;
|
|
96
|
+
withStatus: (body: StandardSchemaV1.InferOutput<BodySchema>, status: Status, at: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
97
|
+
prepare: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
98
|
+
merge: (stored: StandardSchemaV1.InferOutput<BodySchema>, incoming: StandardSchemaV1.InferOutput<IncomingSchema>, at: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
99
|
+
onSent: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
100
|
+
copy: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
101
|
+
removable: boolean;
|
|
102
|
+
card: {
|
|
103
|
+
schema: CardSchema;
|
|
104
|
+
of: (document: DocumentInstance<Name, StandardSchemaV1.InferOutput<BodySchema>>, names: {
|
|
105
|
+
counterpartyName: string;
|
|
106
|
+
}) => StandardSchemaV1.InferOutput<CardSchema>;
|
|
107
|
+
};
|
|
108
|
+
/** What a single-document read RENDERS — absent, the body a consumer already declared. */
|
|
109
|
+
view?: ViewSchema;
|
|
110
|
+
};
|
|
111
|
+
/** A stored document of one entity: the head every document has, the record's clocks, its body. */
|
|
112
|
+
type DocumentInstance<Name extends string, Body> = DocumentHead & DocumentRecord & {
|
|
113
|
+
entity: Name;
|
|
114
|
+
number: string;
|
|
115
|
+
body: Body;
|
|
116
|
+
};
|
|
117
|
+
type DocumentEntity<Name extends string, BodySchema extends StandardSchemaV1, DraftSchema extends StandardSchemaV1, IncomingSchema extends StandardSchemaV1, Status extends string, CardSchema extends StandardSchemaV1, Path extends string, ViewSchema extends StandardSchemaV1 = BodySchema> = DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, CardSchema, Path, ViewSchema> & {
|
|
118
|
+
is: (document: {
|
|
119
|
+
entity: string;
|
|
120
|
+
}) => document is DocumentInstance<Name, StandardSchemaV1.InferOutput<BodySchema>>;
|
|
121
|
+
view: ViewSchema;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* The abstract Document. Never stored as itself — a consumer's entities extend it, and everything
|
|
125
|
+
* written against `Document` runs for every one of them.
|
|
126
|
+
*/
|
|
127
|
+
declare const Document: {
|
|
128
|
+
head: StandardSchemaV1<unknown, DocumentHead>;
|
|
129
|
+
headFields: {
|
|
130
|
+
readonly status: "text";
|
|
131
|
+
readonly issueDate: "calendarDate";
|
|
132
|
+
readonly dueDate: "calendarDate";
|
|
133
|
+
readonly counterparty: "text";
|
|
134
|
+
readonly externalRef: "text";
|
|
135
|
+
readonly revision: "text";
|
|
136
|
+
readonly total: "decimal";
|
|
137
|
+
};
|
|
138
|
+
record: StandardSchemaV1<unknown, DocumentRecord>;
|
|
139
|
+
recordFields: {
|
|
140
|
+
readonly version: "positiveInt";
|
|
141
|
+
readonly createdAt: "int";
|
|
142
|
+
readonly updatedAt: "int";
|
|
143
|
+
};
|
|
144
|
+
extend: <Name extends string, BodySchema extends StandardSchemaV1, DraftSchema extends StandardSchemaV1, IncomingSchema extends StandardSchemaV1, Status extends string, CardSchema extends StandardSchemaV1, Path extends string, ViewSchema extends StandardSchemaV1 = BodySchema>(definition: DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, CardSchema, Path, ViewSchema>) => DocumentEntity<Name, BodySchema, DraftSchema, IncomingSchema, Status, CardSchema, Path, ViewSchema>;
|
|
145
|
+
};
|
|
146
|
+
type Parts<Entity> = Entity extends DocumentDefinition<infer Name, infer BodySchema, infer DraftSchema, infer IncomingSchema, infer Status, infer CardSchema, infer _Path, infer ViewSchema> ? {
|
|
147
|
+
name: Name;
|
|
148
|
+
body: StandardSchemaV1.InferOutput<BodySchema>;
|
|
149
|
+
draft: StandardSchemaV1.InferOutput<DraftSchema>;
|
|
150
|
+
incoming: StandardSchemaV1.InferOutput<IncomingSchema>;
|
|
151
|
+
status: Status;
|
|
152
|
+
card: StandardSchemaV1.InferOutput<CardSchema>;
|
|
153
|
+
view: StandardSchemaV1.InferOutput<ViewSchema>;
|
|
154
|
+
} : never;
|
|
155
|
+
type NameOf<Entity> = Parts<Entity>['name'];
|
|
156
|
+
type BodyOf<Entity> = Parts<Entity>['body'];
|
|
157
|
+
type DraftOf<Entity> = Parts<Entity>['draft'];
|
|
158
|
+
type IncomingOf<Entity> = Parts<Entity>['incoming'];
|
|
159
|
+
type StatusOf<Entity> = Parts<Entity>['status'];
|
|
160
|
+
type CardOf<Entity> = Parts<Entity>['card'];
|
|
161
|
+
type ViewOf<Entity> = Parts<Entity>['view'];
|
|
162
|
+
type InstanceOf<Entity> = DocumentInstance<NameOf<Entity>, BodyOf<Entity>>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The least a map's value has to be for `documentEntities` to do its own work: a name to check
|
|
166
|
+
* against the key, a body and a card schema to fold over. The FULL entity type stays whatever the
|
|
167
|
+
* caller passed — this is a constraint bound, not the type inference reads back — because a status
|
|
168
|
+
* machine typed to its own narrow union is not assignable to one held at `string`, and widening it
|
|
169
|
+
* here would break every entity's own `withStatus` the moment two of them met in one map.
|
|
170
|
+
*
|
|
171
|
+
* Exported for a layer that composes over the registry's OWN values generically (the store): the
|
|
172
|
+
* same minimal bound, never a second one.
|
|
173
|
+
*/
|
|
174
|
+
type AnyDocumentEntity = {
|
|
175
|
+
body: StandardSchemaV1;
|
|
176
|
+
card: {
|
|
177
|
+
schema: StandardSchemaV1;
|
|
178
|
+
};
|
|
179
|
+
name: string;
|
|
180
|
+
view: StandardSchemaV1;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* `documentEntities(map)`: every entity that extends `Document`, by the name its rows carry.
|
|
184
|
+
*
|
|
185
|
+
* A per-entity fact is never read off a map a caller fills in per entity — `fold` reads it off
|
|
186
|
+
* every entity the registry holds, in one place, typed over the union of the map's own values.
|
|
187
|
+
* Adding an entity is one `Document.extend` call and one line in the map handed here; the returned
|
|
188
|
+
* `names` union is DERIVED from the map's own keys.
|
|
189
|
+
*/
|
|
190
|
+
declare const documentEntities: <Map extends Readonly<Record<string, AnyDocumentEntity>>>(map: Map) => {
|
|
191
|
+
bodies: StandardSchemaV1[];
|
|
192
|
+
cards: StandardSchemaV1[];
|
|
193
|
+
entities: Map;
|
|
194
|
+
fold: <Result>(pick: (entity: Map[keyof Map]) => Result) => Result[];
|
|
195
|
+
instance: <Name extends keyof Map & string>(name: Name, parts: Omit<InstanceOf<Map[Name]>, "entity" | "body"> & {
|
|
196
|
+
body: unknown;
|
|
197
|
+
}) => DocumentInstance<Name, InstanceOf<Map[Name]>["body"]>;
|
|
198
|
+
named: <Name extends keyof Map & string>(name: Name) => Map[Name];
|
|
199
|
+
names: readonly (keyof Map & string)[];
|
|
200
|
+
views: StandardSchemaV1[];
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The archive's own key convention: `<tenant>/<entity>/<lineage>/<version>.json` — one object per
|
|
205
|
+
* revision, named so a lineage's own history sits together and a new lineage (`succeed`) never
|
|
206
|
+
* collides with the one it replaced.
|
|
207
|
+
*/
|
|
208
|
+
type ArchivePort = {
|
|
209
|
+
head: (key: string) => Promise<{
|
|
210
|
+
hash: string;
|
|
211
|
+
} | null>;
|
|
212
|
+
put: (key: string, bytes: Uint8Array, meta: {
|
|
213
|
+
hash: string;
|
|
214
|
+
immutable: true;
|
|
215
|
+
}) => Promise<void>;
|
|
216
|
+
};
|
|
217
|
+
/** A trusted third party's timestamp over a hash — never implemented here, only declared. */
|
|
218
|
+
type TimestampPort = {
|
|
219
|
+
stamp: (hash: string) => Promise<{
|
|
220
|
+
at: number;
|
|
221
|
+
token: Uint8Array;
|
|
222
|
+
}>;
|
|
223
|
+
};
|
|
224
|
+
type LinkVerifierPort = {
|
|
225
|
+
verify: (token: string) => Promise<{
|
|
226
|
+
entity: string;
|
|
227
|
+
exp: number;
|
|
228
|
+
org: string;
|
|
229
|
+
ref: string;
|
|
230
|
+
} | null>;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export { type AnyDocumentEntity as A, BODY_VERSION_KEY as B, type CardOf as C, Document as D, type FieldKind as F, type IncomingOf as I, type LinkVerifierPort as L, type NameOf as N, type StatusOf as S, type TimestampPort as T, type ViewOf as V, type ArchivePort as a, type BodyOf as b, type DocumentBodyMigrations as c, type DocumentBodyVersion as d, type DocumentDefinition as e, type DocumentEntity as f, type DocumentHead as g, type DocumentInstance as h, type DocumentNumbering as i, type DocumentRecord as j, type DocumentStatusMachine as k, type DraftOf as l, type InstanceOf as m, canTransition as n, documentBodyVersioning as o, documentEntities as p, fieldPatterns as q, isTerminalStatus as r, migrateBody as s, stampBody as t, validatorOf as v };
|