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,363 @@
1
+ import pLimit from "p-limit";
2
+ import { logger } from "../../shared/logging.js";
3
+
4
+ export interface RateLimitConfig {
5
+ // Data operations
6
+ dataInsertion?: number;
7
+ dataUpdate?: number;
8
+ dataQuery?: number;
9
+
10
+ // File operations
11
+ fileUpload?: number;
12
+ fileDownload?: number;
13
+
14
+ // Validation operations
15
+ validation?: number;
16
+
17
+ // Relationship operations
18
+ relationshipResolution?: number;
19
+
20
+ // User operations
21
+ userCreation?: number;
22
+ userUpdate?: number;
23
+
24
+ // API operations
25
+ apiCalls?: number;
26
+ }
27
+
28
+ /**
29
+ * Manages rate limiting across the import system.
30
+ * Provides configurable p-limit instances for different operation types.
31
+ * Builds on existing p-limit usage in attributeManager and other components.
32
+ */
33
+ export class RateLimitManager {
34
+ private limits: Map<string, any> = new Map();
35
+ private config: Required<RateLimitConfig>;
36
+
37
+ // Default rate limits based on existing usage and best practices
38
+ private static readonly DEFAULT_LIMITS: Required<RateLimitConfig> = {
39
+ dataInsertion: 5, // Conservative for database writes
40
+ dataUpdate: 8, // Slightly higher for updates
41
+ dataQuery: 25, // Higher for read operations (from existing queryLimit)
42
+ fileUpload: 2, // Very conservative for file uploads
43
+ fileDownload: 3, // Slightly higher for downloads
44
+ validation: 10, // Higher for validation operations
45
+ relationshipResolution: 8, // Moderate for relationship operations
46
+ userCreation: 3, // Conservative for user creation
47
+ userUpdate: 5, // Moderate for user updates
48
+ apiCalls: 15, // General API call limit
49
+ };
50
+
51
+ constructor(config?: Partial<RateLimitConfig>) {
52
+ this.config = { ...RateLimitManager.DEFAULT_LIMITS, ...config };
53
+ this.initializeLimits();
54
+ }
55
+
56
+ /**
57
+ * Initializes p-limit instances for all operation types.
58
+ */
59
+ private initializeLimits(): void {
60
+ for (const [operation, limit] of Object.entries(this.config)) {
61
+ this.limits.set(operation, pLimit(limit));
62
+ logger.debug(`Rate limit for ${operation}: ${limit} concurrent operations`);
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Gets the rate limiter for data insertion operations.
68
+ */
69
+ get dataInsertion() {
70
+ return this.limits.get('dataInsertion');
71
+ }
72
+
73
+ /**
74
+ * Gets the rate limiter for data update operations.
75
+ */
76
+ get dataUpdate() {
77
+ return this.limits.get('dataUpdate');
78
+ }
79
+
80
+ /**
81
+ * Gets the rate limiter for data query operations.
82
+ */
83
+ get dataQuery() {
84
+ return this.limits.get('dataQuery');
85
+ }
86
+
87
+ /**
88
+ * Gets the rate limiter for file upload operations.
89
+ */
90
+ get fileUpload() {
91
+ return this.limits.get('fileUpload');
92
+ }
93
+
94
+ /**
95
+ * Gets the rate limiter for file download operations.
96
+ */
97
+ get fileDownload() {
98
+ return this.limits.get('fileDownload');
99
+ }
100
+
101
+ /**
102
+ * Gets the rate limiter for validation operations.
103
+ */
104
+ get validation() {
105
+ return this.limits.get('validation');
106
+ }
107
+
108
+ /**
109
+ * Gets the rate limiter for relationship resolution operations.
110
+ */
111
+ get relationshipResolution() {
112
+ return this.limits.get('relationshipResolution');
113
+ }
114
+
115
+ /**
116
+ * Gets the rate limiter for user creation operations.
117
+ */
118
+ get userCreation() {
119
+ return this.limits.get('userCreation');
120
+ }
121
+
122
+ /**
123
+ * Gets the rate limiter for user update operations.
124
+ */
125
+ get userUpdate() {
126
+ return this.limits.get('userUpdate');
127
+ }
128
+
129
+ /**
130
+ * Gets the rate limiter for general API calls.
131
+ */
132
+ get apiCalls() {
133
+ return this.limits.get('apiCalls');
134
+ }
135
+
136
+ /**
137
+ * Gets a rate limiter by operation type name.
138
+ *
139
+ * @param operationType - The type of operation
140
+ * @returns The p-limit instance for the operation type
141
+ */
142
+ getLimiter(operationType: keyof RateLimitConfig): any {
143
+ const limiter = this.limits.get(operationType);
144
+ if (!limiter) {
145
+ logger.warn(`No rate limiter found for operation type: ${operationType}. Using default API calls limiter.`);
146
+ return this.limits.get('apiCalls');
147
+ }
148
+ return limiter;
149
+ }
150
+
151
+ /**
152
+ * Updates the rate limit for a specific operation type.
153
+ *
154
+ * @param operationType - The operation type to update
155
+ * @param newLimit - The new concurrent operation limit
156
+ */
157
+ updateLimit(operationType: keyof RateLimitConfig, newLimit: number): void {
158
+ this.config[operationType] = newLimit;
159
+ this.limits.set(operationType, pLimit(newLimit));
160
+ logger.info(`Updated rate limit for ${operationType}: ${newLimit} concurrent operations`);
161
+ }
162
+
163
+ /**
164
+ * Updates multiple rate limits at once.
165
+ *
166
+ * @param newConfig - Partial configuration with new limits
167
+ */
168
+ updateLimits(newConfig: Partial<RateLimitConfig>): void {
169
+ for (const [operation, limit] of Object.entries(newConfig)) {
170
+ if (limit !== undefined) {
171
+ this.updateLimit(operation as keyof RateLimitConfig, limit);
172
+ }
173
+ }
174
+ }
175
+
176
+ /**
177
+ * Gets the current configuration.
178
+ */
179
+ getConfig(): Required<RateLimitConfig> {
180
+ return { ...this.config };
181
+ }
182
+
183
+ /**
184
+ * Gets statistics about pending and active operations.
185
+ * Useful for monitoring and debugging rate limiting.
186
+ */
187
+ getStatistics(): { [operationType: string]: { pending: number; active: number } } {
188
+ const stats: { [operationType: string]: { pending: number; active: number } } = {};
189
+
190
+ for (const [operationType, limiter] of this.limits.entries()) {
191
+ stats[operationType] = {
192
+ pending: limiter.pendingCount,
193
+ active: limiter.activeCount,
194
+ };
195
+ }
196
+
197
+ return stats;
198
+ }
199
+
200
+ /**
201
+ * Waits for all pending operations to complete.
202
+ * Useful for graceful shutdown or testing.
203
+ *
204
+ * @param timeout - Maximum time to wait in milliseconds (default: 30 seconds)
205
+ */
206
+ async waitForCompletion(timeout: number = 30000): Promise<void> {
207
+ const startTime = Date.now();
208
+
209
+ while (Date.now() - startTime < timeout) {
210
+ const stats = this.getStatistics();
211
+ const hasPendingOperations = Object.values(stats).some(
212
+ stat => stat.pending > 0 || stat.active > 0
213
+ );
214
+
215
+ if (!hasPendingOperations) {
216
+ logger.info("All rate-limited operations completed");
217
+ return;
218
+ }
219
+
220
+ // Log current status
221
+ const pendingOperations = Object.entries(stats)
222
+ .filter(([_, stat]) => stat.pending > 0 || stat.active > 0)
223
+ .map(([type, stat]) => `${type}: ${stat.pending} pending, ${stat.active} active`)
224
+ .join(", ");
225
+
226
+ logger.debug(`Waiting for operations to complete: ${pendingOperations}`);
227
+
228
+ // Wait a bit before checking again
229
+ await new Promise(resolve => setTimeout(resolve, 1000));
230
+ }
231
+
232
+ logger.warn(`Timeout waiting for rate-limited operations to complete after ${timeout}ms`);
233
+ }
234
+
235
+ /**
236
+ * Creates a rate limiter with automatic retry logic.
237
+ * Combines p-limit with retry functionality for robust operation handling.
238
+ *
239
+ * @param operationType - The operation type to get the limiter for
240
+ * @param maxRetries - Maximum number of retries (default: 3)
241
+ * @param retryDelay - Delay between retries in milliseconds (default: 1000)
242
+ */
243
+ createRetryLimiter(
244
+ operationType: keyof RateLimitConfig,
245
+ maxRetries: number = 3,
246
+ retryDelay: number = 1000
247
+ ) {
248
+ const limiter = this.getLimiter(operationType);
249
+
250
+ return async <T>(operation: () => Promise<T>): Promise<T> => {
251
+ return limiter(async () => {
252
+ let lastError: Error | undefined;
253
+
254
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
255
+ try {
256
+ return await operation();
257
+ } catch (error) {
258
+ lastError = error as Error;
259
+
260
+ if (attempt < maxRetries) {
261
+ logger.warn(
262
+ `Operation failed (attempt ${attempt + 1}/${maxRetries + 1}): ${lastError.message}. Retrying in ${retryDelay}ms...`
263
+ );
264
+ await new Promise(resolve => setTimeout(resolve, retryDelay));
265
+ }
266
+ }
267
+ }
268
+
269
+ // If we get here, all retries failed
270
+ logger.error(`Operation failed after ${maxRetries + 1} attempts: ${lastError?.message}`);
271
+ throw lastError;
272
+ });
273
+ };
274
+ }
275
+
276
+ /**
277
+ * Adjusts rate limits based on API response times and error rates.
278
+ * Implements adaptive rate limiting for optimal performance.
279
+ *
280
+ * @param operationType - The operation type to adjust
281
+ * @param responseTime - Average response time in milliseconds
282
+ * @param errorRate - Error rate as a percentage (0-100)
283
+ */
284
+ adaptiveAdjust(operationType: keyof RateLimitConfig, responseTime: number, errorRate: number): void {
285
+ const currentLimit = this.config[operationType];
286
+ let newLimit = currentLimit;
287
+
288
+ // If error rate is high, reduce concurrency
289
+ if (errorRate > 10) {
290
+ newLimit = Math.max(1, Math.floor(currentLimit * 0.7));
291
+ logger.info(`Reducing ${operationType} limit due to high error rate (${errorRate}%): ${currentLimit} -> ${newLimit}`);
292
+ }
293
+ // If response time is high, reduce concurrency
294
+ else if (responseTime > 5000) {
295
+ newLimit = Math.max(1, Math.floor(currentLimit * 0.8));
296
+ logger.info(`Reducing ${operationType} limit due to high response time (${responseTime}ms): ${currentLimit} -> ${newLimit}`);
297
+ }
298
+ // If performance is good, gradually increase concurrency
299
+ else if (errorRate < 2 && responseTime < 1000 && currentLimit < RateLimitManager.DEFAULT_LIMITS[operationType] * 2) {
300
+ newLimit = Math.min(RateLimitManager.DEFAULT_LIMITS[operationType] * 2, currentLimit + 1);
301
+ logger.info(`Increasing ${operationType} limit due to good performance: ${currentLimit} -> ${newLimit}`);
302
+ }
303
+
304
+ if (newLimit !== currentLimit) {
305
+ this.updateLimit(operationType, newLimit);
306
+ }
307
+ }
308
+
309
+ /**
310
+ * Resets all rate limits to default values.
311
+ */
312
+ resetToDefaults(): void {
313
+ logger.info("Resetting all rate limits to default values");
314
+ this.config = { ...RateLimitManager.DEFAULT_LIMITS };
315
+ this.initializeLimits();
316
+ }
317
+
318
+ /**
319
+ * Creates a specialized batch processor with rate limiting.
320
+ * Useful for processing large datasets with controlled concurrency.
321
+ *
322
+ * @param operationType - The operation type for rate limiting
323
+ * @param batchSize - Number of items to process in each batch
324
+ */
325
+ createBatchProcessor<T, R>(
326
+ operationType: keyof RateLimitConfig,
327
+ batchSize: number = 50
328
+ ) {
329
+ const limiter = this.getLimiter(operationType);
330
+
331
+ return async (
332
+ items: T[],
333
+ processor: (item: T) => Promise<R>,
334
+ onProgress?: (processed: number, total: number) => void
335
+ ): Promise<R[]> => {
336
+ const results: R[] = [];
337
+
338
+ for (let i = 0; i < items.length; i += batchSize) {
339
+ const batch = items.slice(i, i + batchSize);
340
+
341
+ const batchPromises = batch.map(item =>
342
+ limiter(() => processor(item))
343
+ );
344
+
345
+ const batchResults = await Promise.allSettled(batchPromises);
346
+
347
+ for (const result of batchResults) {
348
+ if (result.status === 'fulfilled') {
349
+ results.push(result.value);
350
+ } else {
351
+ logger.error(`Batch item failed: ${result.reason}`);
352
+ }
353
+ }
354
+
355
+ if (onProgress) {
356
+ onProgress(Math.min(i + batchSize, items.length), items.length);
357
+ }
358
+ }
359
+
360
+ return results;
361
+ };
362
+ }
363
+ }