appwrite-utils-cli 0.10.86 → 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.
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 +260 -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 +156 -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 +377 -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 +212 -0
  160. package/src/utils/loadConfigs.ts +414 -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,156 @@
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(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
+ // Read and parse the TypeScript config
61
+ const configContent = await fs.readFile(configFilePath, 'utf8');
62
+ const config = await parseTypeScriptConfig(configContent);
63
+ // Create .appwrite directory
64
+ await fs.mkdir(appwriteDir, { recursive: true });
65
+ // Convert config to YAML and save
66
+ const yamlConfig = convertToYAMLConfig(config);
67
+ const yamlContent = yaml.dump(yamlConfig, {
68
+ indent: 2,
69
+ lineWidth: 120,
70
+ noRefs: true
71
+ });
72
+ await fs.writeFile(path.join(appwriteDir, 'appwriteConfig.yaml'), yamlContent);
73
+ // Move related directories
74
+ const foldersToMove = ['collections', 'schemas', 'importData', 'functions'];
75
+ for (const folder of foldersToMove) {
76
+ const sourcePath = path.join(configDir, folder);
77
+ const targetPath = path.join(appwriteDir, folder);
78
+ if (existsSync(sourcePath)) {
79
+ await fs.rename(sourcePath, targetPath);
80
+ MessageFormatter.info(`Moved ${folder}/ to .appwrite/${folder}/`, { prefix: "Migration" });
81
+ }
82
+ }
83
+ // Backup original config file
84
+ const backupPath = configFilePath + '.backup';
85
+ await fs.copyFile(configFilePath, backupPath);
86
+ MessageFormatter.info(`Created backup at ${path.relative(workingDir, backupPath)}`, { prefix: "Migration" });
87
+ // Optionally remove original config file
88
+ const shouldRemoveOriginal = await ConfirmationDialogs.confirmRemoval(`Remove original ${path.relative(workingDir, configFilePath)}?`);
89
+ if (shouldRemoveOriginal) {
90
+ await fs.unlink(configFilePath);
91
+ MessageFormatter.info(`Removed original ${path.relative(workingDir, configFilePath)}`, { prefix: "Migration" });
92
+ }
93
+ MessageFormatter.success(`Migration completed for ${path.relative(workingDir, configFilePath)}`, { prefix: "Migration" });
94
+ }
95
+ async function parseTypeScriptConfig(content) {
96
+ // This is a simplified parser - in a real implementation, you might want to use a proper TypeScript parser
97
+ // For now, we'll use a regex-based approach to extract the config object
98
+ try {
99
+ // Remove comments and imports
100
+ const cleanContent = content
101
+ .replace(/\/\*[\s\S]*?\*\//g, '') // Remove block comments
102
+ .replace(/\/\/.*$/gm, '') // Remove line comments
103
+ .replace(/^import.*$/gm, '') // Remove imports
104
+ .replace(/^export.*$/gm, ''); // Remove exports
105
+ // Find the config object
106
+ const configMatch = cleanContent.match(/const\s+\w+\s*:\s*\w+\s*=\s*({[\s\S]*?});/);
107
+ if (!configMatch) {
108
+ throw new Error('Could not find config object in TypeScript file');
109
+ }
110
+ // Convert to JSON-like format and parse
111
+ let configStr = configMatch[1];
112
+ // Replace TypeScript-specific syntax
113
+ configStr = configStr
114
+ .replace(/(\w+):/g, '"$1":') // Quote property names
115
+ .replace(/'/g, '"') // Convert single quotes to double quotes
116
+ .replace(/,(\s*[}\]])/g, '$1'); // Remove trailing commas
117
+ const config = JSON.parse(configStr);
118
+ return config;
119
+ }
120
+ catch (error) {
121
+ MessageFormatter.error("Could not parse TypeScript config", error instanceof Error ? error : new Error(String(error)), { prefix: "Migration" });
122
+ throw new Error('Failed to parse TypeScript configuration file. Please ensure it follows standard format.');
123
+ }
124
+ }
125
+ function convertToYAMLConfig(config) {
126
+ // Convert the config to YAML-friendly format
127
+ const yamlConfig = {
128
+ appwriteEndpoint: config.appwriteEndpoint,
129
+ appwriteProject: config.appwriteProject,
130
+ appwriteKey: config.appwriteKey,
131
+ databases: config.databases || [],
132
+ buckets: config.buckets || [],
133
+ };
134
+ // Add optional properties if they exist
135
+ if (config.enableBackups !== undefined)
136
+ yamlConfig.enableBackups = config.enableBackups;
137
+ if (config.backupInterval !== undefined)
138
+ yamlConfig.backupInterval = config.backupInterval;
139
+ if (config.backupRetention !== undefined)
140
+ yamlConfig.backupRetention = config.backupRetention;
141
+ if (config.enableBackupCleanup !== undefined)
142
+ yamlConfig.enableBackupCleanup = config.enableBackupCleanup;
143
+ if (config.enableMockData !== undefined)
144
+ yamlConfig.enableMockData = config.enableMockData;
145
+ if (config.documentBucketId !== undefined)
146
+ yamlConfig.documentBucketId = config.documentBucketId;
147
+ if (config.usersCollectionName !== undefined)
148
+ yamlConfig.usersCollectionName = config.usersCollectionName;
149
+ // Copy any additional properties
150
+ for (const [key, value] of Object.entries(config)) {
151
+ if (!(key in yamlConfig)) {
152
+ yamlConfig[key] = value;
153
+ }
154
+ }
155
+ return yamlConfig;
156
+ }
@@ -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;
@@ -0,0 +1,139 @@
1
+ import { converterFunctions } from "appwrite-utils";
2
+ import { cloneDeep, isPlainObject } from "es-toolkit";
3
+ /**
4
+ * Deeply converts all properties of an object (or array) to strings.
5
+ * @param data The input data to convert.
6
+ * @returns The data with all its properties converted to strings.
7
+ */
8
+ export const deepAnyToString = (data) => {
9
+ if (Array.isArray(data)) {
10
+ return data.map((item) => deepAnyToString(item));
11
+ }
12
+ else if (isPlainObject(data)) {
13
+ return Object.keys(data).reduce((acc, key) => {
14
+ acc[key] = deepAnyToString(data[key]);
15
+ return acc;
16
+ }, {});
17
+ }
18
+ else {
19
+ return converterFunctions.anyToString(data);
20
+ }
21
+ };
22
+ /**
23
+ * Performs a deep conversion of all values in a nested structure to the specified type.
24
+ * Uses a conversion function like anyToString, anyToNumber, etc.
25
+ * @param data The data to convert.
26
+ * @param convertFn The conversion function to apply.
27
+ * @returns The converted data.
28
+ */
29
+ export const deepConvert = (data, convertFn) => {
30
+ if (Array.isArray(data)) {
31
+ return data.map((item) => deepConvert(item, convertFn));
32
+ }
33
+ else if (isPlainObject(data)) {
34
+ return Object.keys(data).reduce((acc, key) => {
35
+ acc[key] = deepConvert(data[key], convertFn);
36
+ return acc;
37
+ }, {});
38
+ }
39
+ else {
40
+ return convertFn(data);
41
+ }
42
+ };
43
+ /**
44
+ * Converts an entire object's properties to different types based on a provided schema.
45
+ * @param obj The object to convert.
46
+ * @param schema A mapping of object keys to conversion functions.
47
+ * @returns The converted object.
48
+ */
49
+ export const convertObjectBySchema = (obj, schema) => {
50
+ return Object.keys(obj).reduce((acc, key) => {
51
+ const convertFn = schema[key];
52
+ acc[key] = convertFn ? convertFn(obj[key]) : obj[key];
53
+ return acc;
54
+ }, {});
55
+ };
56
+ /**
57
+ * Converts the keys of an object based on a provided attributeMappings.
58
+ * Each key in the object is checked against attributeMappings; if a matching entry is found,
59
+ * the key is renamed to the targetKey specified in attributeMappings.
60
+ *
61
+ * @param obj The object to convert.
62
+ * @param attributeMappings The attributeMappings defining how keys in the object should be converted.
63
+ * @returns The converted object with keys renamed according to attributeMappings.
64
+ */
65
+ export const convertObjectByAttributeMappings = (obj, attributeMappings) => {
66
+ const result = {};
67
+ // Correctly handle [any] notation by mapping or aggregating over all elements or keys
68
+ const resolveValue = (obj, path) => {
69
+ const parts = path.split(".");
70
+ let current = obj;
71
+ for (let i = 0; i < parts.length; i++) {
72
+ if (parts[i] === "[any]") {
73
+ if (Array.isArray(current)) {
74
+ // If current is an array, apply resolution to each item
75
+ return current.map((item) => resolveValue(item, parts.slice(i + 1).join(".")));
76
+ }
77
+ else if (typeof current === "object" && current !== null) {
78
+ // If current is an object, aggregate values from all keys
79
+ return Object.values(current).map((value) => resolveValue(value, parts.slice(i + 1).join(".")));
80
+ }
81
+ }
82
+ else {
83
+ current = current[parts[i]];
84
+ if (current === undefined)
85
+ return undefined;
86
+ }
87
+ }
88
+ return current;
89
+ };
90
+ for (const mapping of attributeMappings) {
91
+ if (mapping.valueToSet !== undefined) {
92
+ result[mapping.targetKey] = mapping.valueToSet;
93
+ }
94
+ else if (Array.isArray(mapping.oldKeys)) {
95
+ // Collect and flatten values from multiple oldKeys
96
+ const values = mapping.oldKeys
97
+ .map((oldKey) => resolveValue(obj, oldKey))
98
+ .flat(Infinity);
99
+ if (values.length > 0) {
100
+ result[mapping.targetKey] = values.filter((value) => value !== undefined);
101
+ }
102
+ else {
103
+ result[mapping.targetKey] = null;
104
+ }
105
+ }
106
+ else if (mapping.oldKey) {
107
+ // Resolve single oldKey
108
+ const value = resolveValue(obj, mapping.oldKey);
109
+ if (value !== undefined) {
110
+ result[mapping.targetKey] = Array.isArray(value)
111
+ ? value.flat(Infinity)
112
+ : value;
113
+ }
114
+ else {
115
+ result[mapping.targetKey] = value ? value : null;
116
+ }
117
+ }
118
+ }
119
+ return result;
120
+ };
121
+ /**
122
+ * Ensures data conversion without mutating the original input.
123
+ * @param data The data to convert.
124
+ * @param convertFn The conversion function to apply.
125
+ * @returns The converted data.
126
+ */
127
+ export const immutableConvert = (data, convertFn) => {
128
+ const clonedData = cloneDeep(data);
129
+ return convertFn(clonedData);
130
+ };
131
+ /**
132
+ * Validates a string against a regular expression and returns the string if valid, or null.
133
+ * @param value The string to validate.
134
+ * @param pattern The regex pattern to validate against.
135
+ * @returns The original string if valid, otherwise null.
136
+ */
137
+ export const validateString = (value, pattern) => {
138
+ return pattern.test(value) ? value : null;
139
+ };
@@ -1,14 +1,25 @@
1
1
  import { type AppwriteConfig } from "appwrite-utils";
