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.
Files changed (54) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cli/index.cjs +94 -10
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.js +94 -10
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/cli/migrate.cjs +94 -10
  7. package/dist/cli/migrate.cjs.map +1 -1
  8. package/dist/cli/migrate.js +94 -10
  9. package/dist/cli/migrate.js.map +1 -1
  10. package/dist/index.cjs +100 -12
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +100 -12
  15. package/dist/index.js.map +1 -1
  16. package/dist/migration/analyzer.cjs +36 -7
  17. package/dist/migration/analyzer.cjs.map +1 -1
  18. package/dist/migration/analyzer.d.cts +26 -3
  19. package/dist/migration/analyzer.d.ts +26 -3
  20. package/dist/migration/analyzer.js +36 -8
  21. package/dist/migration/analyzer.js.map +1 -1
  22. package/dist/migration/diff.cjs +7 -2
  23. package/dist/migration/diff.cjs.map +1 -1
  24. package/dist/migration/diff.js +7 -2
  25. package/dist/migration/diff.js.map +1 -1
  26. package/dist/migration/generator.cjs +52 -1
  27. package/dist/migration/generator.cjs.map +1 -1
  28. package/dist/migration/generator.d.cts +0 -7
  29. package/dist/migration/generator.d.ts +0 -7
  30. package/dist/migration/generator.js +52 -1
  31. package/dist/migration/generator.js.map +1 -1
  32. package/dist/migration/index.cjs +94 -10
  33. package/dist/migration/index.cjs.map +1 -1
  34. package/dist/migration/index.js +94 -10
  35. package/dist/migration/index.js.map +1 -1
  36. package/dist/migration/snapshot.cjs.map +1 -1
  37. package/dist/migration/snapshot.js.map +1 -1
  38. package/dist/mutator.cjs +9 -3
  39. package/dist/mutator.cjs.map +1 -1
  40. package/dist/mutator.d.cts +3 -1
  41. package/dist/mutator.d.ts +3 -1
  42. package/dist/mutator.js +9 -3
  43. package/dist/mutator.js.map +1 -1
  44. package/dist/schema.cjs +9 -3
  45. package/dist/schema.cjs.map +1 -1
  46. package/dist/schema.d.cts +32 -3
  47. package/dist/schema.d.ts +32 -3
  48. package/dist/schema.js +9 -3
  49. package/dist/schema.js.map +1 -1
  50. package/dist/types.d.cts +1 -1
  51. package/dist/types.d.ts +1 -1
  52. package/dist/{user-DTJQIj4K.d.cts → user-BnFWg5tw.d.cts} +13 -1
  53. package/dist/{user-DTJQIj4K.d.ts → user-BnFWg5tw.d.ts} +13 -1
  54. package/package.json +1 -1
@@ -13,7 +13,9 @@ import ora from 'ora';
13
13
  id: z.string().describe("unique id"),
14
14
  collectionId: z.string().describe("collection id"),
15
15
  collectionName: z.string().describe("collection name"),
16
- expand: z.record(z.any()).describe("expandable fields")
16
+ expand: z.record(z.any()).describe("expandable fields"),
17
+ created: z.string().describe("creation timestamp"),
18
+ updated: z.string().describe("last update timestamp")
17
19
  });
18
20
  ({
19
21
  created: z.string().describe("creation timestamp"),
@@ -1076,7 +1078,7 @@ var DEFAULT_CONFIG = {
1076
1078
  "permission-templates.js"
1077
1079
  ],
1078
1080
  includeExtensions: [".ts", ".js"],
1079
- schemaPatterns: ["Schema", "InputSchema"],
1081
+ schemaPatterns: ["Schema", "InputSchema", "Collection"],
1080
1082
  useCompiledFiles: true
1081
1083
  };
1082
1084
  function mergeConfig(config) {
@@ -1213,14 +1215,35 @@ function extractCollectionNameFromSchema(zodSchema) {
1213
1215
  }
1214
1216
  return null;
1215
1217
  }
1216
- function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1218
+ function extractCollectionTypeFromSchema(zodSchema) {
1219
+ if (!zodSchema.description) {
1220
+ return null;
1221
+ }
1222
+ try {
1223
+ const metadata = JSON.parse(zodSchema.description);
1224
+ if (metadata.type === "base" || metadata.type === "auth") {
1225
+ return metadata.type;
1226
+ }
1227
+ } catch {
1228
+ }
1229
+ return null;
1230
+ }
1231
+ function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
1217
1232
  const result = {};
1233
+ if (module.default instanceof z.ZodObject) {
1234
+ result.schema = module.default;
1235
+ }
1218
1236
  for (const [key, value] of Object.entries(module)) {
1237
+ if (key === "default") continue;
1219
1238
  if (value instanceof z.ZodObject) {
1220
1239
  if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
1221
1240
  result.inputSchema = value;
1222
- } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1223
- result.schema = value;
1241
+ } else if (!result.schema) {
1242
+ if (patterns.includes("Collection") && key.endsWith("Collection")) {
1243
+ result.schema = value;
1244
+ } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1245
+ result.schema = value;
1246
+ }
1224
1247
  }
