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.
@@ -1,254 +0,0 @@
1
- import { type Index, type CollectionCreate } from "appwrite-utils";
2
- import { Databases, IndexType, Query, type Models } 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
-
8
- // Concurrency limits for different operations
9
- const indexLimit = pLimit(3); // Low limit for index operations
10
- const queryLimit = pLimit(25); // Higher limit for read operations
11
-
12
- export const indexesSame = (
13
- databaseIndex: Models.Index,
14
- configIndex: Index
15
- ): boolean => {
16
- return (
17
- databaseIndex.key === configIndex.key &&
18
- databaseIndex.type === configIndex.type &&
19
- JSON.stringify(databaseIndex.attributes) === JSON.stringify(configIndex.attributes) &&
20
- JSON.stringify(databaseIndex.orders) === JSON.stringify(configIndex.orders)
21
- );
22
- };
23
-
24
- export const createOrUpdateIndex = async (
25
- dbId: string,
26
- db: Databases,
27
- collectionId: string,
28
- index: Index,
29
- options: {
30
- verbose?: boolean;
31
- forceRecreate?: boolean;
32
- } = {}
33
- ): Promise<Models.Index | null> => {
34
- const { verbose = false, forceRecreate = false } = options;
35
-
36
- return await indexLimit(async () => {
37
- // Check for existing index
38
- const existingIndexes = await queryLimit(() =>
39
- tryAwaitWithRetry(async () =>
40
- await db.listIndexes(dbId, collectionId, [Query.equal("key", index.key)])
41
- )
42
- );
43
-
44
- let shouldCreate = false;
45
- let existingIndex: Models.Index | undefined;
46
-
47
- if (existingIndexes.total > 0) {
48
- existingIndex = existingIndexes.indexes[0];
49
-
50
- if (forceRecreate || !indexesSame(existingIndex, index)) {
51
- if (verbose) {
52
- MessageFormatter.warning(`Updating index ${index.key} in collection ${collectionId}`, { prefix: "Index Manager" });
53
- }
54
-
55
- // Delete existing index
56
- await tryAwaitWithRetry(async () => {
57
- await db.deleteIndex(dbId, collectionId, existingIndex!.key);
58
- });
59
-
60
- await delay(500); // Wait for deletion to complete
61
- shouldCreate = true;
62
- } else {
63
- if (verbose) {
64
- MessageFormatter.success(`Index ${index.key} is up to date`, { prefix: "Index Manager" });
65
- }
66
- return existingIndex;
67
- }
68
- } else {
69
- shouldCreate = true;
70
- if (verbose) {
71
- MessageFormatter.info(`Creating index ${index.key} in collection ${collectionId}`, { prefix: "Index Manager" });
72
- }
73
- }
74
-
75
- if (shouldCreate) {
76
- const newIndex = await tryAwaitWithRetry(async () => {
77
- return await db.createIndex(
78
- dbId,
79
- collectionId,
80
- index.key,
81
- index.type as IndexType,
82
- index.attributes,
83
- index.orders
84
- );
85
- });
86
-
87
- if (verbose) {
88
- MessageFormatter.success(`Created index ${index.key}`, { prefix: "Index Manager" });
89
- }
90
-
91
- return newIndex;
92
- }
93
-
94
- return null;
95
- });
96
- };
97
-
98
- export const createOrUpdateIndexes = async (
99
- dbId: string,
100
- db: Databases,
101
- collectionId: string,
102
- indexes: Index[],
103
- options: {
104
- verbose?: boolean;
105
- forceRecreate?: boolean;
106
- } = {}
107
- ): Promise<void> => {
108
- const { verbose = false } = options;
109
-
110
- if (!indexes || indexes.length === 0) {
111
- return;
112
- }
113
-
114
- if (verbose) {
115
- MessageFormatter.info(`Processing ${indexes.length} indexes for collection ${collectionId}`, { prefix: "Index Manager" });
116
- }
117
-
118
- // Process indexes sequentially to avoid conflicts
119
- for (const index of indexes) {
120
- try {
121
- await createOrUpdateIndex(dbId, db, collectionId, index, options);
122
-
123
- // Add delay between index operations to prevent rate limiting
124
- await delay(250);
125
- } catch (error) {
126
- MessageFormatter.error(`Failed to process index ${index.key}`, error as Error, { prefix: "Index Manager" });
127
- throw error;
128
- }
129
- }
130
-
131
- if (verbose) {
132
- MessageFormatter.success(`Completed processing indexes for collection ${collectionId}`, { prefix: "Index Manager" });
133
- }
134
- };
135
-
136
- export const createUpdateCollectionIndexes = async (
137
- db: Databases,
138
- dbId: string,
139
- collection: Models.Collection,
140
- collectionConfig: CollectionCreate,
141
- options: {
142
- verbose?: boolean;
143
- forceRecreate?: boolean;
144
- } = {}
145
- ): Promise<void> => {
146
- if (!collectionConfig.indexes) return;
147
-
148
- await createOrUpdateIndexes(
149
- dbId,
150
- db,
151
- collection.$id,
152
- collectionConfig.indexes,
153
- options
154
- );
155
- };
156
-
157
- export const deleteObsoleteIndexes = async (
158
- db: Databases,
159
- dbId: string,
160
- collection: Models.Collection,
161
- collectionConfig: CollectionCreate,
162
- options: {
163
- verbose?: boolean;
164
- } = {}
165
- ): Promise<void> => {
166
- const { verbose = false } = options;
167
-
168
- const configIndexes = collectionConfig.indexes || [];
169
- const configIndexKeys = new Set(configIndexes.map(index => index.key));
170
-
171
- // Get all existing indexes
172
- const existingIndexes = await queryLimit(() =>
173
- tryAwaitWithRetry(async () =>
174
- await db.listIndexes(dbId, collection.$id)
175
- )
176
- );
177
-
178
- // Find indexes that exist in the database but not in the config
179
- const obsoleteIndexes = existingIndexes.indexes.filter(
180
- (index) => !configIndexKeys.has(index.key)
181
- );
182
-
183
- if (obsoleteIndexes.length === 0) {
184
- return;
185
- }
186
-
187
- if (verbose) {
188
- MessageFormatter.warning(`Removing ${obsoleteIndexes.length} obsolete indexes from collection ${collection.name}`, { prefix: "Index Manager" });
189
- }
190
-
191
- // Process deletions with rate limiting
192
- for (const index of obsoleteIndexes) {
193
- await indexLimit(async () => {
194
- await tryAwaitWithRetry(async () => {
195
- await db.deleteIndex(dbId, collection.$id, index.key);
196
- });
197
- });
198
-
199
- if (verbose) {
200
- MessageFormatter.info(`Deleted obsolete index ${index.key}`, { prefix: "Index Manager" });
201
- }
202
-
203
- await delay(250);
204
- }
205
- };
206
-
207
- export const validateIndexConfiguration = (
208
- indexes: Index[],
209
- options: {
210
- verbose?: boolean;
211
- } = {}
212
- ): { valid: boolean; errors: string[] } => {
213
- const { verbose = false } = options;
214
- const errors: string[] = [];
215
-
216
- for (const index of indexes) {
217
- // Validate required fields
218
- if (!index.key) {
219
- errors.push(`Index missing required 'key' field`);
220
- }
221
-
222
- if (!index.type) {
223
- errors.push(`Index '${index.key}' missing required 'type' field`);
224
- }
225
-
226
- if (!index.attributes || index.attributes.length === 0) {
227
- errors.push(`Index '${index.key}' missing required 'attributes' field`);
228
- }
229
-
230
- // Validate index type
231
- const validTypes = Object.values(IndexType);
232
- if (index.type && !validTypes.includes(index.type as IndexType)) {
233
- errors.push(`Index '${index.key}' has invalid type '${index.type}'. Valid types: ${validTypes.join(', ')}`);
234
- }
235
-
236
- // Validate orders array matches attributes length (if provided)
237
- if (index.orders && index.attributes && index.orders.length !== index.attributes.length) {
238
- errors.push(`Index '${index.key}' orders array length (${index.orders.length}) does not match attributes array length (${index.attributes.length})`);
239
- }
240
-
241
- // Check for duplicate keys within the same collection
242
- const duplicateKeys = indexes.filter(i => i.key === index.key);
243
- if (duplicateKeys.length > 1) {
244
- errors.push(`Duplicate index key '${index.key}' found`);
245
- }
246
- }
247
-
248
- if (verbose && errors.length > 0) {
249
- MessageFormatter.error("Index validation errors", undefined, { prefix: "Index Manager" });
250
- errors.forEach(error => MessageFormatter.error(` - ${error}`, undefined, { prefix: "Index Manager" }));
251
- }
252
-
253
- return { valid: errors.length === 0, errors };
254
- };