appwrite-utils-cli 0.10.86 → 1.0.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/.appwrite/.yaml_schemas/appwrite-config.schema.json +380 -0
- package/.appwrite/.yaml_schemas/collection.schema.json +255 -0
- package/.appwrite/collections/Categories.yaml +182 -0
- package/.appwrite/collections/ExampleCollection.yaml +36 -0
- package/.appwrite/collections/Posts.yaml +227 -0
- package/.appwrite/collections/Users.yaml +149 -0
- package/.appwrite/config.yaml +109 -0
- package/.appwrite/import/README.md +148 -0
- package/.appwrite/import/categories-import.yaml +129 -0
- package/.appwrite/import/posts-import.yaml +208 -0
- package/.appwrite/import/users-import.yaml +130 -0
- package/.appwrite/importData/categories.json +194 -0
- package/.appwrite/importData/posts.json +270 -0
- package/.appwrite/importData/users.json +220 -0
- package/.appwrite/schemas/categories.json +128 -0
- package/.appwrite/schemas/exampleCollection.json +52 -0
- package/.appwrite/schemas/posts.json +173 -0
- package/.appwrite/schemas/users.json +125 -0
- package/README.md +264 -33
- package/dist/collections/attributes.js +3 -2
- package/dist/collections/methods.js +56 -38
- package/dist/config/yamlConfig.d.ts +501 -0
- package/dist/config/yamlConfig.js +452 -0
- package/dist/databases/setup.d.ts +6 -0
- package/dist/databases/setup.js +119 -0
- package/dist/functions/methods.d.ts +1 -1
- package/dist/functions/methods.js +5 -2
- package/dist/functions/openapi.d.ts +4 -0
- package/dist/functions/openapi.js +60 -0
- package/dist/interactiveCLI.d.ts +5 -0
- package/dist/interactiveCLI.js +194 -49
- package/dist/main.js +91 -30
- package/dist/migrations/afterImportActions.js +2 -2
- package/dist/migrations/appwriteToX.d.ts +10 -0
- package/dist/migrations/appwriteToX.js +15 -4
- package/dist/migrations/backup.d.ts +16 -16
- package/dist/migrations/dataLoader.d.ts +83 -1
- package/dist/migrations/dataLoader.js +4 -4
- package/dist/migrations/importController.js +25 -18
- package/dist/migrations/importDataActions.js +2 -2
- package/dist/migrations/logging.d.ts +9 -1
- package/dist/migrations/logging.js +41 -22
- package/dist/migrations/migrationHelper.d.ts +4 -4
- package/dist/migrations/relationships.js +1 -1
- package/dist/migrations/services/DataTransformationService.d.ts +55 -0
- package/dist/migrations/services/DataTransformationService.js +158 -0
- package/dist/migrations/services/FileHandlerService.d.ts +75 -0
- package/dist/migrations/services/FileHandlerService.js +236 -0
- package/dist/migrations/services/ImportOrchestrator.d.ts +97 -0
- package/dist/migrations/services/ImportOrchestrator.js +488 -0
- package/dist/migrations/services/RateLimitManager.d.ts +138 -0
- package/dist/migrations/services/RateLimitManager.js +279 -0
- package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
- package/dist/migrations/services/RelationshipResolver.js +332 -0
- package/dist/migrations/services/UserMappingService.d.ts +109 -0
- package/dist/migrations/services/UserMappingService.js +277 -0
- package/dist/migrations/services/ValidationService.d.ts +74 -0
- package/dist/migrations/services/ValidationService.js +260 -0
- package/dist/migrations/transfer.d.ts +0 -6
- package/dist/migrations/transfer.js +16 -132
- package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +384 -0
- package/dist/migrations/yaml/YamlImportConfigLoader.js +375 -0
- package/dist/migrations/yaml/YamlImportIntegration.d.ts +87 -0
- package/dist/migrations/yaml/YamlImportIntegration.js +330 -0
- package/dist/migrations/yaml/generateImportSchemas.d.ts +17 -0
- package/dist/migrations/yaml/generateImportSchemas.js +575 -0
- package/dist/schemas/authUser.d.ts +9 -9
- package/dist/shared/attributeManager.d.ts +17 -0
- package/dist/shared/attributeManager.js +273 -0
- package/dist/shared/confirmationDialogs.d.ts +75 -0
- package/dist/shared/confirmationDialogs.js +236 -0
- package/dist/shared/functionManager.d.ts +48 -0
- package/dist/shared/functionManager.js +322 -0
- package/dist/shared/indexManager.d.ts +24 -0
- package/dist/shared/indexManager.js +150 -0
- package/dist/shared/jsonSchemaGenerator.d.ts +51 -0
- package/dist/shared/jsonSchemaGenerator.js +313 -0
- package/dist/shared/logging.d.ts +10 -0
- package/dist/shared/logging.js +46 -0
- package/dist/shared/messageFormatter.d.ts +37 -0
- package/dist/shared/messageFormatter.js +152 -0
- package/dist/shared/migrationHelpers.d.ts +173 -0
- package/dist/shared/migrationHelpers.js +142 -0
- package/dist/shared/operationLogger.d.ts +3 -0
- package/dist/shared/operationLogger.js +25 -0
- package/dist/shared/operationQueue.d.ts +13 -0
- package/dist/shared/operationQueue.js +79 -0
- package/dist/shared/progressManager.d.ts +62 -0
- package/dist/shared/progressManager.js +215 -0
- package/dist/shared/schemaGenerator.d.ts +18 -0
- package/dist/shared/schemaGenerator.js +523 -0
- package/dist/storage/methods.d.ts +3 -1
- package/dist/storage/methods.js +144 -55
- package/dist/storage/schemas.d.ts +56 -16
- package/dist/types.d.ts +2 -2
- package/dist/types.js +1 -1
- package/dist/users/methods.d.ts +16 -0
- package/dist/users/methods.js +276 -0
- package/dist/utils/configMigration.d.ts +1 -0
- package/dist/utils/configMigration.js +262 -0
- package/dist/utils/dataConverters.d.ts +46 -0
- package/dist/utils/dataConverters.js +139 -0
- package/dist/utils/loadConfigs.d.ts +15 -4
- package/dist/utils/loadConfigs.js +379 -51
- package/dist/utils/schemaStrings.js +2 -1
- package/dist/utils/setupFiles.d.ts +2 -1
- package/dist/utils/setupFiles.js +723 -28
- package/dist/utils/validationRules.d.ts +43 -0
- package/dist/utils/validationRules.js +42 -0
- package/dist/utils/yamlConverter.d.ts +48 -0
- package/dist/utils/yamlConverter.js +98 -0
- package/dist/utilsController.js +65 -43
- package/package.json +19 -15
- package/src/collections/attributes.ts +3 -2
- package/src/collections/methods.ts +85 -51
- package/src/config/yamlConfig.ts +488 -0
- package/src/{migrations/setupDatabase.ts → databases/setup.ts} +11 -5
- package/src/functions/methods.ts +8 -4
- package/src/functions/templates/count-docs-in-collection/package.json +25 -0
- package/src/functions/templates/count-docs-in-collection/tsconfig.json +28 -0
- package/src/functions/templates/typescript-node/package.json +24 -0
- package/src/functions/templates/typescript-node/tsconfig.json +28 -0
- package/src/functions/templates/uv/README.md +31 -0
- package/src/functions/templates/uv/pyproject.toml +29 -0
- package/src/interactiveCLI.ts +226 -61
- package/src/main.ts +111 -37
- package/src/migrations/afterImportActions.ts +2 -2
- package/src/migrations/appwriteToX.ts +17 -4
- package/src/migrations/dataLoader.ts +4 -4
- package/src/migrations/importController.ts +30 -22
- package/src/migrations/importDataActions.ts +2 -2
- package/src/migrations/relationships.ts +1 -1
- package/src/migrations/services/DataTransformationService.ts +196 -0
- package/src/migrations/services/FileHandlerService.ts +311 -0
- package/src/migrations/services/ImportOrchestrator.ts +669 -0
- package/src/migrations/services/RateLimitManager.ts +363 -0
- package/src/migrations/services/RelationshipResolver.ts +461 -0
- package/src/migrations/services/UserMappingService.ts +345 -0
- package/src/migrations/services/ValidationService.ts +349 -0
- package/src/migrations/transfer.ts +22 -228
- package/src/migrations/yaml/YamlImportConfigLoader.ts +427 -0
- package/src/migrations/yaml/YamlImportIntegration.ts +419 -0
- package/src/migrations/yaml/generateImportSchemas.ts +589 -0
- package/src/shared/attributeManager.ts +429 -0
- package/src/shared/confirmationDialogs.ts +327 -0
- package/src/shared/functionManager.ts +515 -0
- package/src/shared/indexManager.ts +253 -0
- package/src/shared/jsonSchemaGenerator.ts +403 -0
- package/src/shared/logging.ts +74 -0
- package/src/shared/messageFormatter.ts +195 -0
- package/src/{migrations/migrationHelper.ts → shared/migrationHelpers.ts} +22 -4
- package/src/{migrations/helper.ts → shared/operationLogger.ts} +7 -2
- package/src/{migrations/queue.ts → shared/operationQueue.ts} +1 -1
- package/src/shared/progressManager.ts +278 -0
- package/src/{migrations/schemaStrings.ts → shared/schemaGenerator.ts} +71 -17
- package/src/storage/methods.ts +199 -78
- package/src/types.ts +2 -2
- package/src/{migrations/users.ts → users/methods.ts} +2 -2
- package/src/utils/configMigration.ts +349 -0
- package/src/utils/loadConfigs.ts +416 -52
- package/src/utils/schemaStrings.ts +2 -1
- package/src/utils/setupFiles.ts +742 -40
- package/src/{migrations → utils}/validationRules.ts +1 -1
- package/src/utils/yamlConverter.ts +131 -0
- package/src/utilsController.ts +75 -54
- package/src/functions/templates/poetry/README.md +0 -30
- package/src/functions/templates/poetry/pyproject.toml +0 -16
- package/src/migrations/attributes.ts +0 -561
- package/src/migrations/backup.ts +0 -205
- package/src/migrations/databases.ts +0 -39
- package/src/migrations/dbHelpers.ts +0 -92
- package/src/migrations/indexes.ts +0 -40
- package/src/migrations/logging.ts +0 -29
- package/src/migrations/storage.ts +0 -538
- /package/src/{migrations → functions}/openapi.ts +0 -0
- /package/src/functions/templates/{poetry → uv}/src/__init__.py +0 -0
- /package/src/functions/templates/{poetry → uv}/src/index.py +0 -0
- /package/src/{migrations/converters.ts → utils/dataConverters.ts} +0 -0
@@ -0,0 +1,253 @@
|
|
1
|
+
import { type Index, type CollectionCreate } from "appwrite-utils";
|
2
|
+
import { Databases, IndexType, Query, type Models } from "node-appwrite";
|
3
|
+
import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
4
|
+
import chalk from "chalk";
|
5
|
+
import pLimit from "p-limit";
|
6
|
+
|
7
|
+
// Concurrency limits for different operations
|
8
|
+
const indexLimit = pLimit(3); // Low limit for index operations
|
9
|
+
const queryLimit = pLimit(25); // Higher limit for read operations
|
10
|
+
|
11
|
+
export const indexesSame = (
|
12
|
+
databaseIndex: Models.Index,
|
13
|
+
configIndex: Index
|
14
|
+
): boolean => {
|
15
|
+
return (
|
16
|
+
databaseIndex.key === configIndex.key &&
|
17
|
+
databaseIndex.type === configIndex.type &&
|
18
|
+
JSON.stringify(databaseIndex.attributes) === JSON.stringify(configIndex.attributes) &&
|
19
|
+
JSON.stringify(databaseIndex.orders) === JSON.stringify(configIndex.orders)
|
20
|
+
);
|
21
|
+
};
|
22
|
+
|
23
|
+
export const createOrUpdateIndex = async (
|
24
|
+
dbId: string,
|
25
|
+
db: Databases,
|
26
|
+
collectionId: string,
|
27
|
+
index: Index,
|
28
|
+
options: {
|
29
|
+
verbose?: boolean;
|
30
|
+
forceRecreate?: boolean;
|
31
|
+
} = {}
|
32
|
+
): Promise<Models.Index | null> => {
|
33
|
+
const { verbose = false, forceRecreate = false } = options;
|
34
|
+
|
35
|
+
return await indexLimit(async () => {
|
36
|
+
// Check for existing index
|
37
|
+
const existingIndexes = await queryLimit(() =>
|
38
|
+
tryAwaitWithRetry(async () =>
|
39
|
+
await db.listIndexes(dbId, collectionId, [Query.equal("key", index.key)])
|
40
|
+
)
|
41
|
+
);
|
42
|
+
|
43
|
+
let shouldCreate = false;
|
44
|
+
let existingIndex: Models.Index | undefined;
|
45
|
+
|
46
|
+
if (existingIndexes.total > 0) {
|
47
|
+
existingIndex = existingIndexes.indexes[0];
|
48
|
+
|
49
|
+
if (forceRecreate || !indexesSame(existingIndex, index)) {
|
50
|
+
if (verbose) {
|
51
|
+
console.log(chalk.yellow(`⚠ Updating index ${index.key} in collection ${collectionId}`));
|
52
|
+
}
|
53
|
+
|
54
|
+
// Delete existing index
|
55
|
+
await tryAwaitWithRetry(async () => {
|
56
|
+
await db.deleteIndex(dbId, collectionId, existingIndex!.key);
|
57
|
+
});
|
58
|
+
|
59
|
+
await delay(500); // Wait for deletion to complete
|
60
|
+
shouldCreate = true;
|
61
|
+
} else {
|
62
|
+
if (verbose) {
|
63
|
+
console.log(chalk.green(`✓ Index ${index.key} is up to date`));
|
64
|
+
}
|
65
|
+
return existingIndex;
|
66
|
+
}
|
67
|
+
} else {
|
68
|
+
shouldCreate = true;
|
69
|
+
if (verbose) {
|
70
|
+
console.log(chalk.blue(`+ Creating index ${index.key} in collection ${collectionId}`));
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
if (shouldCreate) {
|
75
|
+
const newIndex = await tryAwaitWithRetry(async () => {
|
76
|
+
return await db.createIndex(
|
77
|
+
dbId,
|
78
|
+
collectionId,
|
79
|
+
index.key,
|
80
|
+
index.type as IndexType,
|
81
|
+
index.attributes,
|
82
|
+
index.orders
|
83
|
+
);
|
84
|
+
});
|
85
|
+
|
86
|
+
if (verbose) {
|
87
|
+
console.log(chalk.green(`✓ Created index ${index.key}`));
|
88
|
+
}
|
89
|
+
|
90
|
+
return newIndex;
|
91
|
+
}
|
92
|
+
|
93
|
+
return null;
|
94
|
+
});
|
95
|
+
};
|
96
|
+
|
97
|
+
export const createOrUpdateIndexes = async (
|
98
|
+
dbId: string,
|
99
|
+
db: Databases,
|
100
|
+
collectionId: string,
|
101
|
+
indexes: Index[],
|
102
|
+
options: {
|
103
|
+
verbose?: boolean;
|
104
|
+
forceRecreate?: boolean;
|
105
|
+
} = {}
|
106
|
+
): Promise<void> => {
|
107
|
+
const { verbose = false } = options;
|
108
|
+
|
109
|
+
if (!indexes || indexes.length === 0) {
|
110
|
+
return;
|
111
|
+
}
|
112
|
+
|
113
|
+
if (verbose) {
|
114
|
+
console.log(chalk.blue(`Processing ${indexes.length} indexes for collection ${collectionId}`));
|
115
|
+
}
|
116
|
+
|
117
|
+
// Process indexes sequentially to avoid conflicts
|
118
|
+
for (const index of indexes) {
|
119
|
+
try {
|
120
|
+
await createOrUpdateIndex(dbId, db, collectionId, index, options);
|
121
|
+
|
122
|
+
// Add delay between index operations to prevent rate limiting
|
123
|
+
await delay(250);
|
124
|
+
} catch (error) {
|
125
|
+
console.error(chalk.red(`❌ Failed to process index ${index.key}:`), error);
|
126
|
+
throw error;
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
if (verbose) {
|
131
|
+
console.log(chalk.green(`✓ Completed processing indexes for collection ${collectionId}`));
|
132
|
+
}
|
133
|
+
};
|
134
|
+
|
135
|
+
export const createUpdateCollectionIndexes = async (
|
136
|
+
db: Databases,
|
137
|
+
dbId: string,
|
138
|
+
collection: Models.Collection,
|
139
|
+
collectionConfig: CollectionCreate,
|
140
|
+
options: {
|
141
|
+
verbose?: boolean;
|
142
|
+
forceRecreate?: boolean;
|
143
|
+
} = {}
|
144
|
+
): Promise<void> => {
|
145
|
+
if (!collectionConfig.indexes) return;
|
146
|
+
|
147
|
+
await createOrUpdateIndexes(
|
148
|
+
dbId,
|
149
|
+
db,
|
150
|
+
collection.$id,
|
151
|
+
collectionConfig.indexes,
|
152
|
+
options
|
153
|
+
);
|
154
|
+
};
|
155
|
+
|
156
|
+
export const deleteObsoleteIndexes = async (
|
157
|
+
db: Databases,
|
158
|
+
dbId: string,
|
159
|
+
collection: Models.Collection,
|
160
|
+
collectionConfig: CollectionCreate,
|
161
|
+
options: {
|
162
|
+
verbose?: boolean;
|
163
|
+
} = {}
|
164
|
+
): Promise<void> => {
|
165
|
+
const { verbose = false } = options;
|
166
|
+
|
167
|
+
const configIndexes = collectionConfig.indexes || [];
|
168
|
+
const configIndexKeys = new Set(configIndexes.map(index => index.key));
|
169
|
+
|
170
|
+
// Get all existing indexes
|
171
|
+
const existingIndexes = await queryLimit(() =>
|
172
|
+
tryAwaitWithRetry(async () =>
|
173
|
+
await db.listIndexes(dbId, collection.$id)
|
174
|
+
)
|
175
|
+
);
|
176
|
+
|
177
|
+
// Find indexes that exist in the database but not in the config
|
178
|
+
const obsoleteIndexes = existingIndexes.indexes.filter(
|
179
|
+
(index) => !configIndexKeys.has(index.key)
|
180
|
+
);
|
181
|
+
|
182
|
+
if (obsoleteIndexes.length === 0) {
|
183
|
+
return;
|
184
|
+
}
|
185
|
+
|
186
|
+
if (verbose) {
|
187
|
+
console.log(chalk.yellow(`🗑️ Removing ${obsoleteIndexes.length} obsolete indexes from collection ${collection.name}`));
|
188
|
+
}
|
189
|
+
|
190
|
+
// Process deletions with rate limiting
|
191
|
+
for (const index of obsoleteIndexes) {
|
192
|
+
await indexLimit(async () => {
|
193
|
+
await tryAwaitWithRetry(async () => {
|
194
|
+
await db.deleteIndex(dbId, collection.$id, index.key);
|
195
|
+
});
|
196
|
+
});
|
197
|
+
|
198
|
+
if (verbose) {
|
199
|
+
console.log(chalk.gray(`🗑️ Deleted obsolete index ${index.key}`));
|
200
|
+
}
|
201
|
+
|
202
|
+
await delay(250);
|
203
|
+
}
|
204
|
+
};
|
205
|
+
|
206
|
+
export const validateIndexConfiguration = (
|
207
|
+
indexes: Index[],
|
208
|
+
options: {
|
209
|
+
verbose?: boolean;
|
210
|
+
} = {}
|
211
|
+
): { valid: boolean; errors: string[] } => {
|
212
|
+
const { verbose = false } = options;
|
213
|
+
const errors: string[] = [];
|
214
|
+
|
215
|
+
for (const index of indexes) {
|
216
|
+
// Validate required fields
|
217
|
+
if (!index.key) {
|
218
|
+
errors.push(`Index missing required 'key' field`);
|
219
|
+
}
|
220
|
+
|
221
|
+
if (!index.type) {
|
222
|
+
errors.push(`Index '${index.key}' missing required 'type' field`);
|
223
|
+
}
|
224
|
+
|
225
|
+
if (!index.attributes || index.attributes.length === 0) {
|
226
|
+
errors.push(`Index '${index.key}' missing required 'attributes' field`);
|
227
|
+
}
|
228
|
+
|
229
|
+
// Validate index type
|
230
|
+
const validTypes = Object.values(IndexType);
|
231
|
+
if (index.type && !validTypes.includes(index.type as IndexType)) {
|
232
|
+
errors.push(`Index '${index.key}' has invalid type '${index.type}'. Valid types: ${validTypes.join(', ')}`);
|
233
|
+
}
|
234
|
+
|
235
|
+
// Validate orders array matches attributes length (if provided)
|
236
|
+
if (index.orders && index.attributes && index.orders.length !== index.attributes.length) {
|
237
|
+
errors.push(`Index '${index.key}' orders array length (${index.orders.length}) does not match attributes array length (${index.attributes.length})`);
|
238
|
+
}
|
239
|
+
|
240
|
+
// Check for duplicate keys within the same collection
|
241
|
+
const duplicateKeys = indexes.filter(i => i.key === index.key);
|
242
|
+
if (duplicateKeys.length > 1) {
|
243
|
+
errors.push(`Duplicate index key '${index.key}' found`);
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
if (verbose && errors.length > 0) {
|
248
|
+
console.log(chalk.red(`❌ Index validation errors:`));
|
249
|
+
errors.forEach(error => console.log(chalk.red(` - ${error}`)));
|
250
|
+
}
|
251
|
+
|
252
|
+
return { valid: errors.length === 0, errors };
|
253
|
+
};
|
@@ -0,0 +1,403 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import type { AppwriteConfig, Attribute, CollectionCreate } from "appwrite-utils";
|
4
|
+
import { toCamelCase, toPascalCase } from "../utils/index.js";
|
5
|
+
import chalk from "chalk";
|
6
|
+
|
7
|
+
export interface JsonSchemaProperty {
|
8
|
+
type: string | string[];
|
9
|
+
description?: string;
|
10
|
+
format?: string;
|
11
|
+
minimum?: number;
|
12
|
+
maximum?: number;
|
13
|
+
minLength?: number;
|
14
|
+
maxLength?: number;
|
15
|
+
pattern?: string;
|
16
|
+
enum?: any[];
|
17
|
+
items?: JsonSchemaProperty;
|
18
|
+
$ref?: string;
|
19
|
+
properties?: Record<string, JsonSchemaProperty>;
|
20
|
+
additionalProperties?: boolean;
|
21
|
+
required?: string[];
|
22
|
+
default?: any;
|
23
|
+
oneOf?: JsonSchemaProperty[];
|
24
|
+
}
|
25
|
+
|
26
|
+
export interface JsonSchema {
|
27
|
+
$schema: string;
|
28
|
+
$id: string;
|
29
|
+
title: string;
|
30
|
+
description?: string;
|
31
|
+
type: "object";
|
32
|
+
properties: Record<string, JsonSchemaProperty>;
|
33
|
+
required: string[];
|
34
|
+
additionalProperties: boolean;
|
35
|
+
definitions?: Record<string, JsonSchemaProperty>;
|
36
|
+
}
|
37
|
+
|
38
|
+
export class JsonSchemaGenerator {
|
39
|
+
private config: AppwriteConfig;
|
40
|
+
private appwriteFolderPath: string;
|
41
|
+
private relationshipMap = new Map<string, any[]>();
|
42
|
+
|
43
|
+
constructor(config: AppwriteConfig, appwriteFolderPath: string) {
|
44
|
+
this.config = config;
|
45
|
+
this.appwriteFolderPath = appwriteFolderPath;
|
46
|
+
this.extractRelationships();
|
47
|
+
}
|
48
|
+
|
49
|
+
private extractRelationships(): void {
|
50
|
+
if (!this.config.collections) return;
|
51
|
+
|
52
|
+
this.config.collections.forEach((collection) => {
|
53
|
+
if (!collection.attributes) return;
|
54
|
+
|
55
|
+
collection.attributes.forEach((attr) => {
|
56
|
+
if (attr.type === "relationship" && attr.relatedCollection) {
|
57
|
+
const relationships = this.relationshipMap.get(collection.name) || [];
|
58
|
+
relationships.push({
|
59
|
+
attributeKey: attr.key,
|
60
|
+
relatedCollection: attr.relatedCollection,
|
61
|
+
relationType: attr.relationType,
|
62
|
+
isArray: attr.relationType === "oneToMany" || attr.relationType === "manyToMany"
|
63
|
+
});
|
64
|
+
this.relationshipMap.set(collection.name, relationships);
|
65
|
+
}
|
66
|
+
});
|
67
|
+
});
|
68
|
+
}
|
69
|
+
|
70
|
+
private attributeToJsonSchemaProperty(attribute: Attribute): JsonSchemaProperty {
|
71
|
+
const property: JsonSchemaProperty = {
|
72
|
+
type: "string" // Default type
|
73
|
+
};
|
74
|
+
|
75
|
+
// Set description if available
|
76
|
+
if (attribute.description) {
|
77
|
+
property.description = typeof attribute.description === 'string'
|
78
|
+
? attribute.description
|
79
|
+
: JSON.stringify(attribute.description);
|
80
|
+
}
|
81
|
+
|
82
|
+
// Handle array attributes
|
83
|
+
if (attribute.array) {
|
84
|
+
property.type = "array";
|
85
|
+
property.items = this.getBaseTypeSchema(attribute);
|
86
|
+
} else {
|
87
|
+
Object.assign(property, this.getBaseTypeSchema(attribute));
|
88
|
+
}
|
89
|
+
|
90
|
+
// Set default value (only for attributes that support it)
|
91
|
+
if (attribute.type !== "relationship" && "xdefault" in attribute &&
|
92
|
+
attribute.xdefault !== undefined && attribute.xdefault !== null) {
|
93
|
+
property.default = attribute.xdefault;
|
94
|
+
}
|
95
|
+
|
96
|
+
return property;
|
97
|
+
}
|
98
|
+
|
99
|
+
private getBaseTypeSchema(attribute: Attribute): JsonSchemaProperty {
|
100
|
+
const schema: JsonSchemaProperty = {
|
101
|
+
type: "string" // Default type
|
102
|
+
};
|
103
|
+
|
104
|
+
switch (attribute.type) {
|
105
|
+
case "string":
|
106
|
+
schema.type = "string";
|
107
|
+
if (attribute.size) {
|
108
|
+
schema.maxLength = attribute.size;
|
109
|
+
}
|
110
|
+
break;
|
111
|
+
|
112
|
+
case "integer":
|
113
|
+
schema.type = "integer";
|
114
|
+
if (attribute.min !== undefined) {
|
115
|
+
schema.minimum = Number(attribute.min);
|
116
|
+
}
|
117
|
+
if (attribute.max !== undefined) {
|
118
|
+
schema.maximum = Number(attribute.max);
|
119
|
+
}
|
120
|
+
break;
|
121
|
+
|
122
|
+
case "double":
|
123
|
+
case "float": // Backward compatibility
|
124
|
+
schema.type = "number";
|
125
|
+
if (attribute.min !== undefined) {
|
126
|
+
schema.minimum = Number(attribute.min);
|
127
|
+
}
|
128
|
+
if (attribute.max !== undefined) {
|
129
|
+
schema.maximum = Number(attribute.max);
|
130
|
+
}
|
131
|
+
break;
|
132
|
+
|
133
|
+
case "boolean":
|
134
|
+
schema.type = "boolean";
|
135
|
+
break;
|
136
|
+
|
137
|
+
case "datetime":
|
138
|
+
schema.type = "string";
|
139
|
+
schema.format = "date-time";
|
140
|
+
break;
|
141
|
+
|
142
|
+
case "email":
|
143
|
+
schema.type = "string";
|
144
|
+
schema.format = "email";
|
145
|
+
break;
|
146
|
+
|
147
|
+
case "ip":
|
148
|
+
schema.type = "string";
|
149
|
+
schema.format = "ipv4";
|
150
|
+
break;
|
151
|
+
|
152
|
+
case "url":
|
153
|
+
schema.type = "string";
|
154
|
+
schema.format = "uri";
|
155
|
+
break;
|
156
|
+
|
157
|
+
case "enum":
|
158
|
+
schema.type = "string";
|
159
|
+
if (attribute.elements) {
|
160
|
+
schema.enum = attribute.elements;
|
161
|
+
}
|
162
|
+
break;
|
163
|
+
|
164
|
+
case "relationship":
|
165
|
+
if (attribute.relatedCollection) {
|
166
|
+
// For relationships, reference the related collection schema
|
167
|
+
schema.$ref = `#/definitions/${toPascalCase(attribute.relatedCollection)}`;
|
168
|
+
} else {
|
169
|
+
schema.type = "string";
|
170
|
+
schema.description = "Document ID reference";
|
171
|
+
}
|
172
|
+
break;
|
173
|
+
|
174
|
+
default:
|
175
|
+
schema.type = "string";
|
176
|
+
}
|
177
|
+
|
178
|
+
return schema;
|
179
|
+
}
|
180
|
+
|
181
|
+
private createJsonSchema(collection: CollectionCreate): JsonSchema {
|
182
|
+
const pascalName = toPascalCase(collection.name);
|
183
|
+
const schema: JsonSchema = {
|
184
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
185
|
+
$id: `https://example.com/schemas/${toCamelCase(collection.name)}.json`,
|
186
|
+
title: pascalName,
|
187
|
+
description: collection.description || `Schema for ${collection.name} collection`,
|
188
|
+
type: "object",
|
189
|
+
properties: {
|
190
|
+
// Standard Appwrite document fields
|
191
|
+
$id: {
|
192
|
+
type: "string",
|
193
|
+
description: "Document ID",
|
194
|
+
pattern: "^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$"
|
195
|
+
},
|
196
|
+
$createdAt: {
|
197
|
+
type: "string",
|
198
|
+
format: "date-time",
|
199
|
+
description: "Document creation date"
|
200
|
+
},
|
201
|
+
$updatedAt: {
|
202
|
+
type: "string",
|
203
|
+
format: "date-time",
|
204
|
+
description: "Document last update date"
|
205
|
+
},
|
206
|
+
$permissions: {
|
207
|
+
type: "array",
|
208
|
+
items: {
|
209
|
+
type: "string"
|
210
|
+
},
|
211
|
+
description: "Document permissions"
|
212
|
+
}
|
213
|
+
},
|
214
|
+
required: ["$id", "$createdAt", "$updatedAt"],
|
215
|
+
additionalProperties: false
|
216
|
+
};
|
217
|
+
|
218
|
+
// Add custom attributes
|
219
|
+
const requiredFields: string[] = [...schema.required];
|
220
|
+
|
221
|
+
if (collection.attributes) {
|
222
|
+
collection.attributes.forEach((attribute) => {
|
223
|
+
schema.properties[attribute.key] = this.attributeToJsonSchemaProperty(attribute);
|
224
|
+
|
225
|
+
if (attribute.required) {
|
226
|
+
requiredFields.push(attribute.key);
|
227
|
+
}
|
228
|
+
});
|
229
|
+
}
|
230
|
+
|
231
|
+
schema.required = requiredFields;
|
232
|
+
|
233
|
+
// Add relationship definitions if any exist
|
234
|
+
const relationships = this.relationshipMap.get(collection.name);
|
235
|
+
if (relationships && relationships.length > 0) {
|
236
|
+
schema.definitions = {};
|
237
|
+
|
238
|
+
relationships.forEach((rel) => {
|
239
|
+
const relatedPascalName = toPascalCase(rel.relatedCollection);
|
240
|
+
schema.definitions![relatedPascalName] = {
|
241
|
+
type: "object",
|
242
|
+
properties: {
|
243
|
+
$id: { type: "string" },
|
244
|
+
$createdAt: { type: "string", format: "date-time" },
|
245
|
+
$updatedAt: { type: "string", format: "date-time" }
|
246
|
+
},
|
247
|
+
additionalProperties: true,
|
248
|
+
description: `Reference to ${rel.relatedCollection} document`
|
249
|
+
};
|
250
|
+
});
|
251
|
+
}
|
252
|
+
|
253
|
+
return schema;
|
254
|
+
}
|
255
|
+
|
256
|
+
public generateJsonSchemas(options: {
|
257
|
+
outputFormat?: "json" | "typescript" | "both";
|
258
|
+
outputDirectory?: string;
|
259
|
+
verbose?: boolean;
|
260
|
+
} = {}): void {
|
261
|
+
const { outputFormat = "both", outputDirectory = "schemas", verbose = false } = options;
|
262
|
+
|
263
|
+
if (!this.config.collections) {
|
264
|
+
if (verbose) {
|
265
|
+
console.log(chalk.yellow("No collections found in config"));
|
266
|
+
}
|
267
|
+
return;
|
268
|
+
}
|
269
|
+
|
270
|
+
// Create JSON schemas directory using provided outputDirectory
|
271
|
+
const jsonSchemasPath = path.join(this.appwriteFolderPath, outputDirectory);
|
272
|
+
if (!fs.existsSync(jsonSchemasPath)) {
|
273
|
+
fs.mkdirSync(jsonSchemasPath, { recursive: true });
|
274
|
+
}
|
275
|
+
|
276
|
+
if (verbose) {
|
277
|
+
console.log(chalk.blue(`Generating JSON schemas for ${this.config.collections.length} collections...`));
|
278
|
+
}
|
279
|
+
|
280
|
+
this.config.collections.forEach((collection) => {
|
281
|
+
const schema = this.createJsonSchema(collection);
|
282
|
+
const camelCaseName = toCamelCase(collection.name);
|
283
|
+
|
284
|
+
// Generate JSON file
|
285
|
+
if (outputFormat === "json" || outputFormat === "both") {
|
286
|
+
const jsonPath = path.join(jsonSchemasPath, `${camelCaseName}.json`);
|
287
|
+
fs.writeFileSync(jsonPath, JSON.stringify(schema, null, 2), { encoding: "utf-8" });
|
288
|
+
|
289
|
+
if (verbose) {
|
290
|
+
console.log(chalk.green(`✓ JSON schema written to ${jsonPath}`));
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
// Generate TypeScript file
|
295
|
+
if (outputFormat === "typescript" || outputFormat === "both") {
|
296
|
+
const tsContent = this.generateTypeScriptSchema(schema, collection.name);
|
297
|
+
const tsPath = path.join(jsonSchemasPath, `${camelCaseName}.schema.ts`);
|
298
|
+
fs.writeFileSync(tsPath, tsContent, { encoding: "utf-8" });
|
299
|
+
|
300
|
+
if (verbose) {
|
301
|
+
console.log(chalk.green(`✓ TypeScript schema written to ${tsPath}`));
|
302
|
+
}
|
303
|
+
}
|
304
|
+
});
|
305
|
+
|
306
|
+
// Generate index file only for TypeScript output
|
307
|
+
if (outputFormat === "typescript" || outputFormat === "both") {
|
308
|
+
this.generateIndexFile(outputFormat, jsonSchemasPath, verbose);
|
309
|
+
}
|
310
|
+
|
311
|
+
if (verbose) {
|
312
|
+
console.log(chalk.green("✓ JSON schema generation completed"));
|
313
|
+
}
|
314
|
+
}
|
315
|
+
|
316
|
+
private generateTypeScriptSchema(schema: JsonSchema, collectionName: string): string {
|
317
|
+
const camelName = toCamelCase(collectionName);
|
318
|
+
|
319
|
+
return `// Auto-generated JSON schema for ${collectionName}
|
320
|
+
import type { JSONSchema7 } from "json-schema";
|
321
|
+
|
322
|
+
export const ${camelName}JsonSchema: JSONSchema7 = ${JSON.stringify(schema, null, 2)} as const;
|
323
|
+
|
324
|
+
export type ${toPascalCase(collectionName)}JsonSchema = typeof ${camelName}JsonSchema;
|
325
|
+
|
326
|
+
export default ${camelName}JsonSchema;
|
327
|
+
`;
|
328
|
+
}
|
329
|
+
|
330
|
+
private generateIndexFile(outputFormat: string, jsonSchemasPath: string, verbose: boolean): void {
|
331
|
+
if (!this.config.collections) return;
|
332
|
+
|
333
|
+
const indexPath = path.join(jsonSchemasPath, "index.ts");
|
334
|
+
|
335
|
+
const imports: string[] = [];
|
336
|
+
const exports: string[] = [];
|
337
|
+
|
338
|
+
this.config.collections.forEach((collection) => {
|
339
|
+
const camelName = toCamelCase(collection.name);
|
340
|
+
const pascalName = toPascalCase(collection.name);
|
341
|
+
|
342
|
+
if (outputFormat === "typescript" || outputFormat === "both") {
|
343
|
+
imports.push(`import { ${camelName}JsonSchema } from "./${camelName}.schema.js";`);
|
344
|
+
exports.push(` ${camelName}: ${camelName}JsonSchema,`);
|
345
|
+
}
|
346
|
+
});
|
347
|
+
|
348
|
+
const indexContent = `// Auto-generated index for JSON schemas
|
349
|
+
${imports.join('\n')}
|
350
|
+
|
351
|
+
export const jsonSchemas = {
|
352
|
+
${exports.join('\n')}
|
353
|
+
};
|
354
|
+
|
355
|
+
export type JsonSchemas = typeof jsonSchemas;
|
356
|
+
|
357
|
+
// Individual schema exports
|
358
|
+
${this.config.collections.map(collection => {
|
359
|
+
const camelName = toCamelCase(collection.name);
|
360
|
+
return `export { ${camelName}JsonSchema } from "./${camelName}.schema.js";`;
|
361
|
+
}).join('\n')}
|
362
|
+
|
363
|
+
export default jsonSchemas;
|
364
|
+
`;
|
365
|
+
|
366
|
+
fs.writeFileSync(indexPath, indexContent, { encoding: "utf-8" });
|
367
|
+
|
368
|
+
if (verbose) {
|
369
|
+
console.log(chalk.green(`✓ Index file written to ${indexPath}`));
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
public validateSchema(schema: JsonSchema): { valid: boolean; errors: string[] } {
|
374
|
+
const errors: string[] = [];
|
375
|
+
|
376
|
+
// Basic validation
|
377
|
+
if (!schema.$schema) {
|
378
|
+
errors.push("Missing $schema property");
|
379
|
+
}
|
380
|
+
|
381
|
+
if (!schema.title) {
|
382
|
+
errors.push("Missing title property");
|
383
|
+
}
|
384
|
+
|
385
|
+
if (!schema.properties) {
|
386
|
+
errors.push("Missing properties");
|
387
|
+
}
|
388
|
+
|
389
|
+
if (!schema.required || !Array.isArray(schema.required)) {
|
390
|
+
errors.push("Missing or invalid required array");
|
391
|
+
}
|
392
|
+
|
393
|
+
// Validate required Appwrite fields
|
394
|
+
const requiredAppwriteFields = ["$id", "$createdAt", "$updatedAt"];
|
395
|
+
requiredAppwriteFields.forEach((field) => {
|
396
|
+
if (!schema.properties[field]) {
|
397
|
+
errors.push(`Missing required Appwrite field: ${field}`);
|
398
|
+
}
|
399
|
+
});
|
400
|
+
|
401
|
+
return { valid: errors.length === 0, errors };
|
402
|
+
}
|
403
|
+
}
|