@geonosis/document 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/CHANGELOG.md +20 -0
- package/LICENSE +202 -0
- package/README.md +84 -0
- package/dist/index.cjs +145 -0
- package/dist/index.d.cts +192 -0
- package/dist/index.d.ts +192 -0
- package/dist/index.js +110 -0
- package/package.json +62 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @geonosis/document
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- e4da607: The document floor's core: the abstract `Document` (head + record + `extend`), the derived entity
|
|
8
|
+
registry (`documentEntities`, its name union DERIVED off the map's own keys), per-entity body
|
|
9
|
+
versioning (`documentBodyVersioning`, a step declared on one entity never running on another's
|
|
10
|
+
body), the status machine (`canTransition`, `isTerminalStatus`) and `documentIdentifiers`. Engine-
|
|
11
|
+
free, SQL-free — `src/store/`, `src/workflows/`, `src/contract/`, `src/surface/`, `src/ui/` and
|
|
12
|
+
`src/conformance/` are later tickets, and the in-package wall is drawn for each of them already.
|
|
13
|
+
|
|
14
|
+
`@geonosis/doctor`'s `FLOOR_PACKAGES` names `@geonosis/document`, so the uncomposed-battery drift
|
|
15
|
+
line and `tooling/group-membership.test.ts` both ask about it the moment it is installed anywhere.
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [8e23215]
|
|
20
|
+
- @geonosis/db@0.7.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# @geonosis/document
|
|
2
|
+
|
|
3
|
+
The abstract `Document`: an entity-first base, a derived entity registry, and per-entity body
|
|
4
|
+
versioning. Nothing here opens a connection and nothing here imports an engine — this is the pure
|
|
5
|
+
core, engine-free and SQL-free by construction (the in-package wall, `layer-walls`).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @geonosis/document
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## `Document`
|
|
12
|
+
|
|
13
|
+
`Document = { head, record, extend }`. `head` is the indexed facts every document has —
|
|
14
|
+
`status`, `issueDate`, `dueDate`, `counterparty`, `externalRef`, `revision`, `total`, every one
|
|
15
|
+
nullable. Two refusals the schema makes that a looser one would not:
|
|
16
|
+
|
|
17
|
+
- `issueDate` and `dueDate` are calendar strings, `YYYY-MM-DD` or `null` — a `headOf` that answers
|
|
18
|
+
with prose is refused at the `Document.head` parse, not accepted and read wrong three files
|
|
19
|
+
later.
|
|
20
|
+
- `total` is a decimal STRING or `null` — money never crosses this boundary as a float.
|
|
21
|
+
|
|
22
|
+
`record` is `{ version, createdAt, updatedAt }` — what storage adds once a document is written.
|
|
23
|
+
|
|
24
|
+
`Document.extend(definition)` takes the shape an entity decides (`name`, `noun`, `basePath`,
|
|
25
|
+
`body`, `draft`, `incoming`, `statuses`, `numbering`, `fromDraft`, `headOf`, `searchText`,
|
|
26
|
+
`statusOf`, `withStatus`, `prepare`, `merge`, `onSent`, `copy`, `removable`, `card`, and optionally
|
|
27
|
+
`bodyVersion`/`migrations` — see body versioning below) and returns it with `is()`, a type guard
|
|
28
|
+
narrowing on the entity's own name. `InstanceOf`, `BodyOf`, `DraftOf`, `IncomingOf`, `StatusOf`,
|
|
29
|
+
`CardOf` and `NameOf` read the pieces back off an entity, generically.
|
|
30
|
+
|
|
31
|
+
## `documentEntities`
|
|
32
|
+
|
|
33
|
+
`documentEntities({ memo: Memo, receipt: Receipt, … })` takes a map of `Document.extend` results,
|
|
34
|
+
keyed by name, and:
|
|
35
|
+
|
|
36
|
+
- refuses a map whose key does not match the definition's own `name` — a mismatch here is a row
|
|
37
|
+
whose `entity` column nothing can find again;
|
|
38
|
+
- derives `names` off the map's OWN keys — never a hand-written union, so a consumer's own
|
|
39
|
+
`satisfies Record<Names, T>` map fails to compile the moment a new entity joins this one and that
|
|
40
|
+
consumer has not decided for it (the totality guarantee `keyof typeof entities` gave the
|
|
41
|
+
reference this floor was extracted from, and the reason the return type carries `names` rather
|
|
42
|
+
than a caller reading `Object.keys` and hoping);
|
|
43
|
+
- derives `nameSchema` (`z.enum`) and `bodySchema` (`z.union` over the DISTINCT declared body
|
|
44
|
+
schemas — two entities sharing a shape contribute it once) — both read off the map, never
|
|
45
|
+
restated;
|
|
46
|
+
- `bodyDecoder` is `bodySchema` run through `z.compile` once, for a store that parses every row;
|
|
47
|
+
- `instance(name, parts)` stamps a document with the entity name it was minted for; `named(name)`
|
|
48
|
+
hands back one entity's own definition.
|
|
49
|
+
|
|
50
|
+
## Per-entity body versioning
|
|
51
|
+
|
|
52
|
+
`documentBodyVersioning(definition)` returns `{ migrate, stamp }`, bound to ONE entity's own
|
|
53
|
+
`bodyVersion` and `migrations` — never a shared step table. A step declared under one entity's
|
|
54
|
+
`migrations` runs only on that entity's stored bodies; another entity's body reaching the same
|
|
55
|
+
version number runs none of it. `stamp` writes the `_v` key (`BODY_VERSION_KEY`); `migrate` reads
|
|
56
|
+
it, runs every step from the stored version up to the declared one, and strips the key again — it
|
|
57
|
+
runs BEFORE the entity's own schema parses, because an older stored shape does not satisfy today's.
|
|
58
|
+
|
|
59
|
+
## Status and numbering
|
|
60
|
+
|
|
61
|
+
`DocumentStatusMachine<Status>` names every status and what each may move to.
|
|
62
|
+
`canTransition(machine, from, to)` and `isTerminalStatus(machine, status)` read it. `DocumentNumbering<Draft>` is `{ mode: 'given' }` or `{ mode: 'counter', counter, series, format }` — the series is read off the draft BEFORE the counter moves, so a draft that cannot be numbered burns
|
|
63
|
+
nothing.
|
|
64
|
+
|
|
65
|
+
## `documentIdentifiers`
|
|
66
|
+
|
|
67
|
+
`documentIdentifiers(name)` derives every spelling a surface needs from one hyphenated entity name:
|
|
68
|
+
`camel`, `pascal`, `pluralCamel`, `snake`, `snakePlural`, `path`.
|
|
69
|
+
|
|
70
|
+
## What does not exist yet
|
|
71
|
+
|
|
72
|
+
This ticket ships `src/core/` only. `src/store/` (one table, its revisions, the entity-floor
|
|
73
|
+
composition), `src/workflows/` (the org's engine, the only internal folder that may import it),
|
|
74
|
+
`src/contract/` (the wire schemas), `src/surface/` (the procedure/route/tool mounts), `src/ui/`
|
|
75
|
+
(the view axis) and `src/conformance/` (the exam a consumer runs against their own provider) are
|
|
76
|
+
later tickets — the internal layout is declared now (and held by `layer-walls`) so each lands
|
|
77
|
+
inside the wall already drawn for it.
|
|
78
|
+
|
|
79
|
+
## One door, and what holds it
|
|
80
|
+
|
|
81
|
+
`sideEffects: false`; inside the package the in-package wall is `layer-walls`, planted in
|
|
82
|
+
`src/__fixtures__/walls/` and proved through the real binary in `src/walls.test.ts` — the core may
|
|
83
|
+
reach `@geonosis/conformance` and nothing else; only `src/workflows/` may reach `@geonosis/workflows`
|
|
84
|
+
directly (D-052 — the org's engine is a direct dependency of this domain).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BODY_VERSION_KEY: () => BODY_VERSION_KEY,
|
|
24
|
+
Document: () => Document,
|
|
25
|
+
canTransition: () => canTransition,
|
|
26
|
+
documentBodyVersioning: () => documentBodyVersioning,
|
|
27
|
+
documentEntities: () => documentEntities,
|
|
28
|
+
documentIdentifiers: () => documentIdentifiers,
|
|
29
|
+
isTerminalStatus: () => isTerminalStatus,
|
|
30
|
+
migrateBody: () => migrateBody,
|
|
31
|
+
stampBody: () => stampBody
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/core/body-version.ts
|
|
36
|
+
var BODY_VERSION_KEY = "_v";
|
|
37
|
+
var versionOf = (body) => {
|
|
38
|
+
const stamped = body[BODY_VERSION_KEY];
|
|
39
|
+
return typeof stamped === "number" && Number.isInteger(stamped) ? stamped : 1;
|
|
40
|
+
};
|
|
41
|
+
var migrateBody = (definition, stored) => {
|
|
42
|
+
if (stored === null || typeof stored !== "object" || Array.isArray(stored)) return stored;
|
|
43
|
+
const target = definition.bodyVersion ?? 1;
|
|
44
|
+
const steps = definition.migrations ?? {};
|
|
45
|
+
let body = stored;
|
|
46
|
+
for (let version = versionOf(body); version < target; version += 1) {
|
|
47
|
+
const step = steps[version];
|
|
48
|
+
if (step === void 0) break;
|
|
49
|
+
body = step(body);
|
|
50
|
+
}
|
|
51
|
+
const { [BODY_VERSION_KEY]: _stamp, ...withoutStamp } = body;
|
|
52
|
+
return withoutStamp;
|
|
53
|
+
};
|
|
54
|
+
var stampBody = (definition, body) => ({
|
|
55
|
+
...body,
|
|
56
|
+
[BODY_VERSION_KEY]: definition.bodyVersion ?? 1
|
|
57
|
+
});
|
|
58
|
+
var documentBodyVersioning = (definition) => ({
|
|
59
|
+
migrate: (stored) => migrateBody(definition, stored),
|
|
60
|
+
stamp: (body) => stampBody(definition, body)
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// src/core/document.ts
|
|
64
|
+
var import_zod = require("zod");
|
|
65
|
+
var calendarDate = import_zod.z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "a calendar date is `YYYY-MM-DD`, never prose").nullable();
|
|
66
|
+
var decimalAmount = import_zod.z.string().regex(/^-?\d+(?:\.\d+)?$/, "an amount is a decimal string, never a float").nullable();
|
|
67
|
+
var documentHeadSchema = import_zod.z.object({
|
|
68
|
+
status: import_zod.z.string().nullable(),
|
|
69
|
+
issueDate: calendarDate,
|
|
70
|
+
dueDate: calendarDate,
|
|
71
|
+
counterparty: import_zod.z.string().nullable(),
|
|
72
|
+
externalRef: import_zod.z.string().nullable(),
|
|
73
|
+
revision: import_zod.z.string().nullable(),
|
|
74
|
+
total: decimalAmount
|
|
75
|
+
});
|
|
76
|
+
var documentRecordSchema = import_zod.z.object({
|
|
77
|
+
version: import_zod.z.number().int().positive(),
|
|
78
|
+
createdAt: import_zod.z.number().int(),
|
|
79
|
+
updatedAt: import_zod.z.number().int()
|
|
80
|
+
});
|
|
81
|
+
var Document = {
|
|
82
|
+
head: documentHeadSchema,
|
|
83
|
+
record: documentRecordSchema,
|
|
84
|
+
extend: (definition) => ({
|
|
85
|
+
...definition,
|
|
86
|
+
headOf: (body) => documentHeadSchema.parse(definition.headOf(body)),
|
|
87
|
+
is: (document) => document.entity === definition.name
|
|
88
|
+
})
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/core/entities.ts
|
|
92
|
+
var import_zod2 = require("zod");
|
|
93
|
+
var documentEntities = (map) => {
|
|
94
|
+
for (const [key, entity] of Object.entries(map)) {
|
|
95
|
+
if (entity.name !== key) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`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`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const names = Object.keys(map);
|
|
102
|
+
const nameSchema = import_zod2.z.enum(names);
|
|
103
|
+
const distinctBodies = [...new Set(Object.values(map).map((entity) => entity.body))];
|
|
104
|
+
const bodySchema = distinctBodies.length === 1 ? distinctBodies[0] : import_zod2.z.union(distinctBodies);
|
|
105
|
+
const bodyDecoder = import_zod2.z.compile(bodySchema);
|
|
106
|
+
return {
|
|
107
|
+
entities: map,
|
|
108
|
+
names,
|
|
109
|
+
nameSchema,
|
|
110
|
+
bodySchema,
|
|
111
|
+
bodyDecoder,
|
|
112
|
+
instance: (name, parts) => ({ ...parts, entity: name, body: parts.body }),
|
|
113
|
+
named: (name) => map[name]
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/core/naming.ts
|
|
118
|
+
var camelOf = (name) => name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
119
|
+
var documentIdentifiers = (name) => {
|
|
120
|
+
const camel = camelOf(name);
|
|
121
|
+
return {
|
|
122
|
+
camel,
|
|
123
|
+
pascal: `${camel[0]?.toUpperCase() ?? ""}${camel.slice(1)}`,
|
|
124
|
+
pluralCamel: `${camel}s`,
|
|
125
|
+
snake: name.replaceAll("-", "_"),
|
|
126
|
+
snakePlural: `${name.replaceAll("-", "_")}s`,
|
|
127
|
+
path: `/${name}s`
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// src/core/status.ts
|
|
132
|
+
var canTransition = (machine, from, to) => machine.transitions[from].includes(to);
|
|
133
|
+
var isTerminalStatus = (machine, status) => machine.transitions[status].length === 0;
|
|
134
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
135
|
+
0 && (module.exports = {
|
|
136
|
+
BODY_VERSION_KEY,
|
|
137
|
+
Document,
|
|
138
|
+
canTransition,
|
|
139
|
+
documentBodyVersioning,
|
|
140
|
+
documentEntities,
|
|
141
|
+
documentIdentifiers,
|
|
142
|
+
isTerminalStatus,
|
|
143
|
+
migrateBody,
|
|
144
|
+
stampBody
|
|
145
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
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
|
+
/**
|
|
51
|
+
* What every document has before anything knows what kind it is — the indexed facts a list sorts
|
|
52
|
+
* and filters by. Every entity derives these from its own body (`headOf`).
|
|
53
|
+
*/
|
|
54
|
+
declare const documentHeadSchema: z.ZodObject<{
|
|
55
|
+
status: z.ZodNullable<z.ZodString>;
|
|
56
|
+
issueDate: z.ZodNullable<z.ZodString>;
|
|
57
|
+
dueDate: z.ZodNullable<z.ZodString>;
|
|
58
|
+
counterparty: z.ZodNullable<z.ZodString>;
|
|
59
|
+
externalRef: z.ZodNullable<z.ZodString>;
|
|
60
|
+
revision: z.ZodNullable<z.ZodString>;
|
|
61
|
+
total: z.ZodNullable<z.ZodString>;
|
|
62
|
+
}, z.core.$strip>;
|
|
63
|
+
type DocumentHead = z.infer<typeof documentHeadSchema>;
|
|
64
|
+
/** What storage adds: the version a history counts, and the two clocks. */
|
|
65
|
+
declare const documentRecordSchema: z.ZodObject<{
|
|
66
|
+
version: z.ZodNumber;
|
|
67
|
+
createdAt: z.ZodNumber;
|
|
68
|
+
updatedAt: z.ZodNumber;
|
|
69
|
+
}, z.core.$strip>;
|
|
70
|
+
type DocumentRecord = z.infer<typeof documentRecordSchema>;
|
|
71
|
+
/** What an entity decides when it extends `Document`; every side effect is written once over these. */
|
|
72
|
+
type DocumentDefinition<Name extends string, BodySchema extends z.ZodTypeAny, DraftSchema extends z.ZodTypeAny, IncomingSchema extends z.ZodTypeAny, Status extends string, Card, Path extends string> = {
|
|
73
|
+
name: Name;
|
|
74
|
+
noun: string;
|
|
75
|
+
basePath: Path;
|
|
76
|
+
body: BodySchema;
|
|
77
|
+
draft: DraftSchema;
|
|
78
|
+
incoming: IncomingSchema;
|
|
79
|
+
statuses: DocumentStatusMachine<Status>;
|
|
80
|
+
numbering: DocumentNumbering<z.output<DraftSchema>>;
|
|
81
|
+
/** The version this entity's stored body is at today — absent, it is 1. */
|
|
82
|
+
bodyVersion?: number;
|
|
83
|
+
/** Steps that bring an older stored body up to `bodyVersion`, keyed by the version they read. */
|
|
84
|
+
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
85
|
+
/** `number` is the MINTED number — read off the numbering before the counter moves, never a series. */
|
|
86
|
+
fromDraft: (draft: z.output<DraftSchema>, number: string) => z.output<BodySchema>;
|
|
87
|
+
headOf: (body: z.output<BodySchema>) => DocumentHead;
|
|
88
|
+
searchText: (body: z.output<BodySchema>) => {
|
|
89
|
+
body: string;
|
|
90
|
+
};
|
|
91
|
+
statusOf: (body: z.output<BodySchema>) => Status;
|
|
92
|
+
withStatus: (body: z.output<BodySchema>, status: Status, at: string) => z.output<BodySchema>;
|
|
93
|
+
prepare: (body: z.output<BodySchema>) => z.output<BodySchema>;
|
|
94
|
+
merge: (stored: z.output<BodySchema>, incoming: z.output<IncomingSchema>, at: string) => z.output<BodySchema>;
|
|
95
|
+
onSent: (body: z.output<BodySchema>) => z.output<BodySchema>;
|
|
96
|
+
copy: (body: z.output<BodySchema>) => z.output<BodySchema>;
|
|
97
|
+
removable: boolean;
|
|
98
|
+
card: (document: DocumentInstance<Name, z.output<BodySchema>>, names: {
|
|
99
|
+
counterpartyName: string;
|
|
100
|
+
}) => Card;
|
|
101
|
+
};
|
|
102
|
+
/** A stored document of one entity: the head every document has, the record's clocks, its body. */
|
|
103
|
+
type DocumentInstance<Name extends string, Body> = DocumentHead & DocumentRecord & {
|
|
104
|
+
entity: Name;
|
|
105
|
+
number: string;
|
|
106
|
+
body: Body;
|
|
107
|
+
};
|
|
108
|
+
type DocumentEntity<Name extends string, BodySchema extends z.ZodTypeAny, DraftSchema extends z.ZodTypeAny, IncomingSchema extends z.ZodTypeAny, Status extends string, Card, Path extends string> = DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, Card, Path> & {
|
|
109
|
+
is: (document: {
|
|
110
|
+
entity: string;
|
|
111
|
+
}) => document is DocumentInstance<Name, z.output<BodySchema>>;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The abstract Document. Never stored as itself — a consumer's entities extend it, and everything
|
|
115
|
+
* written against `Document` runs for every one of them.
|
|
116
|
+
*/
|
|
117
|
+
declare const Document: {
|
|
118
|
+
head: z.ZodObject<{
|
|
119
|
+
status: z.ZodNullable<z.ZodString>;
|
|
120
|
+
issueDate: z.ZodNullable<z.ZodString>;
|
|
121
|
+
dueDate: z.ZodNullable<z.ZodString>;
|
|
122
|
+
counterparty: z.ZodNullable<z.ZodString>;
|
|
123
|
+
externalRef: z.ZodNullable<z.ZodString>;
|
|
124
|
+
revision: z.ZodNullable<z.ZodString>;
|
|
125
|
+
total: z.ZodNullable<z.ZodString>;
|
|
126
|
+
}, z.core.$strip>;
|
|
127
|
+
record: z.ZodObject<{
|
|
128
|
+
version: z.ZodNumber;
|
|
129
|
+
createdAt: z.ZodNumber;
|
|
130
|
+
updatedAt: z.ZodNumber;
|
|
131
|
+
}, z.core.$strip>;
|
|
132
|
+
extend: <Name extends string, BodySchema extends z.ZodTypeAny, DraftSchema extends z.ZodTypeAny, IncomingSchema extends z.ZodTypeAny, Status extends string, Card, Path extends string>(definition: DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, Card, Path>) => DocumentEntity<Name, BodySchema, DraftSchema, IncomingSchema, Status, Card, Path>;
|
|
133
|
+
};
|
|
134
|
+
type Parts<Entity> = Entity extends DocumentDefinition<infer Name, infer BodySchema, infer DraftSchema, infer IncomingSchema, infer Status, infer Card, infer _Path> ? {
|
|
135
|
+
name: Name;
|
|
136
|
+
body: z.output<BodySchema>;
|
|
137
|
+
draft: z.output<DraftSchema>;
|
|
138
|
+
incoming: z.output<IncomingSchema>;
|
|
139
|
+
status: Status;
|
|
140
|
+
card: Card;
|
|
141
|
+
} : never;
|
|
142
|
+
type NameOf<Entity> = Parts<Entity>['name'];
|
|
143
|
+
type BodyOf<Entity> = Parts<Entity>['body'];
|
|
144
|
+
type DraftOf<Entity> = Parts<Entity>['draft'];
|
|
145
|
+
type IncomingOf<Entity> = Parts<Entity>['incoming'];
|
|
146
|
+
type StatusOf<Entity> = Parts<Entity>['status'];
|
|
147
|
+
type CardOf<Entity> = Parts<Entity>['card'];
|
|
148
|
+
type InstanceOf<Entity> = DocumentInstance<NameOf<Entity>, BodyOf<Entity>>;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 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 union. The FULL entity type stays whatever the caller passed —
|
|
153
|
+
* this is a constraint bound, not the type inference reads back — because a status machine typed
|
|
154
|
+
* to its own narrow union is not assignable to one held at `string`, and widening it here would
|
|
155
|
+
* break every entity's own `withStatus` the moment two of them met in one map.
|
|
156
|
+
*/
|
|
157
|
+
type AnyEntity = {
|
|
158
|
+
name: string;
|
|
159
|
+
body: {
|
|
160
|
+
parse: (input: unknown) => unknown;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* `documentEntities(map)`: every entity that extends `Document`, by the name its rows carry.
|
|
165
|
+
*
|
|
166
|
+
* 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, so a `satisfies Record<Names, T>` a consumer
|
|
168
|
+
* writes elsewhere refuses to compile until it decides for the new one too.
|
|
169
|
+
*/
|
|
170
|
+
declare const documentEntities: <Map extends Readonly<Record<string, AnyEntity>>>(map: Map) => {
|
|
171
|
+
entities: Map;
|
|
172
|
+
names: readonly (keyof Map & string)[];
|
|
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]>;
|
|
176
|
+
instance: <Name extends keyof Map & string>(name: Name, parts: Omit<InstanceOf<Map[Name]>, "entity" | "body"> & {
|
|
177
|
+
body: unknown;
|
|
178
|
+
}) => DocumentInstance<Name, InstanceOf<Map[Name]>["body"]>;
|
|
179
|
+
named: <Name extends keyof Map & string>(name: Name) => Map[Name];
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** The identifiers a surface spells an entity with, all derived from its one hyphenated name. */
|
|
183
|
+
declare const documentIdentifiers: (name: string) => {
|
|
184
|
+
camel: string;
|
|
185
|
+
pascal: string;
|
|
186
|
+
pluralCamel: string;
|
|
187
|
+
snake: string;
|
|
188
|
+
snakePlural: string;
|
|
189
|
+
path: string;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export { BODY_VERSION_KEY, type BodyOf, type CardOf, Document, type DocumentBodyMigrations, type DocumentBodyVersion, type DocumentDefinition, type DocumentEntity, type DocumentHead, type DocumentInstance, type DocumentNumbering, type DocumentRecord, type DocumentStatusMachine, type DraftOf, type IncomingOf, type InstanceOf, type NameOf, type StatusOf, canTransition, documentBodyVersioning, documentEntities, documentIdentifiers, isTerminalStatus, migrateBody, stampBody };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
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
|
+
/**
|
|
51
|
+
* What every document has before anything knows what kind it is — the indexed facts a list sorts
|
|
52
|
+
* and filters by. Every entity derives these from its own body (`headOf`).
|
|
53
|
+
*/
|
|
54
|
+
declare const documentHeadSchema: z.ZodObject<{
|
|
55
|
+
status: z.ZodNullable<z.ZodString>;
|
|
56
|
+
issueDate: z.ZodNullable<z.ZodString>;
|
|
57
|
+
dueDate: z.ZodNullable<z.ZodString>;
|
|
58
|
+
counterparty: z.ZodNullable<z.ZodString>;
|
|
59
|
+
externalRef: z.ZodNullable<z.ZodString>;
|
|
60
|
+
revision: z.ZodNullable<z.ZodString>;
|
|
61
|
+
total: z.ZodNullable<z.ZodString>;
|
|
62
|
+
}, z.core.$strip>;
|
|
63
|
+
type DocumentHead = z.infer<typeof documentHeadSchema>;
|
|
64
|
+
/** What storage adds: the version a history counts, and the two clocks. */
|
|
65
|
+
declare const documentRecordSchema: z.ZodObject<{
|
|
66
|
+
version: z.ZodNumber;
|
|
67
|
+
createdAt: z.ZodNumber;
|
|
68
|
+
updatedAt: z.ZodNumber;
|
|
69
|
+
}, z.core.$strip>;
|
|
70
|
+
type DocumentRecord = z.infer<typeof documentRecordSchema>;
|
|
71
|
+
/** What an entity decides when it extends `Document`; every side effect is written once over these. */
|
|
72
|
+
type DocumentDefinition<Name extends string, BodySchema extends z.ZodTypeAny, DraftSchema extends z.ZodTypeAny, IncomingSchema extends z.ZodTypeAny, Status extends string, Card, Path extends string> = {
|
|
73
|
+
name: Name;
|
|
74
|
+
noun: string;
|
|
75
|
+
basePath: Path;
|
|
76
|
+
body: BodySchema;
|
|
77
|
+
draft: DraftSchema;
|
|
78
|
+
incoming: IncomingSchema;
|
|
79
|
+
statuses: DocumentStatusMachine<Status>;
|
|
80
|
+
numbering: DocumentNumbering<z.output<DraftSchema>>;
|
|
81
|
+
/** The version this entity's stored body is at today — absent, it is 1. */
|
|
82
|
+
bodyVersion?: number;
|
|
83
|
+
/** Steps that bring an older stored body up to `bodyVersion`, keyed by the version they read. */
|
|
84
|
+
migrations?: DocumentBodyMigrations<Record<string, unknown>>;
|
|
85
|
+
/** `number` is the MINTED number — read off the numbering before the counter moves, never a series. */
|
|
86
|
+
fromDraft: (draft: z.output<DraftSchema>, number: string) => z.output<BodySchema>;
|
|
87
|
+
headOf: (body: z.output<BodySchema>) => DocumentHead;
|
|
88
|
+
searchText: (body: z.output<BodySchema>) => {
|
|
89
|
+
body: string;
|
|
90
|
+
};
|
|
91
|
+
statusOf: (body: z.output<BodySchema>) => Status;
|
|
92
|
+
withStatus: (body: z.output<BodySchema>, status: Status, at: string) => z.output<BodySchema>;
|
|
93
|
+
prepare: (body: z.output<BodySchema>) => z.output<BodySchema>;
|
|
94
|
+
merge: (stored: z.output<BodySchema>, incoming: z.output<IncomingSchema>, at: string) => z.output<BodySchema>;
|
|
95
|
+
onSent: (body: z.output<BodySchema>) => z.output<BodySchema>;
|
|
96
|
+
copy: (body: z.output<BodySchema>) => z.output<BodySchema>;
|
|
97
|
+
removable: boolean;
|
|
98
|
+
card: (document: DocumentInstance<Name, z.output<BodySchema>>, names: {
|
|
99
|
+
counterpartyName: string;
|
|
100
|
+
}) => Card;
|
|
101
|
+
};
|
|
102
|
+
/** A stored document of one entity: the head every document has, the record's clocks, its body. */
|
|
103
|
+
type DocumentInstance<Name extends string, Body> = DocumentHead & DocumentRecord & {
|
|
104
|
+
entity: Name;
|
|
105
|
+
number: string;
|
|
106
|
+
body: Body;
|
|
107
|
+
};
|
|
108
|
+
type DocumentEntity<Name extends string, BodySchema extends z.ZodTypeAny, DraftSchema extends z.ZodTypeAny, IncomingSchema extends z.ZodTypeAny, Status extends string, Card, Path extends string> = DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, Card, Path> & {
|
|
109
|
+
is: (document: {
|
|
110
|
+
entity: string;
|
|
111
|
+
}) => document is DocumentInstance<Name, z.output<BodySchema>>;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The abstract Document. Never stored as itself — a consumer's entities extend it, and everything
|
|
115
|
+
* written against `Document` runs for every one of them.
|
|
116
|
+
*/
|
|
117
|
+
declare const Document: {
|
|
118
|
+
head: z.ZodObject<{
|
|
119
|
+
status: z.ZodNullable<z.ZodString>;
|
|
120
|
+
issueDate: z.ZodNullable<z.ZodString>;
|
|
121
|
+
dueDate: z.ZodNullable<z.ZodString>;
|
|
122
|
+
counterparty: z.ZodNullable<z.ZodString>;
|
|
123
|
+
externalRef: z.ZodNullable<z.ZodString>;
|
|
124
|
+
revision: z.ZodNullable<z.ZodString>;
|
|
125
|
+
total: z.ZodNullable<z.ZodString>;
|
|
126
|
+
}, z.core.$strip>;
|
|
127
|
+
record: z.ZodObject<{
|
|
128
|
+
version: z.ZodNumber;
|
|
129
|
+
createdAt: z.ZodNumber;
|
|
130
|
+
updatedAt: z.ZodNumber;
|
|
131
|
+
}, z.core.$strip>;
|
|
132
|
+
extend: <Name extends string, BodySchema extends z.ZodTypeAny, DraftSchema extends z.ZodTypeAny, IncomingSchema extends z.ZodTypeAny, Status extends string, Card, Path extends string>(definition: DocumentDefinition<Name, BodySchema, DraftSchema, IncomingSchema, Status, Card, Path>) => DocumentEntity<Name, BodySchema, DraftSchema, IncomingSchema, Status, Card, Path>;
|
|
133
|
+
};
|
|
134
|
+
type Parts<Entity> = Entity extends DocumentDefinition<infer Name, infer BodySchema, infer DraftSchema, infer IncomingSchema, infer Status, infer Card, infer _Path> ? {
|
|
135
|
+
name: Name;
|
|
136
|
+
body: z.output<BodySchema>;
|
|
137
|
+
draft: z.output<DraftSchema>;
|
|
138
|
+
incoming: z.output<IncomingSchema>;
|
|
139
|
+
status: Status;
|
|
140
|
+
card: Card;
|
|
141
|
+
} : never;
|
|
142
|
+
type NameOf<Entity> = Parts<Entity>['name'];
|
|
143
|
+
type BodyOf<Entity> = Parts<Entity>['body'];
|
|
144
|
+
type DraftOf<Entity> = Parts<Entity>['draft'];
|
|
145
|
+
type IncomingOf<Entity> = Parts<Entity>['incoming'];
|
|
146
|
+
type StatusOf<Entity> = Parts<Entity>['status'];
|
|
147
|
+
type CardOf<Entity> = Parts<Entity>['card'];
|
|
148
|
+
type InstanceOf<Entity> = DocumentInstance<NameOf<Entity>, BodyOf<Entity>>;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 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 union. The FULL entity type stays whatever the caller passed —
|
|
153
|
+
* this is a constraint bound, not the type inference reads back — because a status machine typed
|
|
154
|
+
* to its own narrow union is not assignable to one held at `string`, and widening it here would
|
|
155
|
+
* break every entity's own `withStatus` the moment two of them met in one map.
|
|
156
|
+
*/
|
|
157
|
+
type AnyEntity = {
|
|
158
|
+
name: string;
|
|
159
|
+
body: {
|
|
160
|
+
parse: (input: unknown) => unknown;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* `documentEntities(map)`: every entity that extends `Document`, by the name its rows carry.
|
|
165
|
+
*
|
|
166
|
+
* 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, so a `satisfies Record<Names, T>` a consumer
|
|
168
|
+
* writes elsewhere refuses to compile until it decides for the new one too.
|
|
169
|
+
*/
|
|
170
|
+
declare const documentEntities: <Map extends Readonly<Record<string, AnyEntity>>>(map: Map) => {
|
|
171
|
+
entities: Map;
|
|
172
|
+
names: readonly (keyof Map & string)[];
|
|
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]>;
|
|
176
|
+
instance: <Name extends keyof Map & string>(name: Name, parts: Omit<InstanceOf<Map[Name]>, "entity" | "body"> & {
|
|
177
|
+
body: unknown;
|
|
178
|
+
}) => DocumentInstance<Name, InstanceOf<Map[Name]>["body"]>;
|
|
179
|
+
named: <Name extends keyof Map & string>(name: Name) => Map[Name];
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** The identifiers a surface spells an entity with, all derived from its one hyphenated name. */
|
|
183
|
+
declare const documentIdentifiers: (name: string) => {
|
|
184
|
+
camel: string;
|
|
185
|
+
pascal: string;
|
|
186
|
+
pluralCamel: string;
|
|
187
|
+
snake: string;
|
|
188
|
+
snakePlural: string;
|
|
189
|
+
path: string;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export { BODY_VERSION_KEY, type BodyOf, type CardOf, Document, type DocumentBodyMigrations, type DocumentBodyVersion, type DocumentDefinition, type DocumentEntity, type DocumentHead, type DocumentInstance, type DocumentNumbering, type DocumentRecord, type DocumentStatusMachine, type DraftOf, type IncomingOf, type InstanceOf, type NameOf, type StatusOf, canTransition, documentBodyVersioning, documentEntities, documentIdentifiers, isTerminalStatus, migrateBody, stampBody };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
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
|
+
import { z } from "zod";
|
|
31
|
+
var calendarDate = z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "a calendar date is `YYYY-MM-DD`, never prose").nullable();
|
|
32
|
+
var decimalAmount = z.string().regex(/^-?\d+(?:\.\d+)?$/, "an amount is a decimal string, never a float").nullable();
|
|
33
|
+
var documentHeadSchema = z.object({
|
|
34
|
+
status: z.string().nullable(),
|
|
35
|
+
issueDate: calendarDate,
|
|
36
|
+
dueDate: calendarDate,
|
|
37
|
+
counterparty: z.string().nullable(),
|
|
38
|
+
externalRef: z.string().nullable(),
|
|
39
|
+
revision: z.string().nullable(),
|
|
40
|
+
total: decimalAmount
|
|
41
|
+
});
|
|
42
|
+
var documentRecordSchema = z.object({
|
|
43
|
+
version: z.number().int().positive(),
|
|
44
|
+
createdAt: z.number().int(),
|
|
45
|
+
updatedAt: z.number().int()
|
|
46
|
+
});
|
|
47
|
+
var Document = {
|
|
48
|
+
head: documentHeadSchema,
|
|
49
|
+
record: documentRecordSchema,
|
|
50
|
+
extend: (definition) => ({
|
|
51
|
+
...definition,
|
|
52
|
+
headOf: (body) => documentHeadSchema.parse(definition.headOf(body)),
|
|
53
|
+
is: (document) => document.entity === definition.name
|
|
54
|
+
})
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/core/entities.ts
|
|
58
|
+
import { z as z2 } from "zod";
|
|
59
|
+
var documentEntities = (map) => {
|
|
60
|
+
for (const [key, entity] of Object.entries(map)) {
|
|
61
|
+
if (entity.name !== key) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`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`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const names = Object.keys(map);
|
|
68
|
+
const nameSchema = z2.enum(names);
|
|
69
|
+
const distinctBodies = [...new Set(Object.values(map).map((entity) => entity.body))];
|
|
70
|
+
const bodySchema = distinctBodies.length === 1 ? distinctBodies[0] : z2.union(distinctBodies);
|
|
71
|
+
const bodyDecoder = z2.compile(bodySchema);
|
|
72
|
+
return {
|
|
73
|
+
entities: map,
|
|
74
|
+
names,
|
|
75
|
+
nameSchema,
|
|
76
|
+
bodySchema,
|
|
77
|
+
bodyDecoder,
|
|
78
|
+
instance: (name, parts) => ({ ...parts, entity: name, body: parts.body }),
|
|
79
|
+
named: (name) => map[name]
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/core/naming.ts
|
|
84
|
+
var camelOf = (name) => name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
85
|
+
var documentIdentifiers = (name) => {
|
|
86
|
+
const camel = camelOf(name);
|
|
87
|
+
return {
|
|
88
|
+
camel,
|
|
89
|
+
pascal: `${camel[0]?.toUpperCase() ?? ""}${camel.slice(1)}`,
|
|
90
|
+
pluralCamel: `${camel}s`,
|
|
91
|
+
snake: name.replaceAll("-", "_"),
|
|
92
|
+
snakePlural: `${name.replaceAll("-", "_")}s`,
|
|
93
|
+
path: `/${name}s`
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// src/core/status.ts
|
|
98
|
+
var canTransition = (machine, from, to) => machine.transitions[from].includes(to);
|
|
99
|
+
var isTerminalStatus = (machine, status) => machine.transitions[status].length === 0;
|
|
100
|
+
export {
|
|
101
|
+
BODY_VERSION_KEY,
|
|
102
|
+
Document,
|
|
103
|
+
canTransition,
|
|
104
|
+
documentBodyVersioning,
|
|
105
|
+
documentEntities,
|
|
106
|
+
documentIdentifiers,
|
|
107
|
+
isTerminalStatus,
|
|
108
|
+
migrateBody,
|
|
109
|
+
stampBody
|
|
110
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@geonosis/document",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"types": "./dist/index.d.ts",
|
|
5
|
+
"description": "The abstract Document: an entity-first base every kind of paper-trail record extends, a derived entity registry whose name union keeps every consumer map total, and per-entity body versioning — engine-free, SQL-free, the pure core the store, the workflows and the wire contract build on.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"document",
|
|
8
|
+
"entity",
|
|
9
|
+
"workflow",
|
|
10
|
+
"versioning",
|
|
11
|
+
"totality"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/microcompanies/geonosis/tree/main/domains/document",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/microcompanies/geonosis.git",
|
|
17
|
+
"directory": "domains/document"
|
|
18
|
+
},
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "dist/index.cjs",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"require": {
|
|
30
|
+
"types": "./dist/index.d.cts",
|
|
31
|
+
"default": "./dist/index.cjs"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"CHANGELOG.md",
|
|
37
|
+
"dist",
|
|
38
|
+
"migrations"
|
|
39
|
+
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@geonosis/conformance": "^0.3.0",
|
|
42
|
+
"@geonosis/events": "^2.1.0",
|
|
43
|
+
"@geonosis/workflows": "^0.4.0",
|
|
44
|
+
"@geonosis/db": "^0.7.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"zod": ">=4.5.2"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"zod": "^4.5.2"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=22"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsup",
|
|
60
|
+
"typecheck": "tsc --noEmit"
|
|
61
|
+
}
|
|
62
|
+
}
|