@molecule/api-resource-version-history 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +111 -24
- package/README.md +309 -305
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -1
- package/src/__setup__/versions.sql +16 -0
package/LICENSE
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
Apache License
|
|
2
3
|
Version 2.0, January 2004
|
|
3
4
|
http://www.apache.org/licenses/
|
|
@@ -34,21 +35,33 @@
|
|
|
34
35
|
|
|
35
36
|
"Work" shall mean the work of authorship, whether in Source or
|
|
36
37
|
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
38
40
|
|
|
39
41
|
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
42
|
form, that is based on (or derived from) the Work and for which the
|
|
41
43
|
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
-
represent, as a whole, an original work of authorship.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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."
|
|
49
62
|
|
|
50
63
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
-
on behalf of whom a Contribution has been received by
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
52
65
|
subsequently incorporated within the Work.
|
|
53
66
|
|
|
54
67
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
@@ -61,8 +74,18 @@
|
|
|
61
74
|
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
75
|
this License, each Contributor hereby grants to You a perpetual,
|
|
63
76
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
-
|
|
65
|
-
and otherwise transfer the Work
|
|
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.
|
|
66
89
|
|
|
67
90
|
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
91
|
Work or Derivative Works thereof in any medium, with or without
|
|
@@ -84,23 +107,87 @@
|
|
|
84
107
|
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
108
|
distribution, then any Derivative Works that You distribute must
|
|
86
109
|
include a readable copy of the attribution notices contained
|
|
87
|
-
within such NOTICE file
|
|
88
|
-
|
|
89
|
-
|
|
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.
|
|
90
138
|
|
|
91
139
|
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
-
names, trademarks, service marks, or product names of the Licensor
|
|
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.
|
|
93
143
|
|
|
94
144
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
-
agreed to in writing, Licensor provides the Work
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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]
|
|
104
191
|
|
|
105
192
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
193
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-10-16T05:33:35.111Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-version-history
|
|
@@ -75,52 +75,20 @@ npm install @molecule/api-resource-version-history @molecule/api-database @molec
|
|
|
75
75
|
|
|
76
76
|
### Interfaces
|
|
77
77
|
|
|
78
|
-
#### `
|
|
78
|
+
#### `VersionOwnershipContext`
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
The context an ownership resolver receives to decide access. `userId` is the
|
|
81
|
+
authenticated caller (re-derived from `res.locals.session.userId`, never
|
|
82
|
+
client-supplied).
|
|
81
83
|
|
|
82
84
|
```typescript
|
|
83
|
-
interface
|
|
84
|
-
/** The
|
|
85
|
+
interface VersionOwnershipContext {
|
|
86
|
+
/** The parent resource type (e.g. `'document'`, `'project'`). */
|
|
85
87
|
resourceType: string
|
|
86
|
-
/** The
|
|
88
|
+
/** The parent resource id whose versions are being accessed. */
|
|
87
89
|
resourceId: string
|
|
88
|
-
/**
|
|
89
|
-
|
|
90
|
-
/** ID of the user creating this version, or `null` for system-generated versions. */
|
|
91
|
-
userId?: string | null
|
|
92
|
-
/** Optional human-readable reason. */
|
|
93
|
-
reason?: string | null
|
|
94
|
-
}
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
#### `PaginatedResult`
|
|
98
|
-
|
|
99
|
-
A paginated result set.
|
|
100
|
-
|
|
101
|
-
```typescript
|
|
102
|
-
interface PaginatedResult<T> {
|
|
103
|
-
/** The result items for the current page. */
|
|
104
|
-
data: T[]
|
|
105
|
-
/** Total number of matching items across all pages. */
|
|
106
|
-
total: number
|
|
107
|
-
/** Maximum number of results per page. */
|
|
108
|
-
limit: number
|
|
109
|
-
/** Number of results skipped. */
|
|
110
|
-
offset: number
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
#### `PaginationOptions`
|
|
115
|
-
|
|
116
|
-
Options for paginated queries.
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
interface PaginationOptions {
|
|
120
|
-
/** Maximum number of results to return. */
|
|
121
|
-
limit?: number
|
|
122
|
-
/** Number of results to skip. */
|
|
123
|
-
offset?: number
|
|
90
|
+
/** The authenticated caller's user id. */
|
|
91
|
+
userId: string
|
|
124
92
|
}
|
|
125
93
|
```
|
|
126
94
|
|
|
@@ -154,21 +122,6 @@ interface Version {
|
|
|
154
122
|
}
|
|
155
123
|
```
|
|
156
124
|
|
|
157
|
-
#### `VersionDiff`
|
|
158
|
-
|
|
159
|
-
Result of comparing two versions of the same resource.
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
interface VersionDiff {
|
|
163
|
-
/** The earlier version. */
|
|
164
|
-
from: Version
|
|
165
|
-
/** The later version. */
|
|
166
|
-
to: Version
|
|
167
|
-
/** Per-field changes between `from.snapshot` and `to.snapshot`. */
|
|
168
|
-
changes: VersionChanges
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
125
|
#### `VersionFieldChange`
|
|
173
126
|
|
|
174
127
|
Per-field change record for a single key. `before` is the previous value,
|
|
@@ -183,42 +136,72 @@ interface VersionFieldChange {
|
|
|
183
136
|
}
|
|
184
137
|
```
|
|
185
138
|
|
|
186
|
-
#### `
|
|
139
|
+
#### `CreateVersionInput`
|
|
187
140
|
|
|
188
|
-
|
|
189
|
-
authenticated caller (re-derived from `res.locals.session.userId`, never
|
|
190
|
-
client-supplied).
|
|
141
|
+
Input for creating a new version.
|
|
191
142
|
|
|
192
143
|
```typescript
|
|
193
|
-
interface
|
|
194
|
-
/** The
|
|
144
|
+
interface CreateVersionInput {
|
|
145
|
+
/** The type of resource being versioned. */
|
|
195
146
|
resourceType: string
|
|
196
|
-
/** The
|
|
147
|
+
/** The ID of the resource being versioned. */
|
|
197
148
|
resourceId: string
|
|
198
|
-
/**
|
|
199
|
-
|
|
149
|
+
/** Full snapshot of the resource. */
|
|
150
|
+
snapshot: JSONValue
|
|
151
|
+
/** ID of the user creating this version, or `null` for system-generated versions. */
|
|
152
|
+
userId?: string | null
|
|
153
|
+
/** Optional human-readable reason. */
|
|
154
|
+
reason?: string | null
|
|
200
155
|
}
|
|
201
156
|
```
|
|
202
157
|
|
|
203
|
-
|
|
158
|
+
#### `VersionDiff`
|
|
204
159
|
|
|
205
|
-
|
|
160
|
+
Result of comparing two versions of the same resource.
|
|
206
161
|
|
|
207
|
-
|
|
208
|
-
|
|
162
|
+
```typescript
|
|
163
|
+
interface VersionDiff {
|
|
164
|
+
/** The earlier version. */
|
|
165
|
+
from: Version
|
|
166
|
+
/** The later version. */
|
|
167
|
+
to: Version
|
|
168
|
+
/** Per-field changes between `from.snapshot` and `to.snapshot`. */
|
|
169
|
+
changes: VersionChanges
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### `PaginationOptions`
|
|
174
|
+
|
|
175
|
+
Options for paginated queries.
|
|
209
176
|
|
|
210
177
|
```typescript
|
|
211
|
-
|
|
178
|
+
interface PaginationOptions {
|
|
179
|
+
/** Maximum number of results to return. */
|
|
180
|
+
limit?: number
|
|
181
|
+
/** Number of results to skip. */
|
|
182
|
+
offset?: number
|
|
183
|
+
}
|
|
212
184
|
```
|
|
213
185
|
|
|
214
|
-
#### `
|
|
186
|
+
#### `PaginatedResult`
|
|
215
187
|
|
|
216
|
-
|
|
188
|
+
A paginated result set.
|
|
217
189
|
|
|
218
190
|
```typescript
|
|
219
|
-
|
|
191
|
+
interface PaginatedResult<T> {
|
|
192
|
+
/** The result items for the current page. */
|
|
193
|
+
data: T[]
|
|
194
|
+
/** Total number of matching items across all pages. */
|
|
195
|
+
total: number
|
|
196
|
+
/** Maximum number of results per page. */
|
|
197
|
+
limit: number
|
|
198
|
+
/** Number of results skipped. */
|
|
199
|
+
offset: number
|
|
200
|
+
}
|
|
220
201
|
```
|
|
221
202
|
|
|
203
|
+
### Types
|
|
204
|
+
|
|
222
205
|
#### `VersionOwnershipResolver`
|
|
223
206
|
|
|
224
207
|
Resolves whether the authenticated caller may read/restore versions of a
|
|
@@ -229,78 +212,95 @@ async (e.g. it can look the parent resource up in the database).
|
|
|
229
212
|
type VersionOwnershipResolver = (context: VersionOwnershipContext) => boolean | Promise<boolean>
|
|
230
213
|
```
|
|
231
214
|
|
|
232
|
-
|
|
215
|
+
- `context` — The {@link VersionOwnershipContext} to authorize.
|
|
233
216
|
|
|
234
|
-
|
|
217
|
+
**Returns:** `true` when the caller owns / may access the parent resource.
|
|
235
218
|
|
|
236
|
-
|
|
219
|
+
#### `JSONValue`
|
|
220
|
+
|
|
221
|
+
A JSON-serializable snapshot value. Constrained by what the underlying
|
|
222
|
+
`JSONB` column can accept.
|
|
237
223
|
|
|
238
224
|
```typescript
|
|
239
|
-
|
|
225
|
+
type JSONValue = string | number | boolean | null | JSONValue[] | { [key: string]: JSONValue }
|
|
240
226
|
```
|
|
241
227
|
|
|
242
|
-
#### `
|
|
243
|
-
|
|
244
|
-
Captures a new version of a resource.
|
|
228
|
+
#### `VersionChanges`
|
|
245
229
|
|
|
246
|
-
|
|
247
|
-
when the caller is not authorized for the parent resource — a caller can only
|
|
248
|
-
capture versions of a resource they own, never inject snapshots into another
|
|
249
|
-
tenant's resource.
|
|
230
|
+
Shallow per-field diff between two snapshots, keyed by field name.
|
|
250
231
|
|
|
251
232
|
```typescript
|
|
252
|
-
|
|
233
|
+
type VersionChanges = Record<string, VersionFieldChange>
|
|
253
234
|
```
|
|
254
235
|
|
|
255
|
-
|
|
256
|
-
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
257
|
-
|
|
258
|
-
#### `createVersion(input)`
|
|
236
|
+
### Functions
|
|
259
237
|
|
|
260
|
-
|
|
238
|
+
#### `isVersionAuthorized(res, context)`
|
|
261
239
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
240
|
+
Authorizes whether the authenticated caller may access versions of a parent
|
|
241
|
+
resource. Admins widened by {@link versionHistoryAdmin} are always allowed;
|
|
242
|
+
otherwise the app-registered {@link VersionOwnershipResolver} for the parent
|
|
243
|
+
`resourceType` decides. **Fail-closed:** when no resolver is registered the
|
|
244
|
+
caller is denied, so the polymorphic version store never leaks a snapshot it
|
|
245
|
+
cannot prove the caller owns.
|
|
265
246
|
|
|
266
247
|
```typescript
|
|
267
|
-
function
|
|
248
|
+
function isVersionAuthorized(
|
|
249
|
+
res: MoleculeResponse,
|
|
250
|
+
context: VersionOwnershipContext,
|
|
251
|
+
): Promise<boolean>
|
|
268
252
|
```
|
|
269
253
|
|
|
270
|
-
- `
|
|
254
|
+
- `res` — The response whose `locals.session`/`locals.versionHistoryAdmin` is inspected.
|
|
255
|
+
- `context` — The {@link VersionOwnershipContext} (parent resource + caller).
|
|
271
256
|
|
|
272
|
-
**Returns:**
|
|
257
|
+
**Returns:** `true` when the caller may access the parent resource's versions.
|
|
273
258
|
|
|
274
|
-
#### `
|
|
259
|
+
#### `isVersionHistoryAdmin(res)`
|
|
275
260
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
261
|
+
Resolves whether the current request's session belongs to an actor
|
|
262
|
+
authorized to administer version history (read/restore any user's versions).
|
|
263
|
+
Fail-closed: returns `false` when there is no authenticated session, and
|
|
264
|
+
otherwise only `true` when the session carries an admin claim — `isAdmin ===
|
|
265
|
+
true`, `role === 'admin'`, `roles` containing `'admin'`, or `permissions`
|
|
266
|
+
containing `'admin'` / `'versionHistory:manage'`.
|
|
279
267
|
|
|
280
268
|
```typescript
|
|
281
|
-
function
|
|
269
|
+
function isVersionHistoryAdmin(res: MoleculeResponse): boolean
|
|
282
270
|
```
|
|
283
271
|
|
|
284
|
-
- `
|
|
285
|
-
- `resourceId` — The resource ID.
|
|
272
|
+
- `res` — The response whose `locals.session` is inspected.
|
|
286
273
|
|
|
287
|
-
**Returns:**
|
|
274
|
+
**Returns:** `true` when the session is an authorized version-history admin.
|
|
288
275
|
|
|
289
|
-
#### `
|
|
276
|
+
#### `versionHistoryAdmin()`
|
|
290
277
|
|
|
291
|
-
|
|
278
|
+
Opt-in route middleware that _widens_ an authenticated admin to cross-tenant
|
|
279
|
+
version access by setting `res.locals.versionHistoryAdmin = true`. It never
|
|
280
|
+
blocks: a non-admin (or anonymous) caller passes through unchanged and
|
|
281
|
+
remains subject to the ownership resolver in the handlers, so composing this
|
|
282
|
+
onto a route can only widen for admins, never open the endpoint. Wire it onto
|
|
283
|
+
a dedicated admin route when a support/compliance console needs to read or
|
|
284
|
+
restore every user's versions.
|
|
292
285
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
286
|
+
```typescript
|
|
287
|
+
function versionHistoryAdmin(): MoleculeRequestHandler
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
**Returns:** An Express-compatible middleware function.
|
|
291
|
+
|
|
292
|
+
#### `jsonEqual(a, b)`
|
|
293
|
+
|
|
294
|
+
Returns `true` if `a` and `b` are deeply equal as JSON values.
|
|
297
295
|
|
|
298
296
|
```typescript
|
|
299
|
-
function
|
|
297
|
+
function jsonEqual(a: JSONValue | undefined, b: JSONValue | undefined): boolean
|
|
300
298
|
```
|
|
301
299
|
|
|
302
|
-
- `
|
|
303
|
-
- `
|
|
300
|
+
- `a` — First value to compare.
|
|
301
|
+
- `b` — Second value to compare.
|
|
302
|
+
|
|
303
|
+
**Returns:** Whether the two values are structurally equal.
|
|
304
304
|
|
|
305
305
|
#### `diffSnapshots(before, after)`
|
|
306
306
|
|
|
@@ -320,237 +320,255 @@ function diffSnapshots(before: JSONValue, after: JSONValue): VersionChanges
|
|
|
320
320
|
|
|
321
321
|
**Returns:** A {@link VersionChanges} record. Empty when the snapshots are equal.
|
|
322
322
|
|
|
323
|
-
#### `
|
|
323
|
+
#### `versionCount(req, res)`
|
|
324
324
|
|
|
325
|
-
|
|
325
|
+
Returns the total number of versions for a resource.
|
|
326
326
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
forward delta.
|
|
327
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
328
|
+
when the caller is not authorized for the parent resource.
|
|
330
329
|
|
|
331
330
|
```typescript
|
|
332
|
-
function
|
|
331
|
+
function versionCount(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
333
332
|
```
|
|
334
333
|
|
|
335
|
-
- `
|
|
336
|
-
- `
|
|
334
|
+
- `req` — The request with `resourceType` and `resourceId` params.
|
|
335
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
337
336
|
|
|
338
|
-
|
|
337
|
+
#### `create(req, res)`
|
|
339
338
|
|
|
340
|
-
|
|
339
|
+
Captures a new version of a resource.
|
|
341
340
|
|
|
342
|
-
|
|
341
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
342
|
+
when the caller is not authorized for the parent resource — a caller can only
|
|
343
|
+
capture versions of a resource they own, never inject snapshots into another
|
|
344
|
+
tenant's resource.
|
|
343
345
|
|
|
344
346
|
```typescript
|
|
345
|
-
function
|
|
347
|
+
function create(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
346
348
|
```
|
|
347
349
|
|
|
348
|
-
- `
|
|
349
|
-
- `
|
|
350
|
+
- `req` — The request with `resourceType` / `resourceId` params and a snapshot body.
|
|
351
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
350
352
|
|
|
351
|
-
|
|
353
|
+
#### `diff(req, res)`
|
|
352
354
|
|
|
353
|
-
|
|
355
|
+
Returns the shallow diff between two versions of the same resource.
|
|
354
356
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
358
|
+
when either version is missing OR the caller is not authorized for the parent
|
|
359
|
+
resource the two versions share. An opt-in {@link versionHistoryAdmin} may
|
|
360
|
+
diff any versions.
|
|
357
361
|
|
|
358
362
|
```typescript
|
|
359
|
-
function
|
|
363
|
+
function diff(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
360
364
|
```
|
|
361
365
|
|
|
362
|
-
- `
|
|
366
|
+
- `req` — The request with `fromVersionId` and `toVersionId` params.
|
|
367
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
363
368
|
|
|
364
|
-
|
|
369
|
+
#### `list(req, res)`
|
|
365
370
|
|
|
366
|
-
|
|
371
|
+
Lists paginated versions for a resource, newest version first.
|
|
367
372
|
|
|
368
|
-
|
|
373
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
374
|
+
when the caller is not authorized for the parent resource — only the parent
|
|
375
|
+
resource's owner (or an opt-in {@link versionHistoryAdmin}) sees its versions.
|
|
369
376
|
|
|
370
377
|
```typescript
|
|
371
|
-
function
|
|
378
|
+
function list(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
372
379
|
```
|
|
373
380
|
|
|
374
|
-
- `
|
|
381
|
+
- `req` — The request with `resourceType` and `resourceId` params.
|
|
382
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
375
383
|
|
|
376
|
-
|
|
384
|
+
#### `read(req, res)`
|
|
377
385
|
|
|
378
|
-
|
|
386
|
+
Reads a single version by ID.
|
|
379
387
|
|
|
380
|
-
|
|
388
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
389
|
+
when the version is missing OR the caller is not authorized for its parent
|
|
390
|
+
resource — a non-owner cannot tell the two apart, so another tenant's
|
|
391
|
+
snapshot is never disclosed. An opt-in {@link versionHistoryAdmin} may read
|
|
392
|
+
any version.
|
|
381
393
|
|
|
382
394
|
```typescript
|
|
383
|
-
function
|
|
384
|
-
resourceType: string,
|
|
385
|
-
resourceId: string,
|
|
386
|
-
version: number,
|
|
387
|
-
): Promise<Version | null>
|
|
395
|
+
function read(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
388
396
|
```
|
|
389
397
|
|
|
390
|
-
- `
|
|
391
|
-
- `
|
|
392
|
-
- `version` — The 1-based version number.
|
|
398
|
+
- `req` — The request with `versionId` param.
|
|
399
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
393
400
|
|
|
394
|
-
|
|
401
|
+
#### `readByNumber(req, res)`
|
|
395
402
|
|
|
396
|
-
|
|
403
|
+
Reads a single version of a resource by 1-based version number.
|
|
397
404
|
|
|
398
|
-
|
|
405
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
406
|
+
when the caller is not authorized for the parent resource.
|
|
399
407
|
|
|
400
408
|
```typescript
|
|
401
|
-
function
|
|
409
|
+
function readByNumber(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
402
410
|
```
|
|
403
411
|
|
|
404
|
-
- `
|
|
405
|
-
- `
|
|
412
|
+
- `req` — The request with `resourceType`, `resourceId`, and `version` params.
|
|
413
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
406
414
|
|
|
407
|
-
|
|
415
|
+
#### `restore(req, res)`
|
|
408
416
|
|
|
409
|
-
|
|
417
|
+
Restores a prior version by appending a new version whose snapshot
|
|
418
|
+
matches it. Append-only — the existing rows are never mutated.
|
|
410
419
|
|
|
411
|
-
|
|
420
|
+
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
421
|
+
when the version is missing OR the caller is not authorized for its parent
|
|
422
|
+
resource — a non-owner can neither restore another tenant's version nor learn
|
|
423
|
+
that it exists. An opt-in {@link versionHistoryAdmin} may restore any version.
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
function restore(req: MoleculeRequest, res: MoleculeResponse): Promise<void>
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
- `req` — The request with `versionId` param.
|
|
430
|
+
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
431
|
+
|
|
432
|
+
#### `registerOwnershipResolver(resourceType, resolver)`
|
|
433
|
+
|
|
434
|
+
Registers an ownership resolver for a parent resource type.
|
|
435
|
+
|
|
436
|
+
Subsequent calls with the same `resourceType` overwrite the previous
|
|
437
|
+
registration.
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
function registerOwnershipResolver(resourceType: string, resolver: VersionOwnershipResolver): void
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
- `resourceType` — The parent resource type the resolver authorizes.
|
|
444
|
+
- `resolver` — The resolver to invoke when authorizing access to versions of this resource type.
|
|
445
|
+
|
|
446
|
+
#### `getOwnershipResolver(resourceType)`
|
|
447
|
+
|
|
448
|
+
Returns the ownership resolver registered for a resource type, or
|
|
449
|
+
`undefined` if none has been registered.
|
|
412
450
|
|
|
413
451
|
```typescript
|
|
414
|
-
function
|
|
415
|
-
resourceType: string,
|
|
416
|
-
resourceId: string,
|
|
417
|
-
options?: PaginationOptions,
|
|
418
|
-
): Promise<PaginatedResult<Version>>
|
|
452
|
+
function getOwnershipResolver(resourceType: string): VersionOwnershipResolver | undefined
|
|
419
453
|
```
|
|
420
454
|
|
|
421
|
-
- `resourceType` — The resource type.
|
|
422
|
-
- `resourceId` — The resource ID.
|
|
423
|
-
- `options` — Pagination options.
|
|
455
|
+
- `resourceType` — The resource type to look up.
|
|
424
456
|
|
|
425
|
-
**Returns:**
|
|
457
|
+
**Returns:** The resolver, or `undefined`.
|
|
426
458
|
|
|
427
|
-
#### `
|
|
459
|
+
#### `unregisterOwnershipResolver(resourceType)`
|
|
428
460
|
|
|
429
|
-
|
|
430
|
-
resource. Admins widened by {@link versionHistoryAdmin} are always allowed;
|
|
431
|
-
otherwise the app-registered {@link VersionOwnershipResolver} for the parent
|
|
432
|
-
`resourceType` decides. **Fail-closed:** when no resolver is registered the
|
|
433
|
-
caller is denied, so the polymorphic version store never leaks a snapshot it
|
|
434
|
-
cannot prove the caller owns.
|
|
461
|
+
Removes any registered ownership resolver for the given resource type.
|
|
435
462
|
|
|
436
463
|
```typescript
|
|
437
|
-
function
|
|
438
|
-
res: MoleculeResponse,
|
|
439
|
-
context: VersionOwnershipContext,
|
|
440
|
-
): Promise<boolean>
|
|
464
|
+
function unregisterOwnershipResolver(resourceType: string): boolean
|
|
441
465
|
```
|
|
442
466
|
|
|
443
|
-
- `
|
|
444
|
-
- `context` — The {@link VersionOwnershipContext} (parent resource + caller).
|
|
467
|
+
- `resourceType` — The resource type whose resolver should be removed.
|
|
445
468
|
|
|
446
|
-
**Returns:** `true`
|
|
469
|
+
**Returns:** `true` if a resolver was removed.
|
|
447
470
|
|
|
448
|
-
#### `
|
|
471
|
+
#### `clearOwnershipResolvers()`
|
|
449
472
|
|
|
450
|
-
|
|
451
|
-
authorized to administer version history (read/restore any user's versions).
|
|
452
|
-
Fail-closed: returns `false` when there is no authenticated session, and
|
|
453
|
-
otherwise only `true` when the session carries an admin claim — `isAdmin ===
|
|
454
|
-
true`, `role === 'admin'`, `roles` containing `'admin'`, or `permissions`
|
|
455
|
-
containing `'admin'` / `'versionHistory:manage'`.
|
|
473
|
+
Clears all registered ownership resolvers. Primarily useful in tests.
|
|
456
474
|
|
|
457
475
|
```typescript
|
|
458
|
-
function
|
|
476
|
+
function clearOwnershipResolvers(): void
|
|
459
477
|
```
|
|
460
478
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
**Returns:** `true` when the session is an authorized version-history admin.
|
|
479
|
+
#### `createVersion(input)`
|
|
464
480
|
|
|
465
|
-
|
|
481
|
+
Captures a new version of a resource.
|
|
466
482
|
|
|
467
|
-
|
|
483
|
+
Reads the latest existing version for `(resourceType, resourceId)` to
|
|
484
|
+
compute the next version number and the shallow diff against the prior
|
|
485
|
+
snapshot. The resulting row is append-only.
|
|
468
486
|
|
|
469
487
|
```typescript
|
|
470
|
-
function
|
|
488
|
+
function createVersion(input: CreateVersionInput): Promise<Version>
|
|
471
489
|
```
|
|
472
490
|
|
|
473
|
-
- `
|
|
474
|
-
- `b` — Second value to compare.
|
|
475
|
-
|
|
476
|
-
**Returns:** Whether the two values are structurally equal.
|
|
491
|
+
- `input` — The version creation input.
|
|
477
492
|
|
|
478
|
-
|
|
493
|
+
**Returns:** The created version.
|
|
479
494
|
|
|
480
|
-
|
|
495
|
+
#### `getLatestVersion(resourceType, resourceId)`
|
|
481
496
|
|
|
482
|
-
|
|
483
|
-
when the caller is not authorized for the parent resource — only the parent
|
|
484
|
-
resource's owner (or an opt-in {@link versionHistoryAdmin}) sees its versions.
|
|
497
|
+
Returns the most recent version for a resource, or `null` if no versions exist.
|
|
485
498
|
|
|
486
499
|
```typescript
|
|
487
|
-
function
|
|
500
|
+
function getLatestVersion(resourceType: string, resourceId: string): Promise<Version | null>
|
|
488
501
|
```
|
|
489
502
|
|
|
490
|
-
- `
|
|
491
|
-
- `
|
|
503
|
+
- `resourceType` — The resource type.
|
|
504
|
+
- `resourceId` — The resource ID.
|
|
492
505
|
|
|
493
|
-
|
|
506
|
+
**Returns:** The latest version, or `null`.
|
|
494
507
|
|
|
495
|
-
|
|
508
|
+
#### `getVersionsForResource(resourceType, resourceId, options)`
|
|
496
509
|
|
|
497
|
-
|
|
498
|
-
when the version is missing OR the caller is not authorized for its parent
|
|
499
|
-
resource — a non-owner cannot tell the two apart, so another tenant's
|
|
500
|
-
snapshot is never disclosed. An opt-in {@link versionHistoryAdmin} may read
|
|
501
|
-
any version.
|
|
510
|
+
Retrieves paginated versions for a resource, ordered by version number descending.
|
|
502
511
|
|
|
503
512
|
```typescript
|
|
504
|
-
function
|
|
513
|
+
function getVersionsForResource(
|
|
514
|
+
resourceType: string,
|
|
515
|
+
resourceId: string,
|
|
516
|
+
options?: PaginationOptions,
|
|
517
|
+
): Promise<PaginatedResult<Version>>
|
|
505
518
|
```
|
|
506
519
|
|
|
507
|
-
- `
|
|
508
|
-
- `
|
|
520
|
+
- `resourceType` — The resource type.
|
|
521
|
+
- `resourceId` — The resource ID.
|
|
522
|
+
- `options` — Pagination options.
|
|
509
523
|
|
|
510
|
-
|
|
524
|
+
**Returns:** A paginated result of versions.
|
|
511
525
|
|
|
512
|
-
|
|
526
|
+
#### `getVersionById(versionId)`
|
|
513
527
|
|
|
514
|
-
|
|
515
|
-
when the caller is not authorized for the parent resource.
|
|
528
|
+
Retrieves a version by ID.
|
|
516
529
|
|
|
517
530
|
```typescript
|
|
518
|
-
function
|
|
531
|
+
function getVersionById(versionId: string): Promise<Version | null>
|
|
519
532
|
```
|
|
520
533
|
|
|
521
|
-
- `
|
|
522
|
-
- `res` — The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
534
|
+
- `versionId` — The version ID.
|
|
523
535
|
|
|
524
|
-
|
|
536
|
+
**Returns:** The version, or `null` if not found.
|
|
525
537
|
|
|
526
|
-
|
|
538
|
+
#### `getVersionByNumber(resourceType, resourceId, version)`
|
|
527
539
|
|
|
528
|
-
|
|
529
|
-
registration.
|
|
540
|
+
Retrieves a version by resource and version number.
|
|
530
541
|
|
|
531
542
|
```typescript
|
|
532
|
-
function
|
|
543
|
+
function getVersionByNumber(
|
|
544
|
+
resourceType: string,
|
|
545
|
+
resourceId: string,
|
|
546
|
+
version: number,
|
|
547
|
+
): Promise<Version | null>
|
|
533
548
|
```
|
|
534
549
|
|
|
535
|
-
- `resourceType` — The
|
|
536
|
-
- `
|
|
550
|
+
- `resourceType` — The resource type.
|
|
551
|
+
- `resourceId` — The resource ID.
|
|
552
|
+
- `version` — The 1-based version number.
|
|
537
553
|
|
|
538
|
-
|
|
554
|
+
**Returns:** The version, or `null` if not found.
|
|
539
555
|
|
|
540
|
-
|
|
541
|
-
matches it. Append-only — the existing rows are never mutated.
|
|
556
|
+
#### `diffVersions(fromVersionId, toVersionId)`
|
|
542
557
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
558
|
+
Compares two versions and returns the shallow diff between their snapshots.
|
|
559
|
+
|
|
560
|
+
The argument order does not matter — the lower-numbered version is always
|
|
561
|
+
treated as `from`, the higher-numbered as `to`, so `changes` describes the
|
|
562
|
+
forward delta.
|
|
547
563
|
|
|
548
564
|
```typescript
|
|
549
|
-
function
|
|
565
|
+
function diffVersions(fromVersionId: string, toVersionId: string): Promise<VersionDiff | null>
|
|
550
566
|
```
|
|
551
567
|
|
|
552
|
-
- `
|
|
553
|
-
- `
|
|
568
|
+
- `fromVersionId` — One version ID to compare.
|
|
569
|
+
- `toVersionId` — The other version ID to compare.
|
|
570
|
+
|
|
571
|
+
**Returns:** The {@link VersionDiff}, or `null` if either version is missing or the two versions belong to different resources.
|
|
554
572
|
|
|
555
573
|
#### `restoreVersion(versionId, userId, reason)`
|
|
556
574
|
|
|
@@ -574,62 +592,62 @@ function restoreVersion(
|
|
|
574
592
|
|
|
575
593
|
**Returns:** The newly-appended version, or `null` if `versionId` was not found.
|
|
576
594
|
|
|
577
|
-
#### `
|
|
595
|
+
#### `deleteVersionsForResource(resourceType, resourceId)`
|
|
578
596
|
|
|
579
|
-
|
|
597
|
+
Deletes every version for a resource. Intended for cleanup when the parent
|
|
598
|
+
resource itself is deleted; never call from user-facing handlers without
|
|
599
|
+
authorization checks.
|
|
580
600
|
|
|
581
601
|
```typescript
|
|
582
|
-
function
|
|
602
|
+
function deleteVersionsForResource(resourceType: string, resourceId: string): Promise<number>
|
|
583
603
|
```
|
|
584
604
|
|
|
585
|
-
- `resourceType` — The resource type
|
|
605
|
+
- `resourceType` — The resource type.
|
|
606
|
+
- `resourceId` — The resource ID.
|
|
586
607
|
|
|
587
|
-
**Returns:**
|
|
608
|
+
**Returns:** The number of versions deleted.
|
|
588
609
|
|
|
589
|
-
#### `
|
|
610
|
+
#### `getVersionCount(resourceType, resourceId)`
|
|
590
611
|
|
|
591
612
|
Returns the total number of versions for a resource.
|
|
592
613
|
|
|
593
|
-
Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
594
|
-
when the caller is not authorized for the parent resource.
|
|
595
|
-
|
|
596
614
|
```typescript
|
|
597
|
-
function
|
|
615
|
+
function getVersionCount(resourceType: string, resourceId: string): Promise<number>
|
|
598
616
|
```
|
|
599
617
|
|
|
600
|
-
- `
|
|
601
|
-
- `
|
|
618
|
+
- `resourceType` — The resource type.
|
|
619
|
+
- `resourceId` — The resource ID.
|
|
602
620
|
|
|
603
|
-
|
|
621
|
+
**Returns:** The version count.
|
|
604
622
|
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
restore every user's versions.
|
|
623
|
+
### Constants
|
|
624
|
+
|
|
625
|
+
#### `VERSION_HISTORY_PERMISSION_ACTION`
|
|
626
|
+
|
|
627
|
+
Permission action describing version-history administration, e.g. for an
|
|
628
|
+
app's own `@molecule/api-permissions` wiring.
|
|
612
629
|
|
|
613
630
|
```typescript
|
|
614
|
-
|
|
631
|
+
const VERSION_HISTORY_PERMISSION_ACTION: 'manage'
|
|
615
632
|
```
|
|
616
633
|
|
|
617
|
-
|
|
634
|
+
#### `VERSION_HISTORY_PERMISSION_RESOURCE`
|
|
618
635
|
|
|
619
|
-
|
|
636
|
+
Permission resource describing version-history administration, e.g. for an
|
|
637
|
+
app's own `@molecule/api-permissions` wiring.
|
|
620
638
|
|
|
621
|
-
|
|
639
|
+
```typescript
|
|
640
|
+
const VERSION_HISTORY_PERMISSION_RESOURCE: 'versionHistory'
|
|
641
|
+
```
|
|
622
642
|
|
|
623
|
-
|
|
643
|
+
#### `VERSION_HISTORY_ADMIN_PERMISSION`
|
|
644
|
+
|
|
645
|
+
Session-claim permission string (`'versionHistory:manage'`) that, when
|
|
646
|
+
present in a session's `permissions` array, marks the caller as a
|
|
647
|
+
version-history admin.
|
|
624
648
|
|
|
625
649
|
```typescript
|
|
626
|
-
const
|
|
627
|
-
{
|
|
628
|
-
snapshot: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>
|
|
629
|
-
reason: z.ZodNullable<z.ZodOptional<z.ZodString>>
|
|
630
|
-
},
|
|
631
|
-
z.core.$strip
|
|
632
|
-
>
|
|
650
|
+
const VERSION_HISTORY_ADMIN_PERMISSION: 'versionHistory:manage'
|
|
633
651
|
```
|
|
634
652
|
|
|
635
653
|
#### `i18nRegistered`
|
|
@@ -656,18 +674,6 @@ const requestHandlerMap: {
|
|
|
656
674
|
}
|
|
657
675
|
```
|
|
658
676
|
|
|
659
|
-
#### `restoreVersionSchema`
|
|
660
|
-
|
|
661
|
-
Schema for validating restore-version input. Body is empty — the version
|
|
662
|
-
to restore is identified by URL params.
|
|
663
|
-
|
|
664
|
-
```typescript
|
|
665
|
-
const restoreVersionSchema: z.ZodObject<
|
|
666
|
-
{ reason: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>> },
|
|
667
|
-
z.core.$strip
|
|
668
|
-
>
|
|
669
|
-
```
|
|
670
|
-
|
|
671
677
|
#### `routes`
|
|
672
678
|
|
|
673
679
|
Routes for the version-history resource. All routes require `authenticate`;
|
|
@@ -721,32 +727,30 @@ const routes: readonly [
|
|
|
721
727
|
]
|
|
722
728
|
```
|
|
723
729
|
|
|
724
|
-
#### `
|
|
725
|
-
|
|
726
|
-
Session-claim permission string (`'versionHistory:manage'`) that, when
|
|
727
|
-
present in a session's `permissions` array, marks the caller as a
|
|
728
|
-
version-history admin.
|
|
729
|
-
|
|
730
|
-
```typescript
|
|
731
|
-
const VERSION_HISTORY_ADMIN_PERMISSION: 'versionHistory:manage'
|
|
732
|
-
```
|
|
733
|
-
|
|
734
|
-
#### `VERSION_HISTORY_PERMISSION_ACTION`
|
|
730
|
+
#### `createVersionSchema`
|
|
735
731
|
|
|
736
|
-
|
|
737
|
-
app's own `@molecule/api-permissions` wiring.
|
|
732
|
+
Schema for validating create-version input.
|
|
738
733
|
|
|
739
734
|
```typescript
|
|
740
|
-
const
|
|
735
|
+
const createVersionSchema: z.ZodObject<
|
|
736
|
+
{
|
|
737
|
+
snapshot: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>
|
|
738
|
+
reason: z.ZodNullable<z.ZodOptional<z.ZodString>>
|
|
739
|
+
},
|
|
740
|
+
z.core.$strip
|
|
741
|
+
>
|
|
741
742
|
```
|
|
742
743
|
|
|
743
|
-
#### `
|
|
744
|
+
#### `restoreVersionSchema`
|
|
744
745
|
|
|
745
|
-
|
|
746
|
-
|
|
746
|
+
Schema for validating restore-version input. Body is empty — the version
|
|
747
|
+
to restore is identified by URL params.
|
|
747
748
|
|
|
748
749
|
```typescript
|
|
749
|
-
const
|
|
750
|
+
const restoreVersionSchema: z.ZodObject<
|
|
751
|
+
{ reason: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>> },
|
|
752
|
+
z.core.$strip
|
|
753
|
+
>
|
|
750
754
|
```
|
|
751
755
|
|
|
752
756
|
## Injection Notes
|
|
@@ -790,7 +794,7 @@ Peer dependencies:
|
|
|
790
794
|
either a registered resolver or your own resource-ownership gate.
|
|
791
795
|
|
|
792
796
|
Tables: `src/__setup__/versions.sql` creates `versions`. An mlcl-scaffolded
|
|
793
|
-
API replays `__setup__/*.sql`
|
|
797
|
+
API replays `__setup__/*.sql` on migrate (a `CREATE TABLE IF NOT EXISTS`, so a table the app's own migrations already create keeps only its own columns: ALTER in the ones it lacks); anywhere else run it
|
|
794
798
|
once — nothing at runtime creates them. Handler errors flow through `t()`
|
|
795
799
|
with English defaults; install `@molecule/api-locales-resource-version-history`
|
|
796
800
|
and register it with `registerLocaleModule` for translations.
|
package/dist/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* either a registered resolver or your own resource-ownership gate.
|
|
29
29
|
*
|
|
30
30
|
* Tables: `src/__setup__/versions.sql` creates `versions`. An mlcl-scaffolded
|
|
31
|
-
* API replays `__setup__/*.sql`
|
|
31
|
+
* API replays `__setup__/*.sql` on migrate (a `CREATE TABLE IF NOT EXISTS`, so a table the app's own migrations already create keeps only its own columns: ALTER in the ones it lacks); anywhere else run it
|
|
32
32
|
* once — nothing at runtime creates them. Handler errors flow through `t()`
|
|
33
33
|
* with English defaults; install `@molecule/api-locales-resource-version-history`
|
|
34
34
|
* and register it with `registerLocaleModule` for translations.
|
package/dist/index.js
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* either a registered resolver or your own resource-ownership gate.
|
|
29
29
|
*
|
|
30
30
|
* Tables: `src/__setup__/versions.sql` creates `versions`. An mlcl-scaffolded
|
|
31
|
-
* API replays `__setup__/*.sql`
|
|
31
|
+
* API replays `__setup__/*.sql` on migrate (a `CREATE TABLE IF NOT EXISTS`, so a table the app's own migrations already create keeps only its own columns: ALTER in the ones it lacks); anywhere else run it
|
|
32
32
|
* once — nothing at runtime creates them. Handler errors flow through `t()`
|
|
33
33
|
* with English defaults; install `@molecule/api-locales-resource-version-history`
|
|
34
34
|
* and register it with `registerLocaleModule` for translations.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-resource-version-history",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Append-only versions table with diff and restore — generic versioning helper for any resource.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
|
21
|
+
"src/__setup__",
|
|
21
22
|
"README.md"
|
|
22
23
|
],
|
|
23
24
|
"keywords": [
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
CREATE TABLE IF NOT EXISTS "versions" (
|
|
2
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
3
|
+
"resourceType" VARCHAR(255) NOT NULL,
|
|
4
|
+
"resourceId" VARCHAR(255) NOT NULL,
|
|
5
|
+
"version" INTEGER NOT NULL,
|
|
6
|
+
"userId" UUID,
|
|
7
|
+
"snapshot" JSONB NOT NULL,
|
|
8
|
+
"changes" JSONB,
|
|
9
|
+
"reason" TEXT,
|
|
10
|
+
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
11
|
+
CONSTRAINT "uq_versions_resource_version" UNIQUE ("resourceType", "resourceId", "version")
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
CREATE INDEX IF NOT EXISTS "idx_versions_resource" ON "versions" ("resourceType", "resourceId");
|
|
15
|
+
CREATE INDEX IF NOT EXISTS "idx_versions_created" ON "versions" ("createdAt");
|
|
16
|
+
CREATE INDEX IF NOT EXISTS "idx_versions_user" ON "versions" ("userId");
|