1225
1248
  }
1226
1249
  }
@@ -1309,8 +1332,13 @@ function extractIndexes(schema) {
1309
1332
  }
1310
1333
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1311
1334
  const rawFields = extractFieldDefinitions(zodSchema);
1312
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
1313
- const fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1335
+ const explicitType = extractCollectionTypeFromSchema(zodSchema);
1336
+ const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
1337
+ let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1338
+ if (collectionType === "auth") {
1339
+ const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
1340
+ fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
1341
+ }
1314
1342
  const indexes = extractIndexes(zodSchema) || [];
1315
1343
  const permissionAnalyzer = new PermissionAnalyzer();
1316
1344
  let permissions = void 0;
@@ -1457,9 +1485,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
1457
1485
  if (!previousSnapshot) {
1458
1486
  return matches;
1459
1487
  }
1488
+ const previousCollectionsLower = /* @__PURE__ */ new Map();
1489
+ for (const [name, collection] of previousSnapshot.collections) {
1490
+ previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
1491
+ }
1460
1492
  for (const [collectionName, currentCollection] of currentSchema.collections) {
1461
- const previousCollection = previousSnapshot.collections.get(collectionName);
1462
- if (previousCollection) {
1493
+ const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
1494
+ if (previousEntry) {
1495
+ const [, previousCollection] = previousEntry;
1463
1496
  matches.push([currentCollection, previousCollection]);
1464
1497
  }
1465
1498
  }
@@ -2044,6 +2077,55 @@ function generateIndexesArray(indexes) {
2044
2077
  ${indexStrings.join(",\n ")},
2045
2078
  ]`;
2046
2079
  }
2080
+ function getSystemFields() {
2081
+ return [
2082
+ // id field - primary key, auto-generated
2083
+ {
2084
+ name: "id",
2085
+ type: "text",
2086
+ required: true,
2087
+ options: {
2088
+ autogeneratePattern: "[a-z0-9]{15}",
2089
+ hidden: false,
2090
+ id: "text3208210256",
2091
+ max: 15,
2092
+ min: 15,
2093
+ pattern: "^[a-z0-9]+$",
2094
+ presentable: false,
2095
+ primaryKey: true,
2096
+ system: true
2097
+ }
2098
+ },
2099
+ // created field - autodate, set on creation
2100
+ {
2101
+ name: "created",
2102
+ type: "autodate",
2103
+ required: true,
2104
+ options: {
2105
+ hidden: false,
2106
+ id: "autodate2990389176",
2107
+ onCreate: true,
2108
+ onUpdate: false,
2109
+ presentable: false,
2110
+ system: false
2111
+ }
2112
+ },
2113
+ // updated field - autodate, set on creation and update
2114
+ {
2115
+ name: "updated",
2116
+ type: "autodate",
2117
+ required: true,
2118
+ options: {
2119
+ hidden: false,
2120
+ id: "autodate3332085495",
2121
+ onCreate: true,
2122
+ onUpdate: true,
2123
+ presentable: false,
2124
+ system: false
2125
+ }
2126
+ }
2127
+ ];
2128
+ }
2047
2129
  function generateCollectionCreation(collection, varName = "collection", isLast = false) {
2048
2130
  const lines = [];
2049
2131
  lines.push(` const ${varName} = new Collection({`);
@@ -2056,7 +2138,9 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
2056
2138
  } else if (rulesCode) {
2057
2139
  lines.push(` ${rulesCode},`);
2058
2140
  }
2059
- lines.push(` fields: ${generateFieldsArray(collection.fields)},`);
2141
+ const systemFields = getSystemFields();
2142
+ const allFields = [...systemFields, ...collection.fields];
2143
+ lines.push(` fields: ${generateFieldsArray(allFields)},`);
2060
2144
  lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
2061
2145
  lines.push(` });`);
2062
2146
  lines.push(``);