appwrite-utils-cli 1.9.6 → 1.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/AdapterFactory.d.ts +1 -1
- package/dist/adapters/AdapterFactory.js +52 -37
- package/dist/adapters/DatabaseAdapter.d.ts +1 -0
- package/dist/adapters/LegacyAdapter.js +5 -2
- package/dist/adapters/TablesDBAdapter.js +18 -3
- package/dist/collections/attributes.js +0 -31
- package/dist/collections/methods.js +10 -157
- package/dist/collections/tableOperations.js +25 -12
- package/dist/config/ConfigManager.js +25 -0
- package/dist/shared/attributeMapper.js +1 -1
- package/dist/tables/indexManager.d.ts +65 -0
- package/dist/tables/indexManager.js +294 -0
- package/package.json +1 -1
- package/src/adapters/AdapterFactory.ts +146 -127
- package/src/adapters/DatabaseAdapter.ts +1 -0
- package/src/adapters/LegacyAdapter.ts +5 -2
- package/src/adapters/TablesDBAdapter.ts +18 -10
- package/src/collections/attributes.ts +0 -34
- package/src/collections/methods.ts +11 -126
- package/src/collections/tableOperations.ts +28 -13
- package/src/config/ConfigManager.ts +32 -0
- package/src/shared/attributeMapper.ts +1 -1
- package/src/tables/indexManager.ts +409 -0
- package/dist/shared/indexManager.d.ts +0 -24
- package/dist/shared/indexManager.js +0 -151
- package/src/shared/indexManager.ts +0 -254
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { isIndexEqualToIndex } from "../collections/tableOperations.js";
|
|
2
|
+
import { MessageFormatter } from "../shared/messageFormatter.js";
|
|
3
|
+
import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
|
4
|
+
/**
|
|
5
|
+
* Plan index operations by comparing desired indexes with existing ones
|
|
6
|
+
* Uses the existing isIndexEqualToIndex function for consistent comparison
|
|
7
|
+
*/
|
|
8
|
+
export function planIndexOperations(desiredIndexes, existingIndexes) {
|
|
9
|
+
const plan = {
|
|
10
|
+
toCreate: [],
|
|
11
|
+
toUpdate: [],
|
|
12
|
+
toSkip: [],
|
|
13
|
+
toDelete: []
|
|
14
|
+
};
|
|
15
|
+
for (const desiredIndex of desiredIndexes) {
|
|
16
|
+
const existingIndex = existingIndexes.find(idx => idx.key === desiredIndex.key);
|
|
17
|
+
if (!existingIndex) {
|
|
18
|
+
// Index doesn't exist - create it
|
|
19
|
+
plan.toCreate.push({
|
|
20
|
+
type: 'create',
|
|
21
|
+
index: desiredIndex,
|
|
22
|
+
reason: 'New index'
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
else if (isIndexEqualToIndex(existingIndex, desiredIndex)) {
|
|
26
|
+
// Index exists and is identical - skip it
|
|
27
|
+
plan.toSkip.push({
|
|
28
|
+
type: 'skip',
|
|
29
|
+
index: desiredIndex,
|
|
30
|
+
existingIndex,
|
|
31
|
+
reason: 'Index unchanged'
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
// Index exists but is different - update it
|
|
36
|
+
plan.toUpdate.push({
|
|
37
|
+
type: 'update',
|
|
38
|
+
index: desiredIndex,
|
|
39
|
+
existingIndex,
|
|
40
|
+
reason: 'Index configuration changed'
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return plan;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Plan index deletions for indexes that exist but aren't in the desired configuration
|
|
48
|
+
*/
|
|
49
|
+
export function planIndexDeletions(desiredIndexKeys, existingIndexes) {
|
|
50
|
+
const deletions = [];
|
|
51
|
+
for (const existingIndex of existingIndexes) {
|
|
52
|
+
if (!desiredIndexKeys.has(existingIndex.key)) {
|
|
53
|
+
deletions.push({
|
|
54
|
+
type: 'delete',
|
|
55
|
+
index: existingIndex, // Convert Models.Index to Index for compatibility
|
|
56
|
+
reason: 'Obsolete index'
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return deletions;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Execute index operations with proper error handling and status monitoring
|
|
64
|
+
*/
|
|
65
|
+
export async function executeIndexOperations(adapter, databaseId, tableId, plan) {
|
|
66
|
+
const result = {
|
|
67
|
+
created: [],
|
|
68
|
+
updated: [],
|
|
69
|
+
skipped: [],
|
|
70
|
+
deleted: [],
|
|
71
|
+
errors: [],
|
|
72
|
+
summary: {
|
|
73
|
+
total: 0,
|
|
74
|
+
created: 0,
|
|
75
|
+
updated: 0,
|
|
76
|
+
skipped: 0,
|
|
77
|
+
deleted: 0,
|
|
78
|
+
errors: 0
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
// Execute creates
|
|
82
|
+
for (const operation of plan.toCreate) {
|
|
83
|
+
try {
|
|
84
|
+
await adapter.createIndex({
|
|
85
|
+
databaseId,
|
|
86
|
+
tableId,
|
|
87
|
+
key: operation.index.key,
|
|
88
|
+
type: operation.index.type,
|
|
89
|
+
attributes: operation.index.attributes,
|
|
90
|
+
orders: operation.index.orders || []
|
|
91
|
+
});
|
|
92
|
+
result.created.push(operation.index.key);
|
|
93
|
+
MessageFormatter.success(`Created index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
94
|
+
// Wait for index to become available
|
|
95
|
+
await waitForIndexAvailable(adapter, databaseId, tableId, operation.index.key);
|
|
96
|
+
await delay(150); // Brief delay between operations
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
100
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
101
|
+
MessageFormatter.error(`Failed to create index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Execute updates (delete + recreate)
|
|
105
|
+
for (const operation of plan.toUpdate) {
|
|
106
|
+
try {
|
|
107
|
+
// Delete existing index first
|
|
108
|
+
await adapter.deleteIndex({
|
|
109
|
+
databaseId,
|
|
110
|
+
tableId,
|
|
111
|
+
key: operation.index.key
|
|
112
|
+
});
|
|
113
|
+
await delay(100); // Brief delay for deletion to settle
|
|
114
|
+
// Create new index
|
|
115
|
+
await adapter.createIndex({
|
|
116
|
+
databaseId,
|
|
117
|
+
tableId,
|
|
118
|
+
key: operation.index.key,
|
|
119
|
+
type: operation.index.type,
|
|
120
|
+
attributes: operation.index.attributes,
|
|
121
|
+
orders: operation.index.orders || operation.existingIndex?.orders || []
|
|
122
|
+
});
|
|
123
|
+
result.updated.push(operation.index.key);
|
|
124
|
+
MessageFormatter.success(`Updated index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
125
|
+
// Wait for index to become available
|
|
126
|
+
await waitForIndexAvailable(adapter, databaseId, tableId, operation.index.key);
|
|
127
|
+
await delay(150); // Brief delay between operations
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
131
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
132
|
+
MessageFormatter.error(`Failed to update index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Execute skips
|
|
136
|
+
for (const operation of plan.toSkip) {
|
|
137
|
+
result.skipped.push(operation.index.key);
|
|
138
|
+
MessageFormatter.info(`Index ${operation.index.key} unchanged`, { prefix: 'Indexes' });
|
|
139
|
+
}
|
|
140
|
+
// Calculate summary
|
|
141
|
+
result.summary.total = result.created.length + result.updated.length + result.skipped.length + result.deleted.length;
|
|
142
|
+
result.summary.created = result.created.length;
|
|
143
|
+
result.summary.updated = result.updated.length;
|
|
144
|
+
result.summary.skipped = result.skipped.length;
|
|
145
|
+
result.summary.deleted = result.deleted.length;
|
|
146
|
+
result.summary.errors = result.errors.length;
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Execute index deletions with proper error handling
|
|
151
|
+
*/
|
|
152
|
+
export async function executeIndexDeletions(adapter, databaseId, tableId, deletions) {
|
|
153
|
+
const result = {
|
|
154
|
+
deleted: [],
|
|
155
|
+
errors: []
|
|
156
|
+
};
|
|
157
|
+
for (const operation of deletions) {
|
|
158
|
+
try {
|
|
159
|
+
await adapter.deleteIndex({
|
|
160
|
+
databaseId,
|
|
161
|
+
tableId,
|
|
162
|
+
key: operation.index.key
|
|
163
|
+
});
|
|
164
|
+
result.deleted.push(operation.index.key);
|
|
165
|
+
MessageFormatter.info(`Deleted obsolete index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
166
|
+
// Wait briefly for deletion to settle
|
|
167
|
+
await delay(500);
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
171
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
172
|
+
MessageFormatter.error(`Failed to delete index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Wait for an index to become available with timeout and retry logic
|
|
179
|
+
* This is an adapter-aware version of the logic from collections/indexes.ts
|
|
180
|
+
*/
|
|
181
|
+
async function waitForIndexAvailable(adapter, databaseId, tableId, indexKey, maxWaitTime = 60000, // 1 minute
|
|
182
|
+
checkInterval = 2000 // 2 seconds
|
|
183
|
+
) {
|
|
184
|
+
const startTime = Date.now();
|
|
185
|
+
while (Date.now() - startTime < maxWaitTime) {
|
|
186
|
+
try {
|
|
187
|
+
const indexList = await adapter.listIndexes({ databaseId, tableId });
|
|
188
|
+
const indexes = indexList.data || indexList.indexes || [];
|
|
189
|
+
const index = indexes.find((idx) => idx.key === indexKey);
|
|
190
|
+
if (!index) {
|
|
191
|
+
MessageFormatter.error(`Index '${indexKey}' not found after creation`, undefined, { prefix: 'Indexes' });
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
switch (index.status) {
|
|
195
|
+
case 'available':
|
|
196
|
+
return true;
|
|
197
|
+
case 'failed':
|
|
198
|
+
MessageFormatter.error(`Index '${indexKey}' failed: ${index.error || 'unknown error'}`, undefined, { prefix: 'Indexes' });
|
|
199
|
+
return false;
|
|
200
|
+
case 'stuck':
|
|
201
|
+
MessageFormatter.warning(`Index '${indexKey}' is stuck`, { prefix: 'Indexes' });
|
|
202
|
+
return false;
|
|
203
|
+
case 'processing':
|
|
204
|
+
case 'deleting':
|
|
205
|
+
// Continue waiting
|
|
206
|
+
break;
|
|
207
|
+
default:
|
|
208
|
+
MessageFormatter.warning(`Unknown status '${index.status}' for index '${indexKey}'`, { prefix: 'Indexes' });
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
MessageFormatter.error(`Error checking index '${indexKey}' status: ${error}`, undefined, { prefix: 'Indexes' });
|
|
214
|
+
}
|
|
215
|
+
await delay(checkInterval);
|
|
216
|
+
}
|
|
217
|
+
MessageFormatter.warning(`Timeout waiting for index '${indexKey}' to become available (${maxWaitTime}ms)`, { prefix: 'Indexes' });
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Main function to create/update indexes via adapter
|
|
222
|
+
* This replaces the messy inline code in methods.ts
|
|
223
|
+
*/
|
|
224
|
+
export async function createOrUpdateIndexesViaAdapter(adapter, databaseId, tableId, desiredIndexes, configIndexes) {
|
|
225
|
+
if (!desiredIndexes || desiredIndexes.length === 0) {
|
|
226
|
+
MessageFormatter.info('No indexes to process', { prefix: 'Indexes' });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
MessageFormatter.info(`Processing ${desiredIndexes.length} indexes for table ${tableId}`, { prefix: 'Indexes' });
|
|
230
|
+
try {
|
|
231
|
+
// Get existing indexes
|
|
232
|
+
const existingIdxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
233
|
+
const existingIndexes = existingIdxRes.data || existingIdxRes.indexes || [];
|
|
234
|
+
// Plan operations
|
|
235
|
+
const plan = planIndexOperations(desiredIndexes, existingIndexes);
|
|
236
|
+
// Show plan with icons (consistent with attribute handling)
|
|
237
|
+
const planParts = [];
|
|
238
|
+
if (plan.toCreate.length)
|
|
239
|
+
planParts.push(`➕ ${plan.toCreate.length} (${plan.toCreate.map(op => op.index.key).join(', ')})`);
|
|
240
|
+
if (plan.toUpdate.length)
|
|
241
|
+
planParts.push(`🔧 ${plan.toUpdate.length} (${plan.toUpdate.map(op => op.index.key).join(', ')})`);
|
|
242
|
+
if (plan.toSkip.length)
|
|
243
|
+
planParts.push(`⏭️ ${plan.toSkip.length}`);
|
|
244
|
+
MessageFormatter.info(`Plan → ${planParts.join(' | ') || 'no changes'}`, { prefix: 'Indexes' });
|
|
245
|
+
// Execute operations
|
|
246
|
+
const result = await executeIndexOperations(adapter, databaseId, tableId, plan);
|
|
247
|
+
// Show summary
|
|
248
|
+
MessageFormatter.info(`Summary → ➕ ${result.summary.created} | 🔧 ${result.summary.updated} | ⏭️ ${result.summary.skipped}`, { prefix: 'Indexes' });
|
|
249
|
+
// Handle errors if any
|
|
250
|
+
if (result.errors.length > 0) {
|
|
251
|
+
MessageFormatter.error(`${result.errors.length} index operations failed:`, undefined, { prefix: 'Indexes' });
|
|
252
|
+
for (const error of result.errors) {
|
|
253
|
+
MessageFormatter.error(` ${error.key}: ${error.error}`, undefined, { prefix: 'Indexes' });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
MessageFormatter.error('Failed to process indexes', error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Handle index deletions for obsolete indexes
|
|
264
|
+
*/
|
|
265
|
+
export async function deleteObsoleteIndexesViaAdapter(adapter, databaseId, tableId, desiredIndexKeys) {
|
|
266
|
+
try {
|
|
267
|
+
// Get existing indexes
|
|
268
|
+
const existingIdxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
269
|
+
const existingIndexes = existingIdxRes.data || existingIdxRes.indexes || [];
|
|
270
|
+
// Plan deletions
|
|
271
|
+
const deletions = planIndexDeletions(desiredIndexKeys, existingIndexes);
|
|
272
|
+
if (deletions.length === 0) {
|
|
273
|
+
MessageFormatter.info('Plan → 🗑️ 0 indexes', { prefix: 'Indexes' });
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// Show deletion plan
|
|
277
|
+
MessageFormatter.info(`Plan → 🗑️ ${deletions.length} (${deletions.map(op => op.index.key).join(', ')})`, { prefix: 'Indexes' });
|
|
278
|
+
// Execute deletions
|
|
279
|
+
const result = await executeIndexDeletions(adapter, databaseId, tableId, deletions);
|
|
280
|
+
// Show results
|
|
281
|
+
if (result.deleted.length > 0) {
|
|
282
|
+
MessageFormatter.success(`Deleted ${result.deleted.length} indexes: ${result.deleted.join(', ')}`, { prefix: 'Indexes' });
|
|
283
|
+
}
|
|
284
|
+
if (result.errors.length > 0) {
|
|
285
|
+
MessageFormatter.error(`${result.errors.length} index deletions failed:`, undefined, { prefix: 'Indexes' });
|
|
286
|
+
for (const error of result.errors) {
|
|
287
|
+
MessageFormatter.error(` ${error.key}: ${error.error}`, undefined, { prefix: 'Indexes' });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
MessageFormatter.warning(`Could not evaluate index deletions: ${error?.message || error}`, { prefix: 'Indexes' });
|
|
293
|
+
}
|
|
294
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "appwrite-utils-cli",
|
|
3
3
|
"description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
|
|
4
|
-
"version": "1.9.
|
|
4
|
+
"version": "1.9.7",
|
|
5
5
|
"main": "src/main.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { AppwriteConfig } from "appwrite-utils";
|
|
10
|
-
import { detectAppwriteVersionCached, isVersionAtLeast, type ApiMode, type VersionDetectionResult } from "../utils/versionDetection.js";
|
|
11
|
-
import type
|
|
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';
|
|
10
|
+
import { detectAppwriteVersionCached, isVersionAtLeast, type ApiMode, type VersionDetectionResult } from "../utils/versionDetection.js";
|
|
11
|
+
import { AdapterError, 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
18
|
|
|
19
19
|
export interface AdapterFactoryConfig {
|
|
20
20
|
appwriteEndpoint: string;
|
|
@@ -201,134 +201,153 @@ export class AdapterFactory {
|
|
|
201
201
|
/**
|
|
202
202
|
* Create TablesDB adapter with dynamic import
|
|
203
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,
|
|
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)', {
|
|
265
211
|
endpoint: config.appwriteEndpoint,
|
|
266
212
|
operation: 'createTablesDBAdapter'
|
|
267
213
|
});
|
|
268
214
|
|
|
269
|
-
|
|
270
|
-
|
|
215
|
+
let client = new Client()
|
|
216
|
+
.setEndpoint(config.appwriteEndpoint)
|
|
217
|
+
.setProject(config.appwriteProject);
|
|
218
|
+
|
|
219
|
+
// Set authentication method with mode headers
|
|
220
|
+
// Prefer session with admin mode, fallback to API key with default mode
|
|
221
|
+
if (config.sessionCookie && isValidSessionCookie(config.sessionCookie)) {
|
|
222
|
+
client.setSession(config.sessionCookie);
|
|
223
|
+
client.headers['X-Appwrite-Mode'] = 'admin';
|
|
224
|
+
logger.debug('Using session authentication for TablesDB adapter', {
|
|
225
|
+
project: config.appwriteProject,
|
|
226
|
+
operation: 'createTablesDBAdapter'
|
|
227
|
+
});
|
|
228
|
+
} else if (config.appwriteKey) {
|
|
229
|
+
client.setKey(config.appwriteKey);
|
|
230
|
+
client.headers['X-Appwrite-Mode'] = 'default';
|
|
231
|
+
logger.debug('Using API key authentication for TablesDB adapter', {
|
|
232
|
+
project: config.appwriteProject,
|
|
233
|
+
operation: 'createTablesDBAdapter'
|
|
234
|
+
});
|
|
235
|
+
} else {
|
|
236
|
+
throw new Error("No authentication available for adapter");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const adapter = new TablesDBAdapter(client);
|
|
240
|
+
|
|
241
|
+
const totalDuration = Date.now() - startTime;
|
|
242
|
+
logger.info('TablesDB adapter created successfully', {
|
|
243
|
+
totalDuration,
|
|
244
|
+
endpoint: config.appwriteEndpoint,
|
|
245
|
+
operation: 'createTablesDBAdapter'
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
return { adapter, client };
|
|
249
|
+
|
|
250
|
+
} catch (error) {
|
|
251
|
+
const errorDuration = Date.now() - startTime;
|
|
252
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
253
|
+
|
|
254
|
+
// Analyze the error to determine if fallback is appropriate
|
|
255
|
+
const isAuthError = errorMessage.toLowerCase().includes('unauthorized') ||
|
|
256
|
+
errorMessage.toLowerCase().includes('forbidden') ||
|
|
257
|
+
errorMessage.toLowerCase().includes('invalid') ||
|
|
258
|
+
errorMessage.toLowerCase().includes('authentication');
|
|
259
|
+
|
|
260
|
+
const isVersionError = errorMessage.toLowerCase().includes('not found') ||
|
|
261
|
+
errorMessage.toLowerCase().includes('unsupported') ||
|
|
262
|
+
errorMessage.toLowerCase().includes('tablesdb');
|
|
263
|
+
|
|
264
|
+
// Only fallback to legacy if this is genuinely a TablesDB support issue
|
|
265
|
+
if (isVersionError) {
|
|
266
|
+
MessageFormatter.warning('TablesDB not supported on this server - using legacy adapter', { prefix: "Adapter" });
|
|
267
|
+
logger.warn('TablesDB not supported, falling back to legacy', {
|
|
268
|
+
error: errorMessage,
|
|
269
|
+
errorDuration,
|
|
270
|
+
endpoint: config.appwriteEndpoint,
|
|
271
|
+
operation: 'createTablesDBAdapter'
|
|
272
|
+
});
|
|
273
|
+
return this.createLegacyAdapter(config);
|
|
274
|
+
} else {
|
|
275
|
+
// For auth or other errors, re-throw to surface the real problem
|
|
276
|
+
logger.error('TablesDB adapter creation failed with non-version error', {
|
|
277
|
+
error: errorMessage,
|
|
278
|
+
errorDuration,
|
|
279
|
+
endpoint: config.appwriteEndpoint,
|
|
280
|
+
operation: 'createTablesDBAdapter',
|
|
281
|
+
isAuthError
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
throw new AdapterError(
|
|
285
|
+
`TablesDB adapter creation failed: ${errorMessage}`,
|
|
286
|
+
'TABLESDB_ADAPTER_CREATION_FAILED',
|
|
287
|
+
error instanceof Error ? error : undefined
|
|
288
|
+
);
|
|
289
|
+
}
|
|
271
290
|
}
|
|
272
291
|
}
|
|
273
292
|
|
|
274
293
|
/**
|
|
275
294
|
* Create Legacy adapter with dynamic import
|
|
276
295
|
*/
|
|
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';
|
|
296
|
+
private static async createLegacyAdapter(
|
|
297
|
+
config: AdapterFactoryConfig
|
|
298
|
+
): Promise<{ adapter: DatabaseAdapter; client: any }> {
|
|
299
|
+
const startTime = Date.now();
|
|
300
|
+
|
|
301
|
+
try {
|
|
302
|
+
logger.info('Creating legacy adapter (static SDK imports)', {
|
|
303
|
+
endpoint: config.appwriteEndpoint,
|
|
304
|
+
operation: 'createLegacyAdapter'
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Use pre-configured client or create session-aware client with Legacy Client
|
|
308
|
+
let client: any;
|
|
309
|
+
if (config.preConfiguredClient) {
|
|
310
|
+
client = config.preConfiguredClient;
|
|
311
|
+
} else {
|
|
312
|
+
client = new Client()
|
|
313
|
+
.setEndpoint(config.appwriteEndpoint)
|
|
314
|
+
.setProject(config.appwriteProject);
|
|
315
|
+
|
|
316
|
+
// Set authentication method with mode headers
|
|
317
|
+
// Prefer session with admin mode, fallback to API key with default mode
|
|
318
|
+
if (config.sessionCookie && isValidSessionCookie(config.sessionCookie)) {
|
|
319
|
+
(client as any).setSession(config.sessionCookie);
|
|
320
|
+
client.headers['X-Appwrite-Mode'] = 'admin';
|
|
321
|
+
logger.debug('Using session authentication for Legacy adapter', {
|
|
322
|
+
project: config.appwriteProject,
|
|
323
|
+
operation: 'createLegacyAdapter'
|
|
324
|
+
});
|
|
325
|
+
} else if (config.appwriteKey) {
|
|
326
|
+
client.setKey(config.appwriteKey);
|
|
327
|
+
client.headers['X-Appwrite-Mode'] = 'default';
|
|
328
|
+
logger.debug('Using API key authentication for Legacy adapter', {
|
|
329
|
+
project: config.appwriteProject,
|
|
330
|
+
operation: 'createLegacyAdapter'
|
|
331
|
+
});
|
|
332
|
+
} else {
|
|
333
|
+
throw new Error("No authentication available for adapter");
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const adapter = new LegacyAdapter(client);
|
|
338
|
+
|
|
339
|
+
const totalDuration = Date.now() - startTime;
|
|
340
|
+
logger.info('Legacy adapter created successfully', {
|
|
341
|
+
totalDuration,
|
|
342
|
+
endpoint: config.appwriteEndpoint,
|
|
343
|
+
operation: 'createLegacyAdapter'
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
return { adapter, client };
|
|
347
|
+
|
|
348
|
+
} catch (error) {
|
|
349
|
+
const errorDuration = Date.now() - startTime;
|
|
350
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
332
351
|
|
|
333
352
|
logger.error('Failed to load legacy Appwrite SDK', {
|
|
334
353
|
error: errorMessage,
|
|
@@ -507,4 +526,4 @@ export async function getApiCapabilities(
|
|
|
507
526
|
terminology: metadata.terminology,
|
|
508
527
|
capabilities
|
|
509
528
|
};
|
|
510
|
-
}
|
|
529
|
+
}
|
|
@@ -597,12 +597,15 @@ export class LegacyAdapter extends BaseAdapter {
|
|
|
597
597
|
|
|
598
598
|
case 'enum':
|
|
599
599
|
const enumAttr = existingAttr as Models.AttributeEnum;
|
|
600
|
-
|
|
600
|
+
// Choose elements to send only when provided, otherwise preserve existing
|
|
601
|
+
const provided = params.elements;
|
|
602
|
+
const existing = (enumAttr as any)?.elements;
|
|
603
|
+
const nextElements = (Array.isArray(provided) && provided.length > 0) ? provided : existing;
|
|
601
604
|
result = await this.databases.updateEnumAttribute({
|
|
602
605
|
databaseId: params.databaseId,
|
|
603
606
|
collectionId: params.tableId,
|
|
604
607
|
key: params.key,
|
|
605
|
-
elements:
|
|
608
|
+
elements: nextElements,
|
|
606
609
|
required: params.required ?? enumAttr.required,
|
|
607
610
|
xdefault: params.default !== undefined ? params.default : enumAttr.default
|
|
608
611
|
});
|