@njdamstra/appwrite-utils-cli 1.8.9 → 1.10.0

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 (284) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/CONFIG_TODO.md +1189 -0
  3. package/SELECTION_DIALOGS.md +146 -0
  4. package/SERVICE_IMPLEMENTATION_REPORT.md +462 -0
  5. package/dist/adapters/index.d.ts +7 -8
  6. package/dist/adapters/index.js +7 -9
  7. package/dist/backups/operations/bucketBackup.js +2 -2
  8. package/dist/backups/operations/collectionBackup.d.ts +1 -1
  9. package/dist/backups/operations/collectionBackup.js +3 -3
  10. package/dist/backups/operations/comprehensiveBackup.d.ts +1 -1
  11. package/dist/backups/operations/comprehensiveBackup.js +2 -2
  12. package/dist/backups/tracking/centralizedTracking.d.ts +1 -1
  13. package/dist/backups/tracking/centralizedTracking.js +2 -2
  14. package/dist/cli/commands/configCommands.js +51 -7
  15. package/dist/cli/commands/databaseCommands.d.ts +1 -0
  16. package/dist/cli/commands/databaseCommands.js +119 -9
  17. package/dist/cli/commands/functionCommands.js +3 -3
  18. package/dist/cli/commands/importFileCommands.d.ts +7 -0
  19. package/dist/cli/commands/importFileCommands.js +674 -0
  20. package/dist/cli/commands/schemaCommands.js +3 -3
  21. package/dist/cli/commands/storageCommands.js +2 -3
  22. package/dist/cli/commands/transferCommands.js +3 -5
  23. package/dist/collections/attributes.d.ts +1 -1
  24. package/dist/collections/attributes.js +2 -35
  25. package/dist/collections/indexes.js +1 -3
  26. package/dist/collections/methods.d.ts +1 -1
  27. package/dist/collections/methods.js +111 -192
  28. package/dist/collections/tableOperations.d.ts +1 -0
  29. package/dist/collections/tableOperations.js +55 -23
  30. package/dist/collections/transferOperations.d.ts +1 -1
  31. package/dist/collections/transferOperations.js +3 -4
  32. package/dist/collections/wipeOperations.d.ts +4 -3
  33. package/dist/collections/wipeOperations.js +112 -39
  34. package/dist/databases/methods.js +2 -2
  35. package/dist/databases/setup.js +2 -2
  36. package/dist/examples/yamlTerminologyExample.js +2 -2
  37. package/dist/functions/deployments.d.ts +1 -1
  38. package/dist/functions/deployments.js +5 -5
  39. package/dist/functions/fnConfigDiscovery.js +2 -2
  40. package/dist/functions/methods.js +16 -4
  41. package/dist/init.js +1 -1
  42. package/dist/interactiveCLI.d.ts +6 -1
  43. package/dist/interactiveCLI.js +63 -9
  44. package/dist/main.js +130 -177
  45. package/dist/migrations/afterImportActions.js +2 -3
  46. package/dist/migrations/appwriteToX.d.ts +1 -1
  47. package/dist/migrations/appwriteToX.js +9 -7
  48. package/dist/migrations/comprehensiveTransfer.js +3 -5
  49. package/dist/migrations/dataLoader.js +2 -5
  50. package/dist/migrations/importController.js +3 -4
  51. package/dist/migrations/importDataActions.js +3 -3
  52. package/dist/migrations/relationships.js +1 -2
  53. package/dist/migrations/services/DataTransformationService.js +2 -2
  54. package/dist/migrations/services/FileHandlerService.js +1 -1
  55. package/dist/migrations/services/ImportOrchestrator.js +4 -4
  56. package/dist/migrations/services/RateLimitManager.js +1 -1
  57. package/dist/migrations/services/RelationshipResolver.js +1 -1
  58. package/dist/migrations/services/UserMappingService.js +1 -1
  59. package/dist/migrations/services/ValidationService.js +1 -1
  60. package/dist/migrations/transfer.d.ts +8 -4
  61. package/dist/migrations/transfer.js +106 -55
  62. package/dist/migrations/yaml/YamlImportConfigLoader.js +1 -1
  63. package/dist/migrations/yaml/YamlImportIntegration.js +2 -2
  64. package/dist/migrations/yaml/generateImportSchemas.js +1 -1
  65. package/dist/setupCommands.d.ts +1 -1
  66. package/dist/setupCommands.js +5 -6
  67. package/dist/setupController.js +1 -1
  68. package/dist/shared/backupTracking.d.ts +1 -1
  69. package/dist/shared/backupTracking.js +2 -2
  70. package/dist/shared/confirmationDialogs.js +1 -1
  71. package/dist/shared/migrationHelpers.d.ts +1 -1
  72. package/dist/shared/migrationHelpers.js +3 -3
  73. package/dist/shared/operationQueue.d.ts +1 -1
  74. package/dist/shared/operationQueue.js +2 -3
  75. package/dist/shared/operationsTable.d.ts +1 -1
  76. package/dist/shared/operationsTable.js +2 -2
  77. package/dist/shared/progressManager.js +1 -1
  78. package/dist/shared/selectionDialogs.js +9 -8
  79. package/dist/storage/methods.js +4 -4
  80. package/dist/storage/schemas.d.ts +2 -2
  81. package/dist/tables/indexManager.d.ts +65 -0
  82. package/dist/tables/indexManager.js +294 -0
  83. package/dist/types.d.ts +2 -2
  84. package/dist/types.js +1 -1
  85. package/dist/users/methods.js +2 -3
  86. package/dist/utils/configMigration.js +1 -1
  87. package/dist/utils/index.d.ts +1 -1
  88. package/dist/utils/index.js +1 -1
  89. package/dist/utils/loadConfigs.d.ts +2 -2
  90. package/dist/utils/loadConfigs.js +6 -7
  91. package/dist/utils/setupFiles.js +5 -7
  92. package/dist/utilsController.d.ts +15 -8
  93. package/dist/utilsController.js +57 -28
  94. package/package.json +7 -3
  95. package/src/adapters/index.ts +8 -34
  96. package/src/backups/operations/bucketBackup.ts +2 -2
  97. package/src/backups/operations/collectionBackup.ts +4 -4
  98. package/src/backups/operations/comprehensiveBackup.ts +3 -3
  99. package/src/backups/tracking/centralizedTracking.ts +3 -3
  100. package/src/cli/commands/configCommands.ts +72 -8
  101. package/src/cli/commands/databaseCommands.ts +161 -9
  102. package/src/cli/commands/functionCommands.ts +4 -3
  103. package/src/cli/commands/importFileCommands.ts +815 -0
  104. package/src/cli/commands/schemaCommands.ts +3 -3
  105. package/src/cli/commands/storageCommands.ts +2 -3
  106. package/src/cli/commands/transferCommands.ts +3 -6
  107. package/src/collections/attributes.ts +3 -39
  108. package/src/collections/indexes.ts +2 -4
  109. package/src/collections/methods.ts +115 -150
  110. package/src/collections/tableOperations.ts +57 -21
  111. package/src/collections/transferOperations.ts +4 -5
  112. package/src/collections/wipeOperations.ts +154 -51
  113. package/src/databases/methods.ts +2 -2
  114. package/src/databases/setup.ts +2 -2
  115. package/src/examples/yamlTerminologyExample.ts +2 -2
  116. package/src/functions/deployments.ts +6 -5
  117. package/src/functions/fnConfigDiscovery.ts +2 -2
  118. package/src/functions/methods.ts +17 -4
  119. package/src/init.ts +1 -1
  120. package/src/interactiveCLI.ts +75 -10
  121. package/src/main.ts +143 -287
  122. package/src/migrations/afterImportActions.ts +2 -3
  123. package/src/migrations/appwriteToX.ts +12 -8
  124. package/src/migrations/comprehensiveTransfer.ts +6 -6
  125. package/src/migrations/dataLoader.ts +2 -5
  126. package/src/migrations/importController.ts +3 -4
  127. package/src/migrations/importDataActions.ts +3 -3
  128. package/src/migrations/relationships.ts +1 -2
  129. package/src/migrations/services/DataTransformationService.ts +2 -2
  130. package/src/migrations/services/FileHandlerService.ts +1 -1
  131. package/src/migrations/services/ImportOrchestrator.ts +4 -4
  132. package/src/migrations/services/RateLimitManager.ts +1 -1
  133. package/src/migrations/services/RelationshipResolver.ts +1 -1
  134. package/src/migrations/services/UserMappingService.ts +1 -1
  135. package/src/migrations/services/ValidationService.ts +1 -1
  136. package/src/migrations/transfer.ts +126 -83
  137. package/src/migrations/yaml/YamlImportConfigLoader.ts +1 -1
  138. package/src/migrations/yaml/YamlImportIntegration.ts +2 -2
  139. package/src/migrations/yaml/generateImportSchemas.ts +1 -1
  140. package/src/setupCommands.ts +5 -6
  141. package/src/setupController.ts +1 -1
  142. package/src/shared/backupTracking.ts +3 -3
  143. package/src/shared/confirmationDialogs.ts +1 -1
  144. package/src/shared/migrationHelpers.ts +4 -4
  145. package/src/shared/operationQueue.ts +3 -4
  146. package/src/shared/operationsTable.ts +3 -3
  147. package/src/shared/progressManager.ts +1 -1
  148. package/src/shared/selectionDialogs.ts +9 -8
  149. package/src/storage/methods.ts +4 -4
  150. package/src/tables/indexManager.ts +409 -0
  151. package/src/types.ts +2 -2
  152. package/src/users/methods.ts +2 -3
  153. package/src/utils/configMigration.ts +1 -1
  154. package/src/utils/index.ts +1 -1
  155. package/src/utils/loadConfigs.ts +15 -7
  156. package/src/utils/setupFiles.ts +5 -7
  157. package/src/utilsController.ts +86 -32
  158. package/dist/adapters/AdapterFactory.d.ts +0 -94
  159. package/dist/adapters/AdapterFactory.js +0 -405
  160. package/dist/adapters/DatabaseAdapter.d.ts +0 -233
  161. package/dist/adapters/DatabaseAdapter.js +0 -50
  162. package/dist/adapters/LegacyAdapter.d.ts +0 -50
  163. package/dist/adapters/LegacyAdapter.js +0 -612
  164. package/dist/adapters/TablesDBAdapter.d.ts +0 -45
  165. package/dist/adapters/TablesDBAdapter.js +0 -571
  166. package/dist/config/ConfigManager.d.ts +0 -445
  167. package/dist/config/ConfigManager.js +0 -625
  168. package/dist/config/configMigration.d.ts +0 -87
  169. package/dist/config/configMigration.js +0 -390
  170. package/dist/config/configValidation.d.ts +0 -66
  171. package/dist/config/configValidation.js +0 -358
  172. package/dist/config/index.d.ts +0 -8
  173. package/dist/config/index.js +0 -7
  174. package/dist/config/services/ConfigDiscoveryService.d.ts +0 -126
  175. package/dist/config/services/ConfigDiscoveryService.js +0 -374
  176. package/dist/config/services/ConfigLoaderService.d.ts +0 -129
  177. package/dist/config/services/ConfigLoaderService.js +0 -540
  178. package/dist/config/services/ConfigMergeService.d.ts +0 -208
  179. package/dist/config/services/ConfigMergeService.js +0 -308
  180. package/dist/config/services/ConfigValidationService.d.ts +0 -214
  181. package/dist/config/services/ConfigValidationService.js +0 -310
  182. package/dist/config/services/SessionAuthService.d.ts +0 -225
  183. package/dist/config/services/SessionAuthService.js +0 -456
  184. package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +0 -1
  185. package/dist/config/services/__tests__/ConfigMergeService.test.js +0 -271
  186. package/dist/config/services/index.d.ts +0 -13
  187. package/dist/config/services/index.js +0 -10
  188. package/dist/config/yamlConfig.d.ts +0 -722
  189. package/dist/config/yamlConfig.js +0 -702
  190. package/dist/functions/pathResolution.d.ts +0 -37
  191. package/dist/functions/pathResolution.js +0 -185
  192. package/dist/shared/attributeMapper.d.ts +0 -20
  193. package/dist/shared/attributeMapper.js +0 -203
  194. package/dist/shared/errorUtils.d.ts +0 -54
  195. package/dist/shared/errorUtils.js +0 -95
  196. package/dist/shared/functionManager.d.ts +0 -48
  197. package/dist/shared/functionManager.js +0 -336
  198. package/dist/shared/indexManager.d.ts +0 -24
  199. package/dist/shared/indexManager.js +0 -151
  200. package/dist/shared/jsonSchemaGenerator.d.ts +0 -50
  201. package/dist/shared/jsonSchemaGenerator.js +0 -290
  202. package/dist/shared/logging.d.ts +0 -61
  203. package/dist/shared/logging.js +0 -116
  204. package/dist/shared/messageFormatter.d.ts +0 -39
  205. package/dist/shared/messageFormatter.js +0 -162
  206. package/dist/shared/pydanticModelGenerator.d.ts +0 -17
  207. package/dist/shared/pydanticModelGenerator.js +0 -615
  208. package/dist/shared/schemaGenerator.d.ts +0 -40
  209. package/dist/shared/schemaGenerator.js +0 -556
  210. package/dist/utils/ClientFactory.d.ts +0 -87
  211. package/dist/utils/ClientFactory.js +0 -212
  212. package/dist/utils/configDiscovery.d.ts +0 -78
  213. package/dist/utils/configDiscovery.js +0 -472
  214. package/dist/utils/constantsGenerator.d.ts +0 -31
  215. package/dist/utils/constantsGenerator.js +0 -321
  216. package/dist/utils/dataConverters.d.ts +0 -46
  217. package/dist/utils/dataConverters.js +0 -139
  218. package/dist/utils/directoryUtils.d.ts +0 -22
  219. package/dist/utils/directoryUtils.js +0 -59
  220. package/dist/utils/getClientFromConfig.d.ts +0 -39
  221. package/dist/utils/getClientFromConfig.js +0 -199
  222. package/dist/utils/helperFunctions.d.ts +0 -63
  223. package/dist/utils/helperFunctions.js +0 -156
  224. package/dist/utils/pathResolvers.d.ts +0 -53
  225. package/dist/utils/pathResolvers.js +0 -72
  226. package/dist/utils/projectConfig.d.ts +0 -119
  227. package/dist/utils/projectConfig.js +0 -171
  228. package/dist/utils/retryFailedPromises.d.ts +0 -2
  229. package/dist/utils/retryFailedPromises.js +0 -23
  230. package/dist/utils/sessionAuth.d.ts +0 -48
  231. package/dist/utils/sessionAuth.js +0 -164
  232. package/dist/utils/typeGuards.d.ts +0 -35
  233. package/dist/utils/typeGuards.js +0 -57
  234. package/dist/utils/validationRules.d.ts +0 -43
  235. package/dist/utils/validationRules.js +0 -42
  236. package/dist/utils/versionDetection.d.ts +0 -58
  237. package/dist/utils/versionDetection.js +0 -251
  238. package/dist/utils/yamlConverter.d.ts +0 -100
  239. package/dist/utils/yamlConverter.js +0 -428
  240. package/dist/utils/yamlLoader.d.ts +0 -70
  241. package/dist/utils/yamlLoader.js +0 -267
  242. package/src/adapters/AdapterFactory.ts +0 -510
  243. package/src/adapters/DatabaseAdapter.ts +0 -306
  244. package/src/adapters/LegacyAdapter.ts +0 -841
  245. package/src/adapters/TablesDBAdapter.ts +0 -773
  246. package/src/config/ConfigManager.ts +0 -808
  247. package/src/config/README.md +0 -274
  248. package/src/config/configMigration.ts +0 -575
  249. package/src/config/configValidation.ts +0 -445
  250. package/src/config/index.ts +0 -10
  251. package/src/config/services/ConfigDiscoveryService.ts +0 -463
  252. package/src/config/services/ConfigLoaderService.ts +0 -740
  253. package/src/config/services/ConfigMergeService.ts +0 -388
  254. package/src/config/services/ConfigValidationService.ts +0 -394
  255. package/src/config/services/SessionAuthService.ts +0 -565
  256. package/src/config/services/__tests__/ConfigMergeService.test.ts +0 -351
  257. package/src/config/services/index.ts +0 -29
  258. package/src/config/yamlConfig.ts +0 -761
  259. package/src/functions/pathResolution.ts +0 -227
  260. package/src/shared/attributeMapper.ts +0 -229
  261. package/src/shared/errorUtils.ts +0 -110
  262. package/src/shared/functionManager.ts +0 -525
  263. package/src/shared/indexManager.ts +0 -254
  264. package/src/shared/jsonSchemaGenerator.ts +0 -383
  265. package/src/shared/logging.ts +0 -149
  266. package/src/shared/messageFormatter.ts +0 -208
  267. package/src/shared/pydanticModelGenerator.ts +0 -618
  268. package/src/shared/schemaGenerator.ts +0 -644
  269. package/src/utils/ClientFactory.ts +0 -240
  270. package/src/utils/configDiscovery.ts +0 -557
  271. package/src/utils/constantsGenerator.ts +0 -369
  272. package/src/utils/dataConverters.ts +0 -159
  273. package/src/utils/directoryUtils.ts +0 -61
  274. package/src/utils/getClientFromConfig.ts +0 -257
  275. package/src/utils/helperFunctions.ts +0 -228
  276. package/src/utils/pathResolvers.ts +0 -81
  277. package/src/utils/projectConfig.ts +0 -299
  278. package/src/utils/retryFailedPromises.ts +0 -29
  279. package/src/utils/sessionAuth.ts +0 -230
  280. package/src/utils/typeGuards.ts +0 -65
  281. package/src/utils/validationRules.ts +0 -88
  282. package/src/utils/versionDetection.ts +0 -292
  283. package/src/utils/yamlConverter.ts +0 -542
  284. package/src/utils/yamlLoader.ts +0 -371
