appwrite-utils-cli 0.0.286 → 0.9.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.
- package/README.md +162 -96
- package/dist/collections/attributes.d.ts +4 -0
- package/dist/collections/attributes.js +224 -0
- package/dist/collections/indexes.d.ts +4 -0
- package/dist/collections/indexes.js +27 -0
- package/dist/collections/methods.d.ts +16 -0
- package/dist/collections/methods.js +216 -0
- package/dist/databases/methods.d.ts +6 -0
- package/dist/databases/methods.js +33 -0
- package/dist/interactiveCLI.d.ts +19 -0
- package/dist/interactiveCLI.js +555 -0
- package/dist/main.js +224 -62
- package/dist/migrations/afterImportActions.js +37 -40
- package/dist/migrations/appwriteToX.d.ts +26 -25
- package/dist/migrations/appwriteToX.js +42 -6
- package/dist/migrations/attributes.js +21 -20
- package/dist/migrations/backup.d.ts +93 -87
- package/dist/migrations/collections.d.ts +6 -0
- package/dist/migrations/collections.js +149 -20
- package/dist/migrations/converters.d.ts +2 -18
- package/dist/migrations/converters.js +13 -2
- package/dist/migrations/dataLoader.d.ts +276 -161
- package/dist/migrations/dataLoader.js +535 -292
- package/dist/migrations/databases.js +8 -2
- package/dist/migrations/helper.d.ts +3 -0
- package/dist/migrations/helper.js +21 -0
- package/dist/migrations/importController.d.ts +5 -2
- package/dist/migrations/importController.js +125 -88
- package/dist/migrations/importDataActions.d.ts +9 -1
- package/dist/migrations/importDataActions.js +15 -3
- package/dist/migrations/indexes.js +3 -2
- package/dist/migrations/logging.js +20 -8
- package/dist/migrations/migrationHelper.d.ts +9 -4
- package/dist/migrations/migrationHelper.js +6 -5
- package/dist/migrations/openapi.d.ts +1 -1
- package/dist/migrations/openapi.js +33 -18
- package/dist/migrations/queue.js +3 -2
- package/dist/migrations/relationships.d.ts +2 -2
- package/dist/migrations/schemaStrings.js +53 -41
- package/dist/migrations/setupDatabase.d.ts +2 -4
- package/dist/migrations/setupDatabase.js +24 -105
- package/dist/migrations/storage.d.ts +3 -1
- package/dist/migrations/storage.js +110 -16
- package/dist/migrations/transfer.d.ts +30 -0
- package/dist/migrations/transfer.js +337 -0
- package/dist/migrations/users.d.ts +2 -1
- package/dist/migrations/users.js +78 -43
- package/dist/schemas/authUser.d.ts +2 -2
- package/dist/storage/methods.d.ts +15 -0
- package/dist/storage/methods.js +207 -0
- package/dist/storage/schemas.d.ts +687 -0
- package/dist/storage/schemas.js +175 -0
- package/dist/utils/getClientFromConfig.d.ts +4 -0
- package/dist/utils/getClientFromConfig.js +16 -0
- package/dist/utils/helperFunctions.d.ts +11 -1
- package/dist/utils/helperFunctions.js +38 -0
- package/dist/utils/retryFailedPromises.d.ts +2 -0
- package/dist/utils/retryFailedPromises.js +21 -0
- package/dist/utils/schemaStrings.d.ts +13 -0
- package/dist/utils/schemaStrings.js +403 -0
- package/dist/utils/setupFiles.js +110 -61
- package/dist/utilsController.d.ts +40 -22
- package/dist/utilsController.js +164 -84
- package/package.json +13 -15
- package/src/collections/attributes.ts +483 -0
- package/src/collections/indexes.ts +53 -0
- package/src/collections/methods.ts +331 -0
- package/src/databases/methods.ts +47 -0
- package/src/init.ts +64 -64
- package/src/interactiveCLI.ts +767 -0
- package/src/main.ts +289 -83
- package/src/migrations/afterImportActions.ts +553 -490
- package/src/migrations/appwriteToX.ts +237 -174
- package/src/migrations/attributes.ts +483 -422
- package/src/migrations/backup.ts +205 -205
- package/src/migrations/collections.ts +545 -300
- package/src/migrations/converters.ts +161 -150
- package/src/migrations/dataLoader.ts +1615 -1304
- package/src/migrations/databases.ts +44 -25
- package/src/migrations/dbHelpers.ts +92 -92
- package/src/migrations/helper.ts +40 -0
- package/src/migrations/importController.ts +448 -384
- package/src/migrations/importDataActions.ts +315 -307
- package/src/migrations/indexes.ts +40 -37
- package/src/migrations/logging.ts +29 -16
- package/src/migrations/migrationHelper.ts +207 -201
- package/src/migrations/openapi.ts +83 -70
- package/src/migrations/queue.ts +118 -119
- package/src/migrations/relationships.ts +324 -324
- package/src/migrations/schemaStrings.ts +472 -460
- package/src/migrations/setupDatabase.ts +118 -219
- package/src/migrations/storage.ts +538 -358
- package/src/migrations/transfer.ts +608 -0
- package/src/migrations/users.ts +362 -285
- package/src/migrations/validationRules.ts +63 -63
- package/src/schemas/authUser.ts +23 -23
- package/src/setup.ts +8 -8
- package/src/storage/methods.ts +371 -0
- package/src/storage/schemas.ts +205 -0
- package/src/types.ts +9 -9
- package/src/utils/getClientFromConfig.ts +17 -0
- package/src/utils/helperFunctions.ts +181 -127
- package/src/utils/index.ts +2 -2
- package/src/utils/loadConfigs.ts +59 -59
- package/src/utils/retryFailedPromises.ts +27 -0
- package/src/utils/schemaStrings.ts +473 -0
- package/src/utils/setupFiles.ts +228 -182
- package/src/utilsController.ts +325 -194
- package/tsconfig.json +37 -37
@@ -1,460 +1,472 @@
|
|
1
|
-
import { toCamelCase, toPascalCase } from "../utils/index.js";
|
2
|
-
import type {
|
3
|
-
AppwriteConfig,
|
4
|
-
Attribute,
|
5
|
-
RelationshipAttribute,
|
6
|
-
} from "appwrite-utils";
|
7
|
-
import { z } from "zod";
|
8
|
-
import fs from "fs";
|
9
|
-
import path from "path";
|
10
|
-
import { dump } from "js-yaml";
|
11
|
-
import { getDatabaseFromConfig } from "./afterImportActions.js";
|
12
|
-
|
13
|
-
interface RelationshipDetail {
|
14
|
-
parentCollection: string;
|
15
|
-
childCollection: string;
|
16
|
-
parentKey: string;
|
17
|
-
childKey: string;
|
18
|
-
isArray: boolean;
|
19
|
-
isChild: boolean;
|
20
|
-
}
|
21
|
-
|
22
|
-
export class SchemaGenerator {
|
23
|
-
private relationshipMap = new Map<string, RelationshipDetail[]>();
|
24
|
-
private config: AppwriteConfig;
|
25
|
-
private appwriteFolderPath: string;
|
26
|
-
|
27
|
-
constructor(config: AppwriteConfig, appwriteFolderPath: string) {
|
28
|
-
this.config = config;
|
29
|
-
this.appwriteFolderPath = appwriteFolderPath;
|
30
|
-
this.extractRelationships();
|
31
|
-
}
|
32
|
-
|
33
|
-
public updateTsSchemas(): void {
|
34
|
-
const collections = this.config.collections;
|
35
|
-
delete this.config.collections;
|
36
|
-
|
37
|
-
const configPath = path.join(this.appwriteFolderPath, "appwriteConfig.ts");
|
38
|
-
const configContent = `import { type AppwriteConfig } from "appwrite-utils";
|
39
|
-
|
40
|
-
const appwriteConfig: AppwriteConfig = {
|
41
|
-
appwriteEndpoint: "${this.config.appwriteEndpoint}",
|
42
|
-
appwriteProject: "${this.config.appwriteProject}",
|
43
|
-
appwriteKey: "${this.config.appwriteKey}",
|
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
|
-
const
|
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
|
-
index.
|
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
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
break;
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
relationshipAttr.
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
);
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
const
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
const
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
let
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
let
|
279
|
-
let
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
endNameTypes += "
|
285
|
-
endNameLazy += ".
|
286
|
-
}
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
schemaString
|
302
|
-
schemaString += `
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
attribute.type === "string"
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
if (finalAttribute.
|
344
|
-
baseSchemaCode += `.
|
345
|
-
}
|
346
|
-
if (
|
347
|
-
baseSchemaCode += ".
|
348
|
-
}
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
}
|
369
|
-
if (
|
370
|
-
baseSchemaCode +=
|
371
|
-
}
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
if (finalAttribute.
|
379
|
-
baseSchemaCode += `.
|
380
|
-
}
|
381
|
-
if (finalAttribute.
|
382
|
-
baseSchemaCode += `.
|
383
|
-
}
|
384
|
-
if (
|
385
|
-
baseSchemaCode +=
|
386
|
-
}
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
if (
|
394
|
-
baseSchemaCode +=
|
395
|
-
}
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
if (
|
403
|
-
baseSchemaCode += ".
|
404
|
-
}
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
if (
|
412
|
-
baseSchemaCode += ".
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
if (
|
421
|
-
baseSchemaCode += ".
|
422
|
-
}
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
if (
|
430
|
-
baseSchemaCode += ".
|
431
|
-
}
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
if (
|
441
|
-
baseSchemaCode += ".
|
442
|
-
}
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
if (attribute.array
|
455
|
-
baseSchemaCode
|
456
|
-
}
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
1
|
+
import { toCamelCase, toPascalCase } from "../utils/index.js";
|
2
|
+
import type {
|
3
|
+
AppwriteConfig,
|
4
|
+
Attribute,
|
5
|
+
RelationshipAttribute,
|
6
|
+
} from "appwrite-utils";
|
7
|
+
import { z } from "zod";
|
8
|
+
import fs from "fs";
|
9
|
+
import path from "path";
|
10
|
+
import { dump } from "js-yaml";
|
11
|
+
import { getDatabaseFromConfig } from "./afterImportActions.js";
|
12
|
+
|
13
|
+
interface RelationshipDetail {
|
14
|
+
parentCollection: string;
|
15
|
+
childCollection: string;
|
16
|
+
parentKey: string;
|
17
|
+
childKey: string;
|
18
|
+
isArray: boolean;
|
19
|
+
isChild: boolean;
|
20
|
+
}
|
21
|
+
|
22
|
+
export class SchemaGenerator {
|
23
|
+
private relationshipMap = new Map<string, RelationshipDetail[]>();
|
24
|
+
private config: AppwriteConfig;
|
25
|
+
private appwriteFolderPath: string;
|
26
|
+
|
27
|
+
constructor(config: AppwriteConfig, appwriteFolderPath: string) {
|
28
|
+
this.config = config;
|
29
|
+
this.appwriteFolderPath = appwriteFolderPath;
|
30
|
+
this.extractRelationships();
|
31
|
+
}
|
32
|
+
|
33
|
+
public updateTsSchemas(): void {
|
34
|
+
const collections = this.config.collections;
|
35
|
+
delete this.config.collections;
|
36
|
+
|
37
|
+
const configPath = path.join(this.appwriteFolderPath, "appwriteConfig.ts");
|
38
|
+
const configContent = `import { type AppwriteConfig } from "appwrite-utils";
|
39
|
+
|
40
|
+
const appwriteConfig: AppwriteConfig = {
|
41
|
+
appwriteEndpoint: "${this.config.appwriteEndpoint}",
|
42
|
+
appwriteProject: "${this.config.appwriteProject}",
|
43
|
+
appwriteKey: "${this.config.appwriteKey}",
|
44
|
+
enableBackups: ${this.config.enableBackups},
|
45
|
+
backupInterval: ${this.config.backupInterval},
|
46
|
+
backupRetention: ${this.config.backupRetention},
|
47
|
+
enableBackupCleanup: ${this.config.enableBackupCleanup},
|
48
|
+
enableMockData: ${this.config.enableMockData},
|
49
|
+
documentBucketId: "${this.config.documentBucketId}",
|
50
|
+
usersCollectionName: "${this.config.usersCollectionName}",
|
51
|
+
databases: ${JSON.stringify(this.config.databases)},
|
52
|
+
buckets: ${JSON.stringify(this.config.buckets)}
|
53
|
+
};
|
54
|
+
|
55
|
+
export default appwriteConfig;
|
56
|
+
`;
|
57
|
+
fs.writeFileSync(configPath, configContent, { encoding: "utf-8" });
|
58
|
+
|
59
|
+
const collectionsFolderPath = path.join(
|
60
|
+
this.appwriteFolderPath,
|
61
|
+
"collections"
|
62
|
+
);
|
63
|
+
if (!fs.existsSync(collectionsFolderPath)) {
|
64
|
+
fs.mkdirSync(collectionsFolderPath, { recursive: true });
|
65
|
+
}
|
66
|
+
|
67
|
+
collections?.forEach((collection) => {
|
68
|
+
const { databaseId, ...collectionWithoutDbId } = collection; // Destructure to exclude databaseId
|
69
|
+
const collectionFilePath = path.join(
|
70
|
+
collectionsFolderPath,
|
71
|
+
`${collection.name}.ts`
|
72
|
+
);
|
73
|
+
const collectionContent = `import { type CollectionCreate } from "appwrite-utils";
|
74
|
+
|
75
|
+
const ${collection.name}Config: Partial<CollectionCreate> = {
|
76
|
+
name: "${collection.name}",
|
77
|
+
$id: "${collection.$id}",
|
78
|
+
enabled: ${collection.enabled},
|
79
|
+
documentSecurity: ${collection.documentSecurity},
|
80
|
+
$permissions: [
|
81
|
+
${collection.$permissions
|
82
|
+
.map(
|
83
|
+
(permission) =>
|
84
|
+
`{ permission: "${permission.permission}", target: "${permission.target}" }`
|
85
|
+
)
|
86
|
+
.join(",\n ")}
|
87
|
+
],
|
88
|
+
attributes: [
|
89
|
+
${collection.attributes
|
90
|
+
.map((attr) => {
|
91
|
+
return `{ ${Object.entries(attr)
|
92
|
+
.map(([key, value]) => {
|
93
|
+
// Check the type of the value and format it accordingly
|
94
|
+
if (typeof value === "string") {
|
95
|
+
// If the value is a string, wrap it in quotes
|
96
|
+
return `${key}: "${value.replace(/"/g, '\\"')}"`; // Escape existing quotes in the string
|
97
|
+
} else if (Array.isArray(value)) {
|
98
|
+
// If the value is an array, join it with commas
|
99
|
+
if (value.length > 0) {
|
100
|
+
return `${key}: [${value
|
101
|
+
.map((item) => `"${item}"`)
|
102
|
+
.join(", ")}]`;
|
103
|
+
} else {
|
104
|
+
return `${key}: []`;
|
105
|
+
}
|
106
|
+
} else {
|
107
|
+
// If the value is not a string (e.g., boolean or number), output it directly
|
108
|
+
return `${key}: ${value}`;
|
109
|
+
}
|
110
|
+
})
|
111
|
+
.join(", ")} }`;
|
112
|
+
})
|
113
|
+
.join(",\n ")}
|
114
|
+
],
|
115
|
+
indexes: [
|
116
|
+
${(
|
117
|
+
collection.indexes?.map((index) => {
|
118
|
+
// Map each attribute to ensure it is properly quoted
|
119
|
+
const formattedAttributes =
|
120
|
+
index.attributes.map((attr) => `"${attr}"`).join(", ") ?? "";
|
121
|
+
return `{ key: "${index.key}", type: "${
|
122
|
+
index.type
|
123
|
+
}", attributes: [${formattedAttributes}], orders: [${
|
124
|
+
index.orders
|
125
|
+
?.filter((order) => order !== null)
|
126
|
+
.map((order) => `"${order}"`)
|
127
|
+
.join(", ") ?? ""
|
128
|
+
}] }`;
|
129
|
+
}) ?? []
|
130
|
+
).join(",\n ")}
|
131
|
+
]
|
132
|
+
};
|
133
|
+
|
134
|
+
export default ${collection.name}Config;
|
135
|
+
`;
|
136
|
+
fs.writeFileSync(collectionFilePath, collectionContent, {
|
137
|
+
encoding: "utf-8",
|
138
|
+
});
|
139
|
+
console.log(`Collection schema written to ${collectionFilePath}`);
|
140
|
+
});
|
141
|
+
}
|
142
|
+
|
143
|
+
private extractRelationships(): void {
|
144
|
+
if (!this.config.collections) {
|
145
|
+
return;
|
146
|
+
}
|
147
|
+
this.config.collections.forEach((collection) => {
|
148
|
+
collection.attributes.forEach((attr) => {
|
149
|
+
if (attr.type === "relationship" && attr.twoWay && attr.twoWayKey) {
|
150
|
+
const relationshipAttr = attr as RelationshipAttribute;
|
151
|
+
let isArrayParent = false;
|
152
|
+
let isArrayChild = false;
|
153
|
+
switch (relationshipAttr.relationType) {
|
154
|
+
case "oneToMany":
|
155
|
+
isArrayParent = true;
|
156
|
+
isArrayChild = false;
|
157
|
+
break;
|
158
|
+
case "manyToMany":
|
159
|
+
isArrayParent = true;
|
160
|
+
isArrayChild = true;
|
161
|
+
break;
|
162
|
+
case "oneToOne":
|
163
|
+
isArrayParent = false;
|
164
|
+
isArrayChild = false;
|
165
|
+
break;
|
166
|
+
case "manyToOne":
|
167
|
+
isArrayParent = false;
|
168
|
+
isArrayChild = true;
|
169
|
+
break;
|
170
|
+
default:
|
171
|
+
break;
|
172
|
+
}
|
173
|
+
this.addRelationship(
|
174
|
+
collection.name,
|
175
|
+
relationshipAttr.relatedCollection,
|
176
|
+
attr.key,
|
177
|
+
relationshipAttr.twoWayKey,
|
178
|
+
isArrayParent,
|
179
|
+
isArrayChild
|
180
|
+
);
|
181
|
+
console.log(
|
182
|
+
`Extracted relationship: ${attr.key}\n\t${collection.name} -> ${relationshipAttr.relatedCollection}, databaseId: ${collection.databaseId}`
|
183
|
+
);
|
184
|
+
}
|
185
|
+
});
|
186
|
+
});
|
187
|
+
}
|
188
|
+
|
189
|
+
private addRelationship(
|
190
|
+
parentCollection: string,
|
191
|
+
childCollection: string,
|
192
|
+
parentKey: string,
|
193
|
+
childKey: string,
|
194
|
+
isArrayParent: boolean,
|
195
|
+
isArrayChild: boolean
|
196
|
+
): void {
|
197
|
+
const relationshipsChild = this.relationshipMap.get(childCollection) || [];
|
198
|
+
const relationshipsParent =
|
199
|
+
this.relationshipMap.get(parentCollection) || [];
|
200
|
+
relationshipsParent.push({
|
201
|
+
parentCollection,
|
202
|
+
childCollection,
|
203
|
+
parentKey,
|
204
|
+
childKey,
|
205
|
+
isArray: isArrayParent,
|
206
|
+
isChild: false,
|
207
|
+
});
|
208
|
+
relationshipsChild.push({
|
209
|
+
parentCollection,
|
210
|
+
childCollection,
|
211
|
+
parentKey,
|
212
|
+
childKey,
|
213
|
+
isArray: isArrayChild,
|
214
|
+
isChild: true,
|
215
|
+
});
|
216
|
+
this.relationshipMap.set(childCollection, relationshipsChild);
|
217
|
+
this.relationshipMap.set(parentCollection, relationshipsParent);
|
218
|
+
}
|
219
|
+
|
220
|
+
public generateSchemas(): void {
|
221
|
+
if (!this.config.collections) {
|
222
|
+
return;
|
223
|
+
}
|
224
|
+
this.config.collections.forEach((collection) => {
|
225
|
+
const schemaString = this.createSchemaString(
|
226
|
+
collection.name,
|
227
|
+
collection.attributes
|
228
|
+
);
|
229
|
+
const camelCaseName = toCamelCase(collection.name);
|
230
|
+
const schemaPath = path.join(
|
231
|
+
this.appwriteFolderPath,
|
232
|
+
"schemas",
|
233
|
+
`${camelCaseName}.ts`
|
234
|
+
);
|
235
|
+
fs.writeFileSync(schemaPath, schemaString, { encoding: "utf-8" });
|
236
|
+
console.log(`Schema written to ${schemaPath}`);
|
237
|
+
});
|
238
|
+
}
|
239
|
+
|
240
|
+
createSchemaString = (name: string, attributes: Attribute[]): string => {
|
241
|
+
const pascalName = toPascalCase(name);
|
242
|
+
let imports = `import { z } from "zod";\n`;
|
243
|
+
const hasDescription = attributes.some((attr) => attr.description);
|
244
|
+
if (hasDescription) {
|
245
|
+
imports += `import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";\n`;
|
246
|
+
imports += `extendZodWithOpenApi(z);\n`;
|
247
|
+
}
|
248
|
+
|
249
|
+
// Use the relationshipMap to find related collections
|
250
|
+
const relationshipDetails = this.relationshipMap.get(name) || [];
|
251
|
+
const relatedCollections = relationshipDetails
|
252
|
+
.filter((detail, index, self) => {
|
253
|
+
const uniqueKey = `${detail.parentCollection}-${detail.childCollection}-${detail.parentKey}-${detail.childKey}`;
|
254
|
+
return (
|
255
|
+
index ===
|
256
|
+
self.findIndex(
|
257
|
+
(obj) =>
|
258
|
+
`${obj.parentCollection}-${obj.childCollection}-${obj.parentKey}-${obj.childKey}` ===
|
259
|
+
uniqueKey
|
260
|
+
)
|
261
|
+
);
|
262
|
+
})
|
263
|
+
.map((detail) => {
|
264
|
+
const relatedCollectionName = detail.isChild
|
265
|
+
? detail.parentCollection
|
266
|
+
: detail.childCollection;
|
267
|
+
const key = detail.isChild ? detail.childKey : detail.parentKey;
|
268
|
+
const isArray = detail.isArray ? "array" : "";
|
269
|
+
return [relatedCollectionName, key, isArray];
|
270
|
+
});
|
271
|
+
|
272
|
+
let relatedTypes = "";
|
273
|
+
let relatedTypesLazy = "";
|
274
|
+
let curNum = 0;
|
275
|
+
let maxNum = relatedCollections.length;
|
276
|
+
relatedCollections.forEach((relatedCollection) => {
|
277
|
+
console.log(relatedCollection);
|
278
|
+
let relatedPascalName = toPascalCase(relatedCollection[0]);
|
279
|
+
let relatedCamelName = toCamelCase(relatedCollection[0]);
|
280
|
+
curNum++;
|
281
|
+
let endNameTypes = relatedPascalName;
|
282
|
+
let endNameLazy = `${relatedPascalName}Schema`;
|
283
|
+
if (relatedCollection[2] === "array") {
|
284
|
+
endNameTypes += "[]";
|
285
|
+
endNameLazy += ".array().default([])";
|
286
|
+
} else if (!(relatedCollection[2] === "array")) {
|
287
|
+
endNameTypes += " | null";
|
288
|
+
endNameLazy += ".nullish()";
|
289
|
+
}
|
290
|
+
imports += `import { ${relatedPascalName}Schema, type ${relatedPascalName} } from "./${relatedCamelName}";\n`;
|
291
|
+
relatedTypes += `${relatedCollection[1]}?: ${endNameTypes};\n`;
|
292
|
+
if (relatedTypes.length > 0 && curNum !== maxNum) {
|
293
|
+
relatedTypes += " ";
|
294
|
+
}
|
295
|
+
relatedTypesLazy += `${relatedCollection[1]}: z.lazy(() => ${endNameLazy}),\n`;
|
296
|
+
if (relatedTypesLazy.length > 0 && curNum !== maxNum) {
|
297
|
+
relatedTypesLazy += " ";
|
298
|
+
}
|
299
|
+
});
|
300
|
+
|
301
|
+
let schemaString = `${imports}\n\n`;
|
302
|
+
schemaString += `export const ${pascalName}SchemaBase = z.object({\n`;
|
303
|
+
schemaString += ` $id: z.string().optional(),\n`;
|
304
|
+
schemaString += ` $createdAt: z.date().or(z.string()).optional(),\n`;
|
305
|
+
schemaString += ` $updatedAt: z.date().or(z.string()).optional(),\n`;
|
306
|
+
for (const attribute of attributes) {
|
307
|
+
if (attribute.type === "relationship") {
|
308
|
+
continue;
|
309
|
+
}
|
310
|
+
schemaString += ` ${attribute.key}: ${this.typeToZod(attribute)},\n`;
|
311
|
+
}
|
312
|
+
schemaString += `});\n\n`;
|
313
|
+
schemaString += `export type ${pascalName}Base = z.infer<typeof ${pascalName}SchemaBase>`;
|
314
|
+
if (relatedTypes.length > 0) {
|
315
|
+
schemaString += ` & {\n ${relatedTypes}};\n\n`;
|
316
|
+
} else {
|
317
|
+
schemaString += `;\n\n`;
|
318
|
+
}
|
319
|
+
schemaString += `export const ${pascalName}Schema: z.ZodType<${pascalName}Base> = ${pascalName}SchemaBase`;
|
320
|
+
if (relatedTypes.length > 0) {
|
321
|
+
schemaString += `.extend({\n ${relatedTypesLazy}});\n\n`;
|
322
|
+
} else {
|
323
|
+
schemaString += `;\n`;
|
324
|
+
}
|
325
|
+
schemaString += `export type ${pascalName} = z.infer<typeof ${pascalName}Schema>;\n\n`;
|
326
|
+
|
327
|
+
return schemaString;
|
328
|
+
};
|
329
|
+
|
330
|
+
typeToZod = (attribute: Attribute) => {
|
331
|
+
let baseSchemaCode = "";
|
332
|
+
const finalAttribute: Attribute = (
|
333
|
+
attribute.type === "string" &&
|
334
|
+
attribute.format &&
|
335
|
+
attribute.format === "enum" &&
|
336
|
+
attribute.type === "string"
|
337
|
+
? { ...attribute, type: attribute.format }
|
338
|
+
: attribute
|
339
|
+
) as Attribute;
|
340
|
+
switch (finalAttribute.type) {
|
341
|
+
case "string":
|
342
|
+
baseSchemaCode = "z.string()";
|
343
|
+
if (finalAttribute.size) {
|
344
|
+
baseSchemaCode += `.max(${finalAttribute.size}, "Maximum length of ${finalAttribute.size} characters exceeded")`;
|
345
|
+
}
|
346
|
+
if (finalAttribute.xdefault !== undefined) {
|
347
|
+
baseSchemaCode += `.default("${finalAttribute.xdefault}")`;
|
348
|
+
}
|
349
|
+
if (!attribute.required && !attribute.array) {
|
350
|
+
baseSchemaCode += ".nullish()";
|
351
|
+
}
|
352
|
+
break;
|
353
|
+
case "integer":
|
354
|
+
baseSchemaCode = "z.number().int()";
|
355
|
+
if (finalAttribute.min !== undefined) {
|
356
|
+
if (BigInt(finalAttribute.min) === BigInt(-9223372036854776000)) {
|
357
|
+
delete finalAttribute.min;
|
358
|
+
} else {
|
359
|
+
baseSchemaCode += `.min(${finalAttribute.min}, "Minimum value of ${finalAttribute.min} not met")`;
|
360
|
+
}
|
361
|
+
}
|
362
|
+
if (finalAttribute.max !== undefined) {
|
363
|
+
if (BigInt(finalAttribute.max) === BigInt(9223372036854776000)) {
|
364
|
+
delete finalAttribute.max;
|
365
|
+
} else {
|
366
|
+
baseSchemaCode += `.max(${finalAttribute.max}, "Maximum value of ${finalAttribute.max} exceeded")`;
|
367
|
+
}
|
368
|
+
}
|
369
|
+
if (finalAttribute.xdefault !== undefined) {
|
370
|
+
baseSchemaCode += `.default(${finalAttribute.xdefault})`;
|
371
|
+
}
|
372
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
373
|
+
baseSchemaCode += ".nullish()";
|
374
|
+
}
|
375
|
+
break;
|
376
|
+
case "float":
|
377
|
+
baseSchemaCode = "z.number()";
|
378
|
+
if (finalAttribute.min !== undefined) {
|
379
|
+
baseSchemaCode += `.min(${finalAttribute.min}, "Minimum value of ${finalAttribute.min} not met")`;
|
380
|
+
}
|
381
|
+
if (finalAttribute.max !== undefined) {
|
382
|
+
baseSchemaCode += `.max(${finalAttribute.max}, "Maximum value of ${finalAttribute.max} exceeded")`;
|
383
|
+
}
|
384
|
+
if (finalAttribute.xdefault !== undefined) {
|
385
|
+
baseSchemaCode += `.default(${finalAttribute.xdefault})`;
|
386
|
+
}
|
387
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
388
|
+
baseSchemaCode += ".nullish()";
|
389
|
+
}
|
390
|
+
break;
|
391
|
+
case "boolean":
|
392
|
+
baseSchemaCode = "z.boolean()";
|
393
|
+
if (finalAttribute.xdefault !== undefined) {
|
394
|
+
baseSchemaCode += `.default(${finalAttribute.xdefault})`;
|
395
|
+
}
|
396
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
397
|
+
baseSchemaCode += ".nullish()";
|
398
|
+
}
|
399
|
+
break;
|
400
|
+
case "datetime":
|
401
|
+
baseSchemaCode = "z.date()";
|
402
|
+
if (finalAttribute.xdefault !== undefined) {
|
403
|
+
baseSchemaCode += `.default(new Date("${finalAttribute.xdefault}"))`;
|
404
|
+
}
|
405
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
406
|
+
baseSchemaCode += ".nullish()";
|
407
|
+
}
|
408
|
+
break;
|
409
|
+
case "email":
|
410
|
+
baseSchemaCode = "z.string().email()";
|
411
|
+
if (finalAttribute.xdefault !== undefined) {
|
412
|
+
baseSchemaCode += `.default("${finalAttribute.xdefault}")`;
|
413
|
+
}
|
414
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
415
|
+
baseSchemaCode += ".nullish()";
|
416
|
+
}
|
417
|
+
break;
|
418
|
+
case "ip":
|
419
|
+
baseSchemaCode = "z.string()"; // Add custom validation as needed
|
420
|
+
if (finalAttribute.xdefault !== undefined) {
|
421
|
+
baseSchemaCode += `.default("${finalAttribute.xdefault}")`;
|
422
|
+
}
|
423
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
424
|
+
baseSchemaCode += ".nullish()";
|
425
|
+
}
|
426
|
+
break;
|
427
|
+
case "url":
|
428
|
+
baseSchemaCode = "z.string().url()";
|
429
|
+
if (finalAttribute.xdefault !== undefined) {
|
430
|
+
baseSchemaCode += `.default("${finalAttribute.xdefault}")`;
|
431
|
+
}
|
432
|
+
if (!finalAttribute.required && !finalAttribute.array) {
|
433
|
+
baseSchemaCode += ".nullish()";
|
434
|
+
}
|
435
|
+
break;
|
436
|
+
case "enum":
|
437
|
+
baseSchemaCode = `z.enum([${finalAttribute.elements
|
438
|
+
.map((element) => `"${element}"`)
|
439
|
+
.join(", ")}])`;
|
440
|
+
if (finalAttribute.xdefault !== undefined) {
|
441
|
+
baseSchemaCode += `.default("${finalAttribute.xdefault}")`;
|
442
|
+
}
|
443
|
+
if (!attribute.required && !attribute.array) {
|
444
|
+
baseSchemaCode += ".nullish()";
|
445
|
+
}
|
446
|
+
break;
|
447
|
+
case "relationship":
|
448
|
+
break;
|
449
|
+
default:
|
450
|
+
baseSchemaCode = "z.any()";
|
451
|
+
}
|
452
|
+
|
453
|
+
// Handle arrays
|
454
|
+
if (attribute.array) {
|
455
|
+
baseSchemaCode = `z.array(${baseSchemaCode})`;
|
456
|
+
}
|
457
|
+
if (attribute.array && !attribute.required) {
|
458
|
+
baseSchemaCode += ".nullish()";
|
459
|
+
}
|
460
|
+
if (attribute.description) {
|
461
|
+
if (typeof attribute.description === "string") {
|
462
|
+
baseSchemaCode += `.openapi({ description: "${attribute.description}" })`;
|
463
|
+
} else {
|
464
|
+
baseSchemaCode += `.openapi(${Object.entries(attribute.description)
|
465
|
+
.map(([key, value]) => `"${key}": ${value}`)
|
466
|
+
.join(", ")})`;
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
return baseSchemaCode;
|
471
|
+
};
|
472
|
+
}
|