@prisma-next/family-mongo 0.0.1 → 0.3.0-dev.162
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 +81 -28
- package/dist/control.d.mts +18 -0
- package/dist/control.d.mts.map +1 -0
- package/dist/control.mjs +647 -0
- package/dist/control.mjs.map +1 -0
- package/dist/pack.d.mts +13 -0
- package/dist/pack.d.mts.map +1 -0
- package/dist/pack.mjs +12 -0
- package/dist/pack.mjs.map +1 -0
- package/package.json +13 -7
- package/src/core/control-instance.ts +290 -15
- package/src/core/mongo-target-descriptor.ts +25 -2
- package/src/core/schema-diff.ts +402 -0
- package/src/core/schema-to-view.ts +128 -0
- package/src/exports/pack.ts +10 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SchemaIssue,
|
|
3
|
+
SchemaVerificationNode,
|
|
4
|
+
} from '@prisma-next/framework-components/control';
|
|
5
|
+
import type {
|
|
6
|
+
MongoSchemaCollection,
|
|
7
|
+
MongoSchemaIndex,
|
|
8
|
+
MongoSchemaIR,
|
|
9
|
+
} from '@prisma-next/mongo-schema-ir';
|
|
10
|
+
import { canonicalize, deepEqual } from '@prisma-next/mongo-schema-ir';
|
|
11
|
+
|
|
12
|
+
export function diffMongoSchemas(
|
|
13
|
+
live: MongoSchemaIR,
|
|
14
|
+
expected: MongoSchemaIR,
|
|
15
|
+
strict: boolean,
|
|
16
|
+
): {
|
|
17
|
+
root: SchemaVerificationNode;
|
|
18
|
+
issues: SchemaIssue[];
|
|
19
|
+
counts: { pass: number; warn: number; fail: number; totalNodes: number };
|
|
20
|
+
} {
|
|
21
|
+
const issues: SchemaIssue[] = [];
|
|
22
|
+
const collectionChildren: SchemaVerificationNode[] = [];
|
|
23
|
+
let pass = 0;
|
|
24
|
+
let warn = 0;
|
|
25
|
+
let fail = 0;
|
|
26
|
+
|
|
27
|
+
const allNames = new Set([...live.collectionNames, ...expected.collectionNames]);
|
|
28
|
+
|
|
29
|
+
for (const name of [...allNames].sort()) {
|
|
30
|
+
const liveColl = live.collection(name);
|
|
31
|
+
const expectedColl = expected.collection(name);
|
|
32
|
+
|
|
33
|
+
if (!liveColl && expectedColl) {
|
|
34
|
+
issues.push({
|
|
35
|
+
kind: 'missing_table',
|
|
36
|
+
table: name,
|
|
37
|
+
message: `Collection "${name}" is missing from the database`,
|
|
38
|
+
});
|
|
39
|
+
collectionChildren.push({
|
|
40
|
+
status: 'fail',
|
|
41
|
+
kind: 'collection',
|
|
42
|
+
name,
|
|
43
|
+
contractPath: `storage.collections.${name}`,
|
|
44
|
+
code: 'MISSING_COLLECTION',
|
|
45
|
+
message: `Collection "${name}" is missing`,
|
|
46
|
+
expected: name,
|
|
47
|
+
actual: null,
|
|
48
|
+
children: [],
|
|
49
|
+
});
|
|
50
|
+
fail++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (liveColl && !expectedColl) {
|
|
55
|
+
const status = strict ? 'fail' : 'warn';
|
|
56
|
+
issues.push({
|
|
57
|
+
kind: 'extra_table',
|
|
58
|
+
table: name,
|
|
59
|
+
message: `Extra collection "${name}" exists in the database but not in the contract`,
|
|
60
|
+
});
|
|
61
|
+
collectionChildren.push({
|
|
62
|
+
status,
|
|
63
|
+
kind: 'collection',
|
|
64
|
+
name,
|
|
65
|
+
contractPath: `storage.collections.${name}`,
|
|
66
|
+
code: 'EXTRA_COLLECTION',
|
|
67
|
+
message: `Extra collection "${name}" found`,
|
|
68
|
+
expected: null,
|
|
69
|
+
actual: name,
|
|
70
|
+
children: [],
|
|
71
|
+
});
|
|
72
|
+
if (status === 'fail') fail++;
|
|
73
|
+
else warn++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const lc = liveColl as MongoSchemaCollection;
|
|
78
|
+
const ec = expectedColl as MongoSchemaCollection;
|
|
79
|
+
const indexChildren = diffIndexes(name, lc, ec, strict, issues);
|
|
80
|
+
const validatorChildren = diffValidator(name, lc, ec, strict, issues);
|
|
81
|
+
const optionsChildren = diffOptions(name, lc, ec, strict, issues);
|
|
82
|
+
const children = [...indexChildren, ...validatorChildren, ...optionsChildren];
|
|
83
|
+
|
|
84
|
+
const worstStatus = children.reduce<'pass' | 'warn' | 'fail'>(
|
|
85
|
+
(s, c) => (c.status === 'fail' ? 'fail' : c.status === 'warn' && s !== 'fail' ? 'warn' : s),
|
|
86
|
+
'pass',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
for (const c of children) {
|
|
90
|
+
if (c.status === 'pass') pass++;
|
|
91
|
+
else if (c.status === 'warn') warn++;
|
|
92
|
+
else fail++;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (children.length === 0) {
|
|
96
|
+
pass++;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
collectionChildren.push({
|
|
100
|
+
status: worstStatus,
|
|
101
|
+
kind: 'collection',
|
|
102
|
+
name,
|
|
103
|
+
contractPath: `storage.collections.${name}`,
|
|
104
|
+
code: worstStatus === 'pass' ? 'MATCH' : 'DRIFT',
|
|
105
|
+
message:
|
|
106
|
+
worstStatus === 'pass' ? `Collection "${name}" matches` : `Collection "${name}" has drift`,
|
|
107
|
+
expected: name,
|
|
108
|
+
actual: name,
|
|
109
|
+
children,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const rootStatus = fail > 0 ? 'fail' : warn > 0 ? 'warn' : 'pass';
|
|
114
|
+
const totalNodes = pass + warn + fail + collectionChildren.length;
|
|
115
|
+
|
|
116
|
+
const root: SchemaVerificationNode = {
|
|
117
|
+
status: rootStatus,
|
|
118
|
+
kind: 'root',
|
|
119
|
+
name: 'mongo-schema',
|
|
120
|
+
contractPath: 'storage',
|
|
121
|
+
code: rootStatus === 'pass' ? 'MATCH' : 'DRIFT',
|
|
122
|
+
message: rootStatus === 'pass' ? 'Schema matches' : 'Schema has drift',
|
|
123
|
+
expected: null,
|
|
124
|
+
actual: null,
|
|
125
|
+
children: collectionChildren,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return { root, issues, counts: { pass, warn, fail, totalNodes } };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function buildIndexLookupKey(index: MongoSchemaIndex): string {
|
|
132
|
+
const keys = index.keys.map((k) => `${k.field}:${k.direction}`).join(',');
|
|
133
|
+
const opts = [
|
|
134
|
+
index.unique ? 'unique' : '',
|
|
135
|
+
index.sparse ? 'sparse' : '',
|
|
136
|
+
index.expireAfterSeconds != null ? `ttl:${index.expireAfterSeconds}` : '',
|
|
137
|
+
index.partialFilterExpression ? `pfe:${canonicalize(index.partialFilterExpression)}` : '',
|
|
138
|
+
index.wildcardProjection ? `wp:${canonicalize(index.wildcardProjection)}` : '',
|
|
139
|
+
index.collation ? `col:${canonicalize(index.collation)}` : '',
|
|
140
|
+
index.weights ? `wt:${canonicalize(index.weights)}` : '',
|
|
141
|
+
index.default_language ? `dl:${index.default_language}` : '',
|
|
142
|
+
index.language_override ? `lo:${index.language_override}` : '',
|
|
143
|
+
]
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.join(';');
|
|
146
|
+
return opts ? `${keys}|${opts}` : keys;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function formatIndexName(index: MongoSchemaIndex): string {
|
|
150
|
+
return index.keys.map((k) => `${k.field}:${k.direction}`).join(', ');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function diffIndexes(
|
|
154
|
+
collName: string,
|
|
155
|
+
live: MongoSchemaCollection,
|
|
156
|
+
expected: MongoSchemaCollection,
|
|
157
|
+
strict: boolean,
|
|
158
|
+
issues: SchemaIssue[],
|
|
159
|
+
): SchemaVerificationNode[] {
|
|
160
|
+
const nodes: SchemaVerificationNode[] = [];
|
|
161
|
+
const liveLookup = new Map<string, MongoSchemaIndex>();
|
|
162
|
+
for (const idx of live.indexes) liveLookup.set(buildIndexLookupKey(idx), idx);
|
|
163
|
+
|
|
164
|
+
const expectedLookup = new Map<string, MongoSchemaIndex>();
|
|
165
|
+
for (const idx of expected.indexes) expectedLookup.set(buildIndexLookupKey(idx), idx);
|
|
166
|
+
|
|
167
|
+
for (const [key, idx] of expectedLookup) {
|
|
168
|
+
if (liveLookup.has(key)) {
|
|
169
|
+
nodes.push({
|
|
170
|
+
status: 'pass',
|
|
171
|
+
kind: 'index',
|
|
172
|
+
name: formatIndexName(idx),
|
|
173
|
+
contractPath: `storage.collections.${collName}.indexes`,
|
|
174
|
+
code: 'MATCH',
|
|
175
|
+
message: `Index ${formatIndexName(idx)} matches`,
|
|
176
|
+
expected: key,
|
|
177
|
+
actual: key,
|
|
178
|
+
children: [],
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
issues.push({
|
|
182
|
+
kind: 'index_mismatch',
|
|
183
|
+
table: collName,
|
|
184
|
+
indexOrConstraint: formatIndexName(idx),
|
|
185
|
+
message: `Index ${formatIndexName(idx)} missing on collection "${collName}"`,
|
|
186
|
+
});
|
|
187
|
+
nodes.push({
|
|
188
|
+
status: 'fail',
|
|
189
|
+
kind: 'index',
|
|
190
|
+
name: formatIndexName(idx),
|
|
191
|
+
contractPath: `storage.collections.${collName}.indexes`,
|
|
192
|
+
code: 'MISSING_INDEX',
|
|
193
|
+
message: `Index ${formatIndexName(idx)} missing`,
|
|
194
|
+
expected: key,
|
|
195
|
+
actual: null,
|
|
196
|
+
children: [],
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
for (const [key, idx] of liveLookup) {
|
|
202
|
+
if (!expectedLookup.has(key)) {
|
|
203
|
+
const status = strict ? 'fail' : 'warn';
|
|
204
|
+
issues.push({
|
|
205
|
+
kind: 'extra_index',
|
|
206
|
+
table: collName,
|
|
207
|
+
indexOrConstraint: formatIndexName(idx),
|
|
208
|
+
message: `Extra index ${formatIndexName(idx)} on collection "${collName}"`,
|
|
209
|
+
});
|
|
210
|
+
nodes.push({
|
|
211
|
+
status,
|
|
212
|
+
kind: 'index',
|
|
213
|
+
name: formatIndexName(idx),
|
|
214
|
+
contractPath: `storage.collections.${collName}.indexes`,
|
|
215
|
+
code: 'EXTRA_INDEX',
|
|
216
|
+
message: `Extra index ${formatIndexName(idx)}`,
|
|
217
|
+
expected: null,
|
|
218
|
+
actual: key,
|
|
219
|
+
children: [],
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return nodes;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function diffValidator(
|
|
228
|
+
collName: string,
|
|
229
|
+
live: MongoSchemaCollection,
|
|
230
|
+
expected: MongoSchemaCollection,
|
|
231
|
+
strict: boolean,
|
|
232
|
+
issues: SchemaIssue[],
|
|
233
|
+
): SchemaVerificationNode[] {
|
|
234
|
+
if (!live.validator && !expected.validator) return [];
|
|
235
|
+
|
|
236
|
+
if (expected.validator && !live.validator) {
|
|
237
|
+
issues.push({
|
|
238
|
+
kind: 'type_missing',
|
|
239
|
+
table: collName,
|
|
240
|
+
message: `Validator missing on collection "${collName}"`,
|
|
241
|
+
});
|
|
242
|
+
return [
|
|
243
|
+
{
|
|
244
|
+
status: 'fail',
|
|
245
|
+
kind: 'validator',
|
|
246
|
+
name: 'validator',
|
|
247
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
248
|
+
code: 'MISSING_VALIDATOR',
|
|
249
|
+
message: 'Validator missing',
|
|
250
|
+
expected: canonicalize(expected.validator.jsonSchema),
|
|
251
|
+
actual: null,
|
|
252
|
+
children: [],
|
|
253
|
+
},
|
|
254
|
+
];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (!expected.validator && live.validator) {
|
|
258
|
+
const status = strict ? 'fail' : 'warn';
|
|
259
|
+
issues.push({
|
|
260
|
+
kind: 'extra_validator',
|
|
261
|
+
table: collName,
|
|
262
|
+
message: `Extra validator on collection "${collName}"`,
|
|
263
|
+
});
|
|
264
|
+
return [
|
|
265
|
+
{
|
|
266
|
+
status,
|
|
267
|
+
kind: 'validator',
|
|
268
|
+
name: 'validator',
|
|
269
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
270
|
+
code: 'EXTRA_VALIDATOR',
|
|
271
|
+
message: 'Extra validator found',
|
|
272
|
+
expected: null,
|
|
273
|
+
actual: canonicalize(live.validator.jsonSchema),
|
|
274
|
+
children: [],
|
|
275
|
+
},
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const liveVal = live.validator as NonNullable<typeof live.validator>;
|
|
280
|
+
const expectedVal = expected.validator as NonNullable<typeof expected.validator>;
|
|
281
|
+
const liveSchema = canonicalize(liveVal.jsonSchema);
|
|
282
|
+
const expectedSchema = canonicalize(expectedVal.jsonSchema);
|
|
283
|
+
|
|
284
|
+
if (
|
|
285
|
+
liveSchema !== expectedSchema ||
|
|
286
|
+
liveVal.validationLevel !== expectedVal.validationLevel ||
|
|
287
|
+
liveVal.validationAction !== expectedVal.validationAction
|
|
288
|
+
) {
|
|
289
|
+
issues.push({
|
|
290
|
+
kind: 'type_mismatch',
|
|
291
|
+
table: collName,
|
|
292
|
+
expected: expectedSchema,
|
|
293
|
+
actual: liveSchema,
|
|
294
|
+
message: `Validator mismatch on collection "${collName}"`,
|
|
295
|
+
});
|
|
296
|
+
return [
|
|
297
|
+
{
|
|
298
|
+
status: 'fail',
|
|
299
|
+
kind: 'validator',
|
|
300
|
+
name: 'validator',
|
|
301
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
302
|
+
code: 'VALIDATOR_MISMATCH',
|
|
303
|
+
message: 'Validator mismatch',
|
|
304
|
+
expected: {
|
|
305
|
+
jsonSchema: expectedVal.jsonSchema,
|
|
306
|
+
validationLevel: expectedVal.validationLevel,
|
|
307
|
+
validationAction: expectedVal.validationAction,
|
|
308
|
+
},
|
|
309
|
+
actual: {
|
|
310
|
+
jsonSchema: liveVal.jsonSchema,
|
|
311
|
+
validationLevel: liveVal.validationLevel,
|
|
312
|
+
validationAction: liveVal.validationAction,
|
|
313
|
+
},
|
|
314
|
+
children: [],
|
|
315
|
+
},
|
|
316
|
+
];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return [
|
|
320
|
+
{
|
|
321
|
+
status: 'pass',
|
|
322
|
+
kind: 'validator',
|
|
323
|
+
name: 'validator',
|
|
324
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
325
|
+
code: 'MATCH',
|
|
326
|
+
message: 'Validator matches',
|
|
327
|
+
expected: expectedSchema,
|
|
328
|
+
actual: liveSchema,
|
|
329
|
+
children: [],
|
|
330
|
+
},
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function diffOptions(
|
|
335
|
+
collName: string,
|
|
336
|
+
live: MongoSchemaCollection,
|
|
337
|
+
expected: MongoSchemaCollection,
|
|
338
|
+
strict: boolean,
|
|
339
|
+
issues: SchemaIssue[],
|
|
340
|
+
): SchemaVerificationNode[] {
|
|
341
|
+
if (!live.options && !expected.options) return [];
|
|
342
|
+
|
|
343
|
+
if (!expected.options && live.options) {
|
|
344
|
+
const status = strict ? 'fail' : 'warn';
|
|
345
|
+
issues.push({
|
|
346
|
+
kind: 'type_mismatch',
|
|
347
|
+
table: collName,
|
|
348
|
+
actual: canonicalize(live.options),
|
|
349
|
+
message: `Extra collection options on "${collName}"`,
|
|
350
|
+
});
|
|
351
|
+
return [
|
|
352
|
+
{
|
|
353
|
+
status,
|
|
354
|
+
kind: 'options',
|
|
355
|
+
name: 'options',
|
|
356
|
+
contractPath: `storage.collections.${collName}.options`,
|
|
357
|
+
code: 'EXTRA_OPTIONS',
|
|
358
|
+
message: 'Extra collection options found',
|
|
359
|
+
expected: null,
|
|
360
|
+
actual: live.options,
|
|
361
|
+
children: [],
|
|
362
|
+
},
|
|
363
|
+
];
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (deepEqual(live.options, expected.options)) {
|
|
367
|
+
return [
|
|
368
|
+
{
|
|
369
|
+
status: 'pass',
|
|
370
|
+
kind: 'options',
|
|
371
|
+
name: 'options',
|
|
372
|
+
contractPath: `storage.collections.${collName}.options`,
|
|
373
|
+
code: 'MATCH',
|
|
374
|
+
message: 'Collection options match',
|
|
375
|
+
expected: canonicalize(expected.options),
|
|
376
|
+
actual: canonicalize(live.options),
|
|
377
|
+
children: [],
|
|
378
|
+
},
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
issues.push({
|
|
383
|
+
kind: 'type_mismatch',
|
|
384
|
+
table: collName,
|
|
385
|
+
expected: canonicalize(expected.options),
|
|
386
|
+
actual: canonicalize(live.options),
|
|
387
|
+
message: `Collection options mismatch on "${collName}"`,
|
|
388
|
+
});
|
|
389
|
+
return [
|
|
390
|
+
{
|
|
391
|
+
status: 'fail',
|
|
392
|
+
kind: 'options',
|
|
393
|
+
name: 'options',
|
|
394
|
+
contractPath: `storage.collections.${collName}.options`,
|
|
395
|
+
code: 'OPTIONS_MISMATCH',
|
|
396
|
+
message: 'Collection options mismatch',
|
|
397
|
+
expected: expected.options,
|
|
398
|
+
actual: live.options,
|
|
399
|
+
children: [],
|
|
400
|
+
},
|
|
401
|
+
];
|
|
402
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { CoreSchemaView } from '@prisma-next/framework-components/control';
|
|
2
|
+
import { SchemaTreeNode } from '@prisma-next/framework-components/control';
|
|
3
|
+
import type { MongoSchemaCollection, MongoSchemaIR } from '@prisma-next/mongo-schema-ir';
|
|
4
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
5
|
+
|
|
6
|
+
export function mongoSchemaToView(schema: MongoSchemaIR): CoreSchemaView {
|
|
7
|
+
const collectionNodes = schema.collections.map((collection) =>
|
|
8
|
+
collectionToSchemaNode(collection.name, collection),
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
root: new SchemaTreeNode({
|
|
13
|
+
kind: 'root',
|
|
14
|
+
id: 'mongo-schema',
|
|
15
|
+
label: 'database',
|
|
16
|
+
...ifDefined('children', collectionNodes.length > 0 ? collectionNodes : undefined),
|
|
17
|
+
}),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function collectionToSchemaNode(name: string, collection: MongoSchemaCollection): SchemaTreeNode {
|
|
22
|
+
const children: SchemaTreeNode[] = [];
|
|
23
|
+
|
|
24
|
+
for (const index of collection.indexes) {
|
|
25
|
+
const keysSummary = index.keys
|
|
26
|
+
.map((k) => {
|
|
27
|
+
if (k.direction === 1) return k.field;
|
|
28
|
+
if (k.direction === -1) return `${k.field} desc`;
|
|
29
|
+
return `${k.field} ${k.direction}`;
|
|
30
|
+
})
|
|
31
|
+
.join(', ');
|
|
32
|
+
const prefix = index.unique ? 'unique index' : 'index';
|
|
33
|
+
const options: string[] = [];
|
|
34
|
+
if (index.sparse) options.push('sparse');
|
|
35
|
+
if (index.expireAfterSeconds != null) options.push(`ttl: ${index.expireAfterSeconds}s`);
|
|
36
|
+
if (index.partialFilterExpression) options.push('partial');
|
|
37
|
+
const optsSuffix = options.length > 0 ? ` (${options.join(', ')})` : '';
|
|
38
|
+
|
|
39
|
+
children.push(
|
|
40
|
+
new SchemaTreeNode({
|
|
41
|
+
kind: 'index',
|
|
42
|
+
id: `index-${name}-${index.keys.map((k) => `${k.field}_${k.direction}`).join('_')}`,
|
|
43
|
+
label: `${prefix} (${keysSummary})${optsSuffix}`,
|
|
44
|
+
meta: {
|
|
45
|
+
keys: index.keys,
|
|
46
|
+
unique: index.unique,
|
|
47
|
+
...ifDefined('sparse', index.sparse || undefined),
|
|
48
|
+
...ifDefined('expireAfterSeconds', index.expireAfterSeconds ?? undefined),
|
|
49
|
+
...ifDefined('partialFilterExpression', index.partialFilterExpression ?? undefined),
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (collection.validator) {
|
|
56
|
+
const validatorChildren: SchemaTreeNode[] = [];
|
|
57
|
+
const jsonSchema = collection.validator.jsonSchema as Record<string, unknown>;
|
|
58
|
+
const properties = jsonSchema['properties'] as
|
|
59
|
+
| Record<string, Record<string, unknown>>
|
|
60
|
+
| undefined;
|
|
61
|
+
const required = new Set((jsonSchema['required'] as string[] | undefined) ?? []);
|
|
62
|
+
|
|
63
|
+
if (properties) {
|
|
64
|
+
for (const [propName, propDef] of Object.entries(properties)) {
|
|
65
|
+
const bsonType = (propDef['bsonType'] as string) ?? 'unknown';
|
|
66
|
+
const suffix = required.has(propName) ? ' (required)' : '';
|
|
67
|
+
validatorChildren.push(
|
|
68
|
+
new SchemaTreeNode({
|
|
69
|
+
kind: 'field',
|
|
70
|
+
id: `field-${name}-${propName}`,
|
|
71
|
+
label: `${propName}: ${bsonType}${suffix}`,
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
children.push(
|
|
78
|
+
new SchemaTreeNode({
|
|
79
|
+
kind: 'field',
|
|
80
|
+
id: `validator-${name}`,
|
|
81
|
+
label: `validator (level: ${collection.validator.validationLevel}, action: ${collection.validator.validationAction})`,
|
|
82
|
+
meta: {
|
|
83
|
+
validationLevel: collection.validator.validationLevel,
|
|
84
|
+
validationAction: collection.validator.validationAction,
|
|
85
|
+
jsonSchema: collection.validator.jsonSchema,
|
|
86
|
+
},
|
|
87
|
+
...ifDefined('children', validatorChildren.length > 0 ? validatorChildren : undefined),
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (collection.options) {
|
|
93
|
+
const opts = collection.options;
|
|
94
|
+
const optLabels: string[] = [];
|
|
95
|
+
if (opts.capped) optLabels.push('capped');
|
|
96
|
+
if (opts.timeseries) optLabels.push('timeseries');
|
|
97
|
+
if (opts.collation) optLabels.push('collation');
|
|
98
|
+
if (opts.changeStreamPreAndPostImages) optLabels.push('changeStreamPreAndPostImages');
|
|
99
|
+
if (opts.clusteredIndex) optLabels.push('clusteredIndex');
|
|
100
|
+
|
|
101
|
+
if (optLabels.length > 0) {
|
|
102
|
+
children.push(
|
|
103
|
+
new SchemaTreeNode({
|
|
104
|
+
kind: 'field',
|
|
105
|
+
id: `options-${name}`,
|
|
106
|
+
label: `options (${optLabels.join(', ')})`,
|
|
107
|
+
meta: {
|
|
108
|
+
...ifDefined('capped', opts.capped ?? undefined),
|
|
109
|
+
...ifDefined('timeseries', opts.timeseries ?? undefined),
|
|
110
|
+
...ifDefined('collation', opts.collation ?? undefined),
|
|
111
|
+
...ifDefined(
|
|
112
|
+
'changeStreamPreAndPostImages',
|
|
113
|
+
opts.changeStreamPreAndPostImages ?? undefined,
|
|
114
|
+
),
|
|
115
|
+
...ifDefined('clusteredIndex', opts.clusteredIndex ?? undefined),
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return new SchemaTreeNode({
|
|
123
|
+
kind: 'collection',
|
|
124
|
+
id: `collection-${name}`,
|
|
125
|
+
label: `collection ${name}`,
|
|
126
|
+
...ifDefined('children', children.length > 0 ? children : undefined),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FamilyPackRef } from '@prisma-next/framework-components/components';
|
|
2
|
+
|
|
3
|
+
const mongoFamilyPack = {
|
|
4
|
+
kind: 'family',
|
|
5
|
+
id: 'mongo',
|
|
6
|
+
familyId: 'mongo',
|
|
7
|
+
version: '0.0.1',
|
|
8
|
+
} as const;
|
|
9
|
+
|
|
10
|
+
export default mongoFamilyPack as typeof mongoFamilyPack & FamilyPackRef<'mongo'>;
|