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.
Files changed (178) hide show
  1. package/.appwrite/.yaml_schemas/appwrite-config.schema.json +380 -0
  2. package/.appwrite/.yaml_schemas/collection.schema.json +255 -0
  3. package/.appwrite/collections/Categories.yaml +182 -0
  4. package/.appwrite/collections/ExampleCollection.yaml +36 -0
  5. package/.appwrite/collections/Posts.yaml +227 -0
  6. package/.appwrite/collections/Users.yaml +149 -0
  7. package/.appwrite/config.yaml +109 -0
  8. package/.appwrite/import/README.md +148 -0
  9. package/.appwrite/import/categories-import.yaml +129 -0
  10. package/.appwrite/import/posts-import.yaml +208 -0
  11. package/.appwrite/import/users-import.yaml +130 -0
  12. package/.appwrite/importData/categories.json +194 -0
  13. package/.appwrite/importData/posts.json +270 -0
  14. package/.appwrite/importData/users.json +220 -0
  15. package/.appwrite/schemas/categories.json +128 -0
  16. package/.appwrite/schemas/exampleCollection.json +52 -0
  17. package/.appwrite/schemas/posts.json +173 -0
  18. package/.appwrite/schemas/users.json +125 -0
  19. package/README.md +264 -33
  20. package/dist/collections/attributes.js +3 -2
  21. package/dist/collections/methods.js +56 -38
  22. package/dist/config/yamlConfig.d.ts +501 -0
  23. package/dist/config/yamlConfig.js +452 -0
  24. package/dist/databases/setup.d.ts +6 -0
  25. package/dist/databases/setup.js +119 -0
  26. package/dist/functions/methods.d.ts +1 -1
  27. package/dist/functions/methods.js +5 -2
  28. package/dist/functions/openapi.d.ts +4 -0
  29. package/dist/functions/openapi.js +60 -0
  30. package/dist/interactiveCLI.d.ts +5 -0
  31. package/dist/interactiveCLI.js +194 -49
  32. package/dist/main.js +91 -30
  33. package/dist/migrations/afterImportActions.js +2 -2
  34. package/dist/migrations/appwriteToX.d.ts +10 -0
  35. package/dist/migrations/appwriteToX.js +15 -4
  36. package/dist/migrations/backup.d.ts +16 -16
  37. package/dist/migrations/dataLoader.d.ts +83 -1
  38. package/dist/migrations/dataLoader.js +4 -4
  39. package/dist/migrations/importController.js +25 -18
  40. package/dist/migrations/importDataActions.js +2 -2
  41. package/dist/migrations/logging.d.ts +9 -1
  42. package/dist/migrations/logging.js +41 -22
  43. package/dist/migrations/migrationHelper.d.ts +4 -4
  44. package/dist/migrations/relationships.js +1 -1
  45. package/dist/migrations/services/DataTransformationService.d.ts +55 -0
  46. package/dist/migrations/services/DataTransformationService.js +158 -0
  47. package/dist/migrations/services/FileHandlerService.d.ts +75 -0
  48. package/dist/migrations/services/FileHandlerService.js +236 -0
  49. package/dist/migrations/services/ImportOrchestrator.d.ts +97 -0
  50. package/dist/migrations/services/ImportOrchestrator.js +488 -0
  51. package/dist/migrations/services/RateLimitManager.d.ts +138 -0
  52. package/dist/migrations/services/RateLimitManager.js +279 -0
  53. package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
  54. package/dist/migrations/services/RelationshipResolver.js +332 -0
  55. package/dist/migrations/services/UserMappingService.d.ts +109 -0
  56. package/dist/migrations/services/UserMappingService.js +277 -0
  57. package/dist/migrations/services/ValidationService.d.ts +74 -0
  58. package/dist/migrations/services/ValidationService.js +260 -0
  59. package/dist/migrations/transfer.d.ts +0 -6
  60. package/dist/migrations/transfer.js +16 -132
  61. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +384 -0
  62. package/dist/migrations/yaml/YamlImportConfigLoader.js +375 -0
  63. package/dist/migrations/yaml/YamlImportIntegration.d.ts +87 -0
  64. package/dist/migrations/yaml/YamlImportIntegration.js +330 -0
  65. package/dist/migrations/yaml/generateImportSchemas.d.ts +17 -0
  66. package/dist/migrations/yaml/generateImportSchemas.js +575 -0
  67. package/dist/schemas/authUser.d.ts +9 -9
  68. package/dist/shared/attributeManager.d.ts +17 -0
  69. package/dist/shared/attributeManager.js +273 -0
  70. package/dist/shared/confirmationDialogs.d.ts +75 -0
  71. package/dist/shared/confirmationDialogs.js +236 -0
  72. package/dist/shared/functionManager.d.ts +48 -0
  73. package/dist/shared/functionManager.js +322 -0
  74. package/dist/shared/indexManager.d.ts +24 -0
  75. package/dist/shared/indexManager.js +150 -0
  76. package/dist/shared/jsonSchemaGenerator.d.ts +51 -0
  77. package/dist/shared/jsonSchemaGenerator.js +313 -0
  78. package/dist/shared/logging.d.ts +10 -0
  79. package/dist/shared/logging.js +46 -0
  80. package/dist/shared/messageFormatter.d.ts +37 -0
  81. package/dist/shared/messageFormatter.js +152 -0
  82. package/dist/shared/migrationHelpers.d.ts +173 -0
  83. package/dist/shared/migrationHelpers.js +142 -0
  84. package/dist/shared/operationLogger.d.ts +3 -0
  85. package/dist/shared/operationLogger.js +25 -0
  86. package/dist/shared/operationQueue.d.ts +13 -0
  87. package/dist/shared/operationQueue.js +79 -0
  88. package/dist/shared/progressManager.d.ts +62 -0
  89. package/dist/shared/progressManager.js +215 -0
  90. package/dist/shared/schemaGenerator.d.ts +18 -0
  91. package/dist/shared/schemaGenerator.js +523 -0
  92. package/dist/storage/methods.d.ts +3 -1
  93. package/dist/storage/methods.js +144 -55
  94. package/dist/storage/schemas.d.ts +56 -16
  95. package/dist/types.d.ts +2 -2
  96. package/dist/types.js +1 -1
  97. package/dist/users/methods.d.ts +16 -0
  98. package/dist/users/methods.js +276 -0
  99. package/dist/utils/configMigration.d.ts +1 -0
  100. package/dist/utils/configMigration.js +262 -0
  101. package/dist/utils/dataConverters.d.ts +46 -0
  102. package/dist/utils/dataConverters.js +139 -0
  103. package/dist/utils/loadConfigs.d.ts +15 -4
  104. package/dist/utils/loadConfigs.js +379 -51
  105. package/dist/utils/schemaStrings.js +2 -1
  106. package/dist/utils/setupFiles.d.ts +2 -1
  107. package/dist/utils/setupFiles.js +723 -28
  108. package/dist/utils/validationRules.d.ts +43 -0
  109. package/dist/utils/validationRules.js +42 -0
  110. package/dist/utils/yamlConverter.d.ts +48 -0
  111. package/dist/utils/yamlConverter.js +98 -0
  112. package/dist/utilsController.js +65 -43
  113. package/package.json +19 -15
  114. package/src/collections/attributes.ts +3 -2
  115. package/src/collections/methods.ts +85 -51
  116. package/src/config/yamlConfig.ts +488 -0
  117. package/src/{migrations/setupDatabase.ts → databases/setup.ts} +11 -5
  118. package/src/functions/methods.ts +8 -4
  119. package/src/functions/templates/count-docs-in-collection/package.json +25 -0
  120. package/src/functions/templates/count-docs-in-collection/tsconfig.json +28 -0
  121. package/src/functions/templates/typescript-node/package.json +24 -0
  122. package/src/functions/templates/typescript-node/tsconfig.json +28 -0
  123. package/src/functions/templates/uv/README.md +31 -0
  124. package/src/functions/templates/uv/pyproject.toml +29 -0
  125. package/src/interactiveCLI.ts +226 -61
  126. package/src/main.ts +111 -37
  127. package/src/migrations/afterImportActions.ts +2 -2
  128. package/src/migrations/appwriteToX.ts +17 -4
  129. package/src/migrations/dataLoader.ts +4 -4
  130. package/src/migrations/importController.ts +30 -22
  131. package/src/migrations/importDataActions.ts +2 -2
  132. package/src/migrations/relationships.ts +1 -1
  133. package/src/migrations/services/DataTransformationService.ts +196 -0
  134. package/src/migrations/services/FileHandlerService.ts +311 -0
  135. package/src/migrations/services/ImportOrchestrator.ts +669 -0
  136. package/src/migrations/services/RateLimitManager.ts +363 -0
  137. package/src/migrations/services/RelationshipResolver.ts +461 -0
  138. package/src/migrations/services/UserMappingService.ts +345 -0
  139. package/src/migrations/services/ValidationService.ts +349 -0
  140. package/src/migrations/transfer.ts +22 -228
  141. package/src/migrations/yaml/YamlImportConfigLoader.ts +427 -0
  142. package/src/migrations/yaml/YamlImportIntegration.ts +419 -0
  143. package/src/migrations/yaml/generateImportSchemas.ts +589 -0
  144. package/src/shared/attributeManager.ts +429 -0
  145. package/src/shared/confirmationDialogs.ts +327 -0
  146. package/src/shared/functionManager.ts +515 -0
  147. package/src/shared/indexManager.ts +253 -0
  148. package/src/shared/jsonSchemaGenerator.ts +403 -0
  149. package/src/shared/logging.ts +74 -0
  150. package/src/shared/messageFormatter.ts +195 -0
  151. package/src/{migrations/migrationHelper.ts → shared/migrationHelpers.ts} +22 -4
  152. package/src/{migrations/helper.ts → shared/operationLogger.ts} +7 -2
  153. package/src/{migrations/queue.ts → shared/operationQueue.ts} +1 -1
  154. package/src/shared/progressManager.ts +278 -0
  155. package/src/{migrations/schemaStrings.ts → shared/schemaGenerator.ts} +71 -17
  156. package/src/storage/methods.ts +199 -78
  157. package/src/types.ts +2 -2
  158. package/src/{migrations/users.ts → users/methods.ts} +2 -2
  159. package/src/utils/configMigration.ts +349 -0
  160. package/src/utils/loadConfigs.ts +416 -52
  161. package/src/utils/schemaStrings.ts +2 -1
  162. package/src/utils/setupFiles.ts +742 -40
  163. package/src/{migrations → utils}/validationRules.ts +1 -1
  164. package/src/utils/yamlConverter.ts +131 -0
  165. package/src/utilsController.ts +75 -54
  166. package/src/functions/templates/poetry/README.md +0 -30
  167. package/src/functions/templates/poetry/pyproject.toml +0 -16
  168. package/src/migrations/attributes.ts +0 -561
  169. package/src/migrations/backup.ts +0 -205
  170. package/src/migrations/databases.ts +0 -39
  171. package/src/migrations/dbHelpers.ts +0 -92
  172. package/src/migrations/indexes.ts +0 -40
  173. package/src/migrations/logging.ts +0 -29
  174. package/src/migrations/storage.ts +0 -538
  175. /package/src/{migrations → functions}/openapi.ts +0 -0
  176. /package/src/functions/templates/{poetry → uv}/src/__init__.py +0 -0
  177. /package/src/functions/templates/{poetry → uv}/src/index.py +0 -0
  178. /package/src/{migrations/converters.ts → utils/dataConverters.ts} +0 -0
