pocketbase-zod-schema 0.2.0 → 0.2.2

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 (46) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cli/index.cjs +44 -14
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.js +44 -14
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/cli/migrate.cjs +44 -14
  7. package/dist/cli/migrate.cjs.map +1 -1
  8. package/dist/cli/migrate.js +44 -14
  9. package/dist/cli/migrate.js.map +1 -1
  10. package/dist/index.cjs +50 -16
  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 +50 -16
  15. package/dist/index.js.map +1 -1
  16. package/dist/migration/analyzer.cjs +38 -12
  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 +38 -13
  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/index.cjs +44 -14
  27. package/dist/migration/index.cjs.map +1 -1
  28. package/dist/migration/index.js +44 -14
  29. package/dist/migration/index.js.map +1 -1
  30. package/dist/mutator.cjs +9 -3
  31. package/dist/mutator.cjs.map +1 -1
  32. package/dist/mutator.d.cts +3 -1
  33. package/dist/mutator.d.ts +3 -1
  34. package/dist/mutator.js +9 -3
  35. package/dist/mutator.js.map +1 -1
  36. package/dist/schema.cjs +9 -3
  37. package/dist/schema.cjs.map +1 -1
  38. package/dist/schema.d.cts +32 -3
  39. package/dist/schema.d.ts +32 -3
  40. package/dist/schema.js +9 -3
  41. package/dist/schema.js.map +1 -1
  42. package/dist/types.d.cts +1 -1
  43. package/dist/types.d.ts +1 -1
  44. package/dist/{user-DTJQIj4K.d.cts → user-BnFWg5tw.d.cts} +13 -1
  45. package/dist/{user-DTJQIj4K.d.ts → user-BnFWg5tw.d.ts} +13 -1
  46. 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
  }
@@ -2225,11 +2252,8 @@ function buildFieldDefinition(fieldName, zodType) {
2225
2252
  // Default to false, can be configured later
2226
2253
  };
2227
2254
  if (fieldDef.options) {
2228
- const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
2229
- console.log("min", min);
2230
- console.log("max", max);
2231
- console.log("pattern", pattern);
2232
- fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
2255
+ const { min: _min, max: _max, pattern: _pattern, ...relationSafeOptions } = fieldDef.options;
2256
+ fieldDef.options = Object.keys(relationSafeOptions).length ? relationSafeOptions : void 0;
2233
2257
  }
2234
2258
  }
2235
2259
  return fieldDef;
@@ -2250,8 +2274,13 @@ function extractIndexes(schema) {
2250
2274
  }
2251
2275
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
2252
2276
  const rawFields = extractFieldDefinitions(zodSchema);
2253
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
2254
- 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
+ }
2255
2284
  const indexes = extractIndexes(zodSchema) || [];
2256
2285
  const permissionAnalyzer = new PermissionAnalyzer();
2257
2286
  let permissions = void 0;
@@ -3235,9 +3264,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
3235
3264
  if (!previousSnapshot) {
3236
3265
  return matches;
3237
3266
  }
3267
+ const previousCollectionsLower = /* @__PURE__ */ new Map();
3268
+ for (const [name, collection] of previousSnapshot.collections) {
3269
+ previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
3270
+ }
3238
3271
  for (const [collectionName, currentCollection] of currentSchema.collections) {
3239
- const previousCollection = previousSnapshot.collections.get(collectionName);
3240
- if (previousCollection) {
3272
+ const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
3273
+ if (previousEntry) {
3274
+ const [, previousCollection] = previousEntry;
3241
3275
  matches.push([currentCollection, previousCollection]);
3242
3276
  }
3243
3277
  }