pocketbase-zod-schema 0.1.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 (94) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -0
  3. package/README.md +167 -0
  4. package/dist/cli/index.cjs +3383 -0
  5. package/dist/cli/index.cjs.map +1 -0
  6. package/dist/cli/index.d.cts +30 -0
  7. package/dist/cli/index.d.ts +30 -0
  8. package/dist/cli/index.js +3331 -0
  9. package/dist/cli/index.js.map +1 -0
  10. package/dist/cli/migrate.cjs +3380 -0
  11. package/dist/cli/migrate.cjs.map +1 -0
  12. package/dist/cli/migrate.d.cts +1 -0
  13. package/dist/cli/migrate.d.ts +1 -0
  14. package/dist/cli/migrate.js +3353 -0
  15. package/dist/cli/migrate.js.map +1 -0
  16. package/dist/cli/utils/index.cjs +540 -0
  17. package/dist/cli/utils/index.cjs.map +1 -0
  18. package/dist/cli/utils/index.d.cts +232 -0
  19. package/dist/cli/utils/index.d.ts +232 -0
  20. package/dist/cli/utils/index.js +487 -0
  21. package/dist/cli/utils/index.js.map +1 -0
  22. package/dist/enums.cjs +19 -0
  23. package/dist/enums.cjs.map +1 -0
  24. package/dist/enums.d.cts +6 -0
  25. package/dist/enums.d.ts +6 -0
  26. package/dist/enums.js +17 -0
  27. package/dist/enums.js.map +1 -0
  28. package/dist/index.cjs +4900 -0
  29. package/dist/index.cjs.map +1 -0
  30. package/dist/index.d.cts +18 -0
  31. package/dist/index.d.ts +18 -0
  32. package/dist/index.js +4726 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/migration/analyzer.cjs +1267 -0
  35. package/dist/migration/analyzer.cjs.map +1 -0
  36. package/dist/migration/analyzer.d.cts +186 -0
  37. package/dist/migration/analyzer.d.ts +186 -0
  38. package/dist/migration/analyzer.js +1232 -0
  39. package/dist/migration/analyzer.js.map +1 -0
  40. package/dist/migration/diff.cjs +557 -0
  41. package/dist/migration/diff.cjs.map +1 -0
  42. package/dist/migration/diff.d.cts +291 -0
  43. package/dist/migration/diff.d.ts +291 -0
  44. package/dist/migration/diff.js +534 -0
  45. package/dist/migration/diff.js.map +1 -0
  46. package/dist/migration/generator.cjs +778 -0
  47. package/dist/migration/generator.cjs.map +1 -0
  48. package/dist/migration/generator.d.cts +225 -0
  49. package/dist/migration/generator.d.ts +225 -0
  50. package/dist/migration/generator.js +737 -0
  51. package/dist/migration/generator.js.map +1 -0
  52. package/dist/migration/index.cjs +3390 -0
  53. package/dist/migration/index.cjs.map +1 -0
  54. package/dist/migration/index.d.cts +103 -0
  55. package/dist/migration/index.d.ts +103 -0
  56. package/dist/migration/index.js +3265 -0
  57. package/dist/migration/index.js.map +1 -0
  58. package/dist/migration/snapshot.cjs +609 -0
  59. package/dist/migration/snapshot.cjs.map +1 -0
  60. package/dist/migration/snapshot.d.cts +167 -0
  61. package/dist/migration/snapshot.d.ts +167 -0
  62. package/dist/migration/snapshot.js +575 -0
  63. package/dist/migration/snapshot.js.map +1 -0
  64. package/dist/migration/utils/index.cjs +672 -0
  65. package/dist/migration/utils/index.cjs.map +1 -0
  66. package/dist/migration/utils/index.d.cts +207 -0
  67. package/dist/migration/utils/index.d.ts +207 -0
  68. package/dist/migration/utils/index.js +641 -0
  69. package/dist/migration/utils/index.js.map +1 -0
  70. package/dist/mutator.cjs +427 -0
  71. package/dist/mutator.cjs.map +1 -0
  72. package/dist/mutator.d.cts +190 -0
  73. package/dist/mutator.d.ts +190 -0
  74. package/dist/mutator.js +425 -0
  75. package/dist/mutator.js.map +1 -0
  76. package/dist/permissions-ZHafVSIx.d.cts +71 -0
  77. package/dist/permissions-ZHafVSIx.d.ts +71 -0
  78. package/dist/schema.cjs +430 -0
  79. package/dist/schema.cjs.map +1 -0
  80. package/dist/schema.d.cts +316 -0
  81. package/dist/schema.d.ts +316 -0
  82. package/dist/schema.js +396 -0
  83. package/dist/schema.js.map +1 -0
  84. package/dist/types-BbTgmg6H.d.cts +91 -0
  85. package/dist/types-z1Dkjg8m.d.ts +91 -0
  86. package/dist/types.cjs +4 -0
  87. package/dist/types.cjs.map +1 -0
  88. package/dist/types.d.cts +14 -0
  89. package/dist/types.d.ts +14 -0
  90. package/dist/types.js +3 -0
  91. package/dist/types.js.map +1 -0
  92. package/dist/user-jS1aYoeD.d.cts +123 -0
  93. package/dist/user-jS1aYoeD.d.ts +123 -0
  94. package/package.json +165 -0
