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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.3](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.2.2...pocketbase-zod-schema-v0.2.3) (2025-12-20)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * include default system fields (id, updated, created) ([d89ef2e](https://github.com/dastron/pocketbase-zod-schema/commit/d89ef2ec7c3dec52cafce826c12e4e9288d05255))
9
+
10
+ ## [0.2.2](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.2.1...pocketbase-zod-schema-v0.2.2) (2025-12-20)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * update base schema ([98aaaef](https://github.com/dastron/pocketbase-zod-schema/commit/98aaaef365f3de350837c2390adb6870758781a2))
16
+
3
17
  ## [0.2.1](https://github.com/dastron/pocketbase-zod-schema/compare/pocketbase-zod-schema-v0.2.0...pocketbase-zod-schema-v0.2.1) (2025-12-20)
4
18
 
5
19
 
@@ -36,7 +36,9 @@ var ora__default = /*#__PURE__*/_interopDefault(ora);
36
36
  id: zod.z.string().describe("unique id"),
37
37
  collectionId: zod.z.string().describe("collection id"),
38
38
  collectionName: zod.z.string().describe("collection name"),
39
- expand: zod.z.record(zod.z.any()).describe("expandable fields")
39
+ expand: zod.z.record(zod.z.any()).describe("expandable fields"),
40
+ created: zod.z.string().describe("creation timestamp"),
41
+ updated: zod.z.string().describe("last update timestamp")
40
42
  });
41
43
  ({
42
44
  created: zod.z.string().describe("creation timestamp"),
@@ -1099,7 +1101,7 @@ var DEFAULT_CONFIG = {
1099
1101
  "permission-templates.js"
1100
1102
  ],
1101
1103
  includeExtensions: [".ts", ".js"],
1102
- schemaPatterns: ["Schema", "InputSchema"],
1104
+ schemaPatterns: ["Schema", "InputSchema", "Collection"],
1103
1105
  useCompiledFiles: true
1104
1106
  };
1105
1107
  function mergeConfig(config) {
@@ -1236,14 +1238,35 @@ function extractCollectionNameFromSchema(zodSchema) {
1236
1238
  }
1237
1239
  return null;
1238
1240
  }
1239
- function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1241
+ function extractCollectionTypeFromSchema(zodSchema) {
1242
+ if (!zodSchema.description) {
1243
+ return null;
1244
+ }
1245
+ try {
1246
+ const metadata = JSON.parse(zodSchema.description);
1247
+ if (metadata.type === "base" || metadata.type === "auth") {
1248
+ return metadata.type;
1249
+ }
1250
+ } catch {
1251
+ }
1252
+ return null;
1253
+ }
1254
+ function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
1240
1255
  const result = {};
1256
+ if (module.default instanceof zod.z.ZodObject) {
1257
+ result.schema = module.default;
1258
+ }
1241
1259
  for (const [key, value] of Object.entries(module)) {
1260
+ if (key === "default") continue;
1242
1261
  if (value instanceof zod.z.ZodObject) {
1243
1262
  if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
1244
1263
  result.inputSchema = value;
1245
- } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1246
- result.schema = value;
1264
+ } else if (!result.schema) {
1265
+ if (patterns.includes("Collection") && key.endsWith("Collection")) {
1266
+ result.schema = value;
1267
+ } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1268
+ result.schema = value;
1269
+ }
1247
1270
  }
1248
1271
  }
1249
1272
  }
@@ -1332,8 +1355,13 @@ function extractIndexes(schema) {
1332
1355
  }
1333
1356
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1334
1357
  const rawFields = extractFieldDefinitions(zodSchema);
1335
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
1336
- const fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1358
+ const explicitType = extractCollectionTypeFromSchema(zodSchema);
1359
+ const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
1360
+ let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1361
+ if (collectionType === "auth") {
1362
+ const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
1363
+ fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
1364
+ }
1337
1365
  const indexes = extractIndexes(zodSchema) || [];
1338
1366
  const permissionAnalyzer = new PermissionAnalyzer();
1339
1367
  let permissions = void 0;
@@ -1480,9 +1508,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
1480
1508
  if (!previousSnapshot) {
1481
1509
  return matches;
1482
1510
  }
1511
+ const previousCollectionsLower = /* @__PURE__ */ new Map();
1512
+ for (const [name, collection] of previousSnapshot.collections) {
1513
+ previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
1514
+ }
1483
1515
  for (const [collectionName, currentCollection] of currentSchema.collections) {
1484
- const previousCollection = previousSnapshot.collections.get(collectionName);
1485
- if (previousCollection) {
1516
+ const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
1517
+ if (previousEntry) {
1518
+ const [, previousCollection] = previousEntry;
1486
1519
  matches.push([currentCollection, previousCollection]);
1487
1520
  }
1488
1521
  }
@@ -2067,6 +2100,55 @@ function generateIndexesArray(indexes) {
2067
2100
  ${indexStrings.join(",\n ")},
2068
2101
  ]`;
2069
2102
  }
2103
+ function getSystemFields() {
2104
+ return [
2105
+ // id field - primary key, auto-generated
2106
+ {
2107
+ name: "id",
2108
+ type: "text",
2109
+ required: true,
2110
+ options: {
2111
+ autogeneratePattern: "[a-z0-9]{15}",
2112
+ hidden: false,
2113
+ id: "text3208210256",
2114
+ max: 15,
2115
+ min: 15,
2116
+ pattern: "^[a-z0-9]+$",
2117
+ presentable: false,
2118
+ primaryKey: true,
2119
+ system: true
2120
+ }
2121
+ },
2122
+ // created field - autodate, set on creation
2123
+ {
2124
+ name: "created",
2125
+ type: "autodate",
2126
+ required: true,
2127
+ options: {
2128
+ hidden: false,
2129
+ id: "autodate2990389176",
2130
+ onCreate: true,
2131
+ onUpdate: false,
2132
+ presentable: false,
2133
+ system: false
2134
+ }
2135
+ },
2136
+ // updated field - autodate, set on creation and update
2137
+ {
2138
+ name: "updated",
2139
+ type: "autodate",
2140
+ required: true,
2141
+ options: {
2142
+ hidden: false,
2143
+ id: "autodate3332085495",
2144
+ onCreate: true,
2145
+ onUpdate: true,
2146
+ presentable: false,
2147
+ system: false
2148
+ }
2149
+ }
2150
+ ];
2151
+ }
2070
2152
  function generateCollectionCreation(collection, varName = "collection", isLast = false) {
2071
2153
  const lines = [];
2072
2154
  lines.push(` const ${varName} = new Collection({`);
@@ -2079,7 +2161,9 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
2079
2161
  } else if (rulesCode) {
2080
2162
  lines.push(` ${rulesCode},`);
2081
2163
  }
2082
- lines.push(` fields: ${generateFieldsArray(collection.fields)},`);
2164
+ const systemFields = getSystemFields();
2165
+ const allFields = [...systemFields, ...collection.fields];
2166
+ lines.push(` fields: ${generateFieldsArray(allFields)},`);
2083
2167
  lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
2084
2168
  lines.push(` });`);
2085
2169
  lines.push(``);