2
2
  /**
3
- * Recursively searches for a file named 'appwriteConfig.ts' starting from the given directory.
3
+ * Recursively searches for configuration files starting from the given directory.
4
+ * Priority: 1) YAML configs in .appwrite directories, 2) appwriteConfig.ts files in subdirectories
4
5
  * @param dir The directory to start the search from.
5
- * @returns The path to the file if found, or null if not found.
6
+ * @returns The directory path where the config was found, suitable for passing to loadConfig().
6
7
  */
7
8
  export declare const findAppwriteConfig: (dir: string) => string | null;
9
+ /**
10
+ * Loads the Appwrite configuration and returns both config and the path where it was found.
11
+ * @param configDir The directory to search for config files.
12
+ * @returns Object containing the config and the actual path where it was found.
13
+ */
14
+ export declare const loadConfigWithPath: (configDir: string) => Promise<{
15
+ config: AppwriteConfig;
16
+ actualConfigPath: string;
17
+ }>;
8
18
  /**
9
19
  * Loads the Appwrite configuration and all collection configurations from a specified directory.
10
- * @param configDir The directory containing the appwriteConfig.ts and collections folder.
20
+ * Supports both YAML and TypeScript config formats with backward compatibility.
21
+ * @param configDir The directory containing the config file and collections folder.
11
22
  * @returns The loaded Appwrite configuration including collections.
12
23
  */
13
24
  export declare const loadConfig: (configDir: string) => Promise<AppwriteConfig>;
14
- export declare const findFunctionsDir: (dir: string) => string | null;
25
+ export declare const findFunctionsDir: (dir: string, depth?: number) => string | null;