@owlmeans/mongo-resource 0.1.18-rc.9 → 0.1.18
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/README.md +232 -71
- package/agent-meta/manifest.json +3 -3
- package/agent-meta/skills/mongo-resource/SKILL.md +89 -44
- package/build/consts.d.ts +8 -4
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +8 -4
- package/build/consts.js.map +1 -1
- package/build/declarations/types.d.ts +12 -0
- package/build/declarations/types.d.ts.map +1 -0
- package/build/declarations/types.js +2 -0
- package/build/declarations/types.js.map +1 -0
- package/build/declarations.d.ts +4 -9
- package/build/declarations.d.ts.map +1 -1
- package/build/declarations.js +28 -21
- package/build/declarations.js.map +1 -1
- package/build/index.d.ts +4 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/resource.d.ts +2 -2
- package/build/resource.d.ts.map +1 -1
- package/build/resource.js +100 -130
- package/build/resource.js.map +1 -1
- package/build/types.d.ts +11 -5
- package/build/types.d.ts.map +1 -1
- package/build/types.local.d.ts +4 -0
- package/build/types.local.d.ts.map +1 -0
- package/build/types.local.js +2 -0
- package/build/types.local.js.map +1 -0
- package/build/utils/consts.local.d.ts +25 -0
- package/build/utils/consts.local.d.ts.map +1 -0
- package/build/utils/consts.local.js +31 -0
- package/build/utils/consts.local.js.map +1 -0
- package/build/utils/criteria/types.d.ts +27 -0
- package/build/utils/criteria/types.d.ts.map +1 -0
- package/build/utils/criteria/types.js +2 -0
- package/build/utils/criteria/types.js.map +1 -0
- package/build/utils/criteria.d.ts +9 -0
- package/build/utils/criteria.d.ts.map +1 -0
- package/build/utils/criteria.js +204 -0
- package/build/utils/criteria.js.map +1 -0
- package/build/utils/index.d.ts +1 -0
- package/build/utils/index.d.ts.map +1 -1
- package/build/utils/index.js +1 -0
- package/build/utils/index.js.map +1 -1
- package/build/utils/indexes/types.d.ts +19 -0
- package/build/utils/indexes/types.d.ts.map +1 -0
- package/build/utils/indexes/types.js +2 -0
- package/build/utils/indexes/types.js.map +1 -0
- package/build/utils/indexes.d.ts +3 -4
- package/build/utils/indexes.d.ts.map +1 -1
- package/build/utils/indexes.js +44 -28
- package/build/utils/indexes.js.map +1 -1
- package/build/utils/life-cycle/types.d.ts +36 -0
- package/build/utils/life-cycle/types.d.ts.map +1 -0
- package/build/utils/life-cycle/types.js +2 -0
- package/build/utils/life-cycle/types.js.map +1 -0
- package/build/utils/life-cycle.d.ts +3 -30
- package/build/utils/life-cycle.d.ts.map +1 -1
- package/build/utils/life-cycle.js +96 -112
- package/build/utils/life-cycle.js.map +1 -1
- package/build/utils/migrations.d.ts.map +1 -1
- package/build/utils/migrations.js +1 -2
- package/build/utils/migrations.js.map +1 -1
- package/build/utils/name.d.ts.map +1 -1
- package/build/utils/name.js +1 -5
- package/build/utils/name.js.map +1 -1
- package/build/utils/refs/types.d.ts +78 -0
- package/build/utils/refs/types.d.ts.map +1 -0
- package/build/utils/refs/types.js +2 -0
- package/build/utils/refs/types.js.map +1 -0
- package/build/utils/refs.d.ts +13 -67
- package/build/utils/refs.d.ts.map +1 -1
- package/build/utils/refs.js +115 -158
- package/build/utils/refs.js.map +1 -1
- package/build/utils/schema/types.d.ts +16 -0
- package/build/utils/schema/types.d.ts.map +1 -0
- package/build/utils/schema/types.js +2 -0
- package/build/utils/schema/types.js.map +1 -0
- package/build/utils/schema.d.ts +3 -11
- package/build/utils/schema.d.ts.map +1 -1
- package/build/utils/schema.js +86 -88
- package/build/utils/schema.js.map +1 -1
- package/build/utils/types.local.d.ts +13 -0
- package/build/utils/types.local.d.ts.map +1 -0
- package/build/utils/types.local.js +2 -0
- package/build/utils/types.local.js.map +1 -0
- package/package.json +10 -9
- package/src/consts.ts +8 -4
- package/src/declarations/types.ts +12 -0
- package/src/declarations.ts +32 -28
- package/src/index.ts +4 -0
- package/src/resource.ts +121 -163
- package/src/types.local.ts +5 -0
- package/src/types.ts +13 -7
- package/src/utils/consts.local.ts +35 -0
- package/src/utils/criteria/types.ts +28 -0
- package/src/utils/criteria.ts +235 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/indexes/types.ts +20 -0
- package/src/utils/indexes.ts +48 -28
- package/src/utils/life-cycle/types.ts +39 -0
- package/src/utils/life-cycle.ts +120 -134
- package/src/utils/migrations.ts +3 -17
- package/src/utils/name.ts +1 -6
- package/src/utils/refs/types.ts +79 -0
- package/src/utils/refs.ts +147 -189
- package/src/utils/schema/types.ts +17 -0
- package/src/utils/schema.ts +92 -91
- package/src/utils/types.local.ts +13 -0
- package/tests/context.ts +3 -3
- package/tests/criteria.spec.ts +116 -0
- package/tests/crud.spec.ts +2 -2
- package/tests/indexes.spec.ts +42 -0
- package/tests/refs.spec.ts +5 -3
package/build/utils/refs.js
CHANGED
|
@@ -1,137 +1,16 @@
|
|
|
1
1
|
import { MisshapedRecord } from '@owlmeans/resource';
|
|
2
2
|
import { ObjectId } from 'mongodb';
|
|
3
|
+
import { logger } from '@owlmeans/log';
|
|
4
|
+
import { HEX24, LOGICAL_OPERATORS, OPAQUE_OPERATORS } from './consts.local.js';
|
|
5
|
+
const log = logger('mongo-resource');
|
|
3
6
|
/**
|
|
4
|
-
*
|
|
5
|
-
* `ObjectId.isValid`, which also accepts any 12 character string and would silently
|
|
6
|
-
* swallow short business keys.
|
|
7
|
-
*/
|
|
8
|
-
const HEX24 = /^[0-9a-fA-F]{24}$/;
|
|
9
|
-
export const isObjectIdHex = (value) => typeof value === 'string' && HEX24.test(value);
|
|
10
|
-
/**
|
|
11
|
-
* Write side of a declared reference: the string id a record carries becomes the
|
|
12
|
-
* `ObjectId` the collection stores. Arrays convert elementwise.
|
|
13
|
-
*
|
|
14
|
-
* Strict on purpose — a declared reference holding something that is not a mongo id is
|
|
15
|
-
* either a mis-declared field (should never have been a reference) or a bug at the call
|
|
16
|
-
* site, and storing it as a string would silently reintroduce the mixed type state this
|
|
17
|
-
* mechanism exists to remove.
|
|
18
|
-
*
|
|
19
|
-
* @throws {MisshapedRecord}
|
|
20
|
-
*/
|
|
21
|
-
export const marshalReference = (field, value) => {
|
|
22
|
-
if (value == null) {
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
if (value instanceof ObjectId) {
|
|
26
|
-
return value;
|
|
27
|
-
}
|
|
28
|
-
if (Array.isArray(value)) {
|
|
29
|
-
return value.map(item => marshalReference(field, item));
|
|
30
|
-
}
|
|
31
|
-
if (isObjectIdHex(value)) {
|
|
32
|
-
return new ObjectId(value);
|
|
33
|
-
}
|
|
34
|
-
throw new MisshapedRecord(`ref:${field}`);
|
|
35
|
-
};
|
|
36
|
-
/** Read side: `ObjectId` back to the string records carry. Tolerates not yet migrated strings. */
|
|
37
|
-
export const demarshalReference = (value) => {
|
|
38
|
-
if (value instanceof ObjectId) {
|
|
39
|
-
return value.toString();
|
|
40
|
-
}
|
|
41
|
-
if (Array.isArray(value)) {
|
|
42
|
-
return value.map(demarshalReference);
|
|
43
|
-
}
|
|
44
|
-
return value;
|
|
45
|
-
};
|
|
46
|
-
/** Convert every declared reference of a fetched document back to string ids, in place. */
|
|
47
|
-
export const demarshalRefs = (record, refs) => {
|
|
48
|
-
if (refs.size < 1) {
|
|
49
|
-
return record;
|
|
50
|
-
}
|
|
51
|
-
for (const field of refs.keys()) {
|
|
52
|
-
const value = record[field];
|
|
53
|
-
if (value != null) {
|
|
54
|
-
record[field] = demarshalReference(value);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return record;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Operators whose operand is never an id — a 24 hex string under `$regex` is a pattern,
|
|
61
|
-
* not a reference.
|
|
62
|
-
*/
|
|
63
|
-
const OPAQUE_OPERATORS = ['$regex', '$options', '$type', '$size', '$mod', '$exists', '$where'];
|
|
64
|
-
const LOGICAL_OPERATORS = ['$and', '$or', '$nor'];
|
|
65
|
-
const marshalCriteriaValue = (value) => {
|
|
66
|
-
if (typeof value === 'string') {
|
|
67
|
-
return isObjectIdHex(value) ? new ObjectId(value) : value;
|
|
68
|
-
}
|
|
69
|
-
if (Array.isArray(value)) {
|
|
70
|
-
return value.map(marshalCriteriaValue);
|
|
71
|
-
}
|
|
72
|
-
if (value != null && typeof value === 'object' && !(value instanceof ObjectId) && !(value instanceof Date)) {
|
|
73
|
-
return Object.fromEntries(Object.entries(value).map(([operator, operand]) => OPAQUE_OPERATORS.includes(operator)
|
|
74
|
-
? [operator, operand]
|
|
75
|
-
: [operator, marshalCriteriaValue(operand)]));
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
};
|
|
79
|
-
/**
|
|
80
|
-
* Convert list criteria the way records are converted: values addressed at `_id` or at a
|
|
81
|
-
* declared reference become `ObjectId`s, and the `id` alias records actually carry is
|
|
82
|
-
* mapped onto `_id` — documents never store `id`, so before this mapping such criteria
|
|
83
|
-
* silently matched nothing.
|
|
7
|
+
* Ledger registered body of the system reference migration.
|
|
84
8
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
9
|
+
* Stays a module level factory, textually unchanged: the ledger fingerprints a migration by the
|
|
10
|
+
* source text of the function it registers (`apply.toString()`), so moving the returned closure —
|
|
11
|
+
* or renaming what it calls — changes the checksum and raises `MigrationConflict` against every
|
|
12
|
+
* ledger that already applied it.
|
|
88
13
|
*/
|
|
89
|
-
export const marshalCriteria = (criteria, refs) => {
|
|
90
|
-
if (criteria == null) {
|
|
91
|
-
return criteria;
|
|
92
|
-
}
|
|
93
|
-
return Object.fromEntries(Object.entries(criteria).map(([key, value]) => {
|
|
94
|
-
if (LOGICAL_OPERATORS.includes(key) && Array.isArray(value)) {
|
|
95
|
-
return [key, value.map(sub => marshalCriteria(sub, refs))];
|
|
96
|
-
}
|
|
97
|
-
if (key === 'id' || key === '_id') {
|
|
98
|
-
return ['_id', marshalCriteriaValue(value)];
|
|
99
|
-
}
|
|
100
|
-
if (refs.has(key)) {
|
|
101
|
-
return [key, marshalCriteriaValue(value)];
|
|
102
|
-
}
|
|
103
|
-
return [key, value];
|
|
104
|
-
}));
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* Criteria for addressing a single record by a field — `get`/`load`/`update`/`delete`.
|
|
108
|
-
*
|
|
109
|
-
* `_id` keeps its historical strictness (an invalid id throws through the driver). The
|
|
110
|
-
* `id` alias and declared references convert tolerantly, so a caller probing a reference
|
|
111
|
-
* with a value that is not a mongo id gets "not found" rather than a throw.
|
|
112
|
-
*/
|
|
113
|
-
export const identityCriteria = (field, id, refs) => {
|
|
114
|
-
if (field === '_id') {
|
|
115
|
-
return { _id: new ObjectId(id) };
|
|
116
|
-
}
|
|
117
|
-
if (field === 'id') {
|
|
118
|
-
return { _id: isObjectIdHex(id) ? new ObjectId(id) : id };
|
|
119
|
-
}
|
|
120
|
-
if (refs.has(field)) {
|
|
121
|
-
return { [field]: isObjectIdHex(id) ? new ObjectId(id) : id };
|
|
122
|
-
}
|
|
123
|
-
return { [field]: id };
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Name of the system migration that converts a reference field's stored strings.
|
|
127
|
-
*
|
|
128
|
-
* The `@1` is the body's version: the shared body below fingerprints identically for
|
|
129
|
-
* every field, so an edit to it would raise `MigrationConflict` against every ledger on
|
|
130
|
-
* the next boot. Any semantic change to {@link convertReferenceField} must bump this
|
|
131
|
-
* suffix instead — the old name stays applied, the new one runs (idempotently) once.
|
|
132
|
-
*/
|
|
133
|
-
export const refMigrationName = (field) => `$ref:${field}@1`;
|
|
134
|
-
/** Ledger registered body of the system reference migration. */
|
|
135
14
|
export const makeRefMigration = (field) => async (tx) => {
|
|
136
15
|
await convertReferenceField(tx.collection, field);
|
|
137
16
|
};
|
|
@@ -148,21 +27,10 @@ const convertScalar = (path) => ({
|
|
|
148
27
|
]
|
|
149
28
|
});
|
|
150
29
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* Idempotent and interrupt safe: it matches only documents where the field (or one of
|
|
155
|
-
* its elements) is still a string, converts only values that are actually 24 hex, and
|
|
156
|
-
* leaves everything else exactly as it was. Safe to run concurrently from several
|
|
157
|
-
* replicas — a document converts once, the loser's filter no longer matches it.
|
|
158
|
-
*
|
|
159
|
-
* Validation is bypassed deliberately: at `Pre` stage the collection still carries the
|
|
160
|
-
* validator that declares the field a *string*, and after the switch a legacy document
|
|
161
|
-
* may violate the schema in unrelated ways — either would wedge the boot on a write
|
|
162
|
-
* that only makes the data more correct. (Bypassing requires the connection's user to
|
|
163
|
-
* hold the `bypassDocumentValidation` privilege — `dbOwner`/`root` do.)
|
|
30
|
+
* The body {@link makeRefMigration} runs, kept at module level for the same reason it is; the
|
|
31
|
+
* helper exposes the very same function as `convertReferenceField`.
|
|
164
32
|
*/
|
|
165
|
-
|
|
33
|
+
const convertReferenceField = async (collection, field) => {
|
|
166
34
|
const result = await collection.updateMany(
|
|
167
35
|
/** An array valued field matches `$type: 'string'` when any element is a string. */
|
|
168
36
|
{ [field]: { $type: 'string' } }, [{
|
|
@@ -178,20 +46,109 @@ export const convertReferenceField = async (collection, field) => {
|
|
|
178
46
|
}], { bypassDocumentValidation: true });
|
|
179
47
|
return result.modifiedCount;
|
|
180
48
|
};
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
49
|
+
export const createMongoRefHelper = () => {
|
|
50
|
+
const isObjectIdHex = (value) => typeof value === 'string' && HEX24.test(value);
|
|
51
|
+
const marshalReference = (field, value) => {
|
|
52
|
+
if (value == null) {
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
if (value instanceof ObjectId) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
return value.map(item => marshalReference(field, item));
|
|
60
|
+
}
|
|
61
|
+
if (isObjectIdHex(value)) {
|
|
62
|
+
return new ObjectId(value);
|
|
63
|
+
}
|
|
64
|
+
throw new MisshapedRecord(`ref:${field}`);
|
|
65
|
+
};
|
|
66
|
+
const demarshalReference = (value) => {
|
|
67
|
+
if (value instanceof ObjectId) {
|
|
68
|
+
return value.toString();
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(value)) {
|
|
71
|
+
return value.map(demarshalReference);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
};
|
|
75
|
+
const demarshalRefs = (record, refs) => {
|
|
76
|
+
if (refs.size < 1) {
|
|
77
|
+
return record;
|
|
78
|
+
}
|
|
79
|
+
for (const field of refs.keys()) {
|
|
80
|
+
const value = record[field];
|
|
81
|
+
if (value != null) {
|
|
82
|
+
record[field] = demarshalReference(value);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return record;
|
|
86
|
+
};
|
|
87
|
+
const marshalCriteriaValue = (value) => {
|
|
88
|
+
if (typeof value === 'string') {
|
|
89
|
+
return isObjectIdHex(value) ? new ObjectId(value) : value;
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(value)) {
|
|
92
|
+
return value.map(marshalCriteriaValue);
|
|
93
|
+
}
|
|
94
|
+
if (value != null && typeof value === 'object' && !(value instanceof ObjectId) && !(value instanceof Date)) {
|
|
95
|
+
return Object.fromEntries(Object.entries(value).map(([operator, operand]) => OPAQUE_OPERATORS.includes(operator)
|
|
96
|
+
? [operator, operand]
|
|
97
|
+
: [operator, marshalCriteriaValue(operand)]));
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
};
|
|
101
|
+
const marshalCriteria = (criteria, refs) => {
|
|
102
|
+
if (criteria == null) {
|
|
103
|
+
return criteria;
|
|
104
|
+
}
|
|
105
|
+
return Object.fromEntries(Object.entries(criteria).map(([key, value]) => {
|
|
106
|
+
if (LOGICAL_OPERATORS.includes(key) && Array.isArray(value)) {
|
|
107
|
+
return [key, value.map(sub => marshalCriteria(sub, refs))];
|
|
108
|
+
}
|
|
109
|
+
if (key === 'id' || key === '_id') {
|
|
110
|
+
return ['_id', marshalCriteriaValue(value)];
|
|
111
|
+
}
|
|
112
|
+
if (refs.has(key)) {
|
|
113
|
+
return [key, marshalCriteriaValue(value)];
|
|
114
|
+
}
|
|
115
|
+
return [key, value];
|
|
116
|
+
}));
|
|
117
|
+
};
|
|
118
|
+
const identityCriteria = (field, id, refs) => {
|
|
119
|
+
if (field === '_id') {
|
|
120
|
+
return { _id: new ObjectId(id) };
|
|
121
|
+
}
|
|
122
|
+
if (field === 'id') {
|
|
123
|
+
return { _id: isObjectIdHex(id) ? new ObjectId(id) : id };
|
|
124
|
+
}
|
|
125
|
+
if (refs.has(field)) {
|
|
126
|
+
return { [field]: isObjectIdHex(id) ? new ObjectId(id) : id };
|
|
127
|
+
}
|
|
128
|
+
return { [field]: id };
|
|
129
|
+
};
|
|
130
|
+
const refMigrationName = (field) => `$ref:${field}@1`;
|
|
131
|
+
const reconcileReferences = async (collection, refs, alias) => {
|
|
132
|
+
for (const ref of refs) {
|
|
133
|
+
const remnant = await collection.findOne({ [ref.field]: { $type: 'string', $regex: HEX24 } }, { projection: { _id: 1 } });
|
|
134
|
+
if (remnant != null) {
|
|
135
|
+
const converted = await convertReferenceField(collection, ref.field);
|
|
136
|
+
log.warn('Reference field held string ids outside the migration ledger; converted', {
|
|
137
|
+
resource: alias, field: ref.field, converted,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
return {
|
|
143
|
+
isObjectIdHex, marshalReference, demarshalReference, demarshalRefs, marshalCriteria, identityCriteria,
|
|
144
|
+
refMigrationName, convertReferenceField, reconcileReferences
|
|
145
|
+
};
|
|
196
146
|
};
|
|
147
|
+
export const mongoRefHelper = createMongoRefHelper();
|
|
148
|
+
/** @deprecated compat:factory-refactor — use `mongoRefHelper.isObjectIdHex(…)` */
|
|
149
|
+
export const isObjectIdHex = (value) => mongoRefHelper.isObjectIdHex(value);
|
|
150
|
+
/** @deprecated compat:factory-refactor — use `mongoRefHelper.marshalReference(…)` */
|
|
151
|
+
export const marshalReference = (field, value) => mongoRefHelper.marshalReference(field, value);
|
|
152
|
+
/** @deprecated compat:factory-refactor — use `mongoRefHelper.demarshalRefs(…)` */
|
|
153
|
+
export const demarshalRefs = (record, refs) => mongoRefHelper.demarshalRefs(record, refs);
|
|
197
154
|
//# sourceMappingURL=refs.js.map
|
package/build/utils/refs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refs.js","sourceRoot":"","sources":["../../src/utils/refs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"refs.js","sourceRoot":"","sources":["../../src/utils/refs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAkC,MAAM,SAAS,CAAA;AAClE,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAGtC,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAG9E,MAAM,GAAG,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAEpC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,EAAE,EAAW,EAAiB,EAAE;IACtF,MAAM,qBAAqB,CAAC,EAAE,CAAC,UAAU,EAAE,KAAK,CAAC,CAAA;AACnD,CAAC,CAAA;AAED,MAAM,aAAa,GAAG,CAAC,IAAY,EAAY,EAAE,CAAC,CAAC;IACjD,KAAK,EAAE;QACL;YACE,IAAI,EAAE;gBACJ,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,QAAQ,CAAC,EAAE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AC,CAAA;AAEzH,kFAAkF;AAClF,MAAM,CAAC,MAAM,aAAa,GAAG,CAAe,MAAS,EAAE,IAAiC,EAAK,EAAE,CAC7F,cAAc,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnySchema } from 'ajv';
|
|
2
|
+
import type { Document } from 'mongodb';
|
|
3
|
+
import type { MongoReference } from '../../types.js';
|
|
4
|
+
/** An AJV schema as the `$jsonSchema` validator of a collection. */
|
|
5
|
+
export interface MongoSchemaUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Declared references are stored as `ObjectId`s while the AJV schema — which describes
|
|
8
|
+
* the records the app exchanges — keeps calling them strings. The collection validator
|
|
9
|
+
* describes what's stored, so the reference fields are overridden here after the plain
|
|
10
|
+
* conversion. Nullability and array shape carry over from the declared property.
|
|
11
|
+
*/
|
|
12
|
+
applyReferenceTypes: (mongoSchema: Document, schema: AnySchema, refs: MongoReference[]) => Document;
|
|
13
|
+
/** The plain conversion of an object schema into a `$jsonSchema` document. */
|
|
14
|
+
schemaToMongoSchema: (schema: AnySchema) => Document;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/utils/schema/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEvC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,oEAAoE;AACpE,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,mBAAmB,EAAE,CAAC,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,QAAQ,CAAA;IACnG,8EAA8E;IAC9E,mBAAmB,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,QAAQ,CAAA;CACrD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/schema/types.ts"],"names":[],"mappings":""}
|
package/build/utils/schema.d.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Declared references are stored as `ObjectId`s while the AJV schema — which describes
|
|
6
|
-
* the records the app exchanges — keeps calling them strings. The collection validator
|
|
7
|
-
* describes what's stored, so the reference fields are overridden here after the plain
|
|
8
|
-
* conversion. Nullability and array shape carry over from the declared property.
|
|
9
|
-
*/
|
|
10
|
-
export declare const applyReferenceTypes: (mongoSchema: Document, schema: AnySchema, refs: MongoReference[]) => Document;
|
|
11
|
-
export declare const schemaToMongoSchema: (schema: AnySchema) => Document;
|
|
1
|
+
import type { MongoSchemaUtils } from './schema/types.js';
|
|
2
|
+
export declare const createMongoSchemaUtils: () => MongoSchemaUtils;
|
|
3
|
+
export declare const mongoSchemaUtils: MongoSchemaUtils;
|
|
12
4
|
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/utils/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/utils/schema.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAEzD,eAAO,MAAM,sBAAsB,QAAO,gBAiHzC,CAAA;AAED,eAAO,MAAM,gBAAgB,kBAA2B,CAAA"}
|
package/build/utils/schema.js
CHANGED
|
@@ -1,94 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* conversion. Nullability and array shape carry over from the declared property.
|
|
6
|
-
*/
|
|
7
|
-
export const applyReferenceTypes = (mongoSchema, schema, refs) => {
|
|
8
|
-
if (mongoSchema.properties == null || refs.length < 1) {
|
|
9
|
-
return mongoSchema;
|
|
10
|
-
}
|
|
11
|
-
const properties = schema.properties ?? {};
|
|
12
|
-
for (const ref of refs) {
|
|
13
|
-
const declared = properties[ref.field];
|
|
14
|
-
if (declared == null || mongoSchema.properties[ref.field] == null) {
|
|
15
|
-
continue;
|
|
1
|
+
export const createMongoSchemaUtils = () => {
|
|
2
|
+
const applyReferenceTypes = (mongoSchema, schema, refs) => {
|
|
3
|
+
if (mongoSchema.properties == null || refs.length < 1) {
|
|
4
|
+
return mongoSchema;
|
|
16
5
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
6
|
+
const properties = schema.properties ?? {};
|
|
7
|
+
for (const ref of refs) {
|
|
8
|
+
const declared = properties[ref.field];
|
|
9
|
+
if (declared == null || mongoSchema.properties[ref.field] == null) {
|
|
10
|
+
continue;
|
|
21
11
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
const mongoSchems = {
|
|
32
|
-
bsonType: 'object',
|
|
33
|
-
properties: _schema.properties != null
|
|
34
|
-
? 'allOf' in _schema.properties
|
|
35
|
-
? covertAllOfProperties(_schema.properties.allOf) :
|
|
36
|
-
convertProperties(_schema.properties ?? {})
|
|
37
|
-
: undefined,
|
|
38
|
-
additionalProperties: _schema.additionalProperties != null
|
|
39
|
-
? convertAdditionalProperties(_schema.additionalProperties)
|
|
40
|
-
: false,
|
|
41
|
-
...(_schema.required != null ? { required: _schema.required } : {}),
|
|
12
|
+
mongoSchema.properties[ref.field] = declared.type === 'array'
|
|
13
|
+
? {
|
|
14
|
+
bsonType: declared.nullable ? ['array', 'null'] : 'array',
|
|
15
|
+
items: { bsonType: 'objectId' }
|
|
16
|
+
}
|
|
17
|
+
: { bsonType: declared.nullable ? ['objectId', 'null'] : 'objectId' };
|
|
18
|
+
}
|
|
19
|
+
return mongoSchema;
|
|
42
20
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const prepareScalarType = (value) => {
|
|
70
|
-
const type = (Array.isArray(value.type) ? value.type : [value.type])
|
|
71
|
-
.map(type => type === 'boolean' ? 'bool' : type);
|
|
72
|
-
if (value.nullable) {
|
|
73
|
-
type.push('null');
|
|
74
|
-
}
|
|
75
|
-
return type.length === 1 ? type[0] : type;
|
|
76
|
-
};
|
|
77
|
-
const convertToBsonType = (value) => {
|
|
78
|
-
if (Array.isArray(value.type)) {
|
|
79
|
-
if ('properties' in value || 'additionalProperties' in value) {
|
|
80
|
-
return {
|
|
81
|
-
...schemaToMongoSchema({ ...value, type: 'object' }),
|
|
82
|
-
bsonType: prepareScalarType(value)
|
|
83
|
-
};
|
|
21
|
+
const schemaToMongoSchema = (schema) => {
|
|
22
|
+
const _schema = schema;
|
|
23
|
+
if (_schema.type !== 'object') {
|
|
24
|
+
throw new SyntaxError('Only object schemas can be converted to mongo schema');
|
|
25
|
+
}
|
|
26
|
+
const mongoSchems = {
|
|
27
|
+
bsonType: 'object',
|
|
28
|
+
properties: _schema.properties != null
|
|
29
|
+
? 'allOf' in _schema.properties
|
|
30
|
+
? covertAllOfProperties(_schema.properties.allOf) :
|
|
31
|
+
convertProperties(_schema.properties ?? {})
|
|
32
|
+
: undefined,
|
|
33
|
+
additionalProperties: _schema.additionalProperties != null
|
|
34
|
+
? convertAdditionalProperties(_schema.additionalProperties)
|
|
35
|
+
: false,
|
|
36
|
+
...(_schema.required != null ? { required: _schema.required } : {}),
|
|
37
|
+
};
|
|
38
|
+
if (_schema.nullable) {
|
|
39
|
+
mongoSchems.bsonType = [mongoSchems.bsonType, 'null'];
|
|
40
|
+
}
|
|
41
|
+
if ("properties" in mongoSchems && mongoSchems.properties == null) {
|
|
42
|
+
delete mongoSchems.properties;
|
|
43
|
+
}
|
|
44
|
+
if ("required" in mongoSchems && mongoSchems.required == null
|
|
45
|
+
|| Array.isArray(mongoSchems.required) && mongoSchems.required.length === 0) {
|
|
46
|
+
delete mongoSchems.required;
|
|
84
47
|
}
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
48
|
+
return mongoSchems;
|
|
49
|
+
};
|
|
50
|
+
const convertAdditionalProperties = (additionalProperties) => {
|
|
51
|
+
if (typeof additionalProperties === 'boolean') {
|
|
52
|
+
return additionalProperties;
|
|
53
|
+
}
|
|
54
|
+
return convertToBsonType(additionalProperties);
|
|
55
|
+
};
|
|
56
|
+
const convertProperties = (properties) => Object.fromEntries(Object.entries(properties).map(([key, value]) => {
|
|
57
|
+
if (key === 'oneOf') {
|
|
58
|
+
throw new SyntaxError('We dont support schemas with oneof properties definition');
|
|
59
|
+
}
|
|
60
|
+
return [key, convertToBsonType(value)];
|
|
61
|
+
}));
|
|
62
|
+
const covertAllOfProperties = (allOf) => allOf.map(schema => convertProperties(schema.properties ?? {}))
|
|
63
|
+
.reduce((properties, schema) => ({ ...properties, ...schema }), {});
|
|
64
|
+
const prepareScalarType = (value) => {
|
|
65
|
+
const type = (Array.isArray(value.type) ? value.type : [value.type])
|
|
66
|
+
.map(type => type === 'boolean' ? 'bool' : type);
|
|
67
|
+
if (value.nullable) {
|
|
68
|
+
type.push('null');
|
|
69
|
+
}
|
|
70
|
+
return type.length === 1 ? type[0] : type;
|
|
71
|
+
};
|
|
72
|
+
const convertToBsonType = (value) => {
|
|
73
|
+
if (Array.isArray(value.type)) {
|
|
74
|
+
if ('properties' in value || 'additionalProperties' in value) {
|
|
75
|
+
return {
|
|
76
|
+
...schemaToMongoSchema({ ...value, type: 'object' }),
|
|
77
|
+
bsonType: prepareScalarType(value)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return { bsonType: prepareScalarType(value) };
|
|
81
|
+
}
|
|
82
|
+
return value.type === 'object'
|
|
83
|
+
// `DateSchema` is `{ type: 'object', format: 'date-time' }`, so a date is detected by
|
|
84
|
+
// format rather than type — but it still has to honor `nullable`, otherwise an optional
|
|
85
|
+
// date left unset (the driver writes `undefined` as `null`) fails collection validation.
|
|
86
|
+
? value.format === 'date-time' ? { bsonType: value.nullable ? ['date', 'null'] : 'date' } : schemaToMongoSchema(value)
|
|
87
|
+
: { bsonType: prepareScalarType(value) };
|
|
88
|
+
};
|
|
89
|
+
return { applyReferenceTypes, schemaToMongoSchema };
|
|
93
90
|
};
|
|
91
|
+
export const mongoSchemaUtils = createMongoSchemaUtils();
|
|
94
92
|
//# sourceMappingURL=schema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/utils/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/utils/schema.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAqB,EAAE;IAC3D,MAAM,mBAAmB,GAAG,CAC1B,WAAqB,EAAE,MAAiB,EAAE,IAAsB,EACtD,EAAE;QACZ,IAAI,WAAW,CAAC,UAAU,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACtD,OAAO,WAAW,CAAA;QACpB,CAAC;QACD,MAAM,UAAU,GACb,MAAkC,CAAC,UAAU,IAAI,EAAE,CAA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,YAAY,IAAI,KAAK,IAAI,sBAAsB,IAAI,KAAK,EAAE,CAAC;gBAC7D,OAAO;oBACL,GAAG,mBAAmB,CAAC,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;oBACpD,QAAQ,EAAE,iBAAiB,CAAC,KAAK,CAAC;iBACnC,CAAA;YACH,CAAC;YACD,OAAO,EAAE,QAAQ,EAAE,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAA;QAC/C,CAAC;QAED,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;YAC5B,sFAAsF;YACtF,wFAAwF;YACxF,yFAAyF;YACzF,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,mBAAmB,CAAC,KAAK,CAAC;YACtH,CAAC,CAAC,EAAE,QAAQ,EAAE,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAA;IAC5C,CAAC,CAAA;IAED,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,CAAA;AACrD,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,sBAAsB,EAAE,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MigrationStage } from '@owlmeans/resource';
|
|
2
|
+
export interface LedgerRecord {
|
|
3
|
+
alias: string;
|
|
4
|
+
name: string;
|
|
5
|
+
stage: MigrationStage;
|
|
6
|
+
checksum: string | null;
|
|
7
|
+
baseline: boolean;
|
|
8
|
+
startedAt: Date;
|
|
9
|
+
/** `null` while a replica is running it — the claim, not the completion. */
|
|
10
|
+
completedAt: Date | null;
|
|
11
|
+
durationMs: number | null;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=types.local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.local.d.ts","sourceRoot":"","sources":["../../src/utils/types.local.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,cAAc,CAAA;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,QAAQ,EAAE,OAAO,CAAA;IACjB,SAAS,EAAE,IAAI,CAAA;IACf,4EAA4E;IAC5E,WAAW,EAAE,IAAI,GAAG,IAAI,CAAA;IACxB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.local.js","sourceRoot":"","sources":["../../src/utils/types.local.ts"],"names":[],"mappings":""}
|