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,409 @@
|
|
|
1
|
+
import type { Index } from "appwrite-utils";
|
|
2
|
+
import type { DatabaseAdapter } from "../adapters/DatabaseAdapter.js";
|
|
3
|
+
import type { Models } from "node-appwrite";
|
|
4
|
+
import { isIndexEqualToIndex } from "../collections/tableOperations.js";
|
|
5
|
+
import { MessageFormatter } from "../shared/messageFormatter.js";
|
|
6
|
+
import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
|
7
|
+
|
|
8
|
+
// Enhanced index operation interfaces
|
|
9
|
+
export interface IndexOperation {
|
|
10
|
+
type: 'create' | 'update' | 'skip' | 'delete';
|
|
11
|
+
index: Index;
|
|
12
|
+
existingIndex?: Models.Index;
|
|
13
|
+
reason?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface IndexOperationPlan {
|
|
17
|
+
toCreate: IndexOperation[];
|
|
18
|
+
toUpdate: IndexOperation[];
|
|
19
|
+
toSkip: IndexOperation[];
|
|
20
|
+
toDelete: IndexOperation[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface IndexExecutionResult {
|
|
24
|
+
created: string[];
|
|
25
|
+
updated: string[];
|
|
26
|
+
skipped: string[];
|
|
27
|
+
deleted: string[];
|
|
28
|
+
errors: Array<{ key: string; error: string }>;
|
|
29
|
+
summary: {
|
|
30
|
+
total: number;
|
|
31
|
+
created: number;
|
|
32
|
+
updated: number;
|
|
33
|
+
skipped: number;
|
|
34
|
+
deleted: number;
|
|
35
|
+
errors: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Plan index operations by comparing desired indexes with existing ones
|
|
41
|
+
* Uses the existing isIndexEqualToIndex function for consistent comparison
|
|
42
|
+
*/
|
|
43
|
+
export function planIndexOperations(
|
|
44
|
+
desiredIndexes: Index[],
|
|
45
|
+
existingIndexes: Models.Index[]
|
|
46
|
+
): IndexOperationPlan {
|
|
47
|
+
const plan: IndexOperationPlan = {
|
|
48
|
+
toCreate: [],
|
|
49
|
+
toUpdate: [],
|
|
50
|
+
toSkip: [],
|
|
51
|
+
toDelete: []
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
for (const desiredIndex of desiredIndexes) {
|
|
55
|
+
const existingIndex = existingIndexes.find(idx => idx.key === desiredIndex.key);
|
|
56
|
+
|
|
57
|
+
if (!existingIndex) {
|
|
58
|
+
// Index doesn't exist - create it
|
|
59
|
+
plan.toCreate.push({
|
|
60
|
+
type: 'create',
|
|
61
|
+
index: desiredIndex,
|
|
62
|
+
reason: 'New index'
|
|
63
|
+
});
|
|
64
|
+
} else if (isIndexEqualToIndex(existingIndex, desiredIndex)) {
|
|
65
|
+
// Index exists and is identical - skip it
|
|
66
|
+
plan.toSkip.push({
|
|
67
|
+
type: 'skip',
|
|
68
|
+
index: desiredIndex,
|
|
69
|
+
existingIndex,
|
|
70
|
+
reason: 'Index unchanged'
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
// Index exists but is different - update it
|
|
74
|
+
plan.toUpdate.push({
|
|
75
|
+
type: 'update',
|
|
76
|
+
index: desiredIndex,
|
|
77
|
+
existingIndex,
|
|
78
|
+
reason: 'Index configuration changed'
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return plan;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Plan index deletions for indexes that exist but aren't in the desired configuration
|
|
88
|
+
*/
|
|
89
|
+
export function planIndexDeletions(
|
|
90
|
+
desiredIndexKeys: Set<string>,
|
|
91
|
+
existingIndexes: Models.Index[]
|
|
92
|
+
): IndexOperation[] {
|
|
93
|
+
const deletions: IndexOperation[] = [];
|
|
94
|
+
|
|
95
|
+
for (const existingIndex of existingIndexes) {
|
|
96
|
+
if (!desiredIndexKeys.has(existingIndex.key)) {
|
|
97
|
+
deletions.push({
|
|
98
|
+
type: 'delete',
|
|
99
|
+
index: existingIndex as Index, // Convert Models.Index to Index for compatibility
|
|
100
|
+
reason: 'Obsolete index'
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return deletions;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Execute index operations with proper error handling and status monitoring
|
|
110
|
+
*/
|
|
111
|
+
export async function executeIndexOperations(
|
|
112
|
+
adapter: DatabaseAdapter,
|
|
113
|
+
databaseId: string,
|
|
114
|
+
tableId: string,
|
|
115
|
+
plan: IndexOperationPlan
|
|
116
|
+
): Promise<IndexExecutionResult> {
|
|
117
|
+
const result: IndexExecutionResult = {
|
|
118
|
+
created: [],
|
|
119
|
+
updated: [],
|
|
120
|
+
skipped: [],
|
|
121
|
+
deleted: [],
|
|
122
|
+
errors: [],
|
|
123
|
+
summary: {
|
|
124
|
+
total: 0,
|
|
125
|
+
created: 0,
|
|
126
|
+
updated: 0,
|
|
127
|
+
skipped: 0,
|
|
128
|
+
deleted: 0,
|
|
129
|
+
errors: 0
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Execute creates
|
|
134
|
+
for (const operation of plan.toCreate) {
|
|
135
|
+
try {
|
|
136
|
+
await adapter.createIndex({
|
|
137
|
+
databaseId,
|
|
138
|
+
tableId,
|
|
139
|
+
key: operation.index.key,
|
|
140
|
+
type: operation.index.type,
|
|
141
|
+
attributes: operation.index.attributes,
|
|
142
|
+
orders: operation.index.orders || []
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
result.created.push(operation.index.key);
|
|
146
|
+
MessageFormatter.success(`Created index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
147
|
+
|
|
148
|
+
// Wait for index to become available
|
|
149
|
+
await waitForIndexAvailable(adapter, databaseId, tableId, operation.index.key);
|
|
150
|
+
|
|
151
|
+
await delay(150); // Brief delay between operations
|
|
152
|
+
} catch (error) {
|
|
153
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
154
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
155
|
+
MessageFormatter.error(`Failed to create index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Execute updates (delete + recreate)
|
|
160
|
+
for (const operation of plan.toUpdate) {
|
|
161
|
+
try {
|
|
162
|
+
// Delete existing index first
|
|
163
|
+
await adapter.deleteIndex({
|
|
164
|
+
databaseId,
|
|
165
|
+
tableId,
|
|
166
|
+
key: operation.index.key
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
await delay(100); // Brief delay for deletion to settle
|
|
170
|
+
|
|
171
|
+
// Create new index
|
|
172
|
+
await adapter.createIndex({
|
|
173
|
+
databaseId,
|
|
174
|
+
tableId,
|
|
175
|
+
key: operation.index.key,
|
|
176
|
+
type: operation.index.type,
|
|
177
|
+
attributes: operation.index.attributes,
|
|
178
|
+
orders: operation.index.orders || operation.existingIndex?.orders || []
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
result.updated.push(operation.index.key);
|
|
182
|
+
MessageFormatter.success(`Updated index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
183
|
+
|
|
184
|
+
// Wait for index to become available
|
|
185
|
+
await waitForIndexAvailable(adapter, databaseId, tableId, operation.index.key);
|
|
186
|
+
|
|
187
|
+
await delay(150); // Brief delay between operations
|
|
188
|
+
} catch (error) {
|
|
189
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
190
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
191
|
+
MessageFormatter.error(`Failed to update index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Execute skips
|
|
196
|
+
for (const operation of plan.toSkip) {
|
|
197
|
+
result.skipped.push(operation.index.key);
|
|
198
|
+
MessageFormatter.info(`Index ${operation.index.key} unchanged`, { prefix: 'Indexes' });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Calculate summary
|
|
202
|
+
result.summary.total = result.created.length + result.updated.length + result.skipped.length + result.deleted.length;
|
|
203
|
+
result.summary.created = result.created.length;
|
|
204
|
+
result.summary.updated = result.updated.length;
|
|
205
|
+
result.summary.skipped = result.skipped.length;
|
|
206
|
+
result.summary.deleted = result.deleted.length;
|
|
207
|
+
result.summary.errors = result.errors.length;
|
|
208
|
+
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Execute index deletions with proper error handling
|
|
214
|
+
*/
|
|
215
|
+
export async function executeIndexDeletions(
|
|
216
|
+
adapter: DatabaseAdapter,
|
|
217
|
+
databaseId: string,
|
|
218
|
+
tableId: string,
|
|
219
|
+
deletions: IndexOperation[]
|
|
220
|
+
): Promise<{ deleted: string[]; errors: Array<{ key: string; error: string }> }> {
|
|
221
|
+
const result = {
|
|
222
|
+
deleted: [] as string[],
|
|
223
|
+
errors: [] as Array<{ key: string; error: string }>
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
for (const operation of deletions) {
|
|
227
|
+
try {
|
|
228
|
+
await adapter.deleteIndex({
|
|
229
|
+
databaseId,
|
|
230
|
+
tableId,
|
|
231
|
+
key: operation.index.key
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
result.deleted.push(operation.index.key);
|
|
235
|
+
MessageFormatter.info(`Deleted obsolete index ${operation.index.key}`, { prefix: 'Indexes' });
|
|
236
|
+
|
|
237
|
+
// Wait briefly for deletion to settle
|
|
238
|
+
await delay(500);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
241
|
+
result.errors.push({ key: operation.index.key, error: errorMessage });
|
|
242
|
+
MessageFormatter.error(`Failed to delete index ${operation.index.key}`, error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Wait for an index to become available with timeout and retry logic
|
|
251
|
+
* This is an adapter-aware version of the logic from collections/indexes.ts
|
|
252
|
+
*/
|
|
253
|
+
async function waitForIndexAvailable(
|
|
254
|
+
adapter: DatabaseAdapter,
|
|
255
|
+
databaseId: string,
|
|
256
|
+
tableId: string,
|
|
257
|
+
indexKey: string,
|
|
258
|
+
maxWaitTime: number = 60000, // 1 minute
|
|
259
|
+
checkInterval: number = 2000 // 2 seconds
|
|
260
|
+
): Promise<boolean> {
|
|
261
|
+
const startTime = Date.now();
|
|
262
|
+
|
|
263
|
+
while (Date.now() - startTime < maxWaitTime) {
|
|
264
|
+
try {
|
|
265
|
+
const indexList = await adapter.listIndexes({ databaseId, tableId });
|
|
266
|
+
const indexes: any[] = (indexList as any).data || (indexList as any).indexes || [];
|
|
267
|
+
const index = indexes.find((idx: any) => idx.key === indexKey);
|
|
268
|
+
|
|
269
|
+
if (!index) {
|
|
270
|
+
MessageFormatter.error(`Index '${indexKey}' not found after creation`, undefined, { prefix: 'Indexes' });
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
switch (index.status) {
|
|
275
|
+
case 'available':
|
|
276
|
+
return true;
|
|
277
|
+
|
|
278
|
+
case 'failed':
|
|
279
|
+
MessageFormatter.error(`Index '${indexKey}' failed: ${index.error || 'unknown error'}`, undefined, { prefix: 'Indexes' });
|
|
280
|
+
return false;
|
|
281
|
+
|
|
282
|
+
case 'stuck':
|
|
283
|
+
MessageFormatter.warning(`Index '${indexKey}' is stuck`, { prefix: 'Indexes' });
|
|
284
|
+
return false;
|
|
285
|
+
|
|
286
|
+
case 'processing':
|
|
287
|
+
case 'deleting':
|
|
288
|
+
// Continue waiting
|
|
289
|
+
break;
|
|
290
|
+
|
|
291
|
+
default:
|
|
292
|
+
MessageFormatter.warning(`Unknown status '${index.status}' for index '${indexKey}'`, { prefix: 'Indexes' });
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
} catch (error) {
|
|
296
|
+
MessageFormatter.error(`Error checking index '${indexKey}' status: ${error}`, undefined, { prefix: 'Indexes' });
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
await delay(checkInterval);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
MessageFormatter.warning(`Timeout waiting for index '${indexKey}' to become available (${maxWaitTime}ms)`, { prefix: 'Indexes' });
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Main function to create/update indexes via adapter
|
|
308
|
+
* This replaces the messy inline code in methods.ts
|
|
309
|
+
*/
|
|
310
|
+
export async function createOrUpdateIndexesViaAdapter(
|
|
311
|
+
adapter: DatabaseAdapter,
|
|
312
|
+
databaseId: string,
|
|
313
|
+
tableId: string,
|
|
314
|
+
desiredIndexes: Index[],
|
|
315
|
+
configIndexes?: Index[]
|
|
316
|
+
): Promise<void> {
|
|
317
|
+
if (!desiredIndexes || desiredIndexes.length === 0) {
|
|
318
|
+
MessageFormatter.info('No indexes to process', { prefix: 'Indexes' });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
MessageFormatter.info(`Processing ${desiredIndexes.length} indexes for table ${tableId}`, { prefix: 'Indexes' });
|
|
323
|
+
|
|
324
|
+
try {
|
|
325
|
+
// Get existing indexes
|
|
326
|
+
const existingIdxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
327
|
+
const existingIndexes: Models.Index[] = (existingIdxRes as any).data || (existingIdxRes as any).indexes || [];
|
|
328
|
+
|
|
329
|
+
// Plan operations
|
|
330
|
+
const plan = planIndexOperations(desiredIndexes, existingIndexes);
|
|
331
|
+
|
|
332
|
+
// Show plan with icons (consistent with attribute handling)
|
|
333
|
+
const planParts: string[] = [];
|
|
334
|
+
if (plan.toCreate.length) planParts.push(`➕ ${plan.toCreate.length} (${plan.toCreate.map(op => op.index.key).join(', ')})`);
|
|
335
|
+
if (plan.toUpdate.length) planParts.push(`🔧 ${plan.toUpdate.length} (${plan.toUpdate.map(op => op.index.key).join(', ')})`);
|
|
336
|
+
if (plan.toSkip.length) planParts.push(`⏭️ ${plan.toSkip.length}`);
|
|
337
|
+
|
|
338
|
+
MessageFormatter.info(`Plan → ${planParts.join(' | ') || 'no changes'}`, { prefix: 'Indexes' });
|
|
339
|
+
|
|
340
|
+
// Execute operations
|
|
341
|
+
const result = await executeIndexOperations(adapter, databaseId, tableId, plan);
|
|
342
|
+
|
|
343
|
+
// Show summary
|
|
344
|
+
MessageFormatter.info(
|
|
345
|
+
`Summary → ➕ ${result.summary.created} | 🔧 ${result.summary.updated} | ⏭️ ${result.summary.skipped}`,
|
|
346
|
+
{ prefix: 'Indexes' }
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// Handle errors if any
|
|
350
|
+
if (result.errors.length > 0) {
|
|
351
|
+
MessageFormatter.error(`${result.errors.length} index operations failed:`, undefined, { prefix: 'Indexes' });
|
|
352
|
+
for (const error of result.errors) {
|
|
353
|
+
MessageFormatter.error(` ${error.key}: ${error.error}`, undefined, { prefix: 'Indexes' });
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
} catch (error) {
|
|
358
|
+
MessageFormatter.error('Failed to process indexes', error instanceof Error ? error : new Error(String(error)), { prefix: 'Indexes' });
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Handle index deletions for obsolete indexes
|
|
365
|
+
*/
|
|
366
|
+
export async function deleteObsoleteIndexesViaAdapter(
|
|
367
|
+
adapter: DatabaseAdapter,
|
|
368
|
+
databaseId: string,
|
|
369
|
+
tableId: string,
|
|
370
|
+
desiredIndexKeys: Set<string>
|
|
371
|
+
): Promise<void> {
|
|
372
|
+
try {
|
|
373
|
+
// Get existing indexes
|
|
374
|
+
const existingIdxRes = await adapter.listIndexes({ databaseId, tableId });
|
|
375
|
+
const existingIndexes: Models.Index[] = (existingIdxRes as any).data || (existingIdxRes as any).indexes || [];
|
|
376
|
+
|
|
377
|
+
// Plan deletions
|
|
378
|
+
const deletions = planIndexDeletions(desiredIndexKeys, existingIndexes);
|
|
379
|
+
|
|
380
|
+
if (deletions.length === 0) {
|
|
381
|
+
MessageFormatter.info('Plan → 🗑️ 0 indexes', { prefix: 'Indexes' });
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Show deletion plan
|
|
386
|
+
MessageFormatter.info(
|
|
387
|
+
`Plan → 🗑️ ${deletions.length} (${deletions.map(op => op.index.key).join(', ')})`,
|
|
388
|
+
{ prefix: 'Indexes' }
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
// Execute deletions
|
|
392
|
+
const result = await executeIndexDeletions(adapter, databaseId, tableId, deletions);
|
|
393
|
+
|
|
394
|
+
// Show results
|
|
395
|
+
if (result.deleted.length > 0) {
|
|
396
|
+
MessageFormatter.success(`Deleted ${result.deleted.length} indexes: ${result.deleted.join(', ')}`, { prefix: 'Indexes' });
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (result.errors.length > 0) {
|
|
400
|
+
MessageFormatter.error(`${result.errors.length} index deletions failed:`, undefined, { prefix: 'Indexes' });
|
|
401
|
+
for (const error of result.errors) {
|
|
402
|
+
MessageFormatter.error(` ${error.key}: ${error.error}`, undefined, { prefix: 'Indexes' });
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
} catch (error) {
|
|
407
|
+
MessageFormatter.warning(`Could not evaluate index deletions: ${(error as Error)?.message || error}`, { prefix: 'Indexes' });
|
|
408
|
+
}
|
|
409
|
+
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { type Index, type CollectionCreate } from "appwrite-utils";
|
|
2
|
-
import { Databases, type Models } from "node-appwrite";
|
|
3
|
-
export declare const indexesSame: (databaseIndex: Models.Index, configIndex: Index) => boolean;
|
|
4
|
-
export declare const createOrUpdateIndex: (dbId: string, db: Databases, collectionId: string, index: Index, options?: {
|
|
5
|
-
verbose?: boolean;
|
|
6
|
-
forceRecreate?: boolean;
|
|
7
|
-
}) => Promise<Models.Index | null>;
|
|
8
|
-
export declare const createOrUpdateIndexes: (dbId: string, db: Databases, collectionId: string, indexes: Index[], options?: {
|
|
9
|
-
verbose?: boolean;
|
|
10
|
-
forceRecreate?: boolean;
|
|
11
|
-
}) => Promise<void>;
|
|
12
|
-
export declare const createUpdateCollectionIndexes: (db: Databases, dbId: string, collection: Models.Collection, collectionConfig: CollectionCreate, options?: {
|
|
13
|
-
verbose?: boolean;
|
|
14
|
-
forceRecreate?: boolean;
|
|
15
|
-
}) => Promise<void>;
|
|
16
|
-
export declare const deleteObsoleteIndexes: (db: Databases, dbId: string, collection: Models.Collection, collectionConfig: CollectionCreate, options?: {
|
|
17
|
-
verbose?: boolean;
|
|
18
|
-
}) => Promise<void>;
|
|
19
|
-
export declare const validateIndexConfiguration: (indexes: Index[], options?: {
|
|
20
|
-
verbose?: boolean;
|
|
21
|
-
}) => {
|
|
22
|
-
valid: boolean;
|
|
23
|
-
errors: string[];
|
|
24
|
-
};
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import {} from "appwrite-utils";
|
|
2
|
-
import { Databases, IndexType, Query } from "node-appwrite";
|
|
3
|
-
import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
|
4
|
-
import chalk from "chalk";
|
|
5
|
-
import pLimit from "p-limit";
|
|
6
|
-
import { MessageFormatter } from "./messageFormatter.js";
|
|
7
|
-
// Concurrency limits for different operations
|
|
8
|
-
const indexLimit = pLimit(3); // Low limit for index operations
|
|
9
|
-
const queryLimit = pLimit(25); // Higher limit for read operations
|
|
10
|
-
export const indexesSame = (databaseIndex, configIndex) => {
|
|
11
|
-
return (databaseIndex.key === configIndex.key &&
|
|
12
|
-
databaseIndex.type === configIndex.type &&
|
|
13
|
-
JSON.stringify(databaseIndex.attributes) === JSON.stringify(configIndex.attributes) &&
|
|
14
|
-
JSON.stringify(databaseIndex.orders) === JSON.stringify(configIndex.orders));
|
|
15
|
-
};
|
|
16
|
-
export const createOrUpdateIndex = async (dbId, db, collectionId, index, options = {}) => {
|
|
17
|
-
const { verbose = false, forceRecreate = false } = options;
|
|
18
|
-
return await indexLimit(async () => {
|
|
19
|
-
// Check for existing index
|
|
20
|
-
const existingIndexes = await queryLimit(() => tryAwaitWithRetry(async () => await db.listIndexes(dbId, collectionId, [Query.equal("key", index.key)])));
|
|
21
|
-
let shouldCreate = false;
|
|
22
|
-
let existingIndex;
|
|
23
|
-
if (existingIndexes.total > 0) {
|
|
24
|
-
existingIndex = existingIndexes.indexes[0];
|
|
25
|
-
if (forceRecreate || !indexesSame(existingIndex, index)) {
|
|
26
|
-
if (verbose) {
|
|
27
|
-
MessageFormatter.warning(`Updating index ${index.key} in collection ${collectionId}`, { prefix: "Index Manager" });
|
|
28
|
-
}
|
|
29
|
-
// Delete existing index
|
|
30
|
-
await tryAwaitWithRetry(async () => {
|
|
31
|
-
await db.deleteIndex(dbId, collectionId, existingIndex.key);
|
|
32
|
-
});
|
|
33
|
-
await delay(500); // Wait for deletion to complete
|
|
34
|
-
shouldCreate = true;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
if (verbose) {
|
|
38
|
-
MessageFormatter.success(`Index ${index.key} is up to date`, { prefix: "Index Manager" });
|
|
39
|
-
}
|
|
40
|
-
return existingIndex;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
shouldCreate = true;
|
|
45
|
-
if (verbose) {
|
|
46
|
-
MessageFormatter.info(`Creating index ${index.key} in collection ${collectionId}`, { prefix: "Index Manager" });
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (shouldCreate) {
|
|
50
|
-
const newIndex = await tryAwaitWithRetry(async () => {
|
|
51
|
-
return await db.createIndex(dbId, collectionId, index.key, index.type, index.attributes, index.orders);
|
|
52
|
-
});
|
|
53
|
-
if (verbose) {
|
|
54
|
-
MessageFormatter.success(`Created index ${index.key}`, { prefix: "Index Manager" });
|
|
55
|
-
}
|
|
56
|
-
return newIndex;
|
|
57
|
-
}
|
|
58
|
-
return null;
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
export const createOrUpdateIndexes = async (dbId, db, collectionId, indexes, options = {}) => {
|
|
62
|
-
const { verbose = false } = options;
|
|
63
|
-
if (!indexes || indexes.length === 0) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
if (verbose) {
|
|
67
|
-
MessageFormatter.info(`Processing ${indexes.length} indexes for collection ${collectionId}`, { prefix: "Index Manager" });
|
|
68
|
-
}
|
|
69
|
-
// Process indexes sequentially to avoid conflicts
|
|
70
|
-
for (const index of indexes) {
|
|
71
|
-
try {
|
|
72
|
-
await createOrUpdateIndex(dbId, db, collectionId, index, options);
|
|
73
|
-
// Add delay between index operations to prevent rate limiting
|
|
74
|
-
await delay(250);
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
MessageFormatter.error(`Failed to process index ${index.key}`, error, { prefix: "Index Manager" });
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (verbose) {
|
|
82
|
-
MessageFormatter.success(`Completed processing indexes for collection ${collectionId}`, { prefix: "Index Manager" });
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
export const createUpdateCollectionIndexes = async (db, dbId, collection, collectionConfig, options = {}) => {
|
|
86
|
-
if (!collectionConfig.indexes)
|
|
87
|
-
return;
|
|
88
|
-
await createOrUpdateIndexes(dbId, db, collection.$id, collectionConfig.indexes, options);
|
|
89
|
-
};
|
|
90
|
-
export const deleteObsoleteIndexes = async (db, dbId, collection, collectionConfig, options = {}) => {
|
|
91
|
-
const { verbose = false } = options;
|
|
92
|
-
const configIndexes = collectionConfig.indexes || [];
|
|
93
|
-
const configIndexKeys = new Set(configIndexes.map(index => index.key));
|
|
94
|
-
// Get all existing indexes
|
|
95
|
-
const existingIndexes = await queryLimit(() => tryAwaitWithRetry(async () => await db.listIndexes(dbId, collection.$id)));
|
|
96
|
-
// Find indexes that exist in the database but not in the config
|
|
97
|
-
const obsoleteIndexes = existingIndexes.indexes.filter((index) => !configIndexKeys.has(index.key));
|
|
98
|
-
if (obsoleteIndexes.length === 0) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (verbose) {
|
|
102
|
-
MessageFormatter.warning(`Removing ${obsoleteIndexes.length} obsolete indexes from collection ${collection.name}`, { prefix: "Index Manager" });
|
|
103
|
-
}
|
|
104
|
-
// Process deletions with rate limiting
|
|
105
|
-
for (const index of obsoleteIndexes) {
|
|
106
|
-
await indexLimit(async () => {
|
|
107
|
-
await tryAwaitWithRetry(async () => {
|
|
108
|
-
await db.deleteIndex(dbId, collection.$id, index.key);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
if (verbose) {
|
|
112
|
-
MessageFormatter.info(`Deleted obsolete index ${index.key}`, { prefix: "Index Manager" });
|
|
113
|
-
}
|
|
114
|
-
await delay(250);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
export const validateIndexConfiguration = (indexes, options = {}) => {
|
|
118
|
-
const { verbose = false } = options;
|
|
119
|
-
const errors = [];
|
|
120
|
-
for (const index of indexes) {
|
|
121
|
-
// Validate required fields
|
|
122
|
-
if (!index.key) {
|
|
123
|
-
errors.push(`Index missing required 'key' field`);
|
|
124
|
-
}
|
|
125
|
-
if (!index.type) {
|
|
126
|
-
errors.push(`Index '${index.key}' missing required 'type' field`);
|
|
127
|
-
}
|
|
128
|
-
if (!index.attributes || index.attributes.length === 0) {
|
|
129
|
-
errors.push(`Index '${index.key}' missing required 'attributes' field`);
|
|
130
|
-
}
|
|
131
|
-
// Validate index type
|
|
132
|
-
const validTypes = Object.values(IndexType);
|
|
133
|
-
if (index.type && !validTypes.includes(index.type)) {
|
|
134
|
-
errors.push(`Index '${index.key}' has invalid type '${index.type}'. Valid types: ${validTypes.join(', ')}`);
|
|
135
|
-
}
|
|
136
|
-
// Validate orders array matches attributes length (if provided)
|
|
137
|
-
if (index.orders && index.attributes && index.orders.length !== index.attributes.length) {
|
|
138
|
-
errors.push(`Index '${index.key}' orders array length (${index.orders.length}) does not match attributes array length (${index.attributes.length})`);
|
|
139
|
-
}
|
|
140
|
-
// Check for duplicate keys within the same collection
|
|
141
|
-
const duplicateKeys = indexes.filter(i => i.key === index.key);
|
|
142
|
-
if (duplicateKeys.length > 1) {
|
|
143
|
-
errors.push(`Duplicate index key '${index.key}' found`);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
if (verbose && errors.length > 0) {
|
|
147
|
-
MessageFormatter.error("Index validation errors", undefined, { prefix: "Index Manager" });
|
|
148
|
-
errors.forEach(error => MessageFormatter.error(` - ${error}`, undefined, { prefix: "Index Manager" }));
|
|
149
|
-
}
|
|
150
|
-
return { valid: errors.length === 0, errors };
|
|
151
|
-
};
|