@molecule/api-resource-version-history 1.0.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/LICENSE +115 -0
- package/dist/authorizers/index.d.ts +24 -0
- package/dist/authorizers/index.d.ts.map +1 -0
- package/dist/authorizers/index.js +23 -0
- package/dist/authorizers/ownership.d.ts +77 -0
- package/dist/authorizers/ownership.d.ts.map +1 -0
- package/dist/authorizers/ownership.js +112 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/diff.d.ts +33 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +110 -0
- package/dist/handlers/count.d.ts +17 -0
- package/dist/handlers/count.d.ts.map +1 -0
- package/dist/handlers/count.js +58 -0
- package/dist/handlers/create.d.ts +19 -0
- package/dist/handlers/create.d.ts.map +1 -0
- package/dist/handlers/create.js +73 -0
- package/dist/handlers/diff.d.ts +19 -0
- package/dist/handlers/diff.d.ts.map +1 -0
- package/dist/handlers/diff.js +70 -0
- package/dist/handlers/index.d.ts +8 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +7 -0
- package/dist/handlers/list.d.ts +18 -0
- package/dist/handlers/list.d.ts.map +1 -0
- package/dist/handlers/list.js +61 -0
- package/dist/handlers/read.d.ts +20 -0
- package/dist/handlers/read.d.ts.map +1 -0
- package/dist/handlers/read.js +68 -0
- package/dist/handlers/readByNumber.d.ts +17 -0
- package/dist/handlers/readByNumber.d.ts.map +1 -0
- package/dist/handlers/readByNumber.js +80 -0
- package/dist/handlers/restore.d.ts +20 -0
- package/dist/handlers/restore.d.ts.map +1 -0
- package/dist/handlers/restore.js +84 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +17 -0
- package/dist/index.d.ts +130 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +129 -0
- package/dist/registry.d.ts +79 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +65 -0
- package/dist/requestHandlerMap.d.ts +25 -0
- package/dist/requestHandlerMap.d.ts.map +1 -0
- package/dist/requestHandlerMap.js +24 -0
- package/dist/routes.d.ts +67 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +69 -0
- package/dist/service.d.ts +107 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +193 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/dist/validation.d.ts +21 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +34 -0
- package/package.json +70 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version-history business logic service.
|
|
3
|
+
*
|
|
4
|
+
* Uses the abstract DataStore from `@molecule/api-database` for all
|
|
5
|
+
* persistence — never raw SQL. Versions are append-only: nothing in this
|
|
6
|
+
* service ever updates or deletes individual rows except the explicit
|
|
7
|
+
* "delete all versions for a resource" cleanup hook.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { CreateVersionInput, JSONValue, PaginatedResult, PaginationOptions, Version, VersionDiff } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Captures a new version of a resource.
|
|
14
|
+
*
|
|
15
|
+
* Reads the latest existing version for `(resourceType, resourceId)` to
|
|
16
|
+
* compute the next version number and the shallow diff against the prior
|
|
17
|
+
* snapshot. The resulting row is append-only.
|
|
18
|
+
*
|
|
19
|
+
* @param input - The version creation input.
|
|
20
|
+
* @returns The created version.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createVersion(input: CreateVersionInput): Promise<Version>;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the most recent version for a resource, or `null` if no versions exist.
|
|
25
|
+
*
|
|
26
|
+
* @param resourceType - The resource type.
|
|
27
|
+
* @param resourceId - The resource ID.
|
|
28
|
+
* @returns The latest version, or `null`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getLatestVersion(resourceType: string, resourceId: string): Promise<Version | null>;
|
|
31
|
+
/**
|
|
32
|
+
* Retrieves paginated versions for a resource, ordered by version number descending.
|
|
33
|
+
*
|
|
34
|
+
* @param resourceType - The resource type.
|
|
35
|
+
* @param resourceId - The resource ID.
|
|
36
|
+
* @param options - Pagination options.
|
|
37
|
+
* @returns A paginated result of versions.
|
|
38
|
+
*/
|
|
39
|
+
export declare function getVersionsForResource(resourceType: string, resourceId: string, options?: PaginationOptions): Promise<PaginatedResult<Version>>;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves a version by ID.
|
|
42
|
+
*
|
|
43
|
+
* @param versionId - The version ID.
|
|
44
|
+
* @returns The version, or `null` if not found.
|
|
45
|
+
*/
|
|
46
|
+
export declare function getVersionById(versionId: string): Promise<Version | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves a version by resource and version number.
|
|
49
|
+
*
|
|
50
|
+
* @param resourceType - The resource type.
|
|
51
|
+
* @param resourceId - The resource ID.
|
|
52
|
+
* @param version - The 1-based version number.
|
|
53
|
+
* @returns The version, or `null` if not found.
|
|
54
|
+
*/
|
|
55
|
+
export declare function getVersionByNumber(resourceType: string, resourceId: string, version: number): Promise<Version | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Compares two versions and returns the shallow diff between their snapshots.
|
|
58
|
+
*
|
|
59
|
+
* The argument order does not matter — the lower-numbered version is always
|
|
60
|
+
* treated as `from`, the higher-numbered as `to`, so `changes` describes the
|
|
61
|
+
* forward delta.
|
|
62
|
+
*
|
|
63
|
+
* @param fromVersionId - One version ID to compare.
|
|
64
|
+
* @param toVersionId - The other version ID to compare.
|
|
65
|
+
* @returns The {@link VersionDiff}, or `null` if either version is missing or
|
|
66
|
+
* the two versions belong to different resources.
|
|
67
|
+
*/
|
|
68
|
+
export declare function diffVersions(fromVersionId: string, toVersionId: string): Promise<VersionDiff | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Restores a prior version by appending a new version whose snapshot matches it.
|
|
71
|
+
*
|
|
72
|
+
* Append-only: the existing rows are never mutated. The newly-appended
|
|
73
|
+
* version has its own monotonically-increasing `version` number, and its
|
|
74
|
+
* `reason` defaults to `Restored from version <n>` when no reason is supplied.
|
|
75
|
+
*
|
|
76
|
+
* @param versionId - The version ID to restore.
|
|
77
|
+
* @param userId - The user performing the restore, or `null` for system.
|
|
78
|
+
* @param reason - Optional human-readable reason. When omitted, a default
|
|
79
|
+
* `Restored from version <n>` reason is recorded.
|
|
80
|
+
* @returns The newly-appended version, or `null` if `versionId` was not found.
|
|
81
|
+
*/
|
|
82
|
+
export declare function restoreVersion(versionId: string, userId: string | null, reason?: string | null): Promise<Version | null>;
|
|
83
|
+
/**
|
|
84
|
+
* Deletes every version for a resource. Intended for cleanup when the parent
|
|
85
|
+
* resource itself is deleted; never call from user-facing handlers without
|
|
86
|
+
* authorization checks.
|
|
87
|
+
*
|
|
88
|
+
* @param resourceType - The resource type.
|
|
89
|
+
* @param resourceId - The resource ID.
|
|
90
|
+
* @returns The number of versions deleted.
|
|
91
|
+
*/
|
|
92
|
+
export declare function deleteVersionsForResource(resourceType: string, resourceId: string): Promise<number>;
|
|
93
|
+
/**
|
|
94
|
+
* Returns the total number of versions for a resource.
|
|
95
|
+
*
|
|
96
|
+
* @param resourceType - The resource type.
|
|
97
|
+
* @param resourceId - The resource ID.
|
|
98
|
+
* @returns The version count.
|
|
99
|
+
*/
|
|
100
|
+
export declare function getVersionCount(resourceType: string, resourceId: string): Promise<number>;
|
|
101
|
+
/**
|
|
102
|
+
* Re-export the snapshot type so consumers don't need a second import.
|
|
103
|
+
*
|
|
104
|
+
* @internal
|
|
105
|
+
*/
|
|
106
|
+
export type { JSONValue };
|
|
107
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA;;;;;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ACjB,OAAO,CAAC,MAAM,CAAC,CAMjB;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK/F;AAED;;;;GAIG;AACH,YAAY,EAAE,SAAS,EAAE,CAAA"}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version-history business logic service.
|
|
3
|
+
*
|
|
4
|
+
* Uses the abstract DataStore from `@molecule/api-database` for all
|
|
5
|
+
* persistence — never raw SQL. Versions are append-only: nothing in this
|
|
6
|
+
* service ever updates or deletes individual rows except the explicit
|
|
7
|
+
* "delete all versions for a resource" cleanup hook.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { count, create as dbCreate, deleteMany, findById, findMany, findOne, } from '@molecule/api-database';
|
|
12
|
+
import { diffSnapshots } from './diff.js';
|
|
13
|
+
const TABLE = 'versions';
|
|
14
|
+
/**
|
|
15
|
+
* Captures a new version of a resource.
|
|
16
|
+
*
|
|
17
|
+
* Reads the latest existing version for `(resourceType, resourceId)` to
|
|
18
|
+
* compute the next version number and the shallow diff against the prior
|
|
19
|
+
* snapshot. The resulting row is append-only.
|
|
20
|
+
*
|
|
21
|
+
* @param input - The version creation input.
|
|
22
|
+
* @returns The created version.
|
|
23
|
+
*/
|
|
24
|
+
export async function createVersion(input) {
|
|
25
|
+
const previous = await getLatestVersion(input.resourceType, input.resourceId);
|
|
26
|
+
const nextVersion = previous ? previous.version + 1 : 1;
|
|
27
|
+
const previousSnapshot = previous?.snapshot ?? null;
|
|
28
|
+
const changes = diffSnapshots(previousSnapshot, input.snapshot);
|
|
29
|
+
const hasChanges = Object.keys(changes).length > 0;
|
|
30
|
+
const result = await dbCreate(TABLE, {
|
|
31
|
+
resourceType: input.resourceType,
|
|
32
|
+
resourceId: input.resourceId,
|
|
33
|
+
version: nextVersion,
|
|
34
|
+
userId: input.userId ?? null,
|
|
35
|
+
snapshot: input.snapshot,
|
|
36
|
+
changes: previous ? changes : null,
|
|
37
|
+
reason: input.reason ?? null,
|
|
38
|
+
});
|
|
39
|
+
if (!result.data) {
|
|
40
|
+
throw new Error('Failed to create version');
|
|
41
|
+
}
|
|
42
|
+
// Surface a useful no-op signal for callers that care, without changing
|
|
43
|
+
// the append semantics.
|
|
44
|
+
void hasChanges;
|
|
45
|
+
return result.data;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns the most recent version for a resource, or `null` if no versions exist.
|
|
49
|
+
*
|
|
50
|
+
* @param resourceType - The resource type.
|
|
51
|
+
* @param resourceId - The resource ID.
|
|
52
|
+
* @returns The latest version, or `null`.
|
|
53
|
+
*/
|
|
54
|
+
export async function getLatestVersion(resourceType, resourceId) {
|
|
55
|
+
const rows = await findMany(TABLE, {
|
|
56
|
+
where: [
|
|
57
|
+
{ field: 'resourceType', operator: '=', value: resourceType },
|
|
58
|
+
{ field: 'resourceId', operator: '=', value: resourceId },
|
|
59
|
+
],
|
|
60
|
+
orderBy: [{ field: 'version', direction: 'desc' }],
|
|
61
|
+
limit: 1,
|
|
62
|
+
});
|
|
63
|
+
return rows[0] ?? null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves paginated versions for a resource, ordered by version number descending.
|
|
67
|
+
*
|
|
68
|
+
* @param resourceType - The resource type.
|
|
69
|
+
* @param resourceId - The resource ID.
|
|
70
|
+
* @param options - Pagination options.
|
|
71
|
+
* @returns A paginated result of versions.
|
|
72
|
+
*/
|
|
73
|
+
export async function getVersionsForResource(resourceType, resourceId, options = {}) {
|
|
74
|
+
const limit = options.limit ?? 20;
|
|
75
|
+
const offset = options.offset ?? 0;
|
|
76
|
+
const where = [
|
|
77
|
+
{ field: 'resourceType', operator: '=', value: resourceType },
|
|
78
|
+
{ field: 'resourceId', operator: '=', value: resourceId },
|
|
79
|
+
];
|
|
80
|
+
const [data, total] = await Promise.all([
|
|
81
|
+
findMany(TABLE, {
|
|
82
|
+
where,
|
|
83
|
+
orderBy: [{ field: 'version', direction: 'desc' }],
|
|
84
|
+
limit,
|
|
85
|
+
offset,
|
|
86
|
+
}),
|
|
87
|
+
count(TABLE, where),
|
|
88
|
+
]);
|
|
89
|
+
return { data, total, limit, offset };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves a version by ID.
|
|
93
|
+
*
|
|
94
|
+
* @param versionId - The version ID.
|
|
95
|
+
* @returns The version, or `null` if not found.
|
|
96
|
+
*/
|
|
97
|
+
export async function getVersionById(versionId) {
|
|
98
|
+
return findById(TABLE, versionId);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Retrieves a version by resource and version number.
|
|
102
|
+
*
|
|
103
|
+
* @param resourceType - The resource type.
|
|
104
|
+
* @param resourceId - The resource ID.
|
|
105
|
+
* @param version - The 1-based version number.
|
|
106
|
+
* @returns The version, or `null` if not found.
|
|
107
|
+
*/
|
|
108
|
+
export async function getVersionByNumber(resourceType, resourceId, version) {
|
|
109
|
+
return findOne(TABLE, [
|
|
110
|
+
{ field: 'resourceType', operator: '=', value: resourceType },
|
|
111
|
+
{ field: 'resourceId', operator: '=', value: resourceId },
|
|
112
|
+
{ field: 'version', operator: '=', value: version },
|
|
113
|
+
]);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Compares two versions and returns the shallow diff between their snapshots.
|
|
117
|
+
*
|
|
118
|
+
* The argument order does not matter — the lower-numbered version is always
|
|
119
|
+
* treated as `from`, the higher-numbered as `to`, so `changes` describes the
|
|
120
|
+
* forward delta.
|
|
121
|
+
*
|
|
122
|
+
* @param fromVersionId - One version ID to compare.
|
|
123
|
+
* @param toVersionId - The other version ID to compare.
|
|
124
|
+
* @returns The {@link VersionDiff}, or `null` if either version is missing or
|
|
125
|
+
* the two versions belong to different resources.
|
|
126
|
+
*/
|
|
127
|
+
export async function diffVersions(fromVersionId, toVersionId) {
|
|
128
|
+
const [a, b] = await Promise.all([getVersionById(fromVersionId), getVersionById(toVersionId)]);
|
|
129
|
+
if (!a || !b)
|
|
130
|
+
return null;
|
|
131
|
+
if (a.resourceType !== b.resourceType || a.resourceId !== b.resourceId)
|
|
132
|
+
return null;
|
|
133
|
+
const [from, to] = a.version <= b.version ? [a, b] : [b, a];
|
|
134
|
+
return {
|
|
135
|
+
from,
|
|
136
|
+
to,
|
|
137
|
+
changes: diffSnapshots(from.snapshot, to.snapshot),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Restores a prior version by appending a new version whose snapshot matches it.
|
|
142
|
+
*
|
|
143
|
+
* Append-only: the existing rows are never mutated. The newly-appended
|
|
144
|
+
* version has its own monotonically-increasing `version` number, and its
|
|
145
|
+
* `reason` defaults to `Restored from version <n>` when no reason is supplied.
|
|
146
|
+
*
|
|
147
|
+
* @param versionId - The version ID to restore.
|
|
148
|
+
* @param userId - The user performing the restore, or `null` for system.
|
|
149
|
+
* @param reason - Optional human-readable reason. When omitted, a default
|
|
150
|
+
* `Restored from version <n>` reason is recorded.
|
|
151
|
+
* @returns The newly-appended version, or `null` if `versionId` was not found.
|
|
152
|
+
*/
|
|
153
|
+
export async function restoreVersion(versionId, userId, reason) {
|
|
154
|
+
const target = await getVersionById(versionId);
|
|
155
|
+
if (!target)
|
|
156
|
+
return null;
|
|
157
|
+
return createVersion({
|
|
158
|
+
resourceType: target.resourceType,
|
|
159
|
+
resourceId: target.resourceId,
|
|
160
|
+
snapshot: target.snapshot,
|
|
161
|
+
userId,
|
|
162
|
+
reason: reason ?? `Restored from version ${target.version}`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Deletes every version for a resource. Intended for cleanup when the parent
|
|
167
|
+
* resource itself is deleted; never call from user-facing handlers without
|
|
168
|
+
* authorization checks.
|
|
169
|
+
*
|
|
170
|
+
* @param resourceType - The resource type.
|
|
171
|
+
* @param resourceId - The resource ID.
|
|
172
|
+
* @returns The number of versions deleted.
|
|
173
|
+
*/
|
|
174
|
+
export async function deleteVersionsForResource(resourceType, resourceId) {
|
|
175
|
+
const result = await deleteMany(TABLE, [
|
|
176
|
+
{ field: 'resourceType', operator: '=', value: resourceType },
|
|
177
|
+
{ field: 'resourceId', operator: '=', value: resourceId },
|
|
178
|
+
]);
|
|
179
|
+
return result.affected ?? 0;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Returns the total number of versions for a resource.
|
|
183
|
+
*
|
|
184
|
+
* @param resourceType - The resource type.
|
|
185
|
+
* @param resourceId - The resource ID.
|
|
186
|
+
* @returns The version count.
|
|
187
|
+
*/
|
|
188
|
+
export async function getVersionCount(resourceType, resourceId) {
|
|
189
|
+
return count(TABLE, [
|
|
190
|
+
{ field: 'resourceType', operator: '=', value: resourceType },
|
|
191
|
+
{ field: 'resourceId', operator: '=', value: resourceId },
|
|
192
|
+
]);
|
|
193
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version history resource type definitions.
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic, append-only snapshots of any resource. Each version captures
|
|
5
|
+
* a full snapshot plus an optional shallow diff against the prior version,
|
|
6
|
+
* so consumers can rebuild a timeline, render a diff, or restore a prior
|
|
7
|
+
* snapshot without ever issuing raw SQL.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* A JSON-serializable snapshot value. Constrained by what the underlying
|
|
13
|
+
* `JSONB` column can accept.
|
|
14
|
+
*/
|
|
15
|
+
export type JSONValue = string | number | boolean | null | JSONValue[] | {
|
|
16
|
+
[key: string]: JSONValue;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* A point-in-time snapshot of a resource.
|
|
20
|
+
*
|
|
21
|
+
* Versions are append-only. A new row is created each time the resource
|
|
22
|
+
* changes; old rows are never mutated.
|
|
23
|
+
*/
|
|
24
|
+
export interface Version {
|
|
25
|
+
/** Unique version identifier. */
|
|
26
|
+
id: string;
|
|
27
|
+
/** The type of resource this version belongs to (e.g. `'document'`, `'project'`). */
|
|
28
|
+
resourceType: string;
|
|
29
|
+
/** The ID of the resource this version belongs to. */
|
|
30
|
+
resourceId: string;
|
|
31
|
+
/** The monotonically-increasing version number for this resource (1-based). */
|
|
32
|
+
version: number;
|
|
33
|
+
/** The ID of the user who created this version, or `null` if system-generated. */
|
|
34
|
+
userId: string | null;
|
|
35
|
+
/** Full snapshot of the resource at this point in time. */
|
|
36
|
+
snapshot: JSONValue;
|
|
37
|
+
/** Shallow diff against the previous version, or `null` if this is the first version. */
|
|
38
|
+
changes: VersionChanges | null;
|
|
39
|
+
/** Optional human-readable reason for this version (e.g. commit message). */
|
|
40
|
+
reason: string | null;
|
|
41
|
+
/** When this version was created (ISO 8601). */
|
|
42
|
+
createdAt: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Per-field change record for a single key. `before` is the previous value,
|
|
46
|
+
* `after` is the new value. Either may be `undefined` for added/removed fields.
|
|
47
|
+
*/
|
|
48
|
+
export interface VersionFieldChange {
|
|
49
|
+
/** Previous value (`undefined` if the field was added). */
|
|
50
|
+
before?: JSONValue;
|
|
51
|
+
/** New value (`undefined` if the field was removed). */
|
|
52
|
+
after?: JSONValue;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Shallow per-field diff between two snapshots, keyed by field name.
|
|
56
|
+
*/
|
|
57
|
+
export type VersionChanges = Record<string, VersionFieldChange>;
|
|
58
|
+
/**
|
|
59
|
+
* Input for creating a new version.
|
|
60
|
+
*/
|
|
61
|
+
export interface CreateVersionInput {
|
|
62
|
+
/** The type of resource being versioned. */
|
|
63
|
+
resourceType: string;
|
|
64
|
+
/** The ID of the resource being versioned. */
|
|
65
|
+
resourceId: string;
|
|
66
|
+
/** Full snapshot of the resource. */
|
|
67
|
+
snapshot: JSONValue;
|
|
68
|
+
/** ID of the user creating this version, or `null` for system-generated versions. */
|
|
69
|
+
userId?: string | null;
|
|
70
|
+
/** Optional human-readable reason. */
|
|
71
|
+
reason?: string | null;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Result of comparing two versions of the same resource.
|
|
75
|
+
*/
|
|
76
|
+
export interface VersionDiff {
|
|
77
|
+
/** The earlier version. */
|
|
78
|
+
from: Version;
|
|
79
|
+
/** The later version. */
|
|
80
|
+
to: Version;
|
|
81
|
+
/** Per-field changes between `from.snapshot` and `to.snapshot`. */
|
|
82
|
+
changes: VersionChanges;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Options for paginated queries.
|
|
86
|
+
*/
|
|
87
|
+
export interface PaginationOptions {
|
|
88
|
+
/** Maximum number of results to return. */
|
|
89
|
+
limit?: number;
|
|
90
|
+
/** Number of results to skip. */
|
|
91
|
+
offset?: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A paginated result set.
|
|
95
|
+
*/
|
|
96
|
+
export interface PaginatedResult<T> {
|
|
97
|
+
/** The result items for the current page. */
|
|
98
|
+
data: T[];
|
|
99
|
+
/** Total number of matching items across all pages. */
|
|
100
|
+
total: number;
|
|
101
|
+
/** Maximum number of results per page. */
|
|
102
|
+
limit: number;
|
|
103
|
+
/** Number of results skipped. */
|
|
104
|
+
offset: number;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;GAGG;AACH,MAAM,MAAM,SAAS,GACnB,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAA;AAE/E;;;;;GAKG;AACH,MA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3C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,6CAA6C;IAC7C,IAAI,EAAE,CAAC,EAAE,CAAA;IACT,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAA;IACb,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAA;IACb,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAA;CACf"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version history resource type definitions.
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic, append-only snapshots of any resource. Each version captures
|
|
5
|
+
* a full snapshot plus an optional shallow diff against the prior version,
|
|
6
|
+
* so consumers can rebuild a timeline, render a diff, or restore a prior
|
|
7
|
+
* snapshot without ever issuing raw SQL.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version-history input validation schemas.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/**
|
|
8
|
+
* Schema for validating create-version input.
|
|
9
|
+
*/
|
|
10
|
+
export declare const createVersionSchema: z.ZodObject<{
|
|
11
|
+
snapshot: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
12
|
+
reason: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
13
|
+
}, z.core.$strip>;
|
|
14
|
+
/**
|
|
15
|
+
* Schema for validating restore-version input. Body is empty — the version
|
|
16
|
+
* to restore is identified by URL params.
|
|
17
|
+
*/
|
|
18
|
+
export declare const restoreVersionSchema: z.ZodObject<{
|
|
19
|
+
reason: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>>;
|
|
20
|
+
}, z.core.$strip>;
|
|
21
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAcvB;;GAEG;AACH,eAAO,MAAM,mBAAmB;;;iBAK9B,CAAA;AAEF;;;GAGG;AACH,eAAO,MAAM,oBAAoB;;iBAKrB,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version-history input validation schemas.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/** Recursive zod schema covering JSON-serializable snapshot values. */
|
|
8
|
+
const jsonValueSchema = z.lazy(() => z.union([
|
|
9
|
+
z.string(),
|
|
10
|
+
z.number(),
|
|
11
|
+
z.boolean(),
|
|
12
|
+
z.null(),
|
|
13
|
+
z.array(jsonValueSchema),
|
|
14
|
+
z.record(z.string(), jsonValueSchema),
|
|
15
|
+
]));
|
|
16
|
+
/**
|
|
17
|
+
* Schema for validating create-version input.
|
|
18
|
+
*/
|
|
19
|
+
export const createVersionSchema = z.object({
|
|
20
|
+
/** The resource snapshot to capture. Must be a JSON-serializable value. */
|
|
21
|
+
snapshot: jsonValueSchema,
|
|
22
|
+
/** Optional human-readable reason (e.g. commit message). */
|
|
23
|
+
reason: z.string().max(2_000).optional().nullable(),
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Schema for validating restore-version input. Body is empty — the version
|
|
27
|
+
* to restore is identified by URL params.
|
|
28
|
+
*/
|
|
29
|
+
export const restoreVersionSchema = z
|
|
30
|
+
.object({
|
|
31
|
+
/** Optional reason describing why the restore was performed. */
|
|
32
|
+
reason: z.string().max(2_000).optional().nullable(),
|
|
33
|
+
})
|
|
34
|
+
.partial();
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-resource-version-history",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Append-only versions table with diff and restore — generic versioning helper for any resource.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"version",
|
|
25
|
+
"history",
|
|
26
|
+
"audit",
|
|
27
|
+
"snapshot",
|
|
28
|
+
"resource"
|
|
29
|
+
],
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@molecule/api-locales-resource-version-history": "1.0.0",
|
|
33
|
+
"@types/node": "26.1.2",
|
|
34
|
+
"typescript": "6.0.3",
|
|
35
|
+
"vitest": "4.1.10",
|
|
36
|
+
"zod": "4.4.3"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@molecule/api-database": "^1.0.0",
|
|
40
|
+
"@molecule/api-i18n": "^1.0.0",
|
|
41
|
+
"@molecule/api-locales-resource-version-history": "^1.0.0",
|
|
42
|
+
"@molecule/api-logger": "^1.0.0",
|
|
43
|
+
"@molecule/api-resource": "^1.0.0",
|
|
44
|
+
"zod": "^4.0.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"@molecule/api-database": {
|
|
48
|
+
"optional": true
|
|
49
|
+
},
|
|
50
|
+
"@molecule/api-i18n": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"@molecule/api-logger": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"@molecule/api-resource": {
|
|
57
|
+
"optional": true
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"repository": {
|
|
61
|
+
"type": "git",
|
|
62
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
63
|
+
"directory": "packages/api/resources/version-history"
|
|
64
|
+
},
|
|
65
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/resources/version-history",
|
|
66
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
}
|
|
70
|
+
}
|