-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
151 lines (126 loc) · 4.29 KB
/
Copy pathindex.ts
File metadata and controls
151 lines (126 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import PocketBase from 'pocketbase';
import { resolve } from 'path';
import { file } from 'bun';
interface PocketBaseConfig {
url: string;
username: string;
password: string;
}
interface CollectionField {
name: string;
type: string;
required: boolean;
system?: boolean;
options?: {
maxSelect?: number;
collectionId?: string;
};
}
interface Collection {
id: string;
name: string;
type: string;
system?: boolean;
fields: CollectionField[];
}
const toPascalCase = (str: string): string =>
str.match(/[a-zA-Z0-9]+/g)
?.map(word => word[0].toUpperCase() + word.slice(1))
.join('') || '';
const mapFieldType = (
field: CollectionField,
collectionMap: Map<string, string>
): string => {
// Handle relation fields
if (field.type === 'relation') {
const targetId = field.options?.collectionId;
if (targetId) {
const targetName = collectionMap.get(targetId);
if (targetName) {
const typeName = toPascalCase(targetName);
const isArray = (field.options?.maxSelect || 1) > 1;
return `${typeName}${isArray ? '[]' : ''}`;
}
}
return 'string'; // fallback
}
const typeMap: Record<string, string> = {
text: 'string',
number: 'number',
bool: 'boolean',
email: 'string',
url: 'string',
date: 'string',
autodate: 'string',
select: (field.options?.maxSelect || 1) > 1 ? 'string[]' : 'string',
file: (field.options?.maxSelect || 1) > 1 ? 'string[]' : 'string',
password: 'string',
json: 'any',
};
const baseType = typeMap[field.type] || 'any';
return field.required ? baseType : `${baseType} | null`;
};
async function main() {
const configPath = resolve(process.cwd(), '.pocketbase.config.ts');
const config: PocketBaseConfig = await import(configPath).then(m => m.default || m);
console.log('config', config);
const pb = new PocketBase(config.url);
const auth = await pb.collection('_superusers').authWithPassword(config.username, config.password);
console.log(auth);
const collections = (await pb.collections.getFullList() as Collection[])
.filter(c => c.type !== 'view'); // exclude views
// Create ID to collection name mapping for relations
const collectionMap = new Map<string, string>();
collections.forEach(c => collectionMap.set(c.id, c.name));
let output = `// Auto-generated by pocketbase-typegen\n`;
output += `// Generated on ${new Date().toISOString()}\n\n`;
output += `export interface Base {\n id: string;\n created: string;\n updated: string;\n}\n\n`;
for (const collection of collections) {
console.log('Processing collection:', collection.name);
if (!collection.fields) {
console.warn(`Skipping collection "${collection.name}" - no fields found`);
continue;
}
const interfaceName = toPascalCase(collection.name);
// Generate the main interface
output += `export interface ${interfaceName} extends Base {\n`;
// Track relation fields for Expand interface
const relationFields: { name: string; type: string }[] = [];
for (const field of collection.fields) {
// Skip base fields
if (['id', 'created', 'updated'].includes(field.name)) continue;
const fieldType = mapFieldType(field, collectionMap);
output += ` ${field.name}: ${fieldType};\n`;
// Track relation fields
if (field.type === 'relation') {
const targetId = field.options?.collectionId;
if (targetId) {
const targetName = collectionMap.get(targetId);
if (targetName) {
const typeName = toPascalCase(targetName);
const isArray = (field.options?.maxSelect || 1) > 1;
relationFields.push({
name: field.name,
type: `${typeName}${isArray ? '[]' : ''}`
});
}
}
}
}
output += `}\n\n`;
// Generate Expand interface if there are relations
if (relationFields.length > 0) {
output += `export interface ${interfaceName}Expand {\n`;
relationFields.forEach(({ name, type }) => {
output += ` ${name}?: ${type};\n`;
});
output += `}\n\n`;
}
}
await Bun.write('pb.types.ts', output);
console.log('Types generated successfully!');
}
main().catch(err => {
console.error('Generation failed:', err);
process.exit(1);
});