@@ -1,510 +0,0 @@
1
- /**
2
- * AdapterFactory - Unified Client Creation with Automatic API Detection
3
- *
4
- * This factory creates the appropriate database adapter (TablesDB or Legacy)
5
- * based on version detection and configuration. It handles dynamic SDK imports
6
- * and provides a single entry point for all database operations.
7
- */
8
-
9
- import type { AppwriteConfig } from "@njdamstra/appwrite-utils";
10
- import { detectAppwriteVersionCached, isVersionAtLeast, type ApiMode, type VersionDetectionResult } from "../utils/versionDetection.js";
11
- import type { DatabaseAdapter } from './DatabaseAdapter.js';
12
- import { TablesDBAdapter } from './TablesDBAdapter.js';
13
- import { LegacyAdapter } from './LegacyAdapter.js';
14
- import { logger } from '../shared/logging.js';
15
- import { isValidSessionCookie } from '../utils/sessionAuth.js';
16
- import { MessageFormatter } from '../shared/messageFormatter.js';
17
- import { Client } from 'node-appwrite';
18
-
19
- export interface AdapterFactoryConfig {
20
- appwriteEndpoint: string;
21
- appwriteProject: string;
22
- appwriteKey?: string; // Made optional to support session-only auth
23
- apiMode?: 'auto' | 'legacy' | 'tablesdb';
24
- forceRefresh?: boolean; // Skip detection cache
25
- sessionCookie?: string; // Session authentication support
26
- authMethod?: 'session' | 'apikey' | 'auto'; // Authentication method preference
27
- preConfiguredClient?: any; // Pre-configured authenticated client
28
- }
29
-
30
- export interface AdapterFactoryResult {
31
- adapter: DatabaseAdapter;
32
- apiMode: ApiMode;
33
- detectionResult?: VersionDetectionResult;
34
- client: any;
35
- }
36
-
37
- /**
38
- * AdapterFactory - Main factory class for creating database adapters
39
- */
40
- export class AdapterFactory {
41
- private static cache = new Map<string, { adapter: DatabaseAdapter; timestamp: number }>();
42
- private static readonly CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
43
-
44
- /**
45
- * Create a database adapter based on configuration and detection
46
- */
47
- static async create(config: AdapterFactoryConfig): Promise<AdapterFactoryResult> {
48
- const startTime = Date.now();
49
-
50
- // Validate authentication configuration
51
- this.validateAuthConfig(config);
52
-
53
- const cacheKey = `${config.appwriteEndpoint}:${config.appwriteProject}:${config.apiMode || 'auto'}`;
54
-
55
- logger.info('Creating database adapter', {
56
- endpoint: config.appwriteEndpoint,
57
- project: config.appwriteProject,
58
- requestedApiMode: config.apiMode || 'auto',
59
- authMethod: config.authMethod || 'auto',
60
- hasSessionCookie: !!config.sessionCookie,
61
- hasPreConfiguredClient: !!config.preConfiguredClient,
62
- forceRefresh: config.forceRefresh,
63
- cacheKey,
64
- operation: 'AdapterFactory.create'
65
- });
66
-
67
- // Check cache first (unless force refresh)
68
- if (!config.forceRefresh) {
69
- const cached = this.getCachedAdapter(cacheKey);
70
- if (cached) {
71
- const cacheAge = Date.now() - cached.timestamp;
72
- logger.info('Using cached adapter', {
73
- cacheKey,
74
- cacheAge,
75
- apiMode: cached.adapter.getApiMode(),
76
- operation: 'AdapterFactory.create'
77
- });
78
- return {
79
- adapter: cached.adapter,
80
- apiMode: cached.adapter.getApiMode(),
81
- client: cached.adapter.getRawClient()
82
- };
83
- }
84
- }
85
-
86
- // Determine API mode
87
- let apiMode: ApiMode;
88
- let detectionResult: VersionDetectionResult | undefined;
89
-
90
- if (config.apiMode && config.apiMode !== 'auto') {
91
- // Use explicitly configured mode
92
- apiMode = config.apiMode;
93
- logger.info('Using explicitly configured API mode', {
94
- apiMode,
95
- endpoint: config.appwriteEndpoint,
96
- operation: 'AdapterFactory.create'
97
- });
98
- } else {
99
- // Auto-detect API mode
100
- logger.info('Starting API mode auto-detection', {
101
- endpoint: config.appwriteEndpoint,
102
- project: config.appwriteProject,
103
- forceRefresh: config.forceRefresh,
104
- operation: 'AdapterFactory.create'
105
- });
106
-
107
- const detectionStartTime = Date.now();
108
- // For version detection, we need some form of authentication
109
- const authKey = config.appwriteKey || '';
110
- detectionResult = await detectAppwriteVersionCached(
111
- config.appwriteEndpoint,
112
- config.appwriteProject,
113
- authKey,
114
- config.forceRefresh
115
- );
116
- const detectionDuration = Date.now() - detectionStartTime;
117
-
118
- apiMode = detectionResult.apiMode;
119
-
120
- logger.info('API mode detection completed', {
121
- apiMode,
122
- detectionMethod: detectionResult.detectionMethod,
123
- confidence: detectionResult.confidence,
124
- serverVersion: detectionResult.serverVersion,
125
- detectionDuration,
126
- endpoint: config.appwriteEndpoint,
127
- operation: 'AdapterFactory.create'
128
- });
129
-
130
- // Add version-based safety check to prevent using TablesDB on old servers
131
- if (detectionResult.serverVersion &&
132
- !isVersionAtLeast(detectionResult.serverVersion, '1.8.0') &&
133
- apiMode === 'tablesdb') {
134
- logger.warn('Overriding TablesDB detection - server version too old', {
135
- serverVersion: detectionResult.serverVersion,
136
- detectedMode: apiMode,
137
- overrideMode: 'legacy',
138
- operation: 'AdapterFactory.create'
139
- });
140
- apiMode = 'legacy';
141
- }
142
- }
143
-
144
- // Create appropriate adapter
145
- logger.info('Creating adapter instance', {
146
- apiMode,
147
- endpoint: config.appwriteEndpoint,
148
- operation: 'AdapterFactory.create'
149
- });
150
-
151
- const adapterStartTime = Date.now();
152
- const result = await this.createAdapter(config, apiMode);
153
- const adapterDuration = Date.now() - adapterStartTime;
154
-
155
- // Cache the result
156
- this.setCachedAdapter(cacheKey, result.adapter);
157
-
158
- const totalDuration = Date.now() - startTime;
159
- logger.info('Adapter creation completed', {
160
- apiMode,
161
- adapterDuration,
162
- totalDuration,
163
- cached: true,
164
- operation: 'AdapterFactory.create'
165
- });
166
-
167
- return {
168
- ...result,
169
- apiMode,
170
- detectionResult
171
- };
172
- }
173
-
174
- /**
175
- * Create adapter from AppwriteConfig (convenience method)
176
- */
177
- static async createFromConfig(config: AppwriteConfig, forceRefresh?: boolean): Promise<AdapterFactoryResult> {
178
- return this.create({
179
- appwriteEndpoint: config.appwriteEndpoint,
180
- appwriteProject: config.appwriteProject,
181
- appwriteKey: config.appwriteKey,
182
- apiMode: (config as any).apiMode || 'auto', // Cast to access new property
183
- forceRefresh
184
- });
185
- }
186
-
187
- /**
188
- * Create specific adapter type (internal method)
189
- */
190
- private static async createAdapter(
191
- config: AdapterFactoryConfig,
192
- apiMode: ApiMode
193
- ): Promise<{ adapter: DatabaseAdapter; client: any }> {
194
- if (apiMode === 'tablesdb') {
195
- return this.createTablesDBAdapter(config);
196
- } else {
197
- return this.createLegacyAdapter(config);
198
- }
199
- }
200
-
201
- /**
202
- * Create TablesDB adapter with dynamic import
203
- */
204
- private static async createTablesDBAdapter(
205
- config: AdapterFactoryConfig
206
- ): Promise<{ adapter: DatabaseAdapter; client: any }> {
207
- const startTime = Date.now();
208
-
209
- try {
210
- logger.info('Creating TablesDB adapter (static SDK imports)', {
211
- endpoint: config.appwriteEndpoint,
212
- operation: 'createTablesDBAdapter'
213
- });
214
-
215
- // Use pre-configured client or create session-aware client with TablesDB Client
216
- let client: any;
217
- if (config.preConfiguredClient) {
218
- client = config.preConfiguredClient;
219
- } else {
220
- client = new Client()
221
- .setEndpoint(config.appwriteEndpoint)
222
- .setProject(config.appwriteProject);
223
-
224
- // Set authentication method with mode headers
225
- // Prefer session with admin mode, fallback to API key with default mode
226
- if (config.sessionCookie && isValidSessionCookie(config.sessionCookie)) {
227
- client.setSession(config.sessionCookie);
228
- client.headers['X-Appwrite-Mode'] = 'admin';
229
- logger.debug('Using session authentication for TablesDB adapter', {
230
- project: config.appwriteProject,
231
- operation: 'createTablesDBAdapter'
232
- });
233
- } else if (config.appwriteKey) {
234
- client.setKey(config.appwriteKey);
235
- client.headers['X-Appwrite-Mode'] = 'default';
236
- logger.debug('Using API key authentication for TablesDB adapter', {
237
- project: config.appwriteProject,
238
- operation: 'createTablesDBAdapter'
239
- });
240
- } else {
241
- throw new Error("No authentication available for adapter");
242
- }
243
- }
244
-
245
- const adapter = new TablesDBAdapter(client);
246
-
247
- const totalDuration = Date.now() - startTime;
248
- logger.info('TablesDB adapter created successfully', {
249
- totalDuration,
250
- endpoint: config.appwriteEndpoint,
251
- operation: 'createTablesDBAdapter'
252
- });
253
-
254
- return { adapter, client };
255
-
256
- } catch (error) {
257
- const errorDuration = Date.now() - startTime;
258
- const errorMessage = error instanceof Error ? error.message : 'Unknown error';
259
-
260
- MessageFormatter.warning('Failed to create TablesDB adapter - falling back to legacy', { prefix: "Adapter" });
261
-
262
- logger.warn('TablesDB adapter creation failed, falling back to legacy', {
263
- error: errorMessage,
264
- errorDuration,
265
- endpoint: config.appwriteEndpoint,
266
- operation: 'createTablesDBAdapter'
267
- });
268
-
269
- // Fallback to legacy adapter if TablesDB creation fails
270
- return this.createLegacyAdapter(config);
271
- }
272
- }
273
-
274
- /**
275
- * Create Legacy adapter with dynamic import
276
- */
277
- private static async createLegacyAdapter(
278
- config: AdapterFactoryConfig
279
- ): Promise<{ adapter: DatabaseAdapter; client: any }> {
280
- const startTime = Date.now();
281
-
282
- try {
283
- logger.info('Creating legacy adapter (static SDK imports)', {
284
- endpoint: config.appwriteEndpoint,
285
- operation: 'createLegacyAdapter'
286
- });
287
-
288
- // Use pre-configured client or create session-aware client with Legacy Client
289
- let client: any;
290
- if (config.preConfiguredClient) {
291
- client = config.preConfiguredClient;
292
- } else {
293
- client = new Client()
294
- .setEndpoint(config.appwriteEndpoint)
295
- .setProject(config.appwriteProject);
296
-
297
- // Set authentication method with mode headers
298
- // Prefer session with admin mode, fallback to API key with default mode
299
- if (config.sessionCookie && isValidSessionCookie(config.sessionCookie)) {
300
- (client as any).setSession(config.sessionCookie);
301
- client.headers['X-Appwrite-Mode'] = 'admin';
302
- logger.debug('Using session authentication for Legacy adapter', {
303
- project: config.appwriteProject,
304
- operation: 'createLegacyAdapter'
305
- });
306
- } else if (config.appwriteKey) {
307
- client.setKey(config.appwriteKey);
308
- client.headers['X-Appwrite-Mode'] = 'default';
309
- logger.debug('Using API key authentication for Legacy adapter', {
310
- project: config.appwriteProject,
311
- operation: 'createLegacyAdapter'
312
- });
313
- } else {
314
- throw new Error("No authentication available for adapter");
315
- }
316
- }
317
-
318
- const adapter = new LegacyAdapter(client);
319
-
320
- const totalDuration = Date.now() - startTime;
321
- logger.info('Legacy adapter created successfully', {
322
- totalDuration,
323
- endpoint: config.appwriteEndpoint,
324
- operation: 'createLegacyAdapter'
325
- });
326
-
327
- return { adapter, client };
328
-
329
- } catch (error) {
330
- const errorDuration = Date.now() - startTime;
331
- const errorMessage = error instanceof Error ? error.message : 'Unknown error';
332
-
333
- logger.error('Failed to load legacy Appwrite SDK', {
334
- error: errorMessage,
335
- errorDuration,
336
- endpoint: config.appwriteEndpoint,
337
- operation: 'createLegacyAdapter'
338
- });
339
-
340
- throw new Error(`Failed to load legacy Appwrite SDK: ${errorMessage}`);
341
- }
342
- }
343
-
344
- /**
345
- * Get cached adapter if available and not expired
346
- */
347
- private static getCachedAdapter(cacheKey: string): { adapter: DatabaseAdapter; timestamp: number } | null {
348
- const cached = this.cache.get(cacheKey);
349
-
350
- if (!cached) {
351
- return null;
352
- }
353
-
354
- // Check if cache is expired
355
- if (Date.now() - cached.timestamp > this.CACHE_DURATION) {
356
- this.cache.delete(cacheKey);
357
- return null;
358
- }
359
-
360
- return cached;
361
- }
362
-
363
- /**
364
- * Cache adapter instance
365
- */
366
- private static setCachedAdapter(cacheKey: string, adapter: DatabaseAdapter): void {
367
- this.cache.set(cacheKey, {
368
- adapter,
369
- timestamp: Date.now()
370
- });
371
- }
372
-
373
- /**
374
- * Clear adapter cache (useful for testing)
375
- */
376
- static clearCache(): void {
377
- this.cache.clear();
378
- }
379
-
380
- /**
381
- * Validate authentication configuration
382
- */
383
- private static validateAuthConfig(config: AdapterFactoryConfig): void {
384
- const hasApiKey = config.appwriteKey && config.appwriteKey.trim().length > 0;
385
- const hasSessionCookie = config.sessionCookie && isValidSessionCookie(config.sessionCookie);
386
- const hasPreConfiguredClient = !!config.preConfiguredClient;
387
-
388
- // Must have at least one authentication method
389
- if (!hasApiKey && !hasSessionCookie && !hasPreConfiguredClient) {
390
- throw new Error(
391
- `No valid authentication method provided for project ${config.appwriteProject}. ` +
392
- `Please provide an API key, session cookie, or pre-configured client.`
393
- );
394
- }
395
-
396
- // Validate session cookie format if provided
397
- if (config.sessionCookie && !isValidSessionCookie(config.sessionCookie)) {
398
- throw new Error(
399
- `Invalid session cookie format provided for project ${config.appwriteProject}. ` +
400
- `Session cookie must be a valid JWT token.`
401
- );
402
- }
403
-
404
- logger.debug('Authentication configuration validated', {
405
- hasApiKey,
406
- hasSessionCookie,
407
- hasPreConfiguredClient,
408
- authMethod: config.authMethod || 'auto',
409
- operation: 'validateAuthConfig'
410
- });
411
- }
412
-
413
- /**
414
- * Test connection and API capabilities
415
- */
416
- static async testConnection(config: AdapterFactoryConfig): Promise<{
417
- success: boolean;
418
- apiMode: ApiMode;
419
- capabilities: string[];
420
- error?: string;
421
- }> {
422
- try {
423
- const result = await this.create({ ...config, forceRefresh: true });
424
- const metadata = result.adapter.getMetadata();
425
-
426
- // Test basic operations
427
- const capabilities = [];
428
-
429
- if (metadata.capabilities.bulkOperations) {
430
- capabilities.push('Bulk Operations');
431
- }
432
-
433
- if (metadata.capabilities.advancedQueries) {
434
- capabilities.push('Advanced Queries');
435
- }
436
-
437
- if (metadata.capabilities.realtime) {
438
- capabilities.push('Realtime');
439
- }
440
-
441
- if (metadata.capabilities.transactions) {
442
- capabilities.push('Transactions');
443
- }
444
-
445
- return {
446
- success: true,
447
- apiMode: result.apiMode,
448
- capabilities
449
- };
450
-
451
- } catch (error) {
452
- return {
453
- success: false,
454
- apiMode: 'legacy', // Default fallback
455
- capabilities: [],
456
- error: error instanceof Error ? error.message : 'Unknown error'
457
- };
458
- }
459
- }
460
- }
461
-
462
- /**
463
- * Convenience function for quick adapter creation
464
- */
465
- export async function createDatabaseAdapter(
466
- endpoint: string,
467
- project: string,
468
- apiKey?: string,
469
- mode: 'auto' | 'legacy' | 'tablesdb' = 'auto',
470
- sessionCookie?: string
471
- ): Promise<DatabaseAdapter> {
472
- const result = await AdapterFactory.create({
473
- appwriteEndpoint: endpoint,
474
- appwriteProject: project,
475
- appwriteKey: apiKey,
476
- apiMode: mode,
477
- sessionCookie
478
- });
479
-
480
- return result.adapter;
481
- }
482
-
483
- /**
484
- * Helper function to get adapter metadata without creating full adapter
485
- */
486
- export async function getApiCapabilities(
487
- endpoint: string,
488
- project: string,
489
- apiKey?: string,
490
- sessionCookie?: string
491
- ): Promise<{
492
- apiMode: ApiMode;
493
- terminology: { container: string; item: string; service: string };
494
- capabilities: string[];
495
- }> {
496
- const adapter = await createDatabaseAdapter(endpoint, project, apiKey, 'auto', sessionCookie);
497
- const metadata = adapter.getMetadata();
498
-
499
- const capabilities = [];
500
- if (metadata.capabilities.bulkOperations) capabilities.push('Bulk Operations');
501
- if (metadata.capabilities.advancedQueries) capabilities.push('Advanced Queries');
502
- if (metadata.capabilities.realtime) capabilities.push('Realtime');
503
- if (metadata.capabilities.transactions) capabilities.push('Transactions');
504
-
505
- return {
506
- apiMode: metadata.apiMode,
507
- terminology: metadata.terminology,
508
- capabilities
509
- };
510
- }