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
@@ -85,13 +85,36 @@ declare function getCollectionNameFromFile(filePath: string): string;
85
85
  * @returns The collection name if found in metadata, null otherwise
86
86
  */
87
87
  declare function extractCollectionNameFromSchema(zodSchema: z.ZodTypeAny): string | null;
88
+ /**
89
+ * Extracts the collection type from a Zod schema's metadata
90
+ * Checks if the schema was created with defineCollection() which stores
91
+ * the collection type in the schema description
92
+ *
93
+ * @param zodSchema - The Zod schema to extract collection type from
94
+ * @returns The collection type ("base" | "auth") if found in metadata, null otherwise
95
+ */
96
+ declare function extractCollectionTypeFromSchema(zodSchema: z.ZodTypeAny): "base" | "auth" | null;
88
97
  /**
89
98
  * Extracts Zod schema definitions from a module
90
- * Looks for schemas ending with configured patterns (default: "Schema" or "InputSchema")
99
+ *
100
+ * Detection priority (highest to lowest):
101
+ * 1. Default export - recommended pattern for clarity and explicitness
102
+ * 2. Named exports ending with "Collection" - contains metadata (indexes, permissions)
103
+ * 3. Named exports ending with "Schema" - basic schema definitions
104
+ * 4. Named exports ending with "InputSchema" - form input schemas
91
105
  *
92
106
  * @param module - The imported schema module
93
- * @param patterns - Schema name patterns to look for (default: ['Schema', 'InputSchema'])
107
+ * @param patterns - Schema name patterns to look for (default: ['Schema', 'InputSchema', 'Collection'])
94
108
  * @returns Object containing found schemas
109
+ *
110
+ * @example
111
+ * // Recommended: Use default export
112
+ * const UserCollection = defineCollection({ ... });
113
+ * export default UserCollection;
114
+ *
115
+ * @example
116
+ * // Also supported: Named export with pattern
117
+ * export const UserCollection = defineCollection({ ... });
95
118
  */
96
119
  declare function extractSchemaDefinitions(module: any, patterns?: string[]): {
97
120
  inputSchema?: z.ZodObject<any>;
@@ -193,4 +216,4 @@ declare class SchemaAnalyzer {
193
216
  convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
194
217
  }
195
218
 
196
- export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
219
+ export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
@@ -85,13 +85,36 @@ declare function getCollectionNameFromFile(filePath: string): string;
85
85
  * @returns The collection name if found in metadata, null otherwise
86
86
  */
87
87
  declare function extractCollectionNameFromSchema(zodSchema: z.ZodTypeAny): string | null;
88
+ /**
89
+ * Extracts the collection type from a Zod schema's metadata
90
+ * Checks if the schema was created with defineCollection() which stores
91
+ * the collection type in the schema description
92
+ *
93
+ * @param zodSchema - The Zod schema to extract collection type from
94
+ * @returns The collection type ("base" | "auth") if found in metadata, null otherwise
95
+ */
96
+ declare function extractCollectionTypeFromSchema(zodSchema: z.ZodTypeAny): "base" | "auth" | null;
88
97
  /**
89
98
  * Extracts Zod schema definitions from a module
90
- * Looks for schemas ending with configured patterns (default: "Schema" or "InputSchema")
99
+ *
100
+ * Detection priority (highest to lowest):
101
+ * 1. Default export - recommended pattern for clarity and explicitness
102
+ * 2. Named exports ending with "Collection" - contains metadata (indexes, permissions)
103
+ * 3. Named exports ending with "Schema" - basic schema definitions
104
+ * 4. Named exports ending with "InputSchema" - form input schemas
91
105
  *
92
106
  * @param module - The imported schema module
93
- * @param patterns - Schema name patterns to look for (default: ['Schema', 'InputSchema'])
107
+ * @param patterns - Schema name patterns to look for (default: ['Schema', 'InputSchema', 'Collection'])
94
108
  * @returns Object containing found schemas
109
+ *
110
+ * @example
111
+ * // Recommended: Use default export
112
+ * const UserCollection = defineCollection({ ... });
113
+ * export default UserCollection;
114
+ *
115
+ * @example
116
+ * // Also supported: Named export with pattern
117
+ * export const UserCollection = defineCollection({ ... });
95
118
  */
96
119
  declare function extractSchemaDefinitions(module: any, patterns?: string[]): {
97
120
  inputSchema?: z.ZodObject<any>;
@@ -193,4 +216,4 @@ declare class SchemaAnalyzer {
193
216
  convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
194
217
  }
195
218
 
196
- export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
219
+ export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
@@ -7,7 +7,9 @@ import { z } from 'zod';
7
7
  id: z.string().describe("unique id"),
8
8
  collectionId: z.string().describe("collection id"),
9
9
  collectionName: z.string().describe("collection name"),
10
- expand: z.record(z.any()).describe("expandable fields")
10
+ expand: z.record(z.any()).describe("expandable fields"),
11
+ created: z.string().describe("creation timestamp"),
12
+ updated: z.string().describe("last update timestamp")
11
13
  });
