appwrite-utils-cli 0.10.85 → 1.0.1
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 +261 -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 +196 -52
- 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 +156 -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 +377 -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 +230 -63
- 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 +212 -0
- package/src/utils/loadConfigs.ts +414 -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,419 @@
|
|
1
|
+
import type { CollectionCreate, ImportDef } from "appwrite-utils";
|
2
|
+
import { YamlImportConfigLoader, type YamlImportConfig } from "./YamlImportConfigLoader.js";
|
3
|
+
import { createImportSchemas, createImportExamples } from "./generateImportSchemas.js";
|
4
|
+
import { logger } from "../../shared/logging.js";
|
5
|
+
import path from "path";
|
6
|
+
import fs from "fs";
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Integration service that bridges YAML import configurations with the existing import system.
|
10
|
+
* Provides seamless integration between new YAML configs and legacy TypeScript collection definitions.
|
11
|
+
*/
|
12
|
+
export class YamlImportIntegration {
|
13
|
+
private configLoader: YamlImportConfigLoader;
|
14
|
+
private appwriteFolderPath: string;
|
15
|
+
|
16
|
+
constructor(appwriteFolderPath: string) {
|
17
|
+
this.appwriteFolderPath = appwriteFolderPath;
|
18
|
+
this.configLoader = new YamlImportConfigLoader(appwriteFolderPath);
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Initializes the YAML import system.
|
23
|
+
* Creates necessary directories, schemas, and example files.
|
24
|
+
*/
|
25
|
+
async initialize(): Promise<void> {
|
26
|
+
try {
|
27
|
+
// Create import directory structure
|
28
|
+
await this.configLoader.createImportStructure();
|
29
|
+
|
30
|
+
// Generate JSON schemas for IntelliSense
|
31
|
+
await createImportSchemas(this.appwriteFolderPath);
|
32
|
+
|
33
|
+
// Create example configurations
|
34
|
+
await createImportExamples(this.appwriteFolderPath);
|
35
|
+
|
36
|
+
logger.info("YAML import system initialized successfully");
|
37
|
+
} catch (error) {
|
38
|
+
logger.error("Failed to initialize YAML import system:", error);
|
39
|
+
throw error;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Merges YAML import configurations with existing collection definitions.
|
45
|
+
* Allows collections to have both TypeScript and YAML import definitions.
|
46
|
+
*
|
47
|
+
* @param collections - Existing collection configurations
|
48
|
+
* @returns Collections with merged import definitions
|
49
|
+
*/
|
50
|
+
async mergeWithCollections(collections: CollectionCreate[]): Promise<CollectionCreate[]> {
|
51
|
+
try {
|
52
|
+
// Load all YAML import configurations
|
53
|
+
const yamlConfigs = await this.configLoader.loadAllImportConfigs();
|
54
|
+
|
55
|
+
if (yamlConfigs.size === 0) {
|
56
|
+
logger.info("No YAML import configurations found, using existing collection definitions");
|
57
|
+
return collections;
|
58
|
+
}
|
59
|
+
|
60
|
+
logger.info(`Found YAML import configurations for ${yamlConfigs.size} collections`);
|
61
|
+
|
62
|
+
const mergedCollections = [...collections];
|
63
|
+
|
64
|
+
// Process each collection with YAML configs
|
65
|
+
for (const [collectionName, yamlConfigList] of yamlConfigs.entries()) {
|
66
|
+
const existingCollection = mergedCollections.find(c => c.name === collectionName);
|
67
|
+
|
68
|
+
if (existingCollection) {
|
69
|
+
// Merge with existing collection
|
70
|
+
const yamlImportDefs = yamlConfigList.map(yamlConfig =>
|
71
|
+
this.configLoader.convertToImportDef(yamlConfig)
|
72
|
+
);
|
73
|
+
|
74
|
+
// Combine existing and YAML import definitions
|
75
|
+
const existingImportDefs = existingCollection.importDefs || [];
|
76
|
+
existingCollection.importDefs = [...existingImportDefs, ...yamlImportDefs];
|
77
|
+
|
78
|
+
logger.info(`Merged ${yamlImportDefs.length} YAML import definitions with collection: ${collectionName}`);
|
79
|
+
} else {
|
80
|
+
// Create new collection from YAML config
|
81
|
+
const yamlImportDefs = yamlConfigList.map(yamlConfig =>
|
82
|
+
this.configLoader.convertToImportDef(yamlConfig)
|
83
|
+
);
|
84
|
+
|
85
|
+
const newCollection: CollectionCreate = {
|
86
|
+
name: collectionName,
|
87
|
+
$id: collectionName.toLowerCase().replace(/\s+/g, '_'),
|
88
|
+
enabled: true,
|
89
|
+
documentSecurity: false,
|
90
|
+
$permissions: [],
|
91
|
+
attributes: [], // Will be populated from existing collection or schema
|
92
|
+
indexes: [],
|
93
|
+
importDefs: yamlImportDefs,
|
94
|
+
};
|
95
|
+
|
96
|
+
mergedCollections.push(newCollection);
|
97
|
+
logger.info(`Created new collection from YAML config: ${collectionName}`);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
return mergedCollections;
|
102
|
+
} catch (error) {
|
103
|
+
logger.error("Failed to merge YAML configurations with collections:", error);
|
104
|
+
// Return original collections on error to avoid breaking existing functionality
|
105
|
+
return collections;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
/**
|
110
|
+
* Validates YAML import configurations against existing collection schemas.
|
111
|
+
* Ensures that all target fields exist in the collection definitions.
|
112
|
+
*
|
113
|
+
* @param collections - Collection definitions to validate against
|
114
|
+
* @returns Validation results with errors and warnings
|
115
|
+
*/
|
116
|
+
async validateConfigurations(collections: CollectionCreate[]): Promise<{
|
117
|
+
isValid: boolean;
|
118
|
+
errors: string[];
|
119
|
+
warnings: string[];
|
120
|
+
}> {
|
121
|
+
const errors: string[] = [];
|
122
|
+
const warnings: string[] = [];
|
123
|
+
|
124
|
+
try {
|
125
|
+
const yamlConfigs = await this.configLoader.loadAllImportConfigs();
|
126
|
+
|
127
|
+
for (const [collectionName, yamlConfigList] of yamlConfigs.entries()) {
|
128
|
+
const collection = collections.find(c => c.name === collectionName);
|
129
|
+
|
130
|
+
if (!collection) {
|
131
|
+
warnings.push(`YAML import config references non-existent collection: ${collectionName}`);
|
132
|
+
continue;
|
133
|
+
}
|
134
|
+
|
135
|
+
for (let i = 0; i < yamlConfigList.length; i++) {
|
136
|
+
const yamlConfig = yamlConfigList[i];
|
137
|
+
const configErrors = this.configLoader.validateAgainstCollection(
|
138
|
+
yamlConfig,
|
139
|
+
collection.attributes || []
|
140
|
+
);
|
141
|
+
|
142
|
+
errors.push(...configErrors.map(err =>
|
143
|
+
`${collectionName}[${i}]: ${err}`
|
144
|
+
));
|
145
|
+
|
146
|
+
// Additional validation
|
147
|
+
this.validateSourceFiles(yamlConfig, errors, collectionName, i);
|
148
|
+
this.validateConverters(yamlConfig, warnings, collectionName, i);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
return {
|
153
|
+
isValid: errors.length === 0,
|
154
|
+
errors,
|
155
|
+
warnings,
|
156
|
+
};
|
157
|
+
} catch (error) {
|
158
|
+
errors.push(`Failed to validate YAML configurations: ${error}`);
|
159
|
+
return {
|
160
|
+
isValid: false,
|
161
|
+
errors,
|
162
|
+
warnings,
|
163
|
+
};
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
/**
|
168
|
+
* Validates that source files exist for import configurations.
|
169
|
+
*/
|
170
|
+
private validateSourceFiles(
|
171
|
+
yamlConfig: YamlImportConfig,
|
172
|
+
errors: string[],
|
173
|
+
collectionName: string,
|
174
|
+
configIndex: number
|
175
|
+
): void {
|
176
|
+
const sourceFilePath = path.resolve(this.appwriteFolderPath, yamlConfig.source.file);
|
177
|
+
|
178
|
+
if (!fs.existsSync(sourceFilePath)) {
|
179
|
+
errors.push(
|
180
|
+
`${collectionName}[${configIndex}]: Source file not found: ${yamlConfig.source.file}`
|
181
|
+
);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
/**
|
186
|
+
* Validates that converter functions are available.
|
187
|
+
*/
|
188
|
+
private validateConverters(
|
189
|
+
yamlConfig: YamlImportConfig,
|
190
|
+
warnings: string[],
|
191
|
+
collectionName: string,
|
192
|
+
configIndex: number
|
193
|
+
): void {
|
194
|
+
const availableConverters = [
|
195
|
+
"anyToString",
|
196
|
+
"anyToNumber",
|
197
|
+
"anyToBoolean",
|
198
|
+
"anyToDate",
|
199
|
+
"stringToLowerCase",
|
200
|
+
"stringToUpperCase",
|
201
|
+
"stringTrim",
|
202
|
+
"numberToString",
|
203
|
+
"booleanToString",
|
204
|
+
"dateToString",
|
205
|
+
"dateToTimestamp",
|
206
|
+
"timestampToDate",
|
207
|
+
"arrayToString",
|
208
|
+
"stringToArray",
|
209
|
+
"removeNulls",
|
210
|
+
"removeEmpty"
|
211
|
+
];
|
212
|
+
|
213
|
+
for (const mapping of yamlConfig.mapping.attributes) {
|
214
|
+
for (const converter of mapping.converters) {
|
215
|
+
const cleanConverter = converter.replace(/\[arr\]/gi, "").replace(/\[Arr\]/gi, "");
|
216
|
+
if (!availableConverters.includes(cleanConverter)) {
|
217
|
+
warnings.push(
|
218
|
+
`${collectionName}[${configIndex}]: Unknown converter '${converter}' in mapping for '${mapping.targetKey}'`
|
219
|
+
);
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
/**
|
226
|
+
* Generates a YAML import configuration from an existing ImportDef.
|
227
|
+
* Useful for migrating TypeScript configurations to YAML.
|
228
|
+
*
|
229
|
+
* @param importDef - Existing ImportDef to convert
|
230
|
+
* @param collectionName - Name of the collection
|
231
|
+
* @returns YAML configuration string
|
232
|
+
*/
|
233
|
+
convertImportDefToYaml(importDef: ImportDef, collectionName: string): string {
|
234
|
+
const yamlConfig: YamlImportConfig = {
|
235
|
+
source: {
|
236
|
+
file: importDef.filePath,
|
237
|
+
basePath: importDef.basePath,
|
238
|
+
type: "json",
|
239
|
+
},
|
240
|
+
target: {
|
241
|
+
collection: collectionName,
|
242
|
+
type: importDef.type || "create",
|
243
|
+
primaryKey: importDef.primaryKeyField,
|
244
|
+
createUsers: importDef.createUsers || false,
|
245
|
+
},
|
246
|
+
mapping: {
|
247
|
+
attributes: importDef.attributeMappings.map(attr => ({
|
248
|
+
oldKey: attr.oldKey,
|
249
|
+
oldKeys: attr.oldKeys,
|
250
|
+
targetKey: attr.targetKey,
|
251
|
+
valueToSet: attr.valueToSet,
|
252
|
+
fileData: attr.fileData,
|
253
|
+
converters: attr.converters || [],
|
254
|
+
validation: (attr.validationActions || []).map(v => ({
|
255
|
+
rule: v.action,
|
256
|
+
params: v.params,
|
257
|
+
})),
|
258
|
+
afterImport: (attr.postImportActions || []).map(a => ({
|
259
|
+
action: a.action,
|
260
|
+
params: a.params,
|
261
|
+
})),
|
262
|
+
})),
|
263
|
+
relationships: (importDef.idMappings || []).map(rel => ({
|
264
|
+
sourceField: rel.sourceField,
|
265
|
+
targetField: rel.targetField,
|
266
|
+
targetCollection: rel.targetCollection,
|
267
|
+
fieldToSet: rel.fieldToSet,
|
268
|
+
targetFieldToMatch: rel.targetFieldToMatch,
|
269
|
+
})),
|
270
|
+
},
|
271
|
+
options: {
|
272
|
+
batchSize: 50,
|
273
|
+
skipValidation: false,
|
274
|
+
dryRun: false,
|
275
|
+
continueOnError: true,
|
276
|
+
updateMapping: importDef.updateMapping,
|
277
|
+
},
|
278
|
+
};
|
279
|
+
|
280
|
+
const yaml = require("js-yaml");
|
281
|
+
const yamlContent = yaml.dump(yamlConfig, {
|
282
|
+
indent: 2,
|
283
|
+
lineWidth: 120,
|
284
|
+
sortKeys: false,
|
285
|
+
});
|
286
|
+
|
287
|
+
return `# yaml-language-server: $schema=../.yaml_schemas/import-config.schema.json\n# Import Configuration for ${collectionName}\n\n${yamlContent}`;
|
288
|
+
}
|
289
|
+
|
290
|
+
/**
|
291
|
+
* Exports existing TypeScript import configurations to YAML files.
|
292
|
+
* Helps migrate from TypeScript to YAML-based configurations.
|
293
|
+
*
|
294
|
+
* @param collections - Collections with existing import definitions
|
295
|
+
*/
|
296
|
+
async exportToYaml(collections: CollectionCreate[]): Promise<void> {
|
297
|
+
const exportDir = path.join(this.appwriteFolderPath, "import", "exported");
|
298
|
+
|
299
|
+
if (!fs.existsSync(exportDir)) {
|
300
|
+
fs.mkdirSync(exportDir, { recursive: true });
|
301
|
+
}
|
302
|
+
|
303
|
+
let exportedCount = 0;
|
304
|
+
|
305
|
+
for (const collection of collections) {
|
306
|
+
if (!collection.importDefs || collection.importDefs.length === 0) {
|
307
|
+
continue;
|
308
|
+
}
|
309
|
+
|
310
|
+
for (let i = 0; i < collection.importDefs.length; i++) {
|
311
|
+
const importDef = collection.importDefs[i];
|
312
|
+
const yamlContent = this.convertImportDefToYaml(importDef, collection.name);
|
313
|
+
|
314
|
+
const filename = collection.importDefs.length > 1
|
315
|
+
? `${collection.name}-${i + 1}.yaml`
|
316
|
+
: `${collection.name}.yaml`;
|
317
|
+
|
318
|
+
const filePath = path.join(exportDir, filename);
|
319
|
+
fs.writeFileSync(filePath, yamlContent);
|
320
|
+
exportedCount++;
|
321
|
+
|
322
|
+
logger.info(`Exported import configuration: ${filePath}`);
|
323
|
+
}
|
324
|
+
}
|
325
|
+
|
326
|
+
logger.info(`Exported ${exportedCount} import configurations to YAML`);
|
327
|
+
}
|
328
|
+
|
329
|
+
/**
|
330
|
+
* Gets statistics about YAML import configurations.
|
331
|
+
*/
|
332
|
+
async getStatistics(): Promise<{
|
333
|
+
hasYamlConfigs: boolean;
|
334
|
+
totalConfigurations: number;
|
335
|
+
collectionsWithConfigs: number;
|
336
|
+
configurationsByType: { [type: string]: number };
|
337
|
+
totalAttributeMappings: number;
|
338
|
+
totalRelationshipMappings: number;
|
339
|
+
}> {
|
340
|
+
try {
|
341
|
+
const yamlConfigs = await this.configLoader.loadAllImportConfigs();
|
342
|
+
const stats = this.configLoader.getStatistics(yamlConfigs);
|
343
|
+
|
344
|
+
return {
|
345
|
+
hasYamlConfigs: yamlConfigs.size > 0,
|
346
|
+
totalConfigurations: stats.totalConfigurations,
|
347
|
+
collectionsWithConfigs: stats.collectionsWithConfigs,
|
348
|
+
configurationsByType: stats.configsByType,
|
349
|
+
totalAttributeMappings: stats.totalAttributeMappings,
|
350
|
+
totalRelationshipMappings: stats.totalRelationshipMappings,
|
351
|
+
};
|
352
|
+
} catch (error) {
|
353
|
+
logger.error("Failed to get YAML import statistics:", error);
|
354
|
+
return {
|
355
|
+
hasYamlConfigs: false,
|
356
|
+
totalConfigurations: 0,
|
357
|
+
collectionsWithConfigs: 0,
|
358
|
+
configurationsByType: {},
|
359
|
+
totalAttributeMappings: 0,
|
360
|
+
totalRelationshipMappings: 0,
|
361
|
+
};
|
362
|
+
}
|
363
|
+
}
|
364
|
+
|
365
|
+
/**
|
366
|
+
* Creates a new YAML import configuration from a template.
|
367
|
+
*
|
368
|
+
* @param collectionName - Name of the collection
|
369
|
+
* @param sourceFile - Source data file name
|
370
|
+
* @param outputPath - Output file path (optional)
|
371
|
+
*/
|
372
|
+
async createFromTemplate(
|
373
|
+
collectionName: string,
|
374
|
+
sourceFile: string,
|
375
|
+
outputPath?: string
|
376
|
+
): Promise<string> {
|
377
|
+
const template = this.configLoader.generateTemplate(collectionName, sourceFile);
|
378
|
+
|
379
|
+
const fileName = outputPath || `${collectionName.toLowerCase()}-import.yaml`;
|
380
|
+
const fullPath = path.join(this.appwriteFolderPath, "import", fileName);
|
381
|
+
|
382
|
+
// Add schema reference to template
|
383
|
+
const schemaHeader = "# yaml-language-server: $schema=../.yaml_schemas/import-config.schema.json\n";
|
384
|
+
const templateWithSchema = schemaHeader + template;
|
385
|
+
|
386
|
+
fs.writeFileSync(fullPath, templateWithSchema);
|
387
|
+
logger.info(`Created YAML import configuration: ${fullPath}`);
|
388
|
+
|
389
|
+
return fullPath;
|
390
|
+
}
|
391
|
+
|
392
|
+
/**
|
393
|
+
* Checks if YAML import system is properly set up.
|
394
|
+
*/
|
395
|
+
async isSetupComplete(): Promise<{
|
396
|
+
isComplete: boolean;
|
397
|
+
missingComponents: string[];
|
398
|
+
}> {
|
399
|
+
const missingComponents: string[] = [];
|
400
|
+
|
401
|
+
// Check import directory
|
402
|
+
const importDir = path.join(this.appwriteFolderPath, "import");
|
403
|
+
if (!fs.existsSync(importDir)) {
|
404
|
+
missingComponents.push("import directory");
|
405
|
+
}
|
406
|
+
|
407
|
+
// Check schema files
|
408
|
+
const schemaDir = path.join(this.appwriteFolderPath, ".yaml_schemas");
|
409
|
+
const importSchemaPath = path.join(schemaDir, "import-config.schema.json");
|
410
|
+
if (!fs.existsSync(importSchemaPath)) {
|
411
|
+
missingComponents.push("import configuration schema");
|
412
|
+
}
|
413
|
+
|
414
|
+
return {
|
415
|
+
isComplete: missingComponents.length === 0,
|
416
|
+
missingComponents,
|
417
|
+
};
|
418
|
+
}
|
419
|
+
}
|