pocketbase-zod-schema 0.2.1 → 0.2.3
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 +94 -10
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +94 -10
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +94 -10
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +94 -10
- package/dist/cli/migrate.js.map +1 -1
- package/dist/index.cjs +100 -12
- 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 +100 -12
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs +36 -7
- package/dist/migration/analyzer.cjs.map +1 -1
- package/dist/migration/analyzer.d.cts +26 -3
- package/dist/migration/analyzer.d.ts +26 -3
- package/dist/migration/analyzer.js +36 -8
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/diff.cjs +7 -2
- package/dist/migration/diff.cjs.map +1 -1
- package/dist/migration/diff.js +7 -2
- package/dist/migration/diff.js.map +1 -1
- package/dist/migration/generator.cjs +52 -1
- package/dist/migration/generator.cjs.map +1 -1
- package/dist/migration/generator.d.cts +0 -7
- package/dist/migration/generator.d.ts +0 -7
- package/dist/migration/generator.js +52 -1
- package/dist/migration/generator.js.map +1 -1
- package/dist/migration/index.cjs +94 -10
- package/dist/migration/index.cjs.map +1 -1
- package/dist/migration/index.js +94 -10
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.cjs.map +1 -1
- package/dist/migration/snapshot.js.map +1 -1
- package/dist/mutator.cjs +9 -3
- package/dist/mutator.cjs.map +1 -1
- package/dist/mutator.d.cts +3 -1
- package/dist/mutator.d.ts +3 -1
- package/dist/mutator.js +9 -3
- package/dist/mutator.js.map +1 -1
- package/dist/schema.cjs +9 -3
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +32 -3
- package/dist/schema.d.ts +32 -3
- package/dist/schema.js +9 -3
- package/dist/schema.js.map +1 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/{user-DTJQIj4K.d.cts → user-BnFWg5tw.d.cts} +13 -1
- package/dist/{user-DTJQIj4K.d.ts → user-BnFWg5tw.d.ts} +13 -1
- package/package.json +1 -1
package/dist/migration/index.js
CHANGED
|
@@ -7,7 +7,9 @@ import { z } from 'zod';
|
|
|
7
7
|
id: z.string().describe("unique id"),
|
|
8
8
|
collectionId: z.string().describe("collection id"),
|
|
9
9
|
collectionName: z.string().describe("collection name"),
|
|
10
|
-
expand: z.record(z.any()).describe("expandable fields")
|
|
10
|
+
expand: z.record(z.any()).describe("expandable fields"),
|
|
11
|
+
created: z.string().describe("creation timestamp"),
|
|
12
|
+
updated: z.string().describe("last update timestamp")
|
|
11
13
|
});
|
|
12
14
|
({
|
|
13
15
|
created: z.string().describe("creation timestamp"),
|
|
@@ -1342,7 +1344,7 @@ var DEFAULT_CONFIG = {
|
|
|
1342
1344
|
"permission-templates.js"
|
|
1343
1345
|
],
|
|
1344
1346
|
includeExtensions: [".ts", ".js"],
|
|
1345
|
-
schemaPatterns: ["Schema", "InputSchema"],
|
|
1347
|
+
schemaPatterns: ["Schema", "InputSchema", "Collection"],
|
|
1346
1348
|
useCompiledFiles: true
|
|
1347
1349
|
};
|
|
1348
1350
|
function mergeConfig(config) {
|
|
@@ -1479,14 +1481,35 @@ function extractCollectionNameFromSchema(zodSchema) {
|
|
|
1479
1481
|
}
|
|
1480
1482
|
return null;
|
|
1481
1483
|
}
|
|
1482
|
-
function
|
|
1484
|
+
function extractCollectionTypeFromSchema(zodSchema) {
|
|
1485
|
+
if (!zodSchema.description) {
|
|
1486
|
+
return null;
|
|
1487
|
+
}
|
|
1488
|
+
try {
|
|
1489
|
+
const metadata = JSON.parse(zodSchema.description);
|
|
1490
|
+
if (metadata.type === "base" || metadata.type === "auth") {
|
|
1491
|
+
return metadata.type;
|
|
1492
|
+
}
|
|
1493
|
+
} catch {
|
|
1494
|
+
}
|
|
1495
|
+
return null;
|
|
1496
|
+
}
|
|
1497
|
+
function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
|
|
1483
1498
|
const result = {};
|
|
1499
|
+
if (module.default instanceof z.ZodObject) {
|
|
1500
|
+
result.schema = module.default;
|
|
1501
|
+
}
|
|
1484
1502
|
for (const [key, value] of Object.entries(module)) {
|
|
1503
|
+
if (key === "default") continue;
|
|
1485
1504
|
if (value instanceof z.ZodObject) {
|
|
1486
1505
|
if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
|
|
1487
1506
|
result.inputSchema = value;
|
|
1488
|
-
} else if (
|
|
1489
|
-
|
|
1507
|
+
} else if (!result.schema) {
|
|
1508
|
+
if (patterns.includes("Collection") && key.endsWith("Collection")) {
|
|
1509
|
+
result.schema = value;
|
|
1510
|
+
} else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
|
|
1511
|
+
result.schema = value;
|
|
1512
|
+
}
|
|
1490
1513
|
}
|
|
1491
1514
|
}
|
|
1492
1515
|
}
|
|
@@ -1575,8 +1598,13 @@ function extractIndexes(schema) {
|
|
|
1575
1598
|
}
|
|
1576
1599
|
function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
|
|
1577
1600
|
const rawFields = extractFieldDefinitions(zodSchema);
|
|
1578
|
-
const
|
|
1579
|
-
const
|
|
1601
|
+
const explicitType = extractCollectionTypeFromSchema(zodSchema);
|
|
1602
|
+
const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
|
|
1603
|
+
let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
|
|
1604
|
+
if (collectionType === "auth") {
|
|
1605
|
+
const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
|
|
1606
|
+
fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
|
|
1607
|
+
}
|
|
1580
1608
|
const indexes = extractIndexes(zodSchema) || [];
|
|
1581
1609
|
const permissionAnalyzer = new PermissionAnalyzer();
|
|
1582
1610
|
let permissions = void 0;
|
|
@@ -2560,9 +2588,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
|
|
|
2560
2588
|
if (!previousSnapshot) {
|
|
2561
2589
|
return matches;
|
|
2562
2590
|
}
|
|
2591
|
+
const previousCollectionsLower = /* @__PURE__ */ new Map();
|
|
2592
|
+
for (const [name, collection] of previousSnapshot.collections) {
|
|
2593
|
+
previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
|
|
2594
|
+
}
|
|
2563
2595
|
for (const [collectionName, currentCollection] of currentSchema.collections) {
|
|
2564
|
-
const
|
|
2565
|
-
if (
|
|
2596
|
+
const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
|
|
2597
|
+
if (previousEntry) {
|
|
2598
|
+
const [, previousCollection] = previousEntry;
|
|
2566
2599
|
matches.push([currentCollection, previousCollection]);
|
|
2567
2600
|
}
|
|
2568
2601
|
}
|
|
@@ -3300,6 +3333,55 @@ function generateIndexesArray(indexes) {
|
|
|
3300
3333
|
${indexStrings.join(",\n ")},
|
|
3301
3334
|
]`;
|
|
3302
3335
|
}
|
|
3336
|
+
function getSystemFields() {
|
|
3337
|
+
return [
|
|
3338
|
+
// id field - primary key, auto-generated
|
|
3339
|
+
{
|
|
3340
|
+
name: "id",
|
|
3341
|
+
type: "text",
|
|
3342
|
+
required: true,
|
|
3343
|
+
options: {
|
|
3344
|
+
autogeneratePattern: "[a-z0-9]{15}",
|
|
3345
|
+
hidden: false,
|
|
3346
|
+
id: "text3208210256",
|
|
3347
|
+
max: 15,
|
|
3348
|
+
min: 15,
|
|
3349
|
+
pattern: "^[a-z0-9]+$",
|
|
3350
|
+
presentable: false,
|
|
3351
|
+
primaryKey: true,
|
|
3352
|
+
system: true
|
|
3353
|
+
}
|
|
3354
|
+
},
|
|
3355
|
+
// created field - autodate, set on creation
|
|
3356
|
+
{
|
|
3357
|
+
name: "created",
|
|
3358
|
+
type: "autodate",
|
|
3359
|
+
required: true,
|
|
3360
|
+
options: {
|
|
3361
|
+
hidden: false,
|
|
3362
|
+
id: "autodate2990389176",
|
|
3363
|
+
onCreate: true,
|
|
3364
|
+
onUpdate: false,
|
|
3365
|
+
presentable: false,
|
|
3366
|
+
system: false
|
|
3367
|
+
}
|
|
3368
|
+
},
|
|
3369
|
+
// updated field - autodate, set on creation and update
|
|
3370
|
+
{
|
|
3371
|
+
name: "updated",
|
|
3372
|
+
type: "autodate",
|
|
3373
|
+
required: true,
|
|
3374
|
+
options: {
|
|
3375
|
+
hidden: false,
|
|
3376
|
+
id: "autodate3332085495",
|
|
3377
|
+
onCreate: true,
|
|
3378
|
+
onUpdate: true,
|
|
3379
|
+
presentable: false,
|
|
3380
|
+
system: false
|
|
3381
|
+
}
|
|
3382
|
+
}
|
|
3383
|
+
];
|
|
3384
|
+
}
|
|
3303
3385
|
function generateCollectionCreation(collection, varName = "collection", isLast = false) {
|
|
3304
3386
|
const lines = [];
|
|
3305
3387
|
lines.push(` const ${varName} = new Collection({`);
|
|
@@ -3312,7 +3394,9 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
|
|
|
3312
3394
|
} else if (rulesCode) {
|
|
3313
3395
|
lines.push(` ${rulesCode},`);
|
|
3314
3396
|
}
|
|
3315
|
-
|
|
3397
|
+
const systemFields = getSystemFields();
|
|
3398
|
+
const allFields = [...systemFields, ...collection.fields];
|
|
3399
|
+
lines.push(` fields: ${generateFieldsArray(allFields)},`);
|
|
3316
3400
|
lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
|
|
3317
3401
|
lines.push(` });`);
|
|
3318
3402
|
lines.push(``);
|