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/cli/migrate.cjs
CHANGED
|
@@ -41,7 +41,9 @@ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
|
41
41
|
id: zod.z.string().describe("unique id"),
|
|
42
42
|
collectionId: zod.z.string().describe("collection id"),
|
|
43
43
|
collectionName: zod.z.string().describe("collection name"),
|
|
44
|
-
expand: zod.z.record(zod.z.any()).describe("expandable fields")
|
|
44
|
+
expand: zod.z.record(zod.z.any()).describe("expandable fields"),
|
|
45
|
+
created: zod.z.string().describe("creation timestamp"),
|
|
46
|
+
updated: zod.z.string().describe("last update timestamp")
|
|
45
47
|
});
|
|
46
48
|
({
|
|
47
49
|
created: zod.z.string().describe("creation timestamp"),
|
|
@@ -1104,7 +1106,7 @@ var DEFAULT_CONFIG = {
|
|
|
1104
1106
|
"permission-templates.js"
|
|
1105
1107
|
],
|
|
1106
1108
|
includeExtensions: [".ts", ".js"],
|
|
1107
|
-
schemaPatterns: ["Schema", "InputSchema"],
|
|
1109
|
+
schemaPatterns: ["Schema", "InputSchema", "Collection"],
|
|
1108
1110
|
useCompiledFiles: true
|
|
1109
1111
|
};
|
|
1110
1112
|
function mergeConfig(config) {
|
|
@@ -1241,14 +1243,35 @@ function extractCollectionNameFromSchema(zodSchema) {
|
|
|
1241
1243
|
}
|
|
1242
1244
|
return null;
|
|
1243
1245
|
}
|
|
1244
|
-
function
|
|
1246
|
+
function extractCollectionTypeFromSchema(zodSchema) {
|
|
1247
|
+
if (!zodSchema.description) {
|
|
1248
|
+
return null;
|
|
1249
|
+
}
|
|
1250
|
+
try {
|
|
1251
|
+
const metadata = JSON.parse(zodSchema.description);
|
|
1252
|
+
if (metadata.type === "base" || metadata.type === "auth") {
|
|
1253
|
+
return metadata.type;
|
|
1254
|
+
}
|
|
1255
|
+
} catch {
|
|
1256
|
+
}
|
|
1257
|
+
return null;
|
|
1258
|
+
}
|
|
1259
|
+
function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
|
|
1245
1260
|
const result = {};
|
|
1261
|
+
if (module.default instanceof zod.z.ZodObject) {
|
|
1262
|
+
result.schema = module.default;
|
|
1263
|
+
}
|
|
1246
1264
|
for (const [key, value] of Object.entries(module)) {
|
|
1265
|
+
if (key === "default") continue;
|
|
1247
1266
|
if (value instanceof zod.z.ZodObject) {
|
|
1248
1267
|
if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
|
|
1249
1268
|
result.inputSchema = value;
|
|
1250
|
-
} else if (
|
|
1251
|
-
|
|
1269
|
+
} else if (!result.schema) {
|
|
1270
|
+
if (patterns.includes("Collection") && key.endsWith("Collection")) {
|
|
1271
|
+
result.schema = value;
|
|
1272
|
+
} else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
|
|
1273
|
+
result.schema = value;
|
|
1274
|
+
}
|
|
1252
1275
|
}
|
|
1253
1276
|
}
|
|
1254
1277
|
}
|
|
@@ -1337,8 +1360,13 @@ function extractIndexes(schema) {
|
|
|
1337
1360
|
}
|
|
1338
1361
|
function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
|
|
1339
1362
|
const rawFields = extractFieldDefinitions(zodSchema);
|
|
1340
|
-
const
|
|
1341
|
-
const
|
|
1363
|
+
const explicitType = extractCollectionTypeFromSchema(zodSchema);
|
|
1364
|
+
const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
|
|
1365
|
+
let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
|
|
1366
|
+
if (collectionType === "auth") {
|
|
1367
|
+
const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
|
|
1368
|
+
fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
|
|
1369
|
+
}
|
|
1342
1370
|
const indexes = extractIndexes(zodSchema) || [];
|
|
1343
1371
|
const permissionAnalyzer = new PermissionAnalyzer();
|
|
1344
1372
|
let permissions = void 0;
|
|
@@ -1485,9 +1513,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
|
|
|
1485
1513
|
if (!previousSnapshot) {
|
|
1486
1514
|
return matches;
|
|
1487
1515
|
}
|
|
1516
|
+
const previousCollectionsLower = /* @__PURE__ */ new Map();
|
|
1517
|
+
for (const [name, collection] of previousSnapshot.collections) {
|
|
1518
|
+
previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
|
|
1519
|
+
}
|
|
1488
1520
|
for (const [collectionName, currentCollection] of currentSchema.collections) {
|
|
1489
|
-
const
|
|
1490
|
-
if (
|
|
1521
|
+
const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
|
|
1522
|
+
if (previousEntry) {
|
|
1523
|
+
const [, previousCollection] = previousEntry;
|
|
1491
1524
|
matches.push([currentCollection, previousCollection]);
|
|
1492
1525
|
}
|
|
1493
1526
|
}
|
|
@@ -2072,6 +2105,55 @@ function generateIndexesArray(indexes) {
|
|
|
2072
2105
|
${indexStrings.join(",\n ")},
|
|
2073
2106
|
]`;
|
|
2074
2107
|
}
|
|
2108
|
+
function getSystemFields() {
|
|
2109
|
+
return [
|
|
2110
|
+
// id field - primary key, auto-generated
|
|
2111
|
+
{
|
|
2112
|
+
name: "id",
|
|
2113
|
+
type: "text",
|
|
2114
|
+
required: true,
|
|
2115
|
+
options: {
|
|
2116
|
+
autogeneratePattern: "[a-z0-9]{15}",
|
|
2117
|
+
hidden: false,
|
|
2118
|
+
id: "text3208210256",
|
|
2119
|
+
max: 15,
|
|
2120
|
+
min: 15,
|
|
2121
|
+
pattern: "^[a-z0-9]+$",
|
|
2122
|
+
presentable: false,
|
|
2123
|
+
primaryKey: true,
|
|
2124
|
+
system: true
|
|
2125
|
+
}
|
|
2126
|
+
},
|
|
2127
|
+
// created field - autodate, set on creation
|
|
2128
|
+
{
|
|
2129
|
+
name: "created",
|
|
2130
|
+
type: "autodate",
|
|
2131
|
+
required: true,
|
|
2132
|
+
options: {
|
|
2133
|
+
hidden: false,
|
|
2134
|
+
id: "autodate2990389176",
|
|
2135
|
+
onCreate: true,
|
|
2136
|
+
onUpdate: false,
|
|
2137
|
+
presentable: false,
|
|
2138
|
+
system: false
|
|
2139
|
+
}
|
|
2140
|
+
},
|
|
2141
|
+
// updated field - autodate, set on creation and update
|
|
2142
|
+
{
|
|
2143
|
+
name: "updated",
|
|
2144
|
+
type: "autodate",
|
|
2145
|
+
required: true,
|
|
2146
|
+
options: {
|
|
2147
|
+
hidden: false,
|
|
2148
|
+
id: "autodate3332085495",
|
|
2149
|
+
onCreate: true,
|
|
2150
|
+
onUpdate: true,
|
|
2151
|
+
presentable: false,
|
|
2152
|
+
system: false
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
];
|
|
2156
|
+
}
|
|
2075
2157
|
function generateCollectionCreation(collection, varName = "collection", isLast = false) {
|
|
2076
2158
|
const lines = [];
|
|
2077
2159
|
lines.push(` const ${varName} = new Collection({`);
|
|
@@ -2084,7 +2166,9 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
|
|
|
2084
2166
|
} else if (rulesCode) {
|
|
2085
2167
|
lines.push(` ${rulesCode},`);
|
|
2086
2168
|
}
|
|
2087
|
-
|
|
2169
|
+
const systemFields = getSystemFields();
|
|
2170
|
+
const allFields = [...systemFields, ...collection.fields];
|
|
2171
|
+
lines.push(` fields: ${generateFieldsArray(allFields)},`);
|
|
2088
2172
|
lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
|
|
2089
2173
|
lines.push(` });`);
|
|
2090
2174
|
lines.push(``);
|