pocketbase-to-zod 1.0.2 → 1.0.4
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/dist/index.js +14 -6
- package/index.ts +16 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -24,16 +24,24 @@ program
|
|
|
24
24
|
console.log('--- Fetching collections ---');
|
|
25
25
|
const collections = await pb.collections.getFullList();
|
|
26
26
|
let fileContent = `import { z } from 'zod';\n\n`;
|
|
27
|
-
|
|
27
|
+
const filteredCollections = collections.filter((col) => (col.type === 'base' || col.type === 'auth') && !col.system);
|
|
28
|
+
console.log(`--- Generating schemas for ${filteredCollections.length} collections ---`);
|
|
29
|
+
for (const col of filteredCollections) {
|
|
28
30
|
console.log(`--- Generating schema for collection: ${col.name} ---`);
|
|
29
31
|
fileContent += `// Schema for collection: ${col.name}\n`;
|
|
30
32
|
fileContent += `export const ${col.name}Schema = z.object({\n`;
|
|
31
33
|
// Base PocketBase fields
|
|
32
34
|
fileContent += ` id: z.string(),\n`;
|
|
33
|
-
fileContent += ` created: z.
|
|
34
|
-
fileContent += ` updated: z.
|
|
35
|
+
fileContent += ` created: z.date(),\n`;
|
|
36
|
+
fileContent += ` updated: z.date(),\n`;
|
|
35
37
|
for (const field of col.fields) {
|
|
36
38
|
let zodType = 'z.any()';
|
|
39
|
+
if (field.hidden)
|
|
40
|
+
continue;
|
|
41
|
+
if (['id', 'created', 'updated'].includes(field.name)) {
|
|
42
|
+
continue; // Skip specific fields if needed
|
|
43
|
+
}
|
|
44
|
+
console.log(` - Processing field: ${field.name} (type: ${field.type})`);
|
|
37
45
|
switch (field.type) {
|
|
38
46
|
case 'text':
|
|
39
47
|
case 'editor':
|
|
@@ -51,16 +59,16 @@ program
|
|
|
51
59
|
zodType = 'z.string().datetime()'; // PocketBase sends dates as ISO strings
|
|
52
60
|
break;
|
|
53
61
|
case 'select':
|
|
54
|
-
if (field.options.values) {
|
|
62
|
+
if (field.options?.values && Array.isArray(field.options.values) && field.options.values.length > 0) {
|
|
55
63
|
const values = field.options.values.map((v) => `'${v}'`).join(', ');
|
|
56
64
|
zodType = `z.enum([${values}])`;
|
|
57
65
|
}
|
|
58
66
|
break;
|
|
59
67
|
case 'relation':
|
|
60
|
-
zodType = field.options
|
|
68
|
+
zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
|
|
61
69
|
break;
|
|
62
70
|
case 'file':
|
|
63
|
-
zodType = field.options
|
|
71
|
+
zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
|
|
64
72
|
break;
|
|
65
73
|
case 'json':
|
|
66
74
|
zodType = 'z.unknown()';
|
package/index.ts
CHANGED
|
@@ -25,7 +25,9 @@ program
|
|
|
25
25
|
const collections = await pb.collections.getFullList();
|
|
26
26
|
|
|
27
27
|
let fileContent = `import { z } from 'zod';\n\n`;
|
|
28
|
-
const filteredCollections = collections.filter(col => !col.system);
|
|
28
|
+
const filteredCollections = collections.filter((col) => (col.type === 'base' || col.type === 'auth') && !col.system);
|
|
29
|
+
|
|
30
|
+
console.log(`--- Generating schemas for ${filteredCollections.length} collections ---`);
|
|
29
31
|
|
|
30
32
|
for (const col of filteredCollections) {
|
|
31
33
|
console.log(`--- Generating schema for collection: ${col.name} ---`);
|
|
@@ -34,12 +36,20 @@ program
|
|
|
34
36
|
|
|
35
37
|
// Base PocketBase fields
|
|
36
38
|
fileContent += ` id: z.string(),\n`;
|
|
37
|
-
fileContent += ` created: z.
|
|
38
|
-
fileContent += ` updated: z.
|
|
39
|
+
fileContent += ` created: z.date(),\n`;
|
|
40
|
+
fileContent += ` updated: z.date(),\n`;
|
|
39
41
|
|
|
40
42
|
for (const field of col.fields) {
|
|
41
43
|
let zodType = 'z.any()';
|
|
42
44
|
|
|
45
|
+
if (field.hidden) continue;
|
|
46
|
+
|
|
47
|
+
if (['id', 'created', 'updated'].includes(field.name)) {
|
|
48
|
+
continue; // Skip specific fields if needed
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
console.log(` - Processing field: ${field.name} (type: ${field.type})`);
|
|
52
|
+
|
|
43
53
|
switch (field.type) {
|
|
44
54
|
case 'text':
|
|
45
55
|
case 'editor':
|
|
@@ -57,16 +67,16 @@ program
|
|
|
57
67
|
zodType = 'z.string().datetime()'; // PocketBase sends dates as ISO strings
|
|
58
68
|
break;
|
|
59
69
|
case 'select':
|
|
60
|
-
if (field.options.values) {
|
|
70
|
+
if (field.options?.values && Array.isArray(field.options.values) && field.options.values.length > 0) {
|
|
61
71
|
const values = field.options.values.map((v: string) => `'${v}'`).join(', ');
|
|
62
72
|
zodType = `z.enum([${values}])`;
|
|
63
73
|
}
|
|
64
74
|
break;
|
|
65
75
|
case 'relation':
|
|
66
|
-
zodType = field.options
|
|
76
|
+
zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
|
|
67
77
|
break;
|
|
68
78
|
case 'file':
|
|
69
|
-
zodType = field.options
|
|
79
|
+
zodType = field.options?.maxSelect === 1 ? 'z.string()' : 'z.array(z.string())';
|
|
70
80
|
break;
|
|
71
81
|
case 'json':
|
|
72
82
|
zodType = 'z.unknown()';
|