appwrite-utils-cli 1.9.5 → 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,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
- };
@@ -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
- };