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/index.cjs CHANGED
@@ -46,7 +46,9 @@ var baseSchema = {
46
46
  id: zod.z.string().describe("unique id"),
47
47
  collectionId: zod.z.string().describe("collection id"),
48
48
  collectionName: zod.z.string().describe("collection name"),
49
- expand: zod.z.record(zod.z.any()).describe("expandable fields")
49
+ expand: zod.z.record(zod.z.any()).describe("expandable fields"),
50
+ created: zod.z.string().describe("creation timestamp"),
51
+ updated: zod.z.string().describe("last update timestamp")
50
52
  };
51
53
  var baseSchemaWithTimestamps = {
52
54
  ...baseSchema,
@@ -177,10 +179,13 @@ function withIndexes(schema, indexes) {
177
179
  return schema.describe(JSON.stringify(metadata));
178
180
  }
179
181
  function defineCollection(config) {
180
- const { collectionName, schema, permissions, indexes, ...futureOptions } = config;
182
+ const { collectionName, schema, permissions, indexes, type, ...futureOptions } = config;
181
183
  const metadata = {
182
184
  collectionName
183
185
  };
186
+ if (type) {
187
+ metadata.type = type;
188
+ }
184
189
  if (permissions) {
185
190
  metadata.permissions = permissions;
186
191
  }
@@ -449,7 +454,8 @@ var UserCollectionSchema = zod.z.object({
449
454
  });
450
455
  var UserSchema = UserCollectionSchema.extend(baseSchema);
451
456
  var UserCollection = defineCollection({
452
- collectionName: "Users",
457
+ collectionName: "users",
458
+ type: "auth",
453
459
  schema: UserSchema,
454
460
  permissions: {
455
461
  // Users can list their own profile
@@ -2014,7 +2020,7 @@ var DEFAULT_CONFIG = {
2014
2020
  "permission-templates.js"
2015
2021
  ],
2016
2022
  includeExtensions: [".ts", ".js"],
2017
- schemaPatterns: ["Schema", "InputSchema"],
2023
+ schemaPatterns: ["Schema", "InputSchema", "Collection"],
2018
2024
  useCompiledFiles: true
2019
2025
  };
2020
2026
  function mergeConfig(config) {
@@ -2151,14 +2157,35 @@ function extractCollectionNameFromSchema(zodSchema) {
2151
2157
  }
2152
2158
  return null;
2153
2159
  }
2154
- function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
2160
+ function extractCollectionTypeFromSchema(zodSchema) {
2161
+ if (!zodSchema.description) {
2162
+ return null;
2163
+ }
2164
+ try {
2165
+ const metadata = JSON.parse(zodSchema.description);
2166
+ if (metadata.type === "base" || metadata.type === "auth") {
2167
+ return metadata.type;
2168
+ }
2169
+ } catch {
2170
+ }
2171
+ return null;
2172
+ }
2173
+ function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
2155
2174
  const result = {};
2175
+ if (module.default instanceof zod.z.ZodObject) {
2176
+ result.schema = module.default;
2177
+ }
2156
2178
  for (const [key, value] of Object.entries(module)) {
2179
+ if (key === "default") continue;
2157
2180
  if (value instanceof zod.z.ZodObject) {
2158
2181
  if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
2159
2182
  result.inputSchema = value;
2160
- } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
2161
- result.schema = value;
2183
+ } else if (!result.schema) {
2184
+ if (patterns.includes("Collection") && key.endsWith("Collection")) {
2185
+ result.schema = value;
2186
+ } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
2187
+ result.schema = value;
2188
+ }
2162
2189
  }
2163
2190
  }
2164
2191
  }
@@ -2247,8 +2274,13 @@ function extractIndexes(schema) {
2247
2274
  }
2248
2275
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
2249
2276
  const rawFields = extractFieldDefinitions(zodSchema);
2250
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
2251
- const fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
2277
+ const explicitType = extractCollectionTypeFromSchema(zodSchema);
2278
+ const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
2279
+ let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
2280
+ if (collectionType === "auth") {
2281
+ const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
2282
+ fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
2283
+ }
2252
2284
  const indexes = extractIndexes(zodSchema) || [];
2253
2285
  const permissionAnalyzer = new PermissionAnalyzer();
2254
2286
  let permissions = void 0;
@@ -3232,9 +3264,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
3232
3264
  if (!previousSnapshot) {
3233
3265
  return matches;
3234
3266
  }
3267
+ const previousCollectionsLower = /* @__PURE__ */ new Map();
3268
+ for (const [name, collection] of previousSnapshot.collections) {
3269
+ previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
3270
+ }
3235
3271
  for (const [collectionName, currentCollection] of currentSchema.collections) {
3236
- const previousCollection = previousSnapshot.collections.get(collectionName);
3237
- if (previousCollection) {
3272
+ const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
3273
+ if (previousEntry) {
3274
+ const [, previousCollection] = previousEntry;
3238
3275
  matches.push([currentCollection, previousCollection]);
3239
3276
  }
3240
3277
  }
@@ -3972,6 +4009,55 @@ function generateIndexesArray(indexes) {
3972
4009
  ${indexStrings.join(",\n ")},
3973
4010
  ]`;
3974
4011
  }
4012
+ function getSystemFields() {
4013
+ return [
4014
+ // id field - primary key, auto-generated
4015
+ {
4016
+ name: "id",
4017
+ type: "text",
4018
+ required: true,
4019
+ options: {
4020
+ autogeneratePattern: "[a-z0-9]{15}",
4021
+ hidden: false,
4022
+ id: "text3208210256",
4023
+ max: 15,
4024
+ min: 15,
4025
+ pattern: "^[a-z0-9]+$",
4026
+ presentable: false,
4027
+ primaryKey: true,
4028
+ system: true
4029
+ }
4030
+ },
4031
+ // created field - autodate, set on creation
4032
+ {
4033
+ name: "created",
4034
+ type: "autodate",
4035
+ required: true,
4036
+ options: {
4037
+ hidden: false,
4038
+ id: "autodate2990389176",
4039
+ onCreate: true,
4040
+ onUpdate: false,
4041
+ presentable: false,
4042
+ system: false
4043
+ }
4044
+ },
4045
+ // updated field - autodate, set on creation and update
4046
+ {
4047
+ name: "updated",
4048
+ type: "autodate",
4049
+ required: true,
4050
+ options: {
4051
+ hidden: false,
4052
+ id: "autodate3332085495",
4053
+ onCreate: true,
4054
+ onUpdate: true,
4055
+ presentable: false,
4056
+ system: false
4057
+ }
4058
+ }
4059
+ ];
4060
+ }
3975
4061
  function generateCollectionCreation(collection, varName = "collection", isLast = false) {
3976
4062
  const lines = [];
3977
4063
  lines.push(` const ${varName} = new Collection({`);
@@ -3984,7 +4070,9 @@ function generateCollectionCreation(collection, varName = "collection", isLast =
3984
4070
  } else if (rulesCode) {
3985
4071
  lines.push(` ${rulesCode},`);
3986
4072
  }
3987
- lines.push(` fields: ${generateFieldsArray(collection.fields)},`);
4073
+ const systemFields = getSystemFields();
4074
+ const allFields = [...systemFields, ...collection.fields];
4075
+ lines.push(` fields: ${generateFieldsArray(allFields)},`);
3988
4076
  lines.push(` indexes: ${generateIndexesArray(collection.indexes)},`);
3989
4077
  lines.push(` });`);
3990
4078
  lines.push(``);