12
14
  ({
13
15
  created: z.string().describe("creation timestamp"),
@@ -980,7 +982,7 @@ var DEFAULT_CONFIG = {
980
982
  "permission-templates.js"
981
983
  ],
982
984
  includeExtensions: [".ts", ".js"],
983
- schemaPatterns: ["Schema", "InputSchema"],
985
+ schemaPatterns: ["Schema", "InputSchema", "Collection"],
984
986
  useCompiledFiles: true
985
987
  };
986
988
  function mergeConfig(config) {
@@ -1117,14 +1119,35 @@ function extractCollectionNameFromSchema(zodSchema) {
1117
1119
  }
1118
1120
  return null;
1119
1121
  }
1120
- function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
1122
+ function extractCollectionTypeFromSchema(zodSchema) {
1123
+ if (!zodSchema.description) {
1124
+ return null;
1125
+ }
1126
+ try {
1127
+ const metadata = JSON.parse(zodSchema.description);
1128
+ if (metadata.type === "base" || metadata.type === "auth") {
1129
+ return metadata.type;
1130
+ }
1131
+ } catch {
1132
+ }
1133
+ return null;
1134
+ }
1135
+ function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
1121
1136
  const result = {};
1137
+ if (module.default instanceof z.ZodObject) {
1138
+ result.schema = module.default;
1139
+ }
1122
1140
  for (const [key, value] of Object.entries(module)) {
1141
+ if (key === "default") continue;
1123
1142
  if (value instanceof z.ZodObject) {
1124
1143
  if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
1125
1144
  result.inputSchema = value;
1126
- } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1127
- result.schema = value;
1145
+ } else if (!result.schema) {
1146
+ if (patterns.includes("Collection") && key.endsWith("Collection")) {
1147
+ result.schema = value;
1148
+ } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
1149
+ result.schema = value;
1150
+ }
1128
1151
  }
1129
1152
  }
1130
1153
  }
@@ -1191,11 +1214,8 @@ function buildFieldDefinition(fieldName, zodType) {
1191
1214
  // Default to false, can be configured later
1192
1215
  };
1193
1216
  if (fieldDef.options) {
1194
- const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
1195
- console.log("min", min);
1196
- console.log("max", max);
1197
- console.log("pattern", pattern);
1198
- fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
1217
+ const { min: _min, max: _max, pattern: _pattern, ...relationSafeOptions } = fieldDef.options;
1218
+ fieldDef.options = Object.keys(relationSafeOptions).length ? relationSafeOptions : void 0;
1199
1219
  }
1200
1220
  }
1201
1221
  return fieldDef;
@@ -1216,8 +1236,13 @@ function extractIndexes(schema) {
1216
1236
  }
1217
1237
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1218
1238
  const rawFields = extractFieldDefinitions(zodSchema);
1219
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
1220
- const fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1239
+ const explicitType = extractCollectionTypeFromSchema(zodSchema);
1240
+ const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
1241
+ let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1242
+ if (collectionType === "auth") {
1243
+ const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
1244
+ fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
1245
+ }
1221
1246
  const indexes = extractIndexes(zodSchema) || [];
1222
1247
  const permissionAnalyzer = new PermissionAnalyzer();
1223
1248
  let permissions = void 0;
@@ -1337,6 +1362,6 @@ var SchemaAnalyzer = class {
1337
1362
  }
1338
1363
  };
1339
1364
 
1340
- export { SchemaAnalyzer, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
1365
+ export { SchemaAnalyzer, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };
1341
1366
  //# sourceMappingURL=analyzer.js.map
1342
1367
  //# sourceMappingURL=analyzer.js.map