@geonosis/document 0.1.1 → 0.3.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 +152 -0
- package/README.md +180 -23
- package/dist/index.cjs +1223 -33
- package/dist/index.d.cts +405 -73
- package/dist/index.d.ts +405 -73
- package/dist/index.js +1217 -32
- package/migrations/0001_documents.sql +56 -0
- package/migrations/0002_documents_policies.sql +15 -0
- package/migrations/0003_document_revisions.sql +16 -0
- package/migrations/0004_document_revisions_policies.sql +23 -0
- package/package.json +7 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
export { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
import { EntityColumns, EntityName, SessionSettings, EntityMigration, Projection, Executor, Query, EntityUndo, SessionSeam } from '@geonosis/db';
|
|
4
|
+
export { ENTITY_PLACEHOLDERS as DOCUMENT_MIGRATION_PLACEHOLDERS } from '@geonosis/db';
|
|
2
5
|
|
|
3
6
|
declare const BODY_VERSION_KEY = "_v";
|
|
4
7
|
type StoredBody = Record<string, unknown>;
|
|
@@ -47,29 +50,33 @@ type DocumentStatusMachine<Status extends string> = {
|
|
|
47
50
|
declare const canTransition: <Status extends string>(machine: DocumentStatusMachine<Status>, from: Status, to: Status) => boolean;
|
|
48
51
|
declare const isTerminalStatus: <Status extends string>(machine: DocumentStatusMachine<Status>, status: Status) => boolean;
|
|
49
52
|
|
|
53
|
+
/** The five checks `Document.head` and `Document.record` are built from — never a sixth kind. */
|
|
54
|
+
type FieldKind = 'calendarDate' | 'decimal' | 'int' | 'positiveInt' | 'text';
|
|
55
|
+
/** 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). */
|
|
56
|
+
declare const fieldPatterns: Record<'calendarDate' | 'decimal', RegExp>;
|
|
50
57
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
58
|
+
* A `StandardSchemaV1` derived from a declarative field spec — one small checker per `FieldKind`,
|
|
59
|
+
* never a second listing of the fields `Document.head`/`Document.record` already carry.
|
|
53
60
|
*/
|
|
54
|
-
declare const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
declare const validatorOf: <Output>(fields: Readonly<Record<string, FieldKind>>, options?: {
|
|
62
|
+
nullable?: boolean;
|
|
63
|
+
}) => StandardSchemaV1<unknown, Output>;
|
|
64
|
+
type DocumentHead = {
|
|
65
|
+
status: string | null;
|
|
66
|
+
issueDate: string | null;
|
|
67
|
+
dueDate: string | null;
|
|
68
|
+
counterparty: string | null;
|
|
69
|
+
externalRef: string | null;
|
|
70
|
+
revision: string | null;
|
|
71
|
+
total: string | null;
|
|
72
|
+
};
|
|
73
|
+
type DocumentRecord = {
|
|
74
|
+
version: number;
|
|
75
|
+
createdAt: number;
|
|
76
|
+
updatedAt: number;
|
|
77
|
+
};
|
|
71
78
|
/** What an entity decides when it extends `Document`; every side effect is written once over these. */
|
|
72
|
-
type DocumentDefinition<Name extends string, BodySchema extends
|
|
79
|
+
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> = {
|
|
73
80
|
name: Name;
|
|
74
81
|
noun: string;
|
|
75
82
|
basePath: Path;
|
|
@@ -77,27 +84,32 @@ type DocumentDefinition<Name extends string, BodySchema extends z.ZodTypeAny, Dr
|
|
|
77
84
|
draft: DraftSchema;
|
|
78
85
|
incoming: IncomingSchema;
|
|
79
86
|
statuses: DocumentStatusMachine<Status>;
|
|
80
|
-
numbering: DocumentNumbering<
|
|
87
|
+
numbering: DocumentNumbering<StandardSchemaV1.InferOutput<DraftSchema>>;
|
|
81
88
|
/** The version this entity's stored body is at today — absent, it is 1. */
|
|
82
89
|
bodyVersion?: number;
|
|
83
90
|
/** Steps that bring an older stored body up to `bodyVersion`, keyed by the version they read. */
|
|
84
91
|
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
85
92
|
/** `number` is the MINTED number — read off the numbering before the counter moves, never a series. */
|
|
86
|
-
fromDraft: (draft:
|
|
87
|
-
headOf: (body:
|
|
88
|
-
searchText: (body:
|
|
93
|
+
fromDraft: (draft: StandardSchemaV1.InferOutput<DraftSchema>, number: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
94
|
+
headOf: (body: StandardSchemaV1.InferOutput<BodySchema>) => DocumentHead;
|
|
95
|
+
searchText: (body: StandardSchemaV1.InferOutput<BodySchema>) => {
|
|
89
96
|
body: string;
|
|
90
97
|
};
|
|
91
|
-
statusOf: (body:
|
|
92
|
-
withStatus: (body:
|
|
93
|
-
prepare: (body:
|
|
94
|
-
merge: (stored:
|
|
95
|
-
onSent: (body:
|
|
96
|
-
copy: (body:
|
|
98
|
+
statusOf: (body: StandardSchemaV1.InferOutput<BodySchema>) => Status;
|
|
99
|
+
withStatus: (body: StandardSchemaV1.InferOutput<BodySchema>, status: Status, at: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
100
|
+
prepare: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
101
|
+
merge: (stored: StandardSchemaV1.InferOutput<BodySchema>, incoming: StandardSchemaV1.InferOutput<IncomingSchema>, at: string) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
102
|
+
onSent: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
103
|
+
copy: (body: StandardSchemaV1.InferOutput<BodySchema>) => StandardSchemaV1.InferOutput<BodySchema>;
|
|
97
104
|
removable: boolean;
|
|
98
|
-
card:
|
|
99
|
-
|
|
100
|
-
|
|
105
|
+
card: {
|
|
106
|
+
schema: CardSchema;
|
|
107
|
+
of: (document: DocumentInstance<Name, StandardSchemaV1.InferOutput<BodySchema>>, names: {
|
|
108
|
+
counterpartyName: string;
|
|
109
|
+
}) => StandardSchemaV1.InferOutput<CardSchema>;
|
|
110
|
+
};
|
|
111
|
+
/** What a single-document read RENDERS — absent, the body a consumer already declared. */
|
|
112
|
+
view?: ViewSchema;
|
|
101
113
|
};
|
|
102
114
|
/** A stored document of one entity: the head every document has, the record's clocks, its body. */
|
|
103
115
|
type DocumentInstance<Name extends string, Body> = DocumentHead & DocumentRecord & {
|
|
@@ -105,39 +117,43 @@ type DocumentInstance<Name extends string, Body> = DocumentHead & DocumentRecord
|
|
|
105
117
|
number: string;
|
|
106
118
|
body: Body;
|
|
107
119
|
};
|
|
108
|
-
type DocumentEntity<Name extends string, BodySchema extends
|
|
120
|
+
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> & {
|
|
109
121
|
is: (document: {
|
|
110
122
|
entity: string;
|
|
111
|
-
}) => document is DocumentInstance<Name,
|
|
123
|
+
}) => document is DocumentInstance<Name, StandardSchemaV1.InferOutput<BodySchema>>;
|
|
124
|
+
view: ViewSchema;
|
|
112
125
|
};
|
|
113
126
|
/**
|
|
114
127
|
* The abstract Document. Never stored as itself — a consumer's entities extend it, and everything
|
|
115
128
|
* written against `Document` runs for every one of them.
|
|
116
129
|
*/
|
|
117
130
|
declare const Document: {
|
|
118
|
-
head:
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
};
|
|
134
|
-
|
|
131
|
+
head: StandardSchemaV1<unknown, DocumentHead>;
|
|
132
|
+
headFields: {
|
|
133
|
+
readonly status: "text";
|
|
134
|
+
readonly issueDate: "calendarDate";
|
|
135
|
+
readonly dueDate: "calendarDate";
|
|
136
|
+
readonly counterparty: "text";
|
|
137
|
+
readonly externalRef: "text";
|
|
138
|
+
readonly revision: "text";
|
|
139
|
+
readonly total: "decimal";
|
|
140
|
+
};
|
|
141
|
+
record: StandardSchemaV1<unknown, DocumentRecord>;
|
|
142
|
+
recordFields: {
|
|
143
|
+
readonly version: "positiveInt";
|
|
144
|
+
readonly createdAt: "int";
|
|
145
|
+
readonly updatedAt: "int";
|
|
146
|
+
};
|
|
147
|
+
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>;
|
|
148
|
+
};
|
|
149
|
+
type Parts<Entity> = Entity extends DocumentDefinition<infer Name, infer BodySchema, infer DraftSchema, infer IncomingSchema, infer Status, infer CardSchema, infer _Path, infer ViewSchema> ? {
|
|
135
150
|
name: Name;
|
|
136
|
-
body:
|
|
137
|
-
draft:
|
|
138
|
-
incoming:
|
|
151
|
+
body: StandardSchemaV1.InferOutput<BodySchema>;
|
|
152
|
+
draft: StandardSchemaV1.InferOutput<DraftSchema>;
|
|
153
|
+
incoming: StandardSchemaV1.InferOutput<IncomingSchema>;
|
|
139
154
|
status: Status;
|
|
140
|
-
card:
|
|
155
|
+
card: StandardSchemaV1.InferOutput<CardSchema>;
|
|
156
|
+
view: StandardSchemaV1.InferOutput<ViewSchema>;
|
|
141
157
|
} : never;
|
|
142
158
|
type NameOf<Entity> = Parts<Entity>['name'];
|
|
143
159
|
type BodyOf<Entity> = Parts<Entity>['body'];
|
|
@@ -145,40 +161,61 @@ type DraftOf<Entity> = Parts<Entity>['draft'];
|
|
|
145
161
|
type IncomingOf<Entity> = Parts<Entity>['incoming'];
|
|
146
162
|
type StatusOf<Entity> = Parts<Entity>['status'];
|
|
147
163
|
type CardOf<Entity> = Parts<Entity>['card'];
|
|
164
|
+
type ViewOf<Entity> = Parts<Entity>['view'];
|
|
148
165
|
type InstanceOf<Entity> = DocumentInstance<NameOf<Entity>, BodyOf<Entity>>;
|
|
149
166
|
|
|
150
167
|
/**
|
|
151
168
|
* The least a map's value has to be for `documentEntities` to do its own work: a name to check
|
|
152
|
-
* against the key, a body schema to
|
|
153
|
-
* this is a constraint bound, not the type inference reads back — because a status
|
|
154
|
-
* to its own narrow union is not assignable to one held at `string`, and widening it
|
|
155
|
-
* break every entity's own `withStatus` the moment two of them met in one map.
|
|
169
|
+
* against the key, a body and a card schema to fold over. The FULL entity type stays whatever the
|
|
170
|
+
* caller passed — this is a constraint bound, not the type inference reads back — because a status
|
|
171
|
+
* machine typed to its own narrow union is not assignable to one held at `string`, and widening it
|
|
172
|
+
* here would break every entity's own `withStatus` the moment two of them met in one map.
|
|
173
|
+
*
|
|
174
|
+
* Exported for a layer that composes over the registry's OWN values generically (the store): the
|
|
175
|
+
* same minimal bound, never a second one.
|
|
156
176
|
*/
|
|
157
|
-
type
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
177
|
+
type AnyDocumentEntity = {
|
|
178
|
+
body: StandardSchemaV1;
|
|
179
|
+
card: {
|
|
180
|
+
schema: StandardSchemaV1;
|
|
161
181
|
};
|
|
182
|
+
name: string;
|
|
183
|
+
view: StandardSchemaV1;
|
|
162
184
|
};
|
|
163
185
|
/**
|
|
164
186
|
* `documentEntities(map)`: every entity that extends `Document`, by the name its rows carry.
|
|
165
187
|
*
|
|
188
|
+
* A per-entity fact is never read off a map a caller fills in per entity — `fold` reads it off
|
|
189
|
+
* every entity the registry holds, in one place, typed over the union of the map's own values.
|
|
166
190
|
* Adding an entity is one `Document.extend` call and one line in the map handed here; the returned
|
|
167
|
-
* `names` union is DERIVED from the map's own keys
|
|
168
|
-
* writes elsewhere refuses to compile until it decides for the new one too.
|
|
191
|
+
* `names` union is DERIVED from the map's own keys.
|
|
169
192
|
*/
|
|
170
|
-
declare const documentEntities: <Map extends Readonly<Record<string,
|
|
193
|
+
declare const documentEntities: <Map extends Readonly<Record<string, AnyDocumentEntity>>>(map: Map) => {
|
|
194
|
+
bodies: StandardSchemaV1[];
|
|
195
|
+
cards: StandardSchemaV1[];
|
|
171
196
|
entities: Map;
|
|
172
|
-
|
|
173
|
-
nameSchema: z.ZodEnum<{ [K in keyof Map & string]: K; }>;
|
|
174
|
-
bodySchema: z.ZodType<{ [K in keyof Map]: InstanceOf<Map[K]>["body"]; }[keyof Map]>;
|
|
175
|
-
bodyDecoder: z.ZodType<{ [K in keyof Map]: InstanceOf<Map[K]>["body"]; }[keyof Map]>;
|
|
197
|
+
fold: <Result>(pick: (entity: Map[keyof Map]) => Result) => Result[];
|
|
176
198
|
instance: <Name extends keyof Map & string>(name: Name, parts: Omit<InstanceOf<Map[Name]>, "entity" | "body"> & {
|
|
177
199
|
body: unknown;
|
|
178
200
|
}) => DocumentInstance<Name, InstanceOf<Map[Name]>["body"]>;
|
|
179
201
|
named: <Name extends keyof Map & string>(name: Name) => Map[Name];
|
|
202
|
+
names: readonly (keyof Map & string)[];
|
|
203
|
+
views: StandardSchemaV1[];
|
|
180
204
|
};
|
|
181
205
|
|
|
206
|
+
/** An unattended write's principal — `systemActor('recurring.generate')` — never a bare `'system'`. */
|
|
207
|
+
declare const systemActor: (name: string) => string;
|
|
208
|
+
declare const isSystemActor: (actor: string) => boolean;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Deep, key-order-independent: two payloads that hold the same facts hash the same. The ONE
|
|
212
|
+
* canonicaliser both sides use — a consumer's archive recomputes `hashOf` over the identical form,
|
|
213
|
+
* so a second implementation of this function is a second chain that silently stops agreeing.
|
|
214
|
+
*/
|
|
215
|
+
declare const canonicalOf: (input: unknown) => unknown;
|
|
216
|
+
/** sha256 hex over the canonical JSON of `payload` — no whitespace, keys sorted recursively. */
|
|
217
|
+
declare const hashOf: (payload: unknown) => string;
|
|
218
|
+
|
|
182
219
|
/** The identifiers a surface spells an entity with, all derived from its one hyphenated name. */
|
|
183
220
|
declare const documentIdentifiers: (name: string) => {
|
|
184
221
|
camel: string;
|
|
@@ -189,4 +226,299 @@ declare const documentIdentifiers: (name: string) => {
|
|
|
189
226
|
path: string;
|
|
190
227
|
};
|
|
191
228
|
|
|
192
|
-
|
|
229
|
+
/**
|
|
230
|
+
* The archive's own key convention: `<tenant>/<entity>/<lineage>/<version>.json` — one object per
|
|
231
|
+
* revision, named so a lineage's own history sits together and a new lineage (`succeed`) never
|
|
232
|
+
* collides with the one it replaced.
|
|
233
|
+
*/
|
|
234
|
+
type ArchivePort = {
|
|
235
|
+
head: (key: string) => Promise<{
|
|
236
|
+
hash: string;
|
|
237
|
+
} | null>;
|
|
238
|
+
put: (key: string, bytes: Uint8Array, meta: {
|
|
239
|
+
hash: string;
|
|
240
|
+
immutable: true;
|
|
241
|
+
}) => Promise<void>;
|
|
242
|
+
};
|
|
243
|
+
/** A trusted third party's timestamp over a hash — never implemented here, only declared. */
|
|
244
|
+
type TimestampPort = {
|
|
245
|
+
stamp: (hash: string) => Promise<{
|
|
246
|
+
at: number;
|
|
247
|
+
token: Uint8Array;
|
|
248
|
+
}>;
|
|
249
|
+
};
|
|
250
|
+
type LinkVerifierPort = {
|
|
251
|
+
verify: (token: string) => Promise<{
|
|
252
|
+
entity: string;
|
|
253
|
+
exp: number;
|
|
254
|
+
org: string;
|
|
255
|
+
ref: string;
|
|
256
|
+
} | null>;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
declare const DEFAULT_DOCUMENT_TABLE = "documents";
|
|
260
|
+
declare const DEFAULT_REVISIONS_TABLE = "document_revisions";
|
|
261
|
+
declare const DEFAULT_REVISION_KIND_COLUMN = "revision_kind";
|
|
262
|
+
type DocumentColumns = EntityColumns & {
|
|
263
|
+
entity?: string;
|
|
264
|
+
revisionKind?: string;
|
|
265
|
+
total?: EntityName;
|
|
266
|
+
};
|
|
267
|
+
type DocumentRevisionsOptions = {
|
|
268
|
+
table?: string;
|
|
269
|
+
};
|
|
270
|
+
type DocumentMigrationsOptions = {
|
|
271
|
+
appRole?: string;
|
|
272
|
+
columns?: DocumentColumns;
|
|
273
|
+
revisions?: DocumentRevisionsOptions | false;
|
|
274
|
+
schema?: string;
|
|
275
|
+
settings: SessionSettings;
|
|
276
|
+
table?: string;
|
|
277
|
+
};
|
|
278
|
+
type AppendOnlyTablesOptions = {
|
|
279
|
+
table?: string;
|
|
280
|
+
revisions?: DocumentRevisionsOptions | false;
|
|
281
|
+
};
|
|
282
|
+
declare const appendOnlyTables: (options?: AppendOnlyTablesOptions) => string[];
|
|
283
|
+
declare const documentRoleStatements: (appRole: string, options?: AppendOnlyTablesOptions & {
|
|
284
|
+
schema?: string;
|
|
285
|
+
}) => string[];
|
|
286
|
+
declare const documentMigrations: (options: DocumentMigrationsOptions) => EntityMigration[];
|
|
287
|
+
|
|
288
|
+
type StorableDocumentEntity = AnyDocumentEntity & {
|
|
289
|
+
bodyVersion?: number;
|
|
290
|
+
headOf: (body: never) => DocumentHead;
|
|
291
|
+
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
292
|
+
prepare: (body: never) => unknown;
|
|
293
|
+
};
|
|
294
|
+
type Registry$1<Map extends Readonly<Record<string, StorableDocumentEntity>>> = ReturnType<typeof documentEntities<Map>>;
|
|
295
|
+
type DocumentStoreOptions<Map extends Readonly<Record<string, StorableDocumentEntity>>, Key extends string> = {
|
|
296
|
+
archive?: ArchivePort;
|
|
297
|
+
columns?: DocumentColumns;
|
|
298
|
+
registry: Registry$1<Map>;
|
|
299
|
+
revisions?: DocumentRevisionsOptions | false;
|
|
300
|
+
schema?: string;
|
|
301
|
+
seam: SessionSeam<Key, Executor>;
|
|
302
|
+
table?: string;
|
|
303
|
+
};
|
|
304
|
+
type HeadColumn = keyof DocumentHead | 'entity' | 'number';
|
|
305
|
+
type HeadsOrder = {
|
|
306
|
+
column: HeadColumn;
|
|
307
|
+
direction?: 'asc' | 'desc';
|
|
308
|
+
};
|
|
309
|
+
type HeadsWhere = {
|
|
310
|
+
createdAt?: readonly [number, number];
|
|
311
|
+
status?: string;
|
|
312
|
+
};
|
|
313
|
+
type HeadsArgs = {
|
|
314
|
+
columns: readonly HeadColumn[];
|
|
315
|
+
entity?: string | readonly string[];
|
|
316
|
+
order: readonly HeadsOrder[];
|
|
317
|
+
where?: HeadsWhere;
|
|
318
|
+
};
|
|
319
|
+
type AggregateMeasure = 'count' | 'sum:total';
|
|
320
|
+
type AggregateBy = 'entity' | 'status' | {
|
|
321
|
+
bucket: 'day' | 'month' | 'week';
|
|
322
|
+
of: 'issueDate';
|
|
323
|
+
};
|
|
324
|
+
type AggregateWhere = {
|
|
325
|
+
status?: string;
|
|
326
|
+
} & ({
|
|
327
|
+
createdAt: readonly [number, number];
|
|
328
|
+
} | {
|
|
329
|
+
issueDate: readonly [string, string];
|
|
330
|
+
} | Record<string, never>);
|
|
331
|
+
type AggregateRow = {
|
|
332
|
+
by?: string;
|
|
333
|
+
count?: number;
|
|
334
|
+
sum?: string;
|
|
335
|
+
};
|
|
336
|
+
type AggregateArgs = {
|
|
337
|
+
by?: AggregateBy;
|
|
338
|
+
entities?: readonly string[];
|
|
339
|
+
entity?: string;
|
|
340
|
+
measures: readonly AggregateMeasure[];
|
|
341
|
+
where?: AggregateWhere;
|
|
342
|
+
};
|
|
343
|
+
type SortableHeadColumn = keyof DocumentHead;
|
|
344
|
+
type Item = DocumentInstance<string, unknown>;
|
|
345
|
+
type DocumentKind = 'committed' | 'reinstated' | 'retired';
|
|
346
|
+
type CommitOutput = {
|
|
347
|
+
archived?: undefined;
|
|
348
|
+
committed: false;
|
|
349
|
+
hash?: undefined;
|
|
350
|
+
lineage: number;
|
|
351
|
+
previous?: undefined;
|
|
352
|
+
ref: string;
|
|
353
|
+
version: number;
|
|
354
|
+
} | {
|
|
355
|
+
archived?: boolean;
|
|
356
|
+
committed: true;
|
|
357
|
+
hash: string;
|
|
358
|
+
lineage: number;
|
|
359
|
+
previous: {
|
|
360
|
+
hash: string;
|
|
361
|
+
version: number;
|
|
362
|
+
} | null;
|
|
363
|
+
ref: string;
|
|
364
|
+
version: number;
|
|
365
|
+
};
|
|
366
|
+
type RetireOutput = {
|
|
367
|
+
hash: string;
|
|
368
|
+
lineage: number;
|
|
369
|
+
ref: string;
|
|
370
|
+
version: number;
|
|
371
|
+
};
|
|
372
|
+
type ReinstateOutput = {
|
|
373
|
+
hash: string;
|
|
374
|
+
lineage: number;
|
|
375
|
+
ref: string;
|
|
376
|
+
version: number;
|
|
377
|
+
};
|
|
378
|
+
type HistoryEntry = {
|
|
379
|
+
actor: string;
|
|
380
|
+
at: number;
|
|
381
|
+
body: unknown;
|
|
382
|
+
hash: string;
|
|
383
|
+
kind: DocumentKind;
|
|
384
|
+
reason: string | null;
|
|
385
|
+
version: number;
|
|
386
|
+
};
|
|
387
|
+
type VerifyResult = {
|
|
388
|
+
brokenAt: number;
|
|
389
|
+
expected: string;
|
|
390
|
+
found: string;
|
|
391
|
+
ok: false;
|
|
392
|
+
} | {
|
|
393
|
+
ok: true;
|
|
394
|
+
versions: number;
|
|
395
|
+
};
|
|
396
|
+
type DocumentEntityStore<Key extends string> = Pick<Projection<unknown, Item, Key, 'ref', Executor, SortableHeadColumn, SortableHeadColumn>, 'get' | 'list' | 'listAll'> & {
|
|
397
|
+
asOf: Query<Readonly<Record<Key, string>> & {
|
|
398
|
+
at: number;
|
|
399
|
+
ref: string;
|
|
400
|
+
}, Item | null, Executor>;
|
|
401
|
+
at: Query<Readonly<Record<Key, string>> & {
|
|
402
|
+
ref: string;
|
|
403
|
+
version: number;
|
|
404
|
+
}, Item | null, Executor>;
|
|
405
|
+
commit: Query<Readonly<Record<Key, string>> & {
|
|
406
|
+
actor: string;
|
|
407
|
+
body: unknown;
|
|
408
|
+
reason?: string;
|
|
409
|
+
ref: string;
|
|
410
|
+
}, CommitOutput, Executor>;
|
|
411
|
+
history: Query<Readonly<Record<Key, string>> & {
|
|
412
|
+
lineage?: number;
|
|
413
|
+
ref: string;
|
|
414
|
+
}, HistoryEntry[], Executor>;
|
|
415
|
+
reinstate: Query<Readonly<Record<Key, string>> & {
|
|
416
|
+
actor: string;
|
|
417
|
+
reason?: string;
|
|
418
|
+
ref: string;
|
|
419
|
+
}, ReinstateOutput, Executor>;
|
|
420
|
+
retire: Query<Readonly<Record<Key, string>> & {
|
|
421
|
+
actor: string;
|
|
422
|
+
reason?: string;
|
|
423
|
+
ref: string;
|
|
424
|
+
}, RetireOutput, Executor>;
|
|
425
|
+
succeed: Query<Readonly<Record<Key, string>> & {
|
|
426
|
+
actor: string;
|
|
427
|
+
body: unknown;
|
|
428
|
+
reason?: string;
|
|
429
|
+
ref: string;
|
|
430
|
+
}, CommitOutput, Executor>;
|
|
431
|
+
undos: {
|
|
432
|
+
commit: EntityUndo<CommitOutput, Executor>;
|
|
433
|
+
retire: EntityUndo<RetireOutput, Executor>;
|
|
434
|
+
};
|
|
435
|
+
verify: Query<Readonly<Record<Key, string>> & {
|
|
436
|
+
lineage?: number;
|
|
437
|
+
ref: string;
|
|
438
|
+
}, VerifyResult, Executor>;
|
|
439
|
+
};
|
|
440
|
+
type DocumentStore<Map extends Readonly<Record<string, StorableDocumentEntity>>, Key extends string> = {
|
|
441
|
+
[Name in keyof Map & string]: DocumentEntityStore<Key>;
|
|
442
|
+
} & {
|
|
443
|
+
aggregate: Query<Readonly<Record<Key, string>> & AggregateArgs, AggregateRow[], Executor>;
|
|
444
|
+
heads: Query<Readonly<Record<Key, string>> & HeadsArgs, Readonly<Record<string, unknown>>[], Executor>;
|
|
445
|
+
rebuild: Query<Readonly<Record<Key, string>> & {
|
|
446
|
+
entity?: string;
|
|
447
|
+
}, {
|
|
448
|
+
rows: number;
|
|
449
|
+
}, Executor>;
|
|
450
|
+
};
|
|
451
|
+
declare const documentStore: <Map extends Readonly<Record<string, StorableDocumentEntity>>, Key extends string>(options: DocumentStoreOptions<Map, Key>) => DocumentStore<Map, Key>;
|
|
452
|
+
|
|
453
|
+
type SurfaceVerb = 'query' | 'mutation';
|
|
454
|
+
type SurfaceScope = 'org' | 'public' | 'token';
|
|
455
|
+
/** The row's own declaration of what a workflow (not yet landed) will bind it to. */
|
|
456
|
+
type PendingCall = {
|
|
457
|
+
pending: 'T3';
|
|
458
|
+
workflow: string;
|
|
459
|
+
};
|
|
460
|
+
/**
|
|
461
|
+
* A row as a caller calls it — the entity's own signature (D-065's `ref`, never `number`) with a
|
|
462
|
+
* leading handle, the same convention `BoundQuery` already states in `@geonosis/workflows`, made
|
|
463
|
+
* explicit here since a row is DATA before it is a function: reading `params.ref` off `never`
|
|
464
|
+
* compiles (#325) but answers nothing, so a mounting adapter narrows before it reads.
|
|
465
|
+
*/
|
|
466
|
+
type SurfaceCall = (handle: never, params: never) => Promise<unknown>;
|
|
467
|
+
type SurfaceRow = {
|
|
468
|
+
call: PendingCall | SurfaceCall;
|
|
469
|
+
input: StandardSchemaV1;
|
|
470
|
+
name: string;
|
|
471
|
+
output: StandardSchemaV1;
|
|
472
|
+
path: string;
|
|
473
|
+
scope: SurfaceScope;
|
|
474
|
+
verb: SurfaceVerb;
|
|
475
|
+
};
|
|
476
|
+
/**
|
|
477
|
+
* The three builders a validator library supplies so the table stays library-free (#383) while the
|
|
478
|
+
* union/enum/object it derives are still real schemas of THAT library — the one place a consumer's
|
|
479
|
+
* zod enters `@geonosis/document` is through this adapter, never a direct dependency.
|
|
480
|
+
*/
|
|
481
|
+
type SurfaceSchemaAdapter = {
|
|
482
|
+
enum: (values: readonly string[]) => StandardSchemaV1<unknown, string>;
|
|
483
|
+
object: (shape: Readonly<Record<string, StandardSchemaV1>>) => StandardSchemaV1;
|
|
484
|
+
union: (schemas: readonly StandardSchemaV1[]) => StandardSchemaV1;
|
|
485
|
+
};
|
|
486
|
+
/**
|
|
487
|
+
* What the table needs off an entity beyond `documentEntities`'s own minimal bound — plus exactly
|
|
488
|
+
* what `documentStore`'s own `StorableDocumentEntity` needs, so `Map` satisfies both at once and a
|
|
489
|
+
* caller never sees two overlapping bounds for the one registry it built.
|
|
490
|
+
*/
|
|
491
|
+
type SurfaceDocumentEntity = AnyDocumentEntity & {
|
|
492
|
+
basePath: string;
|
|
493
|
+
bodyVersion?: number;
|
|
494
|
+
card: {
|
|
495
|
+
of: (document: never, names: {
|
|
496
|
+
counterpartyName: string;
|
|
497
|
+
}) => unknown;
|
|
498
|
+
schema: StandardSchemaV1;
|
|
499
|
+
};
|
|
500
|
+
draft: StandardSchemaV1;
|
|
501
|
+
headOf: (body: never) => DocumentHead;
|
|
502
|
+
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
503
|
+
prepare: (body: never) => unknown;
|
|
504
|
+
statusOf: (body: never) => string;
|
|
505
|
+
statuses: DocumentStatusMachine<string>;
|
|
506
|
+
withStatus: (body: never, status: string, at: string) => unknown;
|
|
507
|
+
};
|
|
508
|
+
type Registry<Map extends Readonly<Record<string, SurfaceDocumentEntity>>> = ReturnType<typeof documentEntities<Map>>;
|
|
509
|
+
type DocumentSurfaceOptions<Map extends Readonly<Record<string, SurfaceDocumentEntity>>, Key extends string> = {
|
|
510
|
+
registry: Registry<Map>;
|
|
511
|
+
schema: SurfaceSchemaAdapter;
|
|
512
|
+
store: DocumentStore<Map, Key>;
|
|
513
|
+
};
|
|
514
|
+
/**
|
|
515
|
+
* `documentSurface({ registry, store, schema })`: the table every tRPC/REST/MCP mount is derived
|
|
516
|
+
* from — never authored by hand, and never a map a caller fills in per entity (#383). Reads and
|
|
517
|
+
* `commit`/`retire`/`reinstate`/`setStatus` bind `call` to the store already composed; the saga
|
|
518
|
+
* rows (`create`, `send`, `duplicate`, `run-started`) are declared with their own input/output and
|
|
519
|
+
* a `{ pending: 'T3', workflow }` in place of `call` — an adapter mounting the table refuses those
|
|
520
|
+
* BY NAME, never with a stub answer.
|
|
521
|
+
*/
|
|
522
|
+
declare const documentSurface: <Map extends Readonly<Record<string, SurfaceDocumentEntity>>, Key extends string>(options: DocumentSurfaceOptions<Map, Key>) => SurfaceRow[];
|
|
523
|
+
|
|
524
|
+
export { type AggregateArgs, type AggregateBy, type AggregateMeasure, type AggregateRow, type AggregateWhere, type AnyDocumentEntity, type AppendOnlyTablesOptions, type ArchivePort, BODY_VERSION_KEY, type BodyOf, type CardOf, type CommitOutput, DEFAULT_DOCUMENT_TABLE, DEFAULT_REVISIONS_TABLE, DEFAULT_REVISION_KIND_COLUMN, Document, type DocumentBodyMigrations, type DocumentBodyVersion, type DocumentColumns, type DocumentDefinition, type DocumentEntity, type DocumentHead, type DocumentInstance, type DocumentKind, type DocumentMigrationsOptions, type DocumentNumbering, type DocumentRecord, type DocumentRevisionsOptions, type DocumentStatusMachine, type DocumentStore, type DocumentStoreOptions, type DocumentSurfaceOptions, type DraftOf, type FieldKind, type HeadColumn, type HeadsArgs, type HeadsOrder, type HeadsWhere, type HistoryEntry, type IncomingOf, type InstanceOf, type LinkVerifierPort, type NameOf, type PendingCall, type ReinstateOutput, type RetireOutput, type StatusOf, type SurfaceCall, type SurfaceRow, type SurfaceSchemaAdapter, type SurfaceScope, type SurfaceVerb, type TimestampPort, type VerifyResult, type ViewOf, appendOnlyTables, canTransition, canonicalOf, documentBodyVersioning, documentEntities, documentIdentifiers, documentMigrations, documentRoleStatements, documentStore, documentSurface, fieldPatterns, hashOf, isSystemActor, isTerminalStatus, migrateBody, stampBody, systemActor, validatorOf };
|