pocketbase-zod-schema 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +209 -24
- package/dist/cli/index.cjs +406 -294
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.d.cts +3 -1
- package/dist/cli/index.d.ts +3 -1
- package/dist/cli/index.js +406 -294
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +406 -294
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +406 -294
- package/dist/cli/migrate.js.map +1 -1
- package/dist/cli/utils/index.d.cts +3 -1
- package/dist/cli/utils/index.d.ts +3 -1
- package/dist/fields-UcOPu1OQ.d.cts +364 -0
- package/dist/fields-UcOPu1OQ.d.ts +364 -0
- package/dist/index.cjs +633 -112
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +619 -101
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs +44 -0
- package/dist/migration/analyzer.cjs.map +1 -1
- package/dist/migration/analyzer.d.cts +2 -1
- package/dist/migration/analyzer.d.ts +2 -1
- package/dist/migration/analyzer.js +44 -0
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/diff.cjs +76 -1
- package/dist/migration/diff.cjs.map +1 -1
- package/dist/migration/diff.d.cts +3 -1
- package/dist/migration/diff.d.ts +3 -1
- package/dist/migration/diff.js +76 -1
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/generator.cjs +323 -46
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +60 -11
- package/dist/migration/generator.d.ts +60 -11
- package/dist/migration/generator.js +319 -47
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +433 -47
- package/dist/migration/index.cjs.map +1 -1
- package/dist/migration/index.d.cts +3 -2
- package/dist/migration/index.d.ts +3 -2
- package/dist/migration/index.js +432 -48
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.cjs.map +1 -1
- package/dist/migration/snapshot.d.cts +3 -1
- package/dist/migration/snapshot.d.ts +3 -1
- package/dist/migration/snapshot.js.map +1 -1
- package/dist/migration/utils/index.cjs +80 -0
- package/dist/migration/utils/index.cjs.map +1 -1
- package/dist/migration/utils/index.d.cts +39 -202
- package/dist/migration/utils/index.d.ts +39 -202
- package/dist/migration/utils/index.js +77 -1
- package/dist/migration/utils/index.js.map +1 -1
- package/dist/schema.cjs +200 -61
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +2 -85
- package/dist/schema.d.ts +2 -85
- package/dist/schema.js +186 -50
- package/dist/schema.js.map +1 -1
- package/dist/type-mapper-DrQmtznD.d.cts +208 -0
- package/dist/type-mapper-n231Fspm.d.ts +208 -0
- package/dist/{types-z1Dkjg8m.d.ts → types-Ds3NQvny.d.ts} +33 -2
- package/dist/{types-BbTgmg6H.d.cts → types-YoBjsa-A.d.cts} +33 -2
- package/package.json +1 -1
|
@@ -133,6 +133,53 @@ function generateTimestamp(config) {
|
|
|
133
133
|
}
|
|
134
134
|
return Math.floor(Date.now() / 1e3).toString();
|
|
135
135
|
}
|
|
136
|
+
function splitDiffByCollection(diff, baseTimestamp) {
|
|
137
|
+
const operations = [];
|
|
138
|
+
let currentTimestamp = parseInt(baseTimestamp, 10);
|
|
139
|
+
for (const collection of diff.collectionsToCreate) {
|
|
140
|
+
operations.push({
|
|
141
|
+
type: "create",
|
|
142
|
+
collection,
|
|
143
|
+
timestamp: currentTimestamp.toString()
|
|
144
|
+
});
|
|
145
|
+
currentTimestamp += 1;
|
|
146
|
+
}
|
|
147
|
+
for (const modification of diff.collectionsToModify) {
|
|
148
|
+
operations.push({
|
|
149
|
+
type: "modify",
|
|
150
|
+
collection: modification.collection,
|
|
151
|
+
modifications: modification,
|
|
152
|
+
timestamp: currentTimestamp.toString()
|
|
153
|
+
});
|
|
154
|
+
currentTimestamp += 1;
|
|
155
|
+
}
|
|
156
|
+
for (const collection of diff.collectionsToDelete) {
|
|
157
|
+
operations.push({
|
|
158
|
+
type: "delete",
|
|
159
|
+
collection: collection.name || collection,
|
|
160
|
+
// Handle both object and string
|
|
161
|
+
timestamp: currentTimestamp.toString()
|
|
162
|
+
});
|
|
163
|
+
currentTimestamp += 1;
|
|
164
|
+
}
|
|
165
|
+
return operations;
|
|
166
|
+
}
|
|
167
|
+
function generateCollectionMigrationFilename(operation) {
|
|
168
|
+
const timestamp = operation.timestamp;
|
|
169
|
+
const operationType = operation.type === "modify" ? "updated" : operation.type === "create" ? "created" : "deleted";
|
|
170
|
+
let collectionName;
|
|
171
|
+
if (typeof operation.collection === "string") {
|
|
172
|
+
collectionName = operation.collection;
|
|
173
|
+
} else {
|
|
174
|
+
collectionName = operation.collection.name;
|
|
175
|
+
}
|
|
176
|
+
const sanitizedName = collectionName.replace(/[^a-zA-Z0-9_]/g, "_").toLowerCase();
|
|
177
|
+
return `${timestamp}_${operationType}_${sanitizedName}.js`;
|
|
178
|
+
}
|
|
179
|
+
function incrementTimestamp(timestamp) {
|
|
180
|
+
const currentTimestamp = parseInt(timestamp, 10);
|
|
181
|
+
return (currentTimestamp + 1).toString();
|
|
182
|
+
}
|
|
136
183
|
function generateMigrationDescription(diff) {
|
|
137
184
|
const parts = [];
|
|
138
185
|
if (diff.collectionsToCreate.length > 0) {
|
|
@@ -240,14 +287,13 @@ function formatValue(value) {
|
|
|
240
287
|
return "null";
|
|
241
288
|
}
|
|
242
289
|
if (typeof value === "string") {
|
|
243
|
-
return
|
|
290
|
+
return JSON.stringify(value);
|
|
244
291
|
}
|
|
245
292
|
if (typeof value === "number" || typeof value === "boolean") {
|
|
246
293
|
return String(value);
|
|
247
294
|
}
|
|
248
295
|
if (Array.isArray(value)) {
|
|
249
|
-
|
|
250
|
-
return `[${items}]`;
|
|
296
|
+
return JSON.stringify(value).replace(/","/g, '", "');
|
|
251
297
|
}
|
|
252
298
|
if (typeof value === "object") {
|
|
253
299
|
const entries = Object.entries(value).map(([k, v]) => `${k}: ${formatValue(v)}`).join(", ");
|
|
@@ -255,7 +301,7 @@ function formatValue(value) {
|
|
|
255
301
|
}
|
|
256
302
|
return String(value);
|
|
257
303
|
}
|
|
258
|
-
function generateFieldDefinitionObject(field) {
|
|
304
|
+
function generateFieldDefinitionObject(field, collectionIdMap) {
|
|
259
305
|
const parts = [];
|
|
260
306
|
parts.push(` name: "${field.name}"`);
|
|
261
307
|
parts.push(` type: "${field.type}"`);
|
|
@@ -263,34 +309,47 @@ function generateFieldDefinitionObject(field) {
|
|
|
263
309
|
if (field.unique !== void 0) {
|
|
264
310
|
parts.push(` unique: ${field.unique}`);
|
|
265
311
|
}
|
|
312
|
+
if (field.type === "select") {
|
|
313
|
+
const maxSelect = field.options?.maxSelect ?? 1;
|
|
314
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
315
|
+
const values = field.options?.values ?? [];
|
|
316
|
+
parts.push(` values: ${formatValue(values)}`);
|
|
317
|
+
}
|
|
266
318
|
if (field.options && Object.keys(field.options).length > 0) {
|
|
267
319
|
for (const [key, value] of Object.entries(field.options)) {
|
|
320
|
+
if (field.type === "select" && (key === "maxSelect" || key === "values")) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
268
323
|
parts.push(` ${key}: ${formatValue(value)}`);
|
|
269
324
|
}
|
|
270
325
|
}
|
|
271
326
|
if (field.relation) {
|
|
272
327
|
const isUsersCollection = field.relation.collection.toLowerCase() === "users";
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
}
|
|
281
|
-
if (field.relation.cascadeDelete !== void 0) {
|
|
282
|
-
parts.push(` cascadeDelete: ${field.relation.cascadeDelete}`);
|
|
328
|
+
let collectionIdValue;
|
|
329
|
+
if (isUsersCollection) {
|
|
330
|
+
collectionIdValue = '"_pb_users_auth_"';
|
|
331
|
+
} else if (collectionIdMap && collectionIdMap.has(field.relation.collection)) {
|
|
332
|
+
collectionIdValue = `"${collectionIdMap.get(field.relation.collection)}"`;
|
|
333
|
+
} else {
|
|
334
|
+
collectionIdValue = `app.findCollectionByNameOrId("${field.relation.collection}").id`;
|
|
283
335
|
}
|
|
336
|
+
parts.push(` collectionId: ${collectionIdValue}`);
|
|
337
|
+
const maxSelect = field.relation.maxSelect ?? 1;
|
|
338
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
339
|
+
const minSelect = field.relation.minSelect ?? null;
|
|
340
|
+
parts.push(` minSelect: ${minSelect}`);
|
|
341
|
+
const cascadeDelete = field.relation.cascadeDelete ?? false;
|
|
342
|
+
parts.push(` cascadeDelete: ${cascadeDelete}`);
|
|
284
343
|
}
|
|
285
344
|
return ` {
|
|
286
345
|
${parts.join(",\n")},
|
|
287
346
|
}`;
|
|
288
347
|
}
|
|
289
|
-
function generateFieldsArray(fields) {
|
|
348
|
+
function generateFieldsArray(fields, collectionIdMap) {
|
|
290
349
|
if (fields.length === 0) {
|
|
291
350
|
return "[]";
|
|
292
351
|
}
|
|
293
|
-
const fieldObjects = fields.map((field) => generateFieldDefinitionObject(field));
|
|
352
|
+
const fieldObjects = fields.map((field) => generateFieldDefinitionObject(field, collectionIdMap));
|
|
294
353
|
return `[
|
|
295
354
|
${fieldObjects.join(",\n")},
|
|
296
355
|
]`;
|
|
@@ -349,7 +408,7 @@ function generateIndexesArray(indexes) {
|
|
|
349
408
|
if (!indexes || indexes.length === 0) {
|
|
350
409
|
return "[]";
|
|
351
410
|
}
|
|
352
|
-
const indexStrings = indexes.map((idx) =>
|
|
411
|
+
const indexStrings = indexes.map((idx) => JSON.stringify(idx));
|
|
353
412
|
return `[
|
|
354
413
|
${indexStrings.join(",\n ")},
|
|
355
414
|
]`;
|
|
@@ -403,7 +462,7 @@ function getSystemFields() {
|
|
|
403
462
|
}
|
|
404
463
|
];
|
|
405
464
|
}
|
|
406
|
-
function generateCollectionCreation(collection, varName = "collection", isLast = false) {
|
|
465
|
+
function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
|
|
407
466
|
const lines = [];
|
|
408
467
|
lines.push(` const ${varName} = new Collection({`);
|
|
409
468
|
lines.push(` name: "${collection.name}",`);
|
|
@@ -417,7 +476,7 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
|
|
|
417
476
|
}
|
|
418
477
|
const systemFields = getSystemFields();
|
|
419
478
|
const allFields = [...systemFields, ...collection.fields];
|
|
420
|
-
lines.push(` fields: ${generateFieldsArray(allFields)},`);
|
|
479
|
+
lines.push(` fields: ${generateFieldsArray(allFields, collectionIdMap)},`);
|
|
421
480
|
lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
|
|
422
481
|
lines.push(` });`);
|
|
423
482
|
lines.push(``);
|
|
@@ -439,42 +498,59 @@ function getFieldConstructorName(fieldType) {
|
|
|
439
498
|
};
|
|
440
499
|
return constructorMap[fieldType] || "TextField";
|
|
441
500
|
}
|
|
442
|
-
function generateFieldConstructorOptions(field) {
|
|
501
|
+
function generateFieldConstructorOptions(field, collectionIdMap) {
|
|
443
502
|
const parts = [];
|
|
444
503
|
parts.push(` name: "${field.name}"`);
|
|
445
504
|
parts.push(` required: ${field.required}`);
|
|
446
505
|
if (field.unique !== void 0) {
|
|
447
506
|
parts.push(` unique: ${field.unique}`);
|
|
448
507
|
}
|
|
508
|
+
if (field.type === "select") {
|
|
509
|
+
const maxSelect = field.options?.maxSelect ?? 1;
|
|
510
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
511
|
+
const values = field.options?.values ?? [];
|
|
512
|
+
parts.push(` values: ${formatValue(values)}`);
|
|
513
|
+
}
|
|
449
514
|
if (field.options && Object.keys(field.options).length > 0) {
|
|
450
515
|
for (const [key, value] of Object.entries(field.options)) {
|
|
451
|
-
|
|
516
|
+
if (field.type === "select" && (key === "maxSelect" || key === "values")) {
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (field.type === "number" && key === "noDecimal") {
|
|
520
|
+
parts.push(` onlyInt: ${formatValue(value)}`);
|
|
521
|
+
} else {
|
|
522
|
+
parts.push(` ${key}: ${formatValue(value)}`);
|
|
523
|
+
}
|
|
452
524
|
}
|
|
453
525
|
}
|
|
454
526
|
if (field.relation && field.type === "relation") {
|
|
455
527
|
const isUsersCollection = field.relation.collection.toLowerCase() === "users";
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
464
|
-
if (field.relation.cascadeDelete !== void 0) {
|
|
465
|
-
parts.push(` cascadeDelete: ${field.relation.cascadeDelete}`);
|
|
528
|
+
let collectionIdValue;
|
|
529
|
+
if (isUsersCollection) {
|
|
530
|
+
collectionIdValue = '"_pb_users_auth_"';
|
|
531
|
+
} else if (collectionIdMap && collectionIdMap.has(field.relation.collection)) {
|
|
532
|
+
collectionIdValue = `"${collectionIdMap.get(field.relation.collection)}"`;
|
|
533
|
+
} else {
|
|
534
|
+
collectionIdValue = `app.findCollectionByNameOrId("${field.relation.collection}").id`;
|
|
466
535
|
}
|
|
536
|
+
parts.push(` collectionId: ${collectionIdValue}`);
|
|
537
|
+
const maxSelect = field.relation.maxSelect ?? 1;
|
|
538
|
+
parts.push(` maxSelect: ${maxSelect}`);
|
|
539
|
+
const minSelect = field.relation.minSelect ?? null;
|
|
540
|
+
parts.push(` minSelect: ${minSelect}`);
|
|
541
|
+
const cascadeDelete = field.relation.cascadeDelete ?? false;
|
|
542
|
+
parts.push(` cascadeDelete: ${cascadeDelete}`);
|
|
467
543
|
}
|
|
468
544
|
return parts.join(",\n");
|
|
469
545
|
}
|
|
470
|
-
function generateFieldAddition(collectionName, field, varName, isLast = false) {
|
|
546
|
+
function generateFieldAddition(collectionName, field, varName, isLast = false, collectionIdMap) {
|
|
471
547
|
const lines = [];
|
|
472
548
|
const constructorName = getFieldConstructorName(field.type);
|
|
473
549
|
const collectionVar = varName || `collection_${collectionName}_${field.name}`;
|
|
474
550
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
475
551
|
lines.push(``);
|
|
476
552
|
lines.push(` ${collectionVar}.fields.add(new ${constructorName}({`);
|
|
477
|
-
lines.push(generateFieldConstructorOptions(field));
|
|
553
|
+
lines.push(generateFieldConstructorOptions(field, collectionIdMap));
|
|
478
554
|
lines.push(` }));`);
|
|
479
555
|
lines.push(``);
|
|
480
556
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
@@ -524,7 +600,7 @@ function generateIndexAddition(collectionName, index, varName, isLast = false) {
|
|
|
524
600
|
const lines = [];
|
|
525
601
|
const collectionVar = varName || `collection_${collectionName}_idx`;
|
|
526
602
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
527
|
-
lines.push(` ${collectionVar}.indexes.push(
|
|
603
|
+
lines.push(` ${collectionVar}.indexes.push(${JSON.stringify(index)});`);
|
|
528
604
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
529
605
|
return lines.join("\n");
|
|
530
606
|
}
|
|
@@ -533,7 +609,7 @@ function generateIndexRemoval(collectionName, index, varName, isLast = false) {
|
|
|
533
609
|
const collectionVar = varName || `collection_${collectionName}_idx`;
|
|
534
610
|
const indexVar = `${collectionVar}_indexToRemove`;
|
|
535
611
|
lines.push(` const ${collectionVar} = app.findCollectionByNameOrId("${collectionName}");`);
|
|
536
|
-
lines.push(` const ${indexVar} = ${collectionVar}.indexes.findIndex(idx => idx ===
|
|
612
|
+
lines.push(` const ${indexVar} = ${collectionVar}.indexes.findIndex(idx => idx === ${JSON.stringify(index)});`);
|
|
537
613
|
lines.push(` if (${indexVar} !== -1) {`);
|
|
538
614
|
lines.push(` ${collectionVar}.indexes.splice(${indexVar}, 1);`);
|
|
539
615
|
lines.push(` }`);
|
|
@@ -562,16 +638,179 @@ function generateCollectionDeletion(collectionName, varName = "collection", isLa
|
|
|
562
638
|
lines.push(isLast ? ` return app.delete(${varName});` : ` app.delete(${varName});`);
|
|
563
639
|
return lines.join("\n");
|
|
564
640
|
}
|
|
641
|
+
function generateOperationUpMigration(operation, collectionIdMap) {
|
|
642
|
+
const lines = [];
|
|
643
|
+
if (operation.type === "create") {
|
|
644
|
+
const collection = operation.collection;
|
|
645
|
+
const varName = `collection_${collection.name}`;
|
|
646
|
+
lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
|
|
647
|
+
} else if (operation.type === "modify") {
|
|
648
|
+
const modification = operation.modifications;
|
|
649
|
+
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
650
|
+
let operationCount = 0;
|
|
651
|
+
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
|
|
652
|
+
for (const field of modification.fieldsToAdd) {
|
|
653
|
+
operationCount++;
|
|
654
|
+
const varName = `collection_${collectionName}_add_${field.name}`;
|
|
655
|
+
const isLast = operationCount === totalOperations;
|
|
656
|
+
lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
|
|
657
|
+
if (!isLast) lines.push("");
|
|
658
|
+
}
|
|
659
|
+
for (const fieldMod of modification.fieldsToModify) {
|
|
660
|
+
operationCount++;
|
|
661
|
+
const varName = `collection_${collectionName}_modify_${fieldMod.fieldName}`;
|
|
662
|
+
const isLast = operationCount === totalOperations;
|
|
663
|
+
lines.push(generateFieldModification(collectionName, fieldMod, varName, isLast));
|
|
664
|
+
if (!isLast) lines.push("");
|
|
665
|
+
}
|
|
666
|
+
for (const field of modification.fieldsToRemove) {
|
|
667
|
+
operationCount++;
|
|
668
|
+
const varName = `collection_${collectionName}_remove_${field.name}`;
|
|
669
|
+
const isLast = operationCount === totalOperations;
|
|
670
|
+
lines.push(generateFieldDeletion(collectionName, field.name, varName, isLast));
|
|
671
|
+
if (!isLast) lines.push("");
|
|
672
|
+
}
|
|
673
|
+
for (let i = 0; i < modification.indexesToAdd.length; i++) {
|
|
674
|
+
operationCount++;
|
|
675
|
+
const index = modification.indexesToAdd[i];
|
|
676
|
+
const varName = `collection_${collectionName}_addidx_${i}`;
|
|
677
|
+
const isLast = operationCount === totalOperations;
|
|
678
|
+
lines.push(generateIndexAddition(collectionName, index, varName, isLast));
|
|
679
|
+
if (!isLast) lines.push("");
|
|
680
|
+
}
|
|
681
|
+
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
682
|
+
operationCount++;
|
|
683
|
+
const index = modification.indexesToRemove[i];
|
|
684
|
+
const varName = `collection_${collectionName}_rmidx_${i}`;
|
|
685
|
+
const isLast = operationCount === totalOperations;
|
|
686
|
+
lines.push(generateIndexRemoval(collectionName, index, varName, isLast));
|
|
687
|
+
if (!isLast) lines.push("");
|
|
688
|
+
}
|
|
689
|
+
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
690
|
+
for (const permission of modification.permissionsToUpdate) {
|
|
691
|
+
operationCount++;
|
|
692
|
+
const varName = `collection_${collectionName}_perm_${permission.ruleType}`;
|
|
693
|
+
const isLast = operationCount === totalOperations;
|
|
694
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, isLast));
|
|
695
|
+
if (!isLast) lines.push("");
|
|
696
|
+
}
|
|
697
|
+
} else if (modification.rulesToUpdate.length > 0) {
|
|
698
|
+
for (const rule of modification.rulesToUpdate) {
|
|
699
|
+
operationCount++;
|
|
700
|
+
const varName = `collection_${collectionName}_rule_${rule.ruleType}`;
|
|
701
|
+
const isLast = operationCount === totalOperations;
|
|
702
|
+
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.newValue, varName, isLast));
|
|
703
|
+
if (!isLast) lines.push("");
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
} else if (operation.type === "delete") {
|
|
707
|
+
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection.name;
|
|
708
|
+
const varName = `collection_${collectionName}`;
|
|
709
|
+
lines.push(generateCollectionDeletion(collectionName, varName, true));
|
|
710
|
+
}
|
|
711
|
+
return lines.join("\n");
|
|
712
|
+
}
|
|
713
|
+
function generateOperationDownMigration(operation, collectionIdMap) {
|
|
714
|
+
const lines = [];
|
|
715
|
+
if (operation.type === "create") {
|
|
716
|
+
const collection = operation.collection;
|
|
717
|
+
const varName = `collection_${collection.name}`;
|
|
718
|
+
lines.push(generateCollectionDeletion(collection.name, varName, true));
|
|
719
|
+
} else if (operation.type === "modify") {
|
|
720
|
+
const modification = operation.modifications;
|
|
721
|
+
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
722
|
+
let operationCount = 0;
|
|
723
|
+
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.rulesToUpdate.length + modification.permissionsToUpdate.length;
|
|
724
|
+
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
725
|
+
for (const permission of modification.permissionsToUpdate) {
|
|
726
|
+
operationCount++;
|
|
727
|
+
const varName = `collection_${collectionName}_revert_perm_${permission.ruleType}`;
|
|
728
|
+
const isLast = operationCount === totalOperations;
|
|
729
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, isLast));
|
|
730
|
+
if (!isLast) lines.push("");
|
|
731
|
+
}
|
|
732
|
+
} else if (modification.rulesToUpdate.length > 0) {
|
|
733
|
+
for (const rule of modification.rulesToUpdate) {
|
|
734
|
+
operationCount++;
|
|
735
|
+
const varName = `collection_${collectionName}_revert_rule_${rule.ruleType}`;
|
|
736
|
+
const isLast = operationCount === totalOperations;
|
|
737
|
+
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.oldValue, varName, isLast));
|
|
738
|
+
if (!isLast) lines.push("");
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
742
|
+
operationCount++;
|
|
743
|
+
const index = modification.indexesToRemove[i];
|
|
744
|
+
const varName = `collection_${collectionName}_restore_idx_${i}`;
|
|
745
|
+
const isLast = operationCount === totalOperations;
|
|
746
|
+
lines.push(generateIndexAddition(collectionName, index, varName, isLast));
|
|
747
|
+
if (!isLast) lines.push("");
|
|
748
|
+
}
|
|
749
|
+
for (let i = 0; i < modification.indexesToAdd.length; i++) {
|
|
750
|
+
operationCount++;
|
|
751
|
+
const index = modification.indexesToAdd[i];
|
|
752
|
+
const varName = `collection_${collectionName}_revert_idx_${i}`;
|
|
753
|
+
const isLast = operationCount === totalOperations;
|
|
754
|
+
lines.push(generateIndexRemoval(collectionName, index, varName, isLast));
|
|
755
|
+
if (!isLast) lines.push("");
|
|
756
|
+
}
|
|
757
|
+
for (const field of modification.fieldsToRemove) {
|
|
758
|
+
operationCount++;
|
|
759
|
+
const varName = `collection_${collectionName}_restore_${field.name}`;
|
|
760
|
+
const isLast = operationCount === totalOperations;
|
|
761
|
+
lines.push(generateFieldAddition(collectionName, field, varName, isLast, collectionIdMap));
|
|
762
|
+
if (!isLast) lines.push("");
|
|
763
|
+
}
|
|
764
|
+
for (const fieldMod of modification.fieldsToModify) {
|
|
765
|
+
operationCount++;
|
|
766
|
+
const reverseChanges = fieldMod.changes.map((change) => ({
|
|
767
|
+
property: change.property,
|
|
768
|
+
oldValue: change.newValue,
|
|
769
|
+
newValue: change.oldValue
|
|
770
|
+
}));
|
|
771
|
+
const reverseMod = {
|
|
772
|
+
fieldName: fieldMod.fieldName,
|
|
773
|
+
currentDefinition: fieldMod.newDefinition,
|
|
774
|
+
newDefinition: fieldMod.currentDefinition,
|
|
775
|
+
changes: reverseChanges
|
|
776
|
+
};
|
|
777
|
+
const varName = `collection_${collectionName}_revert_${fieldMod.fieldName}`;
|
|
778
|
+
const isLast = operationCount === totalOperations;
|
|
779
|
+
lines.push(generateFieldModification(collectionName, reverseMod, varName, isLast));
|
|
780
|
+
if (!isLast) lines.push("");
|
|
781
|
+
}
|
|
782
|
+
for (const field of modification.fieldsToAdd) {
|
|
783
|
+
operationCount++;
|
|
784
|
+
const varName = `collection_${collectionName}_revert_add_${field.name}`;
|
|
785
|
+
const isLast = operationCount === totalOperations;
|
|
786
|
+
lines.push(generateFieldDeletion(collectionName, field.name, varName, isLast));
|
|
787
|
+
if (!isLast) lines.push("");
|
|
788
|
+
}
|
|
789
|
+
} else if (operation.type === "delete") {
|
|
790
|
+
const collection = operation.collection;
|
|
791
|
+
if (typeof collection !== "string") {
|
|
792
|
+
const varName = `collection_${collection.name}`;
|
|
793
|
+
lines.push(generateCollectionCreation(collection, varName, true, collectionIdMap));
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return lines.join("\n");
|
|
797
|
+
}
|
|
565
798
|
function generateUpMigration(diff) {
|
|
566
799
|
const lines = [];
|
|
567
800
|
lines.push(` // UP MIGRATION`);
|
|
568
801
|
lines.push(``);
|
|
802
|
+
const collectionIdMap = /* @__PURE__ */ new Map();
|
|
803
|
+
for (const collection of diff.collectionsToCreate) {
|
|
804
|
+
if (collection.id) {
|
|
805
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
569
808
|
if (diff.collectionsToCreate.length > 0) {
|
|
570
809
|
lines.push(` // Create new collections`);
|
|
571
810
|
for (let i = 0; i < diff.collectionsToCreate.length; i++) {
|
|
572
811
|
const collection = diff.collectionsToCreate[i];
|
|
573
812
|
const varName = `collection_${collection.name}_create`;
|
|
574
|
-
lines.push(generateCollectionCreation(collection, varName));
|
|
813
|
+
lines.push(generateCollectionCreation(collection, varName, false, collectionIdMap));
|
|
575
814
|
lines.push(``);
|
|
576
815
|
}
|
|
577
816
|
}
|
|
@@ -583,7 +822,7 @@ function generateUpMigration(diff) {
|
|
|
583
822
|
lines.push(` // Add fields to ${collectionName}`);
|
|
584
823
|
for (const field of modification.fieldsToAdd) {
|
|
585
824
|
const varName = `collection_${collectionName}_add_${field.name}`;
|
|
586
|
-
lines.push(generateFieldAddition(collectionName, field, varName));
|
|
825
|
+
lines.push(generateFieldAddition(collectionName, field, varName, false, collectionIdMap));
|
|
587
826
|
lines.push(``);
|
|
588
827
|
}
|
|
589
828
|
}
|
|
@@ -674,12 +913,23 @@ function generateDownMigration(diff) {
|
|
|
674
913
|
const lines = [];
|
|
675
914
|
lines.push(` // DOWN MIGRATION (ROLLBACK)`);
|
|
676
915
|
lines.push(``);
|
|
916
|
+
const collectionIdMap = /* @__PURE__ */ new Map();
|
|
917
|
+
for (const collection of diff.collectionsToCreate) {
|
|
918
|
+
if (collection.id) {
|
|
919
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
for (const collection of diff.collectionsToDelete) {
|
|
923
|
+
if (collection.id) {
|
|
924
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
677
927
|
if (diff.collectionsToDelete.length > 0) {
|
|
678
928
|
lines.push(` // Recreate deleted collections`);
|
|
679
929
|
for (let i = 0; i < diff.collectionsToDelete.length; i++) {
|
|
680
930
|
const collection = diff.collectionsToDelete[i];
|
|
681
931
|
const varName = `collection_${collection.name}_recreate`;
|
|
682
|
-
lines.push(generateCollectionCreation(collection, varName));
|
|
932
|
+
lines.push(generateCollectionCreation(collection, varName, false, collectionIdMap));
|
|
683
933
|
lines.push(``);
|
|
684
934
|
}
|
|
685
935
|
}
|
|
@@ -724,7 +974,7 @@ function generateDownMigration(diff) {
|
|
|
724
974
|
lines.push(` // Restore fields to ${collectionName}`);
|
|
725
975
|
for (const field of modification.fieldsToRemove) {
|
|
726
976
|
const varName = `collection_${collectionName}_restore_${field.name}`;
|
|
727
|
-
lines.push(generateFieldAddition(collectionName, field, varName));
|
|
977
|
+
lines.push(generateFieldAddition(collectionName, field, varName, false, collectionIdMap));
|
|
728
978
|
lines.push(``);
|
|
729
979
|
}
|
|
730
980
|
}
|
|
@@ -793,12 +1043,33 @@ function generate(diff, config) {
|
|
|
793
1043
|
const normalizedConfig = typeof config === "string" ? { migrationDir: config } : config;
|
|
794
1044
|
try {
|
|
795
1045
|
const migrationDir = resolveMigrationDir(normalizedConfig);
|
|
796
|
-
const
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
const
|
|
801
|
-
|
|
1046
|
+
const hasChanges = diff.collectionsToCreate.length > 0 || diff.collectionsToModify.length > 0 || diff.collectionsToDelete.length > 0;
|
|
1047
|
+
if (!hasChanges) {
|
|
1048
|
+
return [];
|
|
1049
|
+
}
|
|
1050
|
+
const collectionIdMap = /* @__PURE__ */ new Map();
|
|
1051
|
+
for (const collection of diff.collectionsToCreate) {
|
|
1052
|
+
if (collection.id) {
|
|
1053
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
for (const collection of diff.collectionsToDelete) {
|
|
1057
|
+
if (collection.id) {
|
|
1058
|
+
collectionIdMap.set(collection.name, collection.id);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
const baseTimestamp = generateTimestamp(normalizedConfig);
|
|
1062
|
+
const operations = splitDiffByCollection(diff, baseTimestamp);
|
|
1063
|
+
const filePaths = [];
|
|
1064
|
+
for (const operation of operations) {
|
|
1065
|
+
const upCode = generateOperationUpMigration(operation, collectionIdMap);
|
|
1066
|
+
const downCode = generateOperationDownMigration(operation, collectionIdMap);
|
|
1067
|
+
const content = createMigrationFileStructure(upCode, downCode, normalizedConfig);
|
|
1068
|
+
const filename = generateCollectionMigrationFilename(operation);
|
|
1069
|
+
const filePath = writeMigrationFile(migrationDir, filename, content);
|
|
1070
|
+
filePaths.push(filePath);
|
|
1071
|
+
}
|
|
1072
|
+
return filePaths;
|
|
802
1073
|
} catch (error) {
|
|
803
1074
|
if (error instanceof MigrationGenerationError || error instanceof FileSystemError) {
|
|
804
1075
|
throw error;
|
|
@@ -816,7 +1087,8 @@ var MigrationGenerator = class {
|
|
|
816
1087
|
this.config = mergeConfig(config);
|
|
817
1088
|
}
|
|
818
1089
|
/**
|
|
819
|
-
* Generates
|
|
1090
|
+
* Generates migration files from a schema diff
|
|
1091
|
+
* Returns array of file paths (one per collection operation)
|
|
820
1092
|
*/
|
|
821
1093
|
generate(diff) {
|
|
822
1094
|
return generate(diff, this.config);
|
|
@@ -845,6 +1117,7 @@ exports.MigrationGenerator = MigrationGenerator;
|
|
|
845
1117
|
exports.createMigrationFileStructure = createMigrationFileStructure;
|
|
846
1118
|
exports.generate = generate;
|
|
847
1119
|
exports.generateCollectionCreation = generateCollectionCreation;
|
|
1120
|
+
exports.generateCollectionMigrationFilename = generateCollectionMigrationFilename;
|
|
848
1121
|
exports.generateCollectionPermissions = generateCollectionPermissions;
|
|
849
1122
|
exports.generateCollectionRules = generateCollectionRules;
|
|
850
1123
|
exports.generateDownMigration = generateDownMigration;
|
|
@@ -856,9 +1129,13 @@ exports.generateFieldsArray = generateFieldsArray;
|
|
|
856
1129
|
exports.generateIndexesArray = generateIndexesArray;
|
|
857
1130
|
exports.generateMigrationDescription = generateMigrationDescription;
|
|
858
1131
|
exports.generateMigrationFilename = generateMigrationFilename;
|
|
1132
|
+
exports.generateOperationDownMigration = generateOperationDownMigration;
|
|
1133
|
+
exports.generateOperationUpMigration = generateOperationUpMigration;
|
|
859
1134
|
exports.generatePermissionUpdate = generatePermissionUpdate;
|
|
860
1135
|
exports.generateTimestamp = generateTimestamp;
|
|
861
1136
|
exports.generateUpMigration = generateUpMigration;
|
|
1137
|
+
exports.incrementTimestamp = incrementTimestamp;
|
|
1138
|
+
exports.splitDiffByCollection = splitDiffByCollection;
|
|
862
1139
|
exports.writeMigrationFile = writeMigrationFile;
|
|
863
1140
|
//# sourceMappingURL=generator.cjs.map
|
|
864
1141
|
//# sourceMappingURL=generator.cjs.map
|