pocketbase-to-zod 1.0.4 → 1.1.0

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.
package/README.md CHANGED
@@ -20,7 +20,7 @@ npx pocketbase-to-zod [options]
20
20
  ## Usage
21
21
 
22
22
  ```bash
23
- pocketbase-to-zod --url http://localhost:8090 --email admin@example.com --password yourpassword --output ./schemas.ts
23
+ pocketbase-to-zod --url http://localhost:8090 --email admin@example.com --password yourpassword --output ./schemas.ts --split
24
24
  ```
25
25
 
26
26
  ## Options
@@ -28,6 +28,7 @@ pocketbase-to-zod --url http://localhost:8090 --email admin@example.com --passwo
28
28
  - `--email <email>`: Admin email for authentication [Required]
29
29
  - `--password <password>`: Admin password for authentication [Required]
30
30
  - `--output <file>`: Output file path for generated schemas (default: `./pocketbase-schemas.ts`) [Optional]
31
+ - `--split`: Generate a separate file for each collection [Optional]
31
32
 
32
33
  ## Example Output
33
34
 
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ program
16
16
  .requiredOption('-e, --email <char>', 'Admin email')
17
17
  .requiredOption('-p, --password <char>', 'Admin password')
18
18
  .option('-o, --output <char>', 'Output file path', './pocketbase-schema.ts')
19
+ .option('-s, --split', 'Generate a file per collection', false)
19
20
  .action(async (options) => {
20
21
  const pb = new pocketbase_1.default(options.url);
21
22
  try {
@@ -46,9 +47,11 @@ program
46
47
  case 'text':
47
48
  case 'editor':
48
49
  case 'url':
49
- case 'email':
50
50
  zodType = 'z.string()';
51
51
  break;
52
+ case 'email':
53
+ zodType = 'z.email()';
54
+ break;
52
55
  case 'number':
53
56
  zodType = 'z.number()';
54
57
  break;
@@ -56,7 +59,7 @@ program
56
59
  zodType = 'z.boolean()';
57
60
  break;
58
61
  case 'date':
59
- zodType = 'z.string().datetime()'; // PocketBase sends dates as ISO strings
62
+ zodType = 'z.date()'; // PocketBase sends dates as ISO strings
60
63
  break;
61
64
  case 'select':
62
65
  if (field.options?.values && Array.isArray(field.options.values) && field.options.values.length > 0) {
@@ -68,7 +71,7 @@ program
68
71
  zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
69
72
  break;
70
73
  case 'file':
71
- zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
74
+ zodType = field.options?.maxSelect === 1 ? 'z.url()' : 'z.array(z.url())';
72
75
  break;
73
76
  case 'json':
74
77
  zodType = 'z.unknown()';
@@ -81,10 +84,22 @@ program
81
84
  }
82
85
  fileContent += `});\n\n`;
83
86
  fileContent += `export type ${col.name.charAt(0).toUpperCase() + col.name.slice(1)} = z.infer<typeof ${col.name}Schema>;\n\n`;
87
+ if (options.split) {
88
+ const outputDir = path_1.default.resolve(process.cwd(), path_1.default.dirname(options.output));
89
+ if (!fs_1.default.existsSync(outputDir)) {
90
+ fs_1.default.mkdirSync(outputDir, { recursive: true });
91
+ }
92
+ const outputPath = path_1.default.resolve(outputDir, `${col.name}-schema.ts`);
93
+ fs_1.default.writeFileSync(outputPath, fileContent);
94
+ console.log(`✅ Schema for collection '${col.name}' generated at: ${outputPath}`);
95
+ fileContent = `import { z } from 'zod';\n\n`; // Reset for next file
96
+ }
97
+ }
98
+ if (!options.split) {
99
+ const outputPath = path_1.default.resolve(process.cwd(), options.output);
100
+ fs_1.default.writeFileSync(outputPath, fileContent);
101
+ console.log(`✅ Schemas generated successfully at: ${outputPath}`);
84
102
  }
85
- const outputPath = path_1.default.resolve(process.cwd(), options.output);
86
- fs_1.default.writeFileSync(outputPath, fileContent);
87
- console.log(`✅ Schemas generated successfully at: ${outputPath}`);
88
103
  }
89
104
  catch (error) {
90
105
  console.error('❌ Error:', error.message);
package/index.ts CHANGED
@@ -14,6 +14,7 @@ program
14
14
  .requiredOption('-e, --email <char>', 'Admin email')
15
15
  .requiredOption('-p, --password <char>', 'Admin password')
16
16
  .option('-o, --output <char>', 'Output file path', './pocketbase-schema.ts')
17
+ .option('-s, --split', 'Generate a file per collection', false)
17
18
  .action(async (options) => {
18
19
  const pb = new PocketBase(options.url);
19
20
 
@@ -54,9 +55,11 @@ program
54
55
  case 'text':
55
56
  case 'editor':
56
57
  case 'url':
58
+ zodType = 'z.string()';
59
+ break;
57
60
  case 'email':
58
- zodType = 'z.string()';
59
- break;
61
+ zodType = 'z.email()';
62
+ break;
60
63
  case 'number':
61
64
  zodType = 'z.number()';
62
65
  break;
@@ -64,7 +67,7 @@ program
64
67
  zodType = 'z.boolean()';
65
68
  break;
66
69
  case 'date':
67
- zodType = 'z.string().datetime()'; // PocketBase sends dates as ISO strings
70
+ zodType = 'z.date()'; // PocketBase sends dates as ISO strings
68
71
  break;
69
72
  case 'select':
70
73
  if (field.options?.values && Array.isArray(field.options.values) && field.options.values.length > 0) {
@@ -76,7 +79,7 @@ program
76
79
  zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
77
80
  break;
78
81
  case 'file':
79
- zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
82
+ zodType = field.options?.maxSelect === 1 ? 'z.url()' : 'z.array(z.url())';
80
83
  break;
81
84
  case 'json':
82
85
  zodType = 'z.unknown()';
@@ -92,11 +95,24 @@ program
92
95
 
93
96
  fileContent += `});\n\n`;
94
97
  fileContent += `export type ${col.name.charAt(0).toUpperCase() + col.name.slice(1)} = z.infer<typeof ${col.name}Schema>;\n\n`;
98
+
99
+ if (options.split) {
100
+ const outputDir = path.resolve(process.cwd(), path.dirname(options.output));
101
+ if (!fs.existsSync(outputDir)) {
102
+ fs.mkdirSync(outputDir, { recursive: true });
103
+ }
104
+ const outputPath = path.resolve(outputDir, `${col.name}-schema.ts`);
105
+ fs.writeFileSync(outputPath, fileContent);
106
+ console.log(`✅ Schema for collection '${col.name}' generated at: ${outputPath}`);
107
+ fileContent = `import { z } from 'zod';\n\n`; // Reset for next file
108
+ }
95
109
  }
96
110
 
97
- const outputPath = path.resolve(process.cwd(), options.output);
98
- fs.writeFileSync(outputPath, fileContent);
99
- console.log(`✅ Schemas generated successfully at: ${outputPath}`);
111
+ if (!options.split) {
112
+ const outputPath = path.resolve(process.cwd(), options.output);
113
+ fs.writeFileSync(outputPath, fileContent);
114
+ console.log(`✅ Schemas generated successfully at: ${outputPath}`);
115
+ }
100
116
 
101
117
  } catch (error: any) {
102
118
  console.error('❌ Error:', error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pocketbase-to-zod",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "This project is an utility to generate Zod schemas from Pocketbase instance ",
5
5
  "main": "index.js",
6
6
  "bin": {