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/dist/cli/index.js CHANGED
@@ -9,7 +9,9 @@ import ora from 'ora';
9
9
  id: z.string().describe("unique id"),
10
10
  collectionId: z.string().describe("collection id"),
11
11
  collectionName: z.string().describe("collection name"),
12
- expand: z.record(z.any()).describe("expandable fields")
12
+ expand: z.record(z.any()).describe("expandable fields"),
13
+ created: z.string().describe("creation timestamp"),
14
+ updated: z.string().describe("last update timestamp")
13
15
  });
14
16
  ({
15
17
  created: z.string().describe("creation timestamp"),
@@ -1072,7 +1074,7 @@ var DEFAULT_CONFIG = {
1072
1074
  "permission-templates.js"
1073
1075
  ],
1074
1076
  includeExtensions: [".ts", ".js"],
1075
- schemaPatterns: ["Schema", "InputSchema"],
1077
+ schemaPatterns: ["Schema", "InputSchema", "Collection"],
1076
1078
  useCompiledFiles: true
1077
1079
  };
1078
1080
  function mergeConfig(config) {
@@ -1209,14 +1211,35 @@ function extractCollectionNameFromSchema(zodSchema) {
1209
1211
  }
1210
1212
  return null;
1211
1213
  }
1212
- function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1214
+ function extractCollectionTypeFromSchema(zodSchema) {
1215
+ if (!zodSchema.description) {
1216
+ return null;
1217
+ }
1218
+ try {
1219
+ const metadata = JSON.parse(zodSchema.description);
1220
+ if (metadata.type === "base" || metadata.type === "auth") {
1221
+ return metadata.type;
1222
+ }
1223
+ } catch {
1224
+ }
1225
+ return null;
1226
+ }
1227
+ function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
1213
1228
  const result = {};
1229
+ if (module.default instanceof z.ZodObject) {
1230
+ result.schema = module.default;
1231
+ }
1214
1232
  for (const [key, value] of Object.entries(module)) {
1233
+ if (key === "default") continue;
1215
1234
  if (value instanceof z.ZodObject) {
1216
1235
  if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
1217
1236
  result.inputSchema = value;
1218
- } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1219
- result.schema = value;
1237
+ } else if (!result.schema) {
1238
+ if (patterns.includes("Collection") && key.endsWith("Collection")) {
1239
+ result.schema = value;
1240
+ } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1241
+ result.schema = value;
1242
+ }
1220
1243
  }
1221
1244
  }
1222
1245
  }
@@ -1305,8 +1328,13 @@ function extractIndexes(schema) {
1305
1328
  }
1306
1329
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1307
1330
  const rawFields = extractFieldDefinitions(zodSchema);
1308
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
1309
- const fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1331
+ const explicitType = extractCollectionTypeFromSchema(zodSchema);
1332
+ const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
1333
+ let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1334
+ if (collectionType === "auth") {
1335
+ const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
1336
+ fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
1337
+ }
1310
1338
  const indexes = extractIndexes(zodSchema) || [];
1311
1339
  const permissionAnalyzer = new PermissionAnalyzer();
1312
1340
  let permissions = void 0;
@@ -1453,9 +1481,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
1453
1481
  if (!previousSnapshot) {
1454
1482
  return matches;
1455
1483
  }
1484
+ const previousCollectionsLower = /* @__PURE__ */ new Map();
1485
+ for (const [name, collection] of previousSnapshot.collections) {
1486
+ previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
1487
+ }
1456
1488
  for (const [collectionName, currentCollection] of currentSchema.collections) {
1457
- const previousCollection = previousSnapshot.collections.get(collectionName);
1458
- if (previousCollection) {
1489
+ const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
1490
+ if (previousEntry) {
1491
+ const [, previousCollection] = previousEntry;
1459
1492
  matches.push([currentCollection, previousCollection]);
1460
1493
  }
1461
1494
  }
@@ -2040,6 +2073,55 @@ function generateIndexesArray(indexes) {
2040
2073
  ${indexStrings.join(",\n ")},
2041
2074
  ]`;
2042
2075
  }
2076
+ function getSystemFields() {
2077
+ return [
2078
+ // id field - primary key, auto-generated
2079
+ {
2080
+ name: "id",
2081
+ type: "text",
2082
+ required: true,
2083
+ options: {
2084
+ autogeneratePattern: "[a-z0-9]{15}",
2085
+ hidden: false,
2086
+ id: "text3208210256",
2087
+ max: 15,
2088
+ min: 15,
2089
+ pattern: "^[a-z0-9]+$",
2090
+ presentable: false,
2091
+ primaryKey: true,
2092
+ system: true
2093
+ }
2094
+ },
2095
+ // created field - autodate, set on creation
2096
+ {
2097
+ name: "created",
2098
+ type: "autodate",
2099
+ required: true,
2100
+ options: {
2101
+ hidden: false,
2102
+ id: "autodate2990389176",
2103
+ onCreate: true,
2104
+ onUpdate: false,
2105
+ presentable: false,
2106
+ system: false
2107
+ }
2108
+ },
2109
+ // updated field - autodate, set on creation and update
2110
+ {
2111
+ name: "updated",
2112
+ type: "autodate",
2113
+ required: true,
2114
+ options: {
2115
+ hidden: false,
2116
+ id: "autodate3332085495",
2117
+ onCreate: true,
2118
+ onUpdate: true,
2119
+ presentable: false,
2120
+ system: false
2121
+ }
2122
+ }
2123
+ ];
2124
+ }
2043
2125
  function generateCollectionCreation(collection, varName = "collection", isLast = false) {
2044
2126
  const lines = [];
2045
2127
  lines.push(` const ${varName} = new Collection({`);
@@ -2052,7 +2134,9 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
2052
2134
  } else if (rulesCode) {
2053
2135
  lines.push(` ${rulesCode},`);
2054
2136
  }
2055
- lines.push(` fields: ${generateFieldsArray(collection.fields)},`);
2137
+ const systemFields = getSystemFields();
2138
+ const allFields = [...systemFields, ...collection.fields];
2139
+ lines.push(` fields: ${generateFieldsArray(allFields)},`);
2056
2140
  lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
2057
2141
  lines.push(` });`);
2058
2142
  lines.push(``);