pocketbase-zod-schema 0.7.0 → 0.7.2
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 +14 -0
- package/dist/cli/index.cjs +449 -50
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.d.cts +1 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +449 -50
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +455 -53
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +455 -53
- package/dist/cli/migrate.js.map +1 -1
- package/dist/cli/utils/index.cjs +8 -1
- package/dist/cli/utils/index.cjs.map +1 -1
- package/dist/cli/utils/index.d.cts +1 -1
- package/dist/cli/utils/index.d.ts +1 -1
- package/dist/cli/utils/index.js +8 -1
- package/dist/cli/utils/index.js.map +1 -1
- package/dist/index.cjs +90 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +87 -2
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs +87 -14
- package/dist/migration/analyzer.cjs.map +1 -1
- package/dist/migration/analyzer.d.cts +12 -4
- package/dist/migration/analyzer.d.ts +12 -4
- package/dist/migration/analyzer.js +87 -15
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/diff.cjs +64 -9
- package/dist/migration/diff.cjs.map +1 -1
- package/dist/migration/diff.d.cts +12 -2
- package/dist/migration/diff.d.ts +12 -2
- package/dist/migration/diff.js +64 -10
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/generator.cjs +206 -39
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +54 -2
- package/dist/migration/generator.d.ts +54 -2
- package/dist/migration/generator.js +203 -40
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +501 -63
- package/dist/migration/index.cjs.map +1 -1
- package/dist/migration/index.d.cts +2 -2
- package/dist/migration/index.d.ts +2 -2
- package/dist/migration/index.js +501 -63
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.cjs +147 -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 +147 -1
- package/dist/migration/snapshot.js.map +1 -1
- package/dist/migration/utils/index.d.cts +1 -1
- package/dist/migration/utils/index.d.ts +1 -1
- package/dist/schema.cjs +90 -1
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +128 -2
- package/dist/schema.d.ts +128 -2
- package/dist/schema.js +87 -2
- package/dist/schema.js.map +1 -1
- package/dist/server.cjs +545 -65
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js +542 -66
- package/dist/server.js.map +1 -1
- package/dist/{types-CWHV6ATd.d.cts → types-CnzfX6JH.d.cts} +20 -2
- package/dist/{types-C2nGWHLV.d.ts → types-Do3jyFBm.d.ts} +20 -2
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as CollectionSchema, F as FieldDefinition, c as FieldModification, e as SchemaDiff, f as CollectionOperation } from '../types-
|
|
1
|
+
import { C as CollectionSchema, F as FieldDefinition, c as FieldModification, e as SchemaDiff, f as CollectionOperation } from '../types-CnzfX6JH.cjs';
|
|
2
2
|
import '../fields-B96iGprI.cjs';
|
|
3
3
|
import 'zod';
|
|
4
4
|
|
|
@@ -291,6 +291,39 @@ declare function generateRuleUpdate(collectionName: string, ruleType: string, ne
|
|
|
291
291
|
* @returns JavaScript code for updating the permission
|
|
292
292
|
*/
|
|
293
293
|
declare function generatePermissionUpdate(collectionName: string, ruleType: string, newValue: string | null, varName?: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
294
|
+
/**
|
|
295
|
+
* Generates code for updating a view collection's SQL query
|
|
296
|
+
*
|
|
297
|
+
* Applied in place so the collection ID stays stable - PocketBase re-derives
|
|
298
|
+
* the view's fields when the collection is saved.
|
|
299
|
+
*
|
|
300
|
+
* Uses unmarshal() rather than a direct `collection.viewQuery = ...`
|
|
301
|
+
* assignment: viewQuery lives on an embedded struct in PocketBase's Go model,
|
|
302
|
+
* and a direct assignment from the migration JS runtime is silently dropped.
|
|
303
|
+
*
|
|
304
|
+
* @param collectionName - Name of the view collection
|
|
305
|
+
* @param viewQuery - The SQL query to set
|
|
306
|
+
* @param varName - Variable name to use for the collection (default: auto-generated)
|
|
307
|
+
* @param isLast - Whether this is the last operation (will return the result)
|
|
308
|
+
* @param collectionIdMap - Map of collection names to IDs
|
|
309
|
+
* @returns JavaScript code for updating the view query
|
|
310
|
+
*/
|
|
311
|
+
declare function generateViewQueryUpdate(collectionName: string, viewQuery: string, varName?: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
312
|
+
/**
|
|
313
|
+
* Generates a single unmarshal({...}, collection) block for multiple rule/permission changes.
|
|
314
|
+
* Used when 2+ rules change at once to match PocketBase's native migration style.
|
|
315
|
+
*
|
|
316
|
+
* @param collectionName - Name of the collection
|
|
317
|
+
* @param entries - Array of { ruleType, value } to include in the unmarshal object
|
|
318
|
+
* @param varSuffix - Suffix for the variable name (e.g. "rules" or "revert_rules")
|
|
319
|
+
* @param isLast - Whether this is the last operation (will use return app.save)
|
|
320
|
+
* @param collectionIdMap - Map of collection names to IDs
|
|
321
|
+
* @returns JavaScript code block
|
|
322
|
+
*/
|
|
323
|
+
declare function generateGroupedRuleUpdates(collectionName: string, entries: Array<{
|
|
324
|
+
ruleType: string;
|
|
325
|
+
value: string | null;
|
|
326
|
+
}>, varSuffix: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
294
327
|
|
|
295
328
|
/**
|
|
296
329
|
* Generates a timestamp for migration filename
|
|
@@ -316,6 +349,18 @@ declare function incrementTimestamp(timestamp: string): string;
|
|
|
316
349
|
* @returns Formatted string representation
|
|
317
350
|
*/
|
|
318
351
|
declare function formatValue(value: any): string;
|
|
352
|
+
/**
|
|
353
|
+
* Formats a SQL view query as a JavaScript template literal
|
|
354
|
+
*
|
|
355
|
+
* Emitted as a backtick template (rather than a JSON string) so the SQL stays
|
|
356
|
+
* readable in the generated migration. Backticks and `${` are escaped so the
|
|
357
|
+
* query can never break out of the literal or be interpolated.
|
|
358
|
+
*
|
|
359
|
+
* @param query - The SQL query
|
|
360
|
+
* @param indent - Indentation prefix applied to each line of SQL
|
|
361
|
+
* @returns Template literal source, including the surrounding backticks
|
|
362
|
+
*/
|
|
363
|
+
declare function formatSqlTemplate(query: string, indent?: string): string;
|
|
319
364
|
/**
|
|
320
365
|
* Gets the appropriate Field constructor name for a field type
|
|
321
366
|
*
|
|
@@ -330,6 +375,13 @@ declare function getFieldConstructorName(fieldType: string): string;
|
|
|
330
375
|
* @returns Array of system field definitions
|
|
331
376
|
*/
|
|
332
377
|
declare function getSystemFields(): FieldDefinition[];
|
|
378
|
+
/**
|
|
379
|
+
* Generates the autodate timestamp system fields (created, updated)
|
|
380
|
+
* These appear at the end of every collection's field list, after user fields
|
|
381
|
+
*
|
|
382
|
+
* @returns Array of system timestamp field definitions
|
|
383
|
+
*/
|
|
384
|
+
declare function getSystemTimestampFields(): FieldDefinition[];
|
|
333
385
|
/**
|
|
334
386
|
* Generates auth-specific system fields
|
|
335
387
|
*
|
|
@@ -395,4 +447,4 @@ declare class MigrationGenerator {
|
|
|
395
447
|
generateMigrationFilename(diff: SchemaDiff): string;
|
|
396
448
|
}
|
|
397
449
|
|
|
398
|
-
export { DEFAULT_CONFIG, DEFAULT_TEMPLATE, MigrationGenerator, type MigrationGeneratorConfig, createMigrationFileStructure, formatValue, generate, generateCollectionCreation, generateCollectionDeletion, generateCollectionMigrationFilename, generateCollectionPermissions, generateCollectionRules, generateDownMigration, generateFieldAddition, generateFieldConstructorOptions, generateFieldDefinitionObject, generateFieldDeletion, generateFieldModification, generateFieldsArray, generateFindCollectionCode, generateIndexAddition, generateIndexRemoval, generateIndexesArray, generateMigrationDescription, generateMigrationFilename, generateOperationDownMigration, generateOperationUpMigration, generatePermissionUpdate, generateRuleUpdate, generateTimestamp, generateUpMigration, getAuthSystemFields, getAuthSystemIndexes, getFieldConstructorName, getSystemFields, incrementTimestamp, mergeConfig, resolveMigrationDir, splitDiffByCollection, writeMigrationFile };
|
|
450
|
+
export { DEFAULT_CONFIG, DEFAULT_TEMPLATE, MigrationGenerator, type MigrationGeneratorConfig, createMigrationFileStructure, formatSqlTemplate, formatValue, generate, generateCollectionCreation, generateCollectionDeletion, generateCollectionMigrationFilename, generateCollectionPermissions, generateCollectionRules, generateDownMigration, generateFieldAddition, generateFieldConstructorOptions, generateFieldDefinitionObject, generateFieldDeletion, generateFieldModification, generateFieldsArray, generateFindCollectionCode, generateGroupedRuleUpdates, generateIndexAddition, generateIndexRemoval, generateIndexesArray, generateMigrationDescription, generateMigrationFilename, generateOperationDownMigration, generateOperationUpMigration, generatePermissionUpdate, generateRuleUpdate, generateTimestamp, generateUpMigration, generateViewQueryUpdate, getAuthSystemFields, getAuthSystemIndexes, getFieldConstructorName, getSystemFields, getSystemTimestampFields, incrementTimestamp, mergeConfig, resolveMigrationDir, splitDiffByCollection, writeMigrationFile };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as CollectionSchema, F as FieldDefinition, c as FieldModification, e as SchemaDiff, f as CollectionOperation } from '../types-
|
|
1
|
+
import { C as CollectionSchema, F as FieldDefinition, c as FieldModification, e as SchemaDiff, f as CollectionOperation } from '../types-Do3jyFBm.js';
|
|
2
2
|
import '../fields-B96iGprI.js';
|
|
3
3
|
import 'zod';
|
|
4
4
|
|
|
@@ -291,6 +291,39 @@ declare function generateRuleUpdate(collectionName: string, ruleType: string, ne
|
|
|
291
291
|
* @returns JavaScript code for updating the permission
|
|
292
292
|
*/
|
|
293
293
|
declare function generatePermissionUpdate(collectionName: string, ruleType: string, newValue: string | null, varName?: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
294
|
+
/**
|
|
295
|
+
* Generates code for updating a view collection's SQL query
|
|
296
|
+
*
|
|
297
|
+
* Applied in place so the collection ID stays stable - PocketBase re-derives
|
|
298
|
+
* the view's fields when the collection is saved.
|
|
299
|
+
*
|
|
300
|
+
* Uses unmarshal() rather than a direct `collection.viewQuery = ...`
|
|
301
|
+
* assignment: viewQuery lives on an embedded struct in PocketBase's Go model,
|
|
302
|
+
* and a direct assignment from the migration JS runtime is silently dropped.
|
|
303
|
+
*
|
|
304
|
+
* @param collectionName - Name of the view collection
|
|
305
|
+
* @param viewQuery - The SQL query to set
|
|
306
|
+
* @param varName - Variable name to use for the collection (default: auto-generated)
|
|
307
|
+
* @param isLast - Whether this is the last operation (will return the result)
|
|
308
|
+
* @param collectionIdMap - Map of collection names to IDs
|
|
309
|
+
* @returns JavaScript code for updating the view query
|
|
310
|
+
*/
|
|
311
|
+
declare function generateViewQueryUpdate(collectionName: string, viewQuery: string, varName?: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
312
|
+
/**
|
|
313
|
+
* Generates a single unmarshal({...}, collection) block for multiple rule/permission changes.
|
|
314
|
+
* Used when 2+ rules change at once to match PocketBase's native migration style.
|
|
315
|
+
*
|
|
316
|
+
* @param collectionName - Name of the collection
|
|
317
|
+
* @param entries - Array of { ruleType, value } to include in the unmarshal object
|
|
318
|
+
* @param varSuffix - Suffix for the variable name (e.g. "rules" or "revert_rules")
|
|
319
|
+
* @param isLast - Whether this is the last operation (will use return app.save)
|
|
320
|
+
* @param collectionIdMap - Map of collection names to IDs
|
|
321
|
+
* @returns JavaScript code block
|
|
322
|
+
*/
|
|
323
|
+
declare function generateGroupedRuleUpdates(collectionName: string, entries: Array<{
|
|
324
|
+
ruleType: string;
|
|
325
|
+
value: string | null;
|
|
326
|
+
}>, varSuffix: string, isLast?: boolean, collectionIdMap?: Map<string, string>): string;
|
|
294
327
|
|
|
295
328
|
/**
|
|
296
329
|
* Generates a timestamp for migration filename
|
|
@@ -316,6 +349,18 @@ declare function incrementTimestamp(timestamp: string): string;
|
|
|
316
349
|
* @returns Formatted string representation
|
|
317
350
|
*/
|
|
318
351
|
declare function formatValue(value: any): string;
|
|
352
|
+
/**
|
|
353
|
+
* Formats a SQL view query as a JavaScript template literal
|
|
354
|
+
*
|
|
355
|
+
* Emitted as a backtick template (rather than a JSON string) so the SQL stays
|
|
356
|
+
* readable in the generated migration. Backticks and `${` are escaped so the
|
|
357
|
+
* query can never break out of the literal or be interpolated.
|
|
358
|
+
*
|
|
359
|
+
* @param query - The SQL query
|
|
360
|
+
* @param indent - Indentation prefix applied to each line of SQL
|
|
361
|
+
* @returns Template literal source, including the surrounding backticks
|
|
362
|
+
*/
|
|
363
|
+
declare function formatSqlTemplate(query: string, indent?: string): string;
|
|
319
364
|
/**
|
|
320
365
|
* Gets the appropriate Field constructor name for a field type
|
|
321
366
|
*
|
|
@@ -330,6 +375,13 @@ declare function getFieldConstructorName(fieldType: string): string;
|
|
|
330
375
|
* @returns Array of system field definitions
|
|
331
376
|
*/
|
|
332
377
|
declare function getSystemFields(): FieldDefinition[];
|
|
378
|
+
/**
|
|
379
|
+
* Generates the autodate timestamp system fields (created, updated)
|
|
380
|
+
* These appear at the end of every collection's field list, after user fields
|
|
381
|
+
*
|
|
382
|
+
* @returns Array of system timestamp field definitions
|
|
383
|
+
*/
|
|
384
|
+
declare function getSystemTimestampFields(): FieldDefinition[];
|
|
333
385
|
/**
|
|
334
386
|
* Generates auth-specific system fields
|
|
335
387
|
*
|
|
@@ -395,4 +447,4 @@ declare class MigrationGenerator {
|
|
|
395
447
|
generateMigrationFilename(diff: SchemaDiff): string;
|
|
396
448
|
}
|
|
397
449
|
|
|
398
|
-
export { DEFAULT_CONFIG, DEFAULT_TEMPLATE, MigrationGenerator, type MigrationGeneratorConfig, createMigrationFileStructure, formatValue, generate, generateCollectionCreation, generateCollectionDeletion, generateCollectionMigrationFilename, generateCollectionPermissions, generateCollectionRules, generateDownMigration, generateFieldAddition, generateFieldConstructorOptions, generateFieldDefinitionObject, generateFieldDeletion, generateFieldModification, generateFieldsArray, generateFindCollectionCode, generateIndexAddition, generateIndexRemoval, generateIndexesArray, generateMigrationDescription, generateMigrationFilename, generateOperationDownMigration, generateOperationUpMigration, generatePermissionUpdate, generateRuleUpdate, generateTimestamp, generateUpMigration, getAuthSystemFields, getAuthSystemIndexes, getFieldConstructorName, getSystemFields, incrementTimestamp, mergeConfig, resolveMigrationDir, splitDiffByCollection, writeMigrationFile };
|
|
450
|
+
export { DEFAULT_CONFIG, DEFAULT_TEMPLATE, MigrationGenerator, type MigrationGeneratorConfig, createMigrationFileStructure, formatSqlTemplate, formatValue, generate, generateCollectionCreation, generateCollectionDeletion, generateCollectionMigrationFilename, generateCollectionPermissions, generateCollectionRules, generateDownMigration, generateFieldAddition, generateFieldConstructorOptions, generateFieldDefinitionObject, generateFieldDeletion, generateFieldModification, generateFieldsArray, generateFindCollectionCode, generateGroupedRuleUpdates, generateIndexAddition, generateIndexRemoval, generateIndexesArray, generateMigrationDescription, generateMigrationFilename, generateOperationDownMigration, generateOperationUpMigration, generatePermissionUpdate, generateRuleUpdate, generateTimestamp, generateUpMigration, generateViewQueryUpdate, getAuthSystemFields, getAuthSystemIndexes, getFieldConstructorName, getSystemFields, getSystemTimestampFields, incrementTimestamp, mergeConfig, resolveMigrationDir, splitDiffByCollection, writeMigrationFile };
|
|
@@ -201,6 +201,13 @@ function formatValue(value) {
|
|
|
201
201
|
}
|
|
202
202
|
return String(value);
|
|
203
203
|
}
|
|
204
|
+
function formatSqlTemplate(query, indent = " ") {
|
|
205
|
+
const escaped = query.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
206
|
+
const body = escaped.split("\n").map((line) => line.trim() === "" ? "" : `${indent}${line}`).join("\n");
|
|
207
|
+
return `\`
|
|
208
|
+
${body}
|
|
209
|
+
${indent.slice(0, -2)}\``;
|
|
210
|
+
}
|
|
204
211
|
function getFieldConstructorName(fieldType) {
|
|
205
212
|
const constructorMap = {
|
|
206
213
|
text: "TextField",
|
|
@@ -239,6 +246,36 @@ function getSystemFields() {
|
|
|
239
246
|
}
|
|
240
247
|
];
|
|
241
248
|
}
|
|
249
|
+
function getSystemTimestampFields() {
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
name: "created",
|
|
253
|
+
id: "autodate2990389176",
|
|
254
|
+
type: "autodate",
|
|
255
|
+
required: false,
|
|
256
|
+
options: {
|
|
257
|
+
hidden: false,
|
|
258
|
+
onCreate: true,
|
|
259
|
+
onUpdate: false,
|
|
260
|
+
presentable: false,
|
|
261
|
+
system: true
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
name: "updated",
|
|
266
|
+
id: "autodate3332085495",
|
|
267
|
+
type: "autodate",
|
|
268
|
+
required: false,
|
|
269
|
+
options: {
|
|
270
|
+
hidden: false,
|
|
271
|
+
onCreate: true,
|
|
272
|
+
onUpdate: true,
|
|
273
|
+
presentable: false,
|
|
274
|
+
system: true
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
];
|
|
278
|
+
}
|
|
242
279
|
function getAuthSystemFields() {
|
|
243
280
|
return [
|
|
244
281
|
{
|
|
@@ -531,6 +568,15 @@ function generateCollectionRules(rules, collectionType = "base") {
|
|
|
531
568
|
if (!rules) {
|
|
532
569
|
return "";
|
|
533
570
|
}
|
|
571
|
+
if (collectionType === "view") {
|
|
572
|
+
return [
|
|
573
|
+
`"listRule": ${formatValue(rules.listRule ?? null)}`,
|
|
574
|
+
`"viewRule": ${formatValue(rules.viewRule ?? null)}`,
|
|
575
|
+
`"createRule": null`,
|
|
576
|
+
`"updateRule": null`,
|
|
577
|
+
`"deleteRule": null`
|
|
578
|
+
].join(",\n ");
|
|
579
|
+
}
|
|
534
580
|
const parts = [];
|
|
535
581
|
if (rules.listRule !== void 0) {
|
|
536
582
|
parts.push(`"listRule": ${formatValue(rules.listRule)}`);
|
|
@@ -556,6 +602,15 @@ function generateCollectionPermissions(permissions, collectionType = "base") {
|
|
|
556
602
|
if (!permissions) {
|
|
557
603
|
return "";
|
|
558
604
|
}
|
|
605
|
+
if (collectionType === "view") {
|
|
606
|
+
return [
|
|
607
|
+
`"listRule": ${formatValue(permissions.listRule ?? null)}`,
|
|
608
|
+
`"viewRule": ${formatValue(permissions.viewRule ?? null)}`,
|
|
609
|
+
`"createRule": null`,
|
|
610
|
+
`"updateRule": null`,
|
|
611
|
+
`"deleteRule": null`
|
|
612
|
+
].join(",\n ");
|
|
613
|
+
}
|
|
559
614
|
const parts = [];
|
|
560
615
|
if (permissions.listRule !== void 0) {
|
|
561
616
|
parts.push(`"listRule": ${formatValue(permissions.listRule)}`);
|
|
@@ -593,6 +648,28 @@ function generatePermissionUpdate(collectionName, ruleType, newValue, varName, i
|
|
|
593
648
|
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
594
649
|
return lines.join("\n");
|
|
595
650
|
}
|
|
651
|
+
function generateViewQueryUpdate(collectionName, viewQuery, varName, isLast = false, collectionIdMap) {
|
|
652
|
+
const lines = [];
|
|
653
|
+
const collectionVar = varName || `collection_${collectionName}_viewQuery`;
|
|
654
|
+
lines.push(` const ${collectionVar} = ${generateFindCollectionCode(collectionName, collectionIdMap)};`);
|
|
655
|
+
lines.push(` unmarshal({`);
|
|
656
|
+
lines.push(` "viewQuery": ${formatSqlTemplate(viewQuery, " ")},`);
|
|
657
|
+
lines.push(` }, ${collectionVar})`);
|
|
658
|
+
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
659
|
+
return lines.join("\n");
|
|
660
|
+
}
|
|
661
|
+
function generateGroupedRuleUpdates(collectionName, entries, varSuffix, isLast = false, collectionIdMap) {
|
|
662
|
+
const collectionVar = `collection_${collectionName}_${varSuffix}`;
|
|
663
|
+
const lines = [];
|
|
664
|
+
lines.push(` const ${collectionVar} = ${generateFindCollectionCode(collectionName, collectionIdMap)};`);
|
|
665
|
+
lines.push(` unmarshal({`);
|
|
666
|
+
for (const entry of entries) {
|
|
667
|
+
lines.push(` "${entry.ruleType}": ${formatValue(entry.value)},`);
|
|
668
|
+
}
|
|
669
|
+
lines.push(` }, ${collectionVar})`);
|
|
670
|
+
lines.push(isLast ? ` return app.save(${collectionVar});` : ` app.save(${collectionVar});`);
|
|
671
|
+
return lines.join("\n");
|
|
672
|
+
}
|
|
596
673
|
|
|
597
674
|
// src/migration/generator/collections.ts
|
|
598
675
|
function generateCollectionCreation(collection, varName = "collection", isLast = false, collectionIdMap) {
|
|
@@ -611,16 +688,24 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
|
|
|
611
688
|
} else if (rulesCode) {
|
|
612
689
|
lines.push(` ${rulesCode},`);
|
|
613
690
|
}
|
|
691
|
+
if (collection.type === "view") {
|
|
692
|
+
lines.push(` "viewQuery": ${formatSqlTemplate(collection.viewQuery ?? "")},`);
|
|
693
|
+
lines.push(` });`);
|
|
694
|
+
lines.push(``);
|
|
695
|
+
lines.push(isLast ? ` return app.save(${varName});` : ` app.save(${varName});`);
|
|
696
|
+
return lines.join("\n");
|
|
697
|
+
}
|
|
614
698
|
const systemFieldNames = ["created", "updated", "id"];
|
|
615
699
|
if (collection.type === "auth") {
|
|
616
700
|
systemFieldNames.push(...getAuthSystemFields().map((f) => f.name));
|
|
617
701
|
}
|
|
618
702
|
const userFields = collection.fields.filter((f) => !systemFieldNames.includes(f.name));
|
|
619
|
-
const allFields = [...getSystemFields()
|
|
703
|
+
const allFields = [...getSystemFields()];
|
|
620
704
|
if (collection.type === "auth") {
|
|
621
705
|
allFields.push(...getAuthSystemFields());
|
|
622
706
|
}
|
|
623
707
|
allFields.push(...userFields);
|
|
708
|
+
allFields.push(...getSystemTimestampFields());
|
|
624
709
|
lines.push(` "fields": ${generateFieldsArray(allFields, collectionIdMap)},`);
|
|
625
710
|
let allIndexes = [...collection.indexes || []];
|
|
626
711
|
if (collection.type === "auth") {
|
|
@@ -650,7 +735,21 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
650
735
|
const modification = operation.modifications;
|
|
651
736
|
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
652
737
|
let operationCount = 0;
|
|
653
|
-
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.
|
|
738
|
+
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + (modification.viewQueryUpdate ? 1 : 0) + (modification.permissionsToUpdate.length > 0 ? 1 : modification.rulesToUpdate.length > 0 ? 1 : 0);
|
|
739
|
+
if (modification.viewQueryUpdate) {
|
|
740
|
+
operationCount++;
|
|
741
|
+
const isLast = operationCount === totalOperations;
|
|
742
|
+
lines.push(
|
|
743
|
+
generateViewQueryUpdate(
|
|
744
|
+
collectionName,
|
|
745
|
+
modification.viewQueryUpdate.newValue,
|
|
746
|
+
void 0,
|
|
747
|
+
isLast,
|
|
748
|
+
collectionIdMap
|
|
749
|
+
)
|
|
750
|
+
);
|
|
751
|
+
if (!isLast) lines.push("");
|
|
752
|
+
}
|
|
654
753
|
for (let i = 0; i < modification.fieldsToAdd.length; i++) {
|
|
655
754
|
const field = modification.fieldsToAdd[i];
|
|
656
755
|
operationCount++;
|
|
@@ -690,23 +789,29 @@ function generateOperationUpMigration(operation, collectionIdMap) {
|
|
|
690
789
|
if (!isLast) lines.push("");
|
|
691
790
|
}
|
|
692
791
|
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
693
|
-
|
|
694
|
-
|
|
792
|
+
operationCount++;
|
|
793
|
+
const isLast = operationCount === totalOperations;
|
|
794
|
+
if (modification.permissionsToUpdate.length >= 2) {
|
|
795
|
+
const entries = modification.permissionsToUpdate.map((p) => ({ ruleType: p.ruleType, value: p.newValue }));
|
|
796
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "rules", isLast, collectionIdMap));
|
|
797
|
+
} else {
|
|
798
|
+
const permission = modification.permissionsToUpdate[0];
|
|
695
799
|
const varName = `collection_${collectionName}_perm_${permission.ruleType}`;
|
|
696
|
-
|
|
697
|
-
lines.push(
|
|
698
|
-
generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, isLast, collectionIdMap)
|
|
699
|
-
);
|
|
700
|
-
if (!isLast) lines.push("");
|
|
800
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, isLast, collectionIdMap));
|
|
701
801
|
}
|
|
802
|
+
if (!isLast) lines.push("");
|
|
702
803
|
} else if (modification.rulesToUpdate.length > 0) {
|
|
703
|
-
|
|
704
|
-
|
|
804
|
+
operationCount++;
|
|
805
|
+
const isLast = operationCount === totalOperations;
|
|
806
|
+
if (modification.rulesToUpdate.length >= 2) {
|
|
807
|
+
const entries = modification.rulesToUpdate.map((r) => ({ ruleType: r.ruleType, value: r.newValue }));
|
|
808
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "rules", isLast, collectionIdMap));
|
|
809
|
+
} else {
|
|
810
|
+
const rule = modification.rulesToUpdate[0];
|
|
705
811
|
const varName = `collection_${collectionName}_rule_${rule.ruleType}`;
|
|
706
|
-
const isLast = operationCount === totalOperations;
|
|
707
812
|
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.newValue, varName, isLast, collectionIdMap));
|
|
708
|
-
if (!isLast) lines.push("");
|
|
709
813
|
}
|
|
814
|
+
if (!isLast) lines.push("");
|
|
710
815
|
}
|
|
711
816
|
} else if (operation.type === "delete") {
|
|
712
817
|
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection.name;
|
|
@@ -745,25 +850,45 @@ function generateOperationDownMigration(operation, collectionIdMap) {
|
|
|
745
850
|
const modification = operation.modifications;
|
|
746
851
|
const collectionName = typeof operation.collection === "string" ? operation.collection : operation.collection?.name ?? modification.collection;
|
|
747
852
|
let operationCount = 0;
|
|
748
|
-
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + modification.
|
|
853
|
+
const totalOperations = modification.fieldsToAdd.length + modification.fieldsToModify.length + modification.fieldsToRemove.length + modification.indexesToAdd.length + modification.indexesToRemove.length + (modification.viewQueryUpdate ? 1 : 0) + (modification.permissionsToUpdate.length > 0 ? 1 : modification.rulesToUpdate.length > 0 ? 1 : 0);
|
|
854
|
+
if (modification.viewQueryUpdate) {
|
|
855
|
+
operationCount++;
|
|
856
|
+
const isLast = operationCount === totalOperations;
|
|
857
|
+
lines.push(
|
|
858
|
+
generateViewQueryUpdate(
|
|
859
|
+
collectionName,
|
|
860
|
+
modification.viewQueryUpdate.oldValue ?? "",
|
|
861
|
+
`collection_${collectionName}_revert_viewQuery`,
|
|
862
|
+
isLast,
|
|
863
|
+
collectionIdMap
|
|
864
|
+
)
|
|
865
|
+
);
|
|
866
|
+
if (!isLast) lines.push("");
|
|
867
|
+
}
|
|
749
868
|
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
750
|
-
|
|
751
|
-
|
|
869
|
+
operationCount++;
|
|
870
|
+
const isLast = operationCount === totalOperations;
|
|
871
|
+
if (modification.permissionsToUpdate.length >= 2) {
|
|
872
|
+
const entries = modification.permissionsToUpdate.map((p) => ({ ruleType: p.ruleType, value: p.oldValue }));
|
|
873
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "revert_rules", isLast, collectionIdMap));
|
|
874
|
+
} else {
|
|
875
|
+
const permission = modification.permissionsToUpdate[0];
|
|
752
876
|
const varName = `collection_${collectionName}_revert_perm_${permission.ruleType}`;
|
|
753
|
-
|
|
754
|
-
lines.push(
|
|
755
|
-
generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, isLast, collectionIdMap)
|
|
756
|
-
);
|
|
757
|
-
if (!isLast) lines.push("");
|
|
877
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, isLast, collectionIdMap));
|
|
758
878
|
}
|
|
879
|
+
if (!isLast) lines.push("");
|
|
759
880
|
} else if (modification.rulesToUpdate.length > 0) {
|
|
760
|
-
|
|
761
|
-
|
|
881
|
+
operationCount++;
|
|
882
|
+
const isLast = operationCount === totalOperations;
|
|
883
|
+
if (modification.rulesToUpdate.length >= 2) {
|
|
884
|
+
const entries = modification.rulesToUpdate.map((r) => ({ ruleType: r.ruleType, value: r.oldValue }));
|
|
885
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "revert_rules", isLast, collectionIdMap));
|
|
886
|
+
} else {
|
|
887
|
+
const rule = modification.rulesToUpdate[0];
|
|
762
888
|
const varName = `collection_${collectionName}_revert_rule_${rule.ruleType}`;
|
|
763
|
-
const isLast = operationCount === totalOperations;
|
|
764
889
|
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.oldValue, varName, isLast, collectionIdMap));
|
|
765
|
-
if (!isLast) lines.push("");
|
|
766
890
|
}
|
|
891
|
+
if (!isLast) lines.push("");
|
|
767
892
|
}
|
|
768
893
|
for (let i = 0; i < modification.indexesToRemove.length; i++) {
|
|
769
894
|
operationCount++;
|
|
@@ -870,6 +995,19 @@ function generateUpMigration(diff) {
|
|
|
870
995
|
lines.push(` // Modify existing collections`);
|
|
871
996
|
for (const modification of diff.collectionsToModify) {
|
|
872
997
|
const collectionName = modification.collection;
|
|
998
|
+
if (modification.viewQueryUpdate) {
|
|
999
|
+
lines.push(` // Update the view query of ${collectionName}`);
|
|
1000
|
+
lines.push(
|
|
1001
|
+
generateViewQueryUpdate(
|
|
1002
|
+
collectionName,
|
|
1003
|
+
modification.viewQueryUpdate.newValue,
|
|
1004
|
+
void 0,
|
|
1005
|
+
false,
|
|
1006
|
+
collectionIdMap
|
|
1007
|
+
)
|
|
1008
|
+
);
|
|
1009
|
+
lines.push(``);
|
|
1010
|
+
}
|
|
873
1011
|
if (modification.fieldsToAdd.length > 0) {
|
|
874
1012
|
lines.push(` // Add fields to ${collectionName}`);
|
|
875
1013
|
for (let i = 0; i < modification.fieldsToAdd.length; i++) {
|
|
@@ -915,20 +1053,26 @@ function generateUpMigration(diff) {
|
|
|
915
1053
|
}
|
|
916
1054
|
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
917
1055
|
lines.push(` // Update permissions for ${collectionName}`);
|
|
918
|
-
|
|
1056
|
+
if (modification.permissionsToUpdate.length >= 2) {
|
|
1057
|
+
const entries = modification.permissionsToUpdate.map((p) => ({ ruleType: p.ruleType, value: p.newValue }));
|
|
1058
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "rules", false, collectionIdMap));
|
|
1059
|
+
} else {
|
|
1060
|
+
const permission = modification.permissionsToUpdate[0];
|
|
919
1061
|
const varName = `collection_${collectionName}_perm_${permission.ruleType}`;
|
|
920
|
-
lines.push(
|
|
921
|
-
generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, false, collectionIdMap)
|
|
922
|
-
);
|
|
923
|
-
lines.push(``);
|
|
1062
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.newValue, varName, false, collectionIdMap));
|
|
924
1063
|
}
|
|
1064
|
+
lines.push(``);
|
|
925
1065
|
} else if (modification.rulesToUpdate.length > 0) {
|
|
926
1066
|
lines.push(` // Update rules for ${collectionName}`);
|
|
927
|
-
|
|
1067
|
+
if (modification.rulesToUpdate.length >= 2) {
|
|
1068
|
+
const entries = modification.rulesToUpdate.map((r) => ({ ruleType: r.ruleType, value: r.newValue }));
|
|
1069
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "rules", false, collectionIdMap));
|
|
1070
|
+
} else {
|
|
1071
|
+
const rule = modification.rulesToUpdate[0];
|
|
928
1072
|
const varName = `collection_${collectionName}_rule_${rule.ruleType}`;
|
|
929
1073
|
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.newValue, varName, false, collectionIdMap));
|
|
930
|
-
lines.push(``);
|
|
931
1074
|
}
|
|
1075
|
+
lines.push(``);
|
|
932
1076
|
}
|
|
933
1077
|
}
|
|
934
1078
|
}
|
|
@@ -1000,22 +1144,41 @@ function generateDownMigration(diff) {
|
|
|
1000
1144
|
lines.push(` // Revert modifications`);
|
|
1001
1145
|
for (const modification of diff.collectionsToModify) {
|
|
1002
1146
|
const collectionName = modification.collection;
|
|
1147
|
+
if (modification.viewQueryUpdate) {
|
|
1148
|
+
lines.push(` // Restore the view query of ${collectionName}`);
|
|
1149
|
+
lines.push(
|
|
1150
|
+
generateViewQueryUpdate(
|
|
1151
|
+
collectionName,
|
|
1152
|
+
modification.viewQueryUpdate.oldValue ?? "",
|
|
1153
|
+
`collection_${collectionName}_revert_viewQuery`,
|
|
1154
|
+
false,
|
|
1155
|
+
collectionIdMap
|
|
1156
|
+
)
|
|
1157
|
+
);
|
|
1158
|
+
lines.push(``);
|
|
1159
|
+
}
|
|
1003
1160
|
if (modification.permissionsToUpdate && modification.permissionsToUpdate.length > 0) {
|
|
1004
1161
|
lines.push(` // Revert permissions for ${collectionName}`);
|
|
1005
|
-
|
|
1162
|
+
if (modification.permissionsToUpdate.length >= 2) {
|
|
1163
|
+
const entries = modification.permissionsToUpdate.map((p) => ({ ruleType: p.ruleType, value: p.oldValue }));
|
|
1164
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "revert_rules", false, collectionIdMap));
|
|
1165
|
+
} else {
|
|
1166
|
+
const permission = modification.permissionsToUpdate[0];
|
|
1006
1167
|
const varName = `collection_${collectionName}_revert_perm_${permission.ruleType}`;
|
|
1007
|
-
lines.push(
|
|
1008
|
-
generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, false, collectionIdMap)
|
|
1009
|
-
);
|
|
1010
|
-
lines.push(``);
|
|
1168
|
+
lines.push(generatePermissionUpdate(collectionName, permission.ruleType, permission.oldValue, varName, false, collectionIdMap));
|
|
1011
1169
|
}
|
|
1170
|
+
lines.push(``);
|
|
1012
1171
|
} else if (modification.rulesToUpdate.length > 0) {
|
|
1013
1172
|
lines.push(` // Revert rules for ${collectionName}`);
|
|
1014
|
-
|
|
1173
|
+
if (modification.rulesToUpdate.length >= 2) {
|
|
1174
|
+
const entries = modification.rulesToUpdate.map((r) => ({ ruleType: r.ruleType, value: r.oldValue }));
|
|
1175
|
+
lines.push(generateGroupedRuleUpdates(collectionName, entries, "revert_rules", false, collectionIdMap));
|
|
1176
|
+
} else {
|
|
1177
|
+
const rule = modification.rulesToUpdate[0];
|
|
1015
1178
|
const varName = `collection_${collectionName}_revert_rule_${rule.ruleType}`;
|
|
1016
1179
|
lines.push(generateRuleUpdate(collectionName, rule.ruleType, rule.oldValue, varName, false, collectionIdMap));
|
|
1017
|
-
lines.push(``);
|
|
1018
1180
|
}
|
|
1181
|
+
lines.push(``);
|
|
1019
1182
|
}
|
|
1020
1183
|
if (modification.indexesToRemove.length > 0) {
|
|
1021
1184
|
lines.push(` // Restore indexes to ${collectionName}`);
|
|
@@ -1341,6 +1504,6 @@ var MigrationGenerator = class {
|
|
|
1341
1504
|
}
|
|
1342
1505
|
};
|
|
1343
1506
|
|
|
1344
|
-
export { DEFAULT_CONFIG, DEFAULT_TEMPLATE, MigrationGenerator, createMigrationFileStructure, formatValue, generate, generateCollectionCreation, generateCollectionDeletion, generateCollectionMigrationFilename, generateCollectionPermissions, generateCollectionRules, generateDownMigration, generateFieldAddition, generateFieldConstructorOptions, generateFieldDefinitionObject, generateFieldDeletion, generateFieldModification, generateFieldsArray, generateFindCollectionCode, generateIndexAddition, generateIndexRemoval, generateIndexesArray, generateMigrationDescription, generateMigrationFilename, generateOperationDownMigration, generateOperationUpMigration, generatePermissionUpdate, generateRuleUpdate, generateTimestamp, generateUpMigration, getAuthSystemFields, getAuthSystemIndexes, getFieldConstructorName, getSystemFields, incrementTimestamp, mergeConfig, resolveMigrationDir, splitDiffByCollection, writeMigrationFile };
|
|
1507
|
+
export { DEFAULT_CONFIG, DEFAULT_TEMPLATE, MigrationGenerator, createMigrationFileStructure, formatSqlTemplate, formatValue, generate, generateCollectionCreation, generateCollectionDeletion, generateCollectionMigrationFilename, generateCollectionPermissions, generateCollectionRules, generateDownMigration, generateFieldAddition, generateFieldConstructorOptions, generateFieldDefinitionObject, generateFieldDeletion, generateFieldModification, generateFieldsArray, generateFindCollectionCode, generateGroupedRuleUpdates, generateIndexAddition, generateIndexRemoval, generateIndexesArray, generateMigrationDescription, generateMigrationFilename, generateOperationDownMigration, generateOperationUpMigration, generatePermissionUpdate, generateRuleUpdate, generateTimestamp, generateUpMigration, generateViewQueryUpdate, getAuthSystemFields, getAuthSystemIndexes, getFieldConstructorName, getSystemFields, getSystemTimestampFields, incrementTimestamp, mergeConfig, resolveMigrationDir, splitDiffByCollection, writeMigrationFile };
|
|
1345
1508
|
//# sourceMappingURL=generator.js.map
|
|
1346
1509
|
//# sourceMappingURL=generator.js.map
|