@@ -0,0 +1,276 @@
1
+ import { AppwriteException, Databases, ID, Query, Users, } from "node-appwrite";
2
+ import { AuthUserSchema, } from "../schemas/authUser.js";
3
+ import { logger } from "../shared/logging.js";
4
+ import { splitIntoBatches } from "../shared/migrationHelpers.js";
5
+ import { getAppwriteClient, tryAwaitWithRetry, } from "../utils/helperFunctions.js";
6
+ import { isUndefined } from "es-toolkit/compat";
7
+ import { isEmpty } from "es-toolkit/compat";
8
+ export class UsersController {
9
+ config;
10
+ users;
11
+ static userFields = [
12
+ "email",
13
+ "name",
14
+ "password",
15
+ "phone",
16
+ "labels",
17
+ "prefs",
18
+ "userId",
19
+ "$createdAt",
20
+ "$updatedAt",
21
+ ];
22
+ constructor(config, db) {
23
+ this.config = config;
24
+ this.users = new Users(this.config.appwriteClient);
25
+ }
26
+ async wipeUsers() {
27
+ const allUsers = await this.getAllUsers();
28
+ console.log("Deleting all users...");
29
+ const createBatches = (finalData, batchSize) => {
30
+ const finalBatches = [];
31
+ for (let i = 0; i < finalData.length; i += batchSize) {
32
+ finalBatches.push(finalData.slice(i, i + batchSize));
33
+ }
34
+ return finalBatches;
35
+ };
36
+ let usersDeleted = 0;
37
+ if (allUsers.length > 0) {
38
+ const batchedUserPromises = createBatches(allUsers, 25); // Batch size of 25
39
+ for (const batch of batchedUserPromises) {
40
+ console.log(`Deleting ${batch.length} users...`);
41
+ await Promise.all(batch.map((user) => tryAwaitWithRetry(async () => await this.users.delete(user.$id))));
42
+ usersDeleted += batch.length;
43
+ if (usersDeleted % 100 === 0) {
44
+ console.log(`Deleted ${usersDeleted} users...`);
45
+ }
46
+ }
47
+ }
48
+ else {
49
+ console.log("No users to delete");
50
+ }
51
+ }
52
+ async getAllUsers() {
53
+ const allUsers = [];
54
+ const users = await tryAwaitWithRetry(async () => await this.users.list([Query.limit(200)]));
55
+ if (users.users.length === 0) {
56
+ return [];
57
+ }
58
+ if (users.users.length === 200) {
59
+ let lastDocumentId = users.users[users.users.length - 1].$id;
60
+ allUsers.push(...users.users);
61
+ while (lastDocumentId) {
62
+ const moreUsers = await tryAwaitWithRetry(async () => await this.users.list([
63
+ Query.limit(200),
64
+ Query.cursorAfter(lastDocumentId),
65
+ ]));
66
+ allUsers.push(...moreUsers.users);
67
+ if (moreUsers.users.length < 200) {
68
+ break;
69
+ }
70
+ lastDocumentId = moreUsers.users[moreUsers.users.length - 1].$id;
71
+ }
72
+ }
73
+ else {
74
+ allUsers.push(...users.users);
75
+ }
76
+ return allUsers;
77
+ }
78
+ async createUsersAndReturn(items) {
79
+ const users = await Promise.all(items.map((item) => this.createUserAndReturn(item)));
80
+ return users;
81
+ }
82
+ async createUserAndReturn(item) {
83
+ try {
84
+ const user = await tryAwaitWithRetry(async () => {
85
+ const createdUser = await this.users.create(item.userId || ID.unique(), item.email || undefined, item.phone && item.phone.length < 15 && item.phone.startsWith("+")
86
+ ? item.phone
87
+ : undefined, `changeMe${item.email?.toLowerCase()}` || `changeMePlease`, item.name || undefined);
88
+ if (item.labels) {
89
+ await this.users.updateLabels(createdUser.$id, item.labels);
90
+ }
91
+ if (item.prefs) {
92
+ await this.users.updatePrefs(createdUser.$id, item.prefs);
93
+ }
94
+ return createdUser;
95
+ }); // Set throwError to true since we want to handle errors
96
+ return user;
97
+ }
98
+ catch (e) {
99
+ if (e instanceof Error) {
100
+ logger.error("FAILED CREATING USER: ", e.message, item);
101
+ }
102
+ }
103
+ }
104
+ async createAndCheckForUserAndReturn(item) {
105
+ let userToReturn = undefined;
106
+ try {
107
+ // Attempt to find an existing user by email or phone.
108
+ let foundUsers = [];
109
+ if (item.email) {
110
+ const foundUsersByEmail = await this.users.list([
111
+ Query.equal("email", item.email),
112
+ ]);
113
+ foundUsers = foundUsersByEmail.users;
114
+ }
115
+ if (item.phone) {
116
+ const foundUsersByPhone = await this.users.list([
117
+ Query.equal("phone", item.phone),
118
+ ]);
119
+ foundUsers = foundUsers.length
120
+ ? foundUsers.concat(foundUsersByPhone.users)
121
+ : foundUsersByPhone.users;
122
+ }
123
+ userToReturn = foundUsers[0] || undefined;
124
+ if (!userToReturn) {
125
+ userToReturn = await this.users.create(item.userId || ID.unique(), item.email || undefined, item.phone && item.phone.length < 15 && item.phone.startsWith("+")
126
+ ? item.phone
127
+ : undefined, item.password?.toLowerCase() ||
128
+ `changeMe${item.email?.toLowerCase()}` ||
129
+ `changeMePlease`, item.name || undefined);
130
+ }
131
+ else {
132
+ // Update user details as necessary, ensuring email uniqueness if attempting an update.
133
+ if (item.email &&
134
+ item.email !== userToReturn.email &&
135
+ !isEmpty(item.email) &&
136
+ !isUndefined(item.email)) {
137
+ const emailExists = await this.users.list([
138
+ Query.equal("email", item.email),
139
+ ]);
140
+ if (emailExists.users.length === 0) {
141
+ userToReturn = await this.users.updateEmail(userToReturn.$id, item.email);
142
+ }
143
+ else {
144
+ console.log("Email update skipped: Email already exists.");
145
+ }
146
+ }
147
+ if (item.password) {
148
+ userToReturn = await this.users.updatePassword(userToReturn.$id, item.password.toLowerCase());
149
+ }
150
+ if (item.name && item.name !== userToReturn.name) {
151
+ userToReturn = await this.users.updateName(userToReturn.$id, item.name);
152
+ }
153
+ if (item.phone &&
154
+ item.phone !== userToReturn.phone &&
155
+ item.phone.length < 15 &&
156
+ item.phone.startsWith("+") &&
157
+ (isUndefined(userToReturn.phone) || isEmpty(userToReturn.phone))) {
158
+ const userFoundWithPhone = await this.users.list([
159
+ Query.equal("phone", item.phone),
160
+ ]);
161
+ if (userFoundWithPhone.total === 0) {
162
+ userToReturn = await this.users.updatePhone(userToReturn.$id, item.phone);
163
+ }
164
+ }
165
+ }
166
+ if (item.$createdAt && item.$updatedAt) {
167
+ console.log("$createdAt and $updatedAt are not yet supported, sorry about that!");
168
+ }
169
+ if (item.labels && item.labels.length) {
170
+ userToReturn = await this.users.updateLabels(userToReturn.$id, item.labels);
171
+ }
172
+ if (item.prefs && Object.keys(item.prefs).length) {
173
+ await this.users.updatePrefs(userToReturn.$id, item.prefs);
174
+ userToReturn.prefs = item.prefs;
175
+ }
176
+ return userToReturn;
177
+ }
178
+ catch (error) {
179
+ return userToReturn;
180
+ }
181
+ }
182
+ async getUserIdByEmailOrPhone(email, phone) {
183
+ if (!email && !phone) {
184
+ return undefined;
185
+ }
186
+ if (email && phone) {
187
+ const foundUsersByEmail = await this.users.list([
188
+ // @ts-ignore
189
+ Query.or([Query.equal("email", email), Query.equal("phone", phone)]),
190
+ ]);
191
+ if (foundUsersByEmail.users.length > 0) {
192
+ return foundUsersByEmail.users[0]?.$id;
193
+ }
194
+ }
195
+ else if (email) {
196
+ const foundUsersByEmail = await this.users.list([
197
+ Query.equal("email", email),
198
+ ]);
199
+ if (foundUsersByEmail.users.length > 0) {
200
+ return foundUsersByEmail.users[0]?.$id;
201
+ }
202
+ else {
203
+ if (!phone) {
204
+ return undefined;
205
+ }
206
+ else {
207
+ const foundUsersByPhone = await this.users.list([
208
+ Query.equal("phone", phone),
209
+ ]);
210
+ if (foundUsersByPhone.users.length > 0) {
211
+ return foundUsersByPhone.users[0]?.$id;
212
+ }
213
+ else {
214
+ return undefined;
215
+ }
216
+ }
217
+ }
218
+ }
219
+ if (phone) {
220
+ const foundUsersByPhone = await this.users.list([
221
+ Query.equal("phone", phone),
222
+ ]);
223
+ if (foundUsersByPhone.users.length > 0) {
224
+ return foundUsersByPhone.users[0]?.$id;
225
+ }
226
+ else {
227
+ return undefined;
228
+ }
229
+ }
230
+ }
231
+ transferUsersBetweenDbsLocalToRemote = async (endpoint, projectId, apiKey) => {
232
+ const localUsers = this.users;
233
+ const client = getAppwriteClient(endpoint, projectId, apiKey);
234
+ const remoteUsers = new Users(client);
235
+ let fromUsers = await localUsers.list([Query.limit(50)]);
236
+ if (fromUsers.users.length === 0) {
237
+ console.log(`No users found`);
238
+ return;
239
+ }
240
+ else if (fromUsers.users.length < 50) {
241
+ console.log(`Transferring ${fromUsers.users.length} users to remote`);
242
+ const batchedPromises = fromUsers.users.map((user) => {
243
+ return tryAwaitWithRetry(async () => {
244
+ const toCreateObject = {
245
+ ...user,
246
+ };
247
+ delete toCreateObject.$id;
248
+ delete toCreateObject.$createdAt;
249
+ delete toCreateObject.$updatedAt;
250
+ await remoteUsers.create(user.$id, user.email, user.phone, user.password, user.name);
251
+ });
252
+ });
253
+ await Promise.all(batchedPromises);
254
+ }
255
+ else {
256
+ while (fromUsers.users.length === 50) {
257
+ fromUsers = await localUsers.list([
258
+ Query.limit(50),
259
+ Query.cursorAfter(fromUsers.users[fromUsers.users.length - 1].$id),
260
+ ]);
261
+ const batchedPromises = fromUsers.users.map((user) => {
262
+ return tryAwaitWithRetry(async () => {
263
+ const toCreateObject = {
264
+ ...user,
265
+ };
266
+ delete toCreateObject.$id;
267
+ delete toCreateObject.$createdAt;
268
+ delete toCreateObject.$updatedAt;
269
+ await remoteUsers.create(user.$id, user.email, user.phone, user.password, user.name);
270
+ });
271
+ });
272
+ await Promise.all(batchedPromises);
273
+ }
274
+ }
275
+ };
276
+ }
@@ -0,0 +1 @@
1
+ export declare function migrateConfig(workingDir: string): Promise<void>;
@@ -0,0 +1,262 @@
1
+ import { promises as fs } from "fs";
2
+ import path from "path";
3
+ import { existsSync } from "fs";
4
+ import { MessageFormatter } from "../shared/messageFormatter.js";
5
+ import { ConfirmationDialogs } from "../shared/confirmationDialogs.js";
6
+ import yaml from "js-yaml";
7
+ export async function migrateConfig(workingDir) {
8
+ try {
9
+ // Look for appwriteConfig.ts files in the working directory and subdirectories
10
+ const configFiles = await findAppwriteConfigFiles(workingDir);
11
+ if (configFiles.length === 0) {
12
+ MessageFormatter.info("No appwriteConfig.ts files found to migrate", { prefix: "Migration" });
13
+ return;
14
+ }
15
+ MessageFormatter.info(`Found ${configFiles.length} appwriteConfig.ts file(s) to migrate`, { prefix: "Migration" });
16
+ for (const configFile of configFiles) {
17
+ await migrateConfigFile(configFile, workingDir);
18
+ }
19
+ MessageFormatter.success("Migration completed successfully", { prefix: "Migration" });
20
+ }
21
+ catch (error) {
22
+ MessageFormatter.error("Migration failed", error instanceof Error ? error : new Error(String(error)), { prefix: "Migration" });
23
+ throw error;
24
+ }
25
+ }
26
+ async function findAppwriteConfigFiles(dir) {
27
+ const configFiles = [];
28
+ const checkDir = async (currentDir) => {
29
+ try {
30
+ const entries = await fs.readdir(currentDir, { withFileTypes: true });
31
+ for (const entry of entries) {
32
+ const fullPath = path.join(currentDir, entry.name);
33
+ if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {
34
+ await checkDir(fullPath);
35
+ }
36
+ else if (entry.isFile() && entry.name === 'appwriteConfig.ts') {
37
+ configFiles.push(fullPath);
38
+ }
39
+ }
40
+ }
41
+ catch (error) {
42
+ // Ignore directory access errors
43
+ }
44
+ };
45
+ await checkDir(dir);
46
+ return configFiles;
47
+ }
48
+ async function migrateConfigFile(configFilePath, workingDir) {
49
+ const configDir = path.dirname(configFilePath);
50
+ const appwriteDir = path.join(path.dirname(configDir), '.appwrite');
51
+ MessageFormatter.info(`Migrating ${path.relative(workingDir, configFilePath)}`, { prefix: "Migration" });
52
+ // Check if .appwrite directory already exists
53
+ if (existsSync(appwriteDir)) {
54
+ const shouldOverwrite = await ConfirmationDialogs.confirmOverwrite(`.appwrite directory already exists at ${path.relative(workingDir, appwriteDir)}`);
55
+ if (!shouldOverwrite) {
56
+ MessageFormatter.info("Skipping migration for this config", { prefix: "Migration" });
57
+ return;
58
+ }
59
+ }
60
+ // Load and parse the TypeScript config
61
+ const config = await parseTypeScriptConfig(configFilePath);
62
+ // Create .appwrite directory
63
+ await fs.mkdir(appwriteDir, { recursive: true });
64
+ // Convert config to YAML and save
65
+ const yamlConfig = convertToYAMLConfig(config);
66
+ const yamlContent = yaml.dump(yamlConfig, {
67
+ indent: 2,
68
+ lineWidth: 120,
69
+ noRefs: true
70
+ });
71
+ await fs.writeFile(path.join(appwriteDir, 'appwriteConfig.yaml'), yamlContent);
72
+ // Copy all directories except collections and schemas (we handle collections separately, skip schemas entirely)
73
+ const entries = await fs.readdir(configDir, { withFileTypes: true });
74
+ for (const entry of entries) {
75
+ if (entry.isDirectory() && entry.name !== 'collections' && entry.name !== 'schemas') {
76
+ const sourcePath = path.join(configDir, entry.name);
77
+ const targetPath = path.join(appwriteDir, entry.name);
78
+ await fs.cp(sourcePath, targetPath, { recursive: true });
79
+ MessageFormatter.info(`Copied ${entry.name}/ to .appwrite/${entry.name}/`, { prefix: "Migration" });
80
+ }
81
+ }
82
+ // Convert TypeScript collections to YAML collections
83
+ const collectionsPath = path.join(configDir, 'collections');
84
+ if (existsSync(collectionsPath)) {
85
+ const targetCollectionsPath = path.join(appwriteDir, 'collections');
86
+ await fs.mkdir(targetCollectionsPath, { recursive: true });
87
+ const collectionFiles = await fs.readdir(collectionsPath);
88
+ for (const file of collectionFiles) {
89
+ if (file.endsWith('.ts')) {
90
+ await convertCollectionToYaml(path.join(collectionsPath, file), targetCollectionsPath);
91
+ }
92
+ }
93
+ MessageFormatter.info(`Converted TypeScript collections to YAML in .appwrite/collections/`, { prefix: "Migration" });
94
+ }
95
+ // Keep original config file in place (no backup needed since we're not deleting it)
96
+ MessageFormatter.success(`Migration completed for ${path.relative(workingDir, configFilePath)}`, { prefix: "Migration" });
97
+ }
98
+ async function parseTypeScriptConfig(configFilePath) {
99
+ try {
100
+ // Use tsx to import the TypeScript config file directly
101
+ const { register } = await import("tsx/esm/api");
102
+ const { pathToFileURL } = await import("node:url");
103
+ const unregister = register();
104
+ try {
105
+ const configUrl = pathToFileURL(configFilePath).href;
106
+ const configModule = await import(configUrl);
107
+ const config = configModule.default?.default || configModule.default || configModule;
108
+ if (!config) {
109
+ throw new Error("Failed to load config from TypeScript file");
110
+ }
111
+ return config;
112
+ }
113
+ finally {
114
+ unregister();
115
+ }
116
+ }
117
+ catch (error) {
118
+ MessageFormatter.error("Could not load TypeScript config", error instanceof Error ? error : new Error(String(error)), { prefix: "Migration" });
119
+ throw new Error('Failed to load TypeScript configuration file. Please ensure it exports a valid config object.');
120
+ }
121
+ }
122
+ function convertToYAMLConfig(config) {
123
+ // Convert the config to the nested YAML structure
124
+ const yamlConfig = {
125
+ appwrite: {
126
+ endpoint: config.appwriteEndpoint,
127
+ project: config.appwriteProject,
128
+ key: config.appwriteKey
129
+ },
130
+ logging: {
131
+ enabled: config.logging?.enabled ?? false,
132
+ level: config.logging?.level ?? "info",
133
+ console: config.logging?.console ?? false,
134
+ logDirectory: "./logs"
135
+ },
136
+ backups: {
137
+ enabled: config.enableBackups ?? false,
138
+ interval: config.backupInterval ?? 3600,
139
+ retention: config.backupRetention ?? 30,
140
+ cleanup: config.enableBackupCleanup ?? false
141
+ },
142
+ data: {
143
+ enableMockData: config.enableMockData ?? false,
144
+ documentBucketId: config.documentBucketId ?? "documents",
145
+ usersCollectionName: config.usersCollectionName ?? "Users",
146
+ importDirectory: "importData"
147
+ },
148
+ schemas: {
149
+ outputDirectory: "schemas",
150
+ yamlSchemaDirectory: ".yaml_schemas"
151
+ },
152
+ migrations: {
153
+ enabled: true
154
+ },
155
+ databases: (config.databases || []).map(db => ({
156
+ id: db.$id,
157
+ name: db.name,
158
+ collections: [] // Collections will be handled separately
159
+ })),
160
+ buckets: (config.buckets || []).map(bucket => ({
161
+ id: bucket.$id,
162
+ name: bucket.name,
163
+ permissions: bucket.$permissions?.map((p) => ({
164
+ permission: p.permission,
165
+ target: p.target
166
+ })) || [],
167
+ fileSecurity: bucket.fileSecurity ?? false,
168
+ enabled: bucket.enabled ?? true,
169
+ maximumFileSize: bucket.maximumFileSize ?? 30000000,
170
+ allowedFileExtensions: bucket.allowedFileExtensions || [],
171
+ compression: bucket.compression || "gzip",
172
+ encryption: bucket.encryption ?? false,
173
+ antivirus: bucket.antivirus ?? false
174
+ })),
175
+ functions: (config.functions || []).map((func) => ({
176
+ id: func.$id,
177
+ name: func.name,
178
+ runtime: func.runtime,
179
+ execute: func.execute || [],
180
+ events: func.events || [],
181
+ schedule: func.schedule || "",
182
+ timeout: func.timeout ?? 15,
183
+ enabled: func.enabled ?? true,
184
+ logging: func.logging ?? false,
185
+ entrypoint: func.entrypoint || "src/main.js",
186
+ commands: func.commands || "",
187
+ scopes: func.scopes || [],
188
+ specification: func.specification || "s-1vcpu-512mb"
189
+ }))
190
+ };
191
+ return yamlConfig;
192
+ }
193
+ async function convertCollectionToYaml(tsFilePath, targetDir) {
194
+ try {
195
+ // Load the TypeScript collection using tsx
196
+ const { register } = await import("tsx/esm/api");
197
+ const { pathToFileURL } = await import("node:url");
198
+ const unregister = register();
199
+ try {
200
+ const configUrl = pathToFileURL(tsFilePath).href;
201
+ const collectionModule = await import(configUrl);
202
+ const collection = collectionModule.default?.default || collectionModule.default || collectionModule;
203
+ if (!collection) {
204
+ throw new Error("Failed to load collection from TypeScript file");
205
+ }
206
+ // Convert collection to YAML format
207
+ const yamlCollection = {
208
+ name: collection.name,
209
+ id: collection.$id,
210
+ documentSecurity: collection.documentSecurity ?? false,
211
+ enabled: collection.enabled ?? true,
212
+ permissions: (collection.permissions || collection.$permissions || []).map((p) => ({
213
+ permission: p.permission,
214
+ target: p.target
215
+ })),
216
+ attributes: (collection.attributes || []).map((attr) => ({
217
+ key: attr.key,
218
+ type: attr.type,
219
+ size: attr.size,
220
+ required: attr.required ?? false,
221
+ array: attr.array,
222
+ default: attr.xdefault || attr.default,
223
+ description: attr.description,
224
+ min: attr.min,
225
+ max: attr.max,
226
+ elements: attr.elements,
227
+ relatedCollection: attr.relatedCollection,
228
+ relationType: attr.relationType,
229
+ twoWay: attr.twoWay,
230
+ twoWayKey: attr.twoWayKey,
231
+ onDelete: attr.onDelete,
232
+ side: attr.side
233
+ })),
234
+ indexes: (collection.indexes || []).map((idx) => ({
235
+ key: idx.key,
236
+ type: idx.type,
237
+ attributes: idx.attributes,
238
+ orders: idx.orders
239
+ })),
240
+ importDefs: collection.importDefs || []
241
+ };
242
+ // Remove undefined values
243
+ const cleanYamlCollection = JSON.parse(JSON.stringify(yamlCollection, (key, value) => value === undefined ? undefined : value));
244
+ // Write YAML file
245
+ const fileName = path.basename(tsFilePath, '.ts') + '.yaml';
246
+ const targetPath = path.join(targetDir, fileName);
247
+ const yamlContent = yaml.dump(cleanYamlCollection, {
248
+ indent: 2,
249
+ lineWidth: 120,
250
+ noRefs: true
251
+ });
252
+ await fs.writeFile(targetPath, yamlContent);
253
+ MessageFormatter.info(`Converted ${path.basename(tsFilePath)} to ${fileName}`, { prefix: "Migration" });
254
+ }
255
+ finally {
256
+ unregister();
257
+ }
258
+ }
259
+ catch (error) {
260
+ MessageFormatter.error(`Failed to convert collection ${path.basename(tsFilePath)}`, error instanceof Error ? error : new Error(String(error)), { prefix: "Migration" });
261
+ }
262
+ }
@@ -0,0 +1,46 @@
1
+ import { type AttributeMappings } from "appwrite-utils";
2
+ /**
3
+ * Deeply converts all properties of an object (or array) to strings.
4
+ * @param data The input data to convert.
5
+ * @returns The data with all its properties converted to strings.
6
+ */
7
+ export declare const deepAnyToString: (data: any) => any;
8
+ /**
9
+ * Performs a deep conversion of all values in a nested structure to the specified type.
10
+ * Uses a conversion function like anyToString, anyToNumber, etc.
11
+ * @param data The data to convert.
12
+ * @param convertFn The conversion function to apply.
13
+ * @returns The converted data.
14
+ */
15
+ export declare const deepConvert: <T>(data: any, convertFn: (value: any) => T) => any;
16
+ /**
17
+ * Converts an entire object's properties to different types based on a provided schema.
18
+ * @param obj The object to convert.
19
+ * @param schema A mapping of object keys to conversion functions.
20
+ * @returns The converted object.
21
+ */
22
+ export declare const convertObjectBySchema: (obj: Record<string, any>, schema: Record<string, (value: any) => any>) => Record<string, any>;
23
+ /**
24
+ * Converts the keys of an object based on a provided attributeMappings.
25
+ * Each key in the object is checked against attributeMappings; if a matching entry is found,
26
+ * the key is renamed to the targetKey specified in attributeMappings.
27
+ *
28
+ * @param obj The object to convert.
29
+ * @param attributeMappings The attributeMappings defining how keys in the object should be converted.
30
+ * @returns The converted object with keys renamed according to attributeMappings.
31
+ */
32
+ export declare const convertObjectByAttributeMappings: (obj: Record<string, any>, attributeMappings: AttributeMappings) => Record<string, any>;
33
+ /**
34
+ * Ensures data conversion without mutating the original input.
35
+ * @param data The data to convert.
36
+ * @param convertFn The conversion function to apply.
37
+ * @returns The converted data.
38
+ */
39
+ export declare const immutableConvert: <T>(data: any, convertFn: (value: any) => T) => T;
40
+ /**
41
+ * Validates a string against a regular expression and returns the string if valid, or null.
42
+ * @param value The string to validate.
43
+ * @param pattern The regex pattern to validate against.
44
+ * @returns The original string if valid, otherwise null.
45
+ */
46
+ export declare const validateString: (value: string, pattern: RegExp) => string | null;