@@ -0,0 +1,186 @@
1
+ import { z } from 'zod';
2
+ import { S as SchemaDefinition, C as CollectionSchema, F as FieldDefinition } from '../types-z1Dkjg8m.js';
3
+ import '../permissions-ZHafVSIx.js';
4
+
5
+ /**
6
+ * Schema Analyzer - Parses Zod schemas and extracts field definitions
7
+ *
8
+ * This module provides a standalone, configurable schema analyzer that can be used
9
+ * by consumer projects to parse Zod schemas and convert them to PocketBase collection schemas.
10
+ */
11
+
12
+ /**
13
+ * Configuration options for schema discovery and parsing
14
+ */
15
+ interface SchemaAnalyzerConfig {
16
+ /**
17
+ * Directory containing schema files (source or compiled)
18
+ * Can be absolute or relative to workspaceRoot
19
+ */
20
+ schemaDir: string;
21
+ /**
22
+ * Workspace root directory for resolving relative paths
23
+ * Defaults to process.cwd()
24
+ */
25
+ workspaceRoot?: string;
26
+ /**
27
+ * File patterns to exclude from schema discovery
28
+ * Defaults to ['base.ts', 'index.ts', 'permissions.ts', 'permission-templates.ts']
29
+ */
30
+ excludePatterns?: string[];
31
+ /**
32
+ * File extensions to include in schema discovery
33
+ * Defaults to ['.ts', '.js']
34
+ */
35
+ includeExtensions?: string[];
36
+ /**
37
+ * Schema export name patterns to look for
38
+ * Defaults to ['Schema', 'InputSchema']
39
+ */
40
+ schemaPatterns?: string[];
41
+ /**
42
+ * Whether to use compiled JavaScript files instead of TypeScript source
43
+ * When true, looks for .js files; when false, looks for .ts files
44
+ * Defaults to true (use compiled files for dynamic import)
45
+ */
46
+ useCompiledFiles?: boolean;
47
+ /**
48
+ * Custom path transformation function for converting source paths to import paths
49
+ * Useful for monorepo setups where source and dist directories differ
50
+ * If not provided, uses the schemaDir directly
51
+ */
52
+ pathTransformer?: (sourcePath: string) => string;
53
+ }
54
+ /**
55
+ * Discovers schema files in the specified directory
56
+ * Filters based on configuration patterns
57
+ *
58
+ * @param config - Schema analyzer configuration
59
+ * @returns Array of schema file paths (without extension)
60
+ */
61
+ declare function discoverSchemaFiles(config: SchemaAnalyzerConfig | string): string[];
62
+ /**
63
+ * Dynamically imports a schema module
64
+ *
65
+ * @param filePath - Path to the schema file (without extension)
66
+ * @param config - Optional configuration for path transformation
67
+ * @returns The imported module
68
+ */
69
+ declare function importSchemaModule(filePath: string, config?: SchemaAnalyzerConfig): Promise<any>;
70
+ /**
71
+ * Gets the collection name from a schema file path
72
+ * Converts the filename (without extension) to a pluralized collection name
73
+ *
74
+ * @param filePath - Path to the schema file (with or without extension)
75
+ * @returns The collection name
76
+ */
77
+ declare function getCollectionNameFromFile(filePath: string): string;
78
+ /**
79
+ * Extracts Zod schema definitions from a module
80
+ * Looks for schemas ending with configured patterns (default: "Schema" or "InputSchema")
81
+ *
82
+ * @param module - The imported schema module
83
+ * @param patterns - Schema name patterns to look for (default: ['Schema', 'InputSchema'])
84
+ * @returns Object containing found schemas
85
+ */
86
+ declare function extractSchemaDefinitions(module: any, patterns?: string[]): {
87
+ inputSchema?: z.ZodObject<any>;
88
+ schema?: z.ZodObject<any>;
89
+ };
90
+ /**
91
+ * Identifies whether to use InputSchema or Schema pattern
92
+ * Prefers Schema over InputSchema as it includes base fields
93
+ *
94
+ * @param schemas - Object containing inputSchema and/or schema
95
+ * @returns The schema to use for collection definition
96
+ */
97
+ declare function selectSchemaForCollection(schemas: {
98
+ inputSchema?: z.ZodObject<any>;
99
+ schema?: z.ZodObject<any>;
100
+ }): z.ZodObject<any> | null;
101
+ /**
102
+ * Extracts field definitions from a Zod object schema
103
+ * Filters out base schema fields (id, collectionId, created, updated, expand)
104
+ *
105
+ * @param zodSchema - The Zod object schema
106
+ * @param excludeFields - Additional fields to exclude (default: base schema fields)
107
+ * @returns Array of field names and their Zod types
108
+ */
109
+ declare function extractFieldDefinitions(zodSchema: z.ZodObject<any>, excludeFields?: string[]): Array<{
110
+ name: string;
111
+ zodType: z.ZodTypeAny;
112
+ }>;
113
+ /**
114
+ * Detects if a collection is an auth collection
115
+ * Auth collections have email and password fields
116
+ *
117
+ * @param fields - Array of field definitions
118
+ * @returns True if the collection is an auth collection
119
+ */
120
+ declare function isAuthCollection(fields: Array<{
121
+ name: string;
122
+ zodType: z.ZodTypeAny;
123
+ }>): boolean;
124
+ /**
125
+ * Extracts validation constraints from Zod type
126
+ * Includes min, max, required, unique, and other options
127
+ *
128
+ * @param fieldName - The field name
129
+ * @param zodType - The Zod type
130
+ * @returns Field definition with constraints
131
+ */
132
+ declare function buildFieldDefinition(fieldName: string, zodType: z.ZodTypeAny): FieldDefinition;
133
+ /**
134
+ * Extracts index definitions from a Zod schema
135
+ * Parses the schema description metadata to find index definitions
136
+ *
137
+ * @param schema - Zod schema with index metadata
138
+ * @returns Array of index SQL statements or undefined
139
+ */
140
+ declare function extractIndexes(schema: z.ZodTypeAny): string[] | undefined;
141
+ /**
142
+ * Converts a Zod schema to a CollectionSchema interface
143
+ *
144
+ * @param collectionName - The name of the collection
145
+ * @param zodSchema - The Zod object schema
146
+ * @returns CollectionSchema definition
147
+ */
148
+ declare function convertZodSchemaToCollectionSchema(collectionName: string, zodSchema: z.ZodObject<any>): CollectionSchema;
149
+ /**
150
+ * Builds a complete SchemaDefinition from schema files
151
+ * Main entry point for the Schema Analyzer
152
+ *
153
+ * @param config - Schema analyzer configuration or path to schema directory
154
+ * @returns Complete SchemaDefinition with all collections
155
+ */
156
+ declare function buildSchemaDefinition(config: SchemaAnalyzerConfig | string): Promise<SchemaDefinition>;
157
+ /**
158
+ * Parses schema files and returns SchemaDefinition
159
+ * Alias for buildSchemaDefinition for better semantic clarity
160
+ *
161
+ * @param config - Schema analyzer configuration or path to schema directory
162
+ * @returns Complete SchemaDefinition with all collections
163
+ */
164
+ declare function parseSchemaFiles(config: SchemaAnalyzerConfig | string): Promise<SchemaDefinition>;
165
+ /**
166
+ * Creates a SchemaAnalyzer instance with the given configuration
167
+ * Provides an object-oriented interface for schema analysis
168
+ */
169
+ declare class SchemaAnalyzer {
170
+ private config;
171
+ constructor(config: SchemaAnalyzerConfig);
172
+ /**
173
+ * Discovers schema files in the configured directory
174
+ */
175
+ discoverSchemaFiles(): string[];
176
+ /**
177
+ * Parses all schema files and returns a SchemaDefinition
178
+ */
179
+ parseSchemaFiles(): Promise<SchemaDefinition>;
180
+ /**
181
+ * Converts a single Zod schema to a CollectionSchema
182
+ */
183
+ convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
184
+ }
185
+
186
+ export { SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection };