appwrite-utils-cli 0.9.984 → 0.9.991

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/README.md CHANGED
@@ -125,6 +125,7 @@ This updated CLI ensures that developers have robust tools at their fingertips t
125
125
 
126
126
  ## Changelog
127
127
 
128
+ - 0.9.990: Fixed `transferFilesLocalToLocal` and `remote` if a document exists with that `$id`, also fixed wipe `"all"` option also wiping the associated buckets
128
129
  - 0.9.983: Fixed `afterImportActions` not resolving
129
130
  - 0.9.981: Try fixing `tryAwaitWithRetry` to catch `522` errors from Cloudflare, they were appearing for some users, also added a 1000ms delay to `tryAwaitWithRetry`
130
131
  - 0.9.98: Fixing some import errors reported by users
package/dist/main.js CHANGED
@@ -141,7 +141,7 @@ async function main() {
141
141
  collections: parsedArgv.collectionIds?.split(","),
142
142
  doBackup: parsedArgv.backup,
143
143
  wipeDatabase: parsedArgv.wipe === "all" || parsedArgv.wipe === "docs",
144
- wipeDocumentStorage: parsedArgv.wipe === "all",
144
+ wipeDocumentStorage: parsedArgv.wipe === "all" || parsedArgv.wipe === "storage",
145
145
  wipeUsers: parsedArgv.wipe === "all" || parsedArgv.wipe === "users",
146
146
  generateSchemas: parsedArgv.generate,
147
147
  importData: parsedArgv.import,
@@ -171,19 +171,30 @@ async function main() {
171
171
  options.wipeDocumentStorage ||
172
172
  options.wipeUsers ||
173
173
  options.wipeCollections) {
174
- if (options.wipeDatabase && options.databases) {
175
- for (const db of options.databases) {
176
- await controller.wipeDatabase(db);
174
+ if (parsedArgv.wipe === "all") {
175
+ if (options.databases) {
176
+ for (const db of options.databases) {
177
+ await controller.wipeDatabase(db, true); // true to wipe associated buckets
178
+ }
177
179
  }
180
+ await controller.wipeUsers();
178
181
  }
179
- if (options.wipeDocumentStorage && parsedArgv.bucketIds) {
180
- for (const bucketId of parsedArgv.bucketIds.split(",")) {
181
- await controller.wipeDocumentStorage(bucketId);
182
+ else if (parsedArgv.wipe === "docs") {
183
+ if (options.databases) {
184
+ for (const db of options.databases) {
185
+ await controller.wipeBucketFromDatabase(db);
186
+ }
187
+ }
188
+ if (parsedArgv.bucketIds) {
189
+ for (const bucketId of parsedArgv.bucketIds.split(",")) {
190
+ await controller.wipeDocumentStorage(bucketId);
191
+ }
182
192
  }
183
193
  }
184
- if (options.wipeUsers) {
194
+ else if (parsedArgv.wipe === "users") {
185
195
  await controller.wipeUsers();
186
196
  }
197
+ // Handle specific collection wipes
187
198
  if (options.wipeCollections && options.databases) {
188
199
  for (const db of options.databases) {
189
200
  const dbCollections = await fetchAllCollections(db.$id, controller.database);
@@ -75,12 +75,26 @@ export class ImportController {
75
75
  // Find the corresponding database configs
76
76
  const updatedDbConfig = this.config.databases.find((db) => db.$id === updatedDb.$id);
77
77
  const targetDbConfig = this.config.databases.find((db) => db.$id === targetDb.$id);
78
- const sourceBucketId = updatedDbConfig?.bucket?.$id ||
79
- (this.config.documentBucketId &&
80
- `${this.config.documentBucketId}_${updatedDb.$id.toLowerCase().trim().replace(" ", "")}`);
81
- const targetBucketId = targetDbConfig?.bucket?.$id ||
82
- (this.config.documentBucketId &&
83
- `${this.config.documentBucketId}_${targetDb.$id.toLowerCase().trim().replace(" ", "")}`);
78
+ const allBuckets = await this.storage.listBuckets([Query.limit(1000)]);
79
+ const bucketsWithDbIdInThem = allBuckets.buckets.filter(bucket => bucket.name.toLowerCase().includes(updatedDb.$id.toLowerCase()));
80
+ const configuredUpdatedBucketId = `${this.config.documentBucketId}_${updatedDb.$id.toLowerCase().trim().replace(" ", "")}`;
81
+ const configuredTargetBucketId = `${this.config.documentBucketId}_${targetDb.$id.toLowerCase().trim().replace(" ", "")}`;
82
+ let sourceBucketId;
83
+ let targetBucketId;
84
+ if (bucketsWithDbIdInThem.find(bucket => bucket.$id === configuredUpdatedBucketId)) {
85
+ sourceBucketId = configuredUpdatedBucketId;
86
+ }
87
+ else if (bucketsWithDbIdInThem.find(bucket => bucket.$id === configuredTargetBucketId)) {
88
+ targetBucketId = configuredTargetBucketId;
89
+ }
90
+ if (!sourceBucketId) {
91
+ sourceBucketId = updatedDbConfig?.bucket?.$id ||
92
+ bucketsWithDbIdInThem[0]?.$id;
93
+ }
94
+ if (!targetBucketId) {
95
+ targetBucketId = targetDbConfig?.bucket?.$id ||
96
+ bucketsWithDbIdInThem[0]?.$id;
97
+ }
84
98
  if (sourceBucketId && targetBucketId) {
85
99
  await transferStorageLocalToLocal(this.storage, sourceBucketId, targetBucketId);
86
100
  }
@@ -33,7 +33,13 @@ export const transferStorageLocalToLocal = async (storage, fromBucketId, toBucke
33
33
  }
34
34
  const fileToCreate = InputFile.fromBuffer(new Uint8Array(fileData), file.name);
35
35
  console.log(`Creating file: ${file.name}`);
36
- tryAwaitWithRetry(async () => await storage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
36
+ try {
37
+ await tryAwaitWithRetry(async () => await storage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
38
+ }
39
+ catch (error) {
40
+ // File already exists, so we can skip it
41
+ continue;
42
+ }
37
43
  numberOfFiles++;
38
44
  }
39
45
  }
@@ -59,7 +65,13 @@ export const transferStorageLocalToLocal = async (storage, fromBucketId, toBucke
59
65
  continue;
60
66
  }
61
67
  const fileToCreate = InputFile.fromBuffer(new Uint8Array(fileData), file.name);
62
- await tryAwaitWithRetry(async () => await storage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
68
+ try {
69
+ await tryAwaitWithRetry(async () => await storage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
70
+ }
71
+ catch (error) {
72
+ // File already exists, so we can skip it
73
+ continue;
74
+ }
63
75
  numberOfFiles++;
64
76
  }
65
77
  }
@@ -90,7 +102,13 @@ export const transferStorageLocalToRemote = async (localStorage, endpoint, proje
90
102
  for (const file of allFromFiles) {
91
103
  const fileData = await tryAwaitWithRetry(async () => await localStorage.getFileDownload(file.bucketId, file.$id));
92
104
  const fileToCreate = InputFile.fromBuffer(new Uint8Array(fileData), file.name);
93
- await tryAwaitWithRetry(async () => await remoteStorage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
105
+ try {
106
+ await tryAwaitWithRetry(async () => await remoteStorage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
107
+ }
108
+ catch (error) {
109
+ // File already exists, so we can skip it
110
+ continue;
111
+ }
94
112
  numberOfFiles++;
95
113
  }
96
114
  console.log(`Transferred ${numberOfFiles} files from ${fromBucketId} to ${toBucketId}`);
@@ -36,10 +36,8 @@ export declare class UtilsController {
36
36
  wipeOtherDatabases(databasesToKeep: Models.Database[]): Promise<void>;
37
37
  wipeUsers(): Promise<void>;
38
38
  backupDatabase(database: Models.Database): Promise<void>;
39
- wipeDatabase(database: Models.Database): Promise<{
40
- collectionId: string;
41
- collectionName: string;
42
- }[]>;
39
+ wipeDatabase(database: Models.Database, wipeBucket?: boolean): Promise<void>;
40
+ wipeBucketFromDatabase(database: Models.Database): Promise<void>;
43
41
  wipeCollection(database: Models.Database, collection: Models.Collection): Promise<void>;
44
42
  wipeDocumentStorage(bucketId: string): Promise<void>;
45
43
  createOrUpdateCollectionsForDatabases(databases: Models.Database[], collections?: Models.Collection[]): Promise<void>;
@@ -123,11 +123,34 @@ export class UtilsController {
123
123
  throw new Error("Database, storage, or config not initialized");
124
124
  await backupDatabase(this.config, this.database, database.$id, this.storage);
125
125
  }
126
- async wipeDatabase(database) {
126
+ async wipeDatabase(database, wipeBucket = false) {
127
127
  await this.init();
128
128
  if (!this.database)
129
129
  throw new Error("Database not initialized");
130
- return await wipeDatabase(this.database, database.$id);
130
+ await wipeDatabase(this.database, database.$id);
131
+ if (wipeBucket) {
132
+ await this.wipeBucketFromDatabase(database);
133
+ }
134
+ }
135
+ async wipeBucketFromDatabase(database) {
136
+ // Check configured bucket in database config
137
+ const configuredBucket = this.config?.databases?.find(db => db.$id === database.$id)?.bucket;
138
+ if (configuredBucket?.$id) {
139
+ await this.wipeDocumentStorage(configuredBucket.$id);
140
+ }
141
+ // Also check for document bucket ID pattern
142
+ if (this.config?.documentBucketId) {
143
+ const documentBucketId = `${this.config.documentBucketId}_${database.$id.toLowerCase().trim().replace(/\s+/g, "")}`;
144
+ try {
145
+ await this.wipeDocumentStorage(documentBucketId);
146
+ }
147
+ catch (error) {
148
+ // Ignore if bucket doesn't exist
149
+ if (error?.type !== 'storage_bucket_not_found') {
150
+ throw error;
151
+ }
152
+ }
153
+ }
131
154
  }
132
155
  async wipeCollection(database, collection) {
133
156
  await this.init();
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": "0.9.984",
4
+ "version": "0.9.991",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
package/src/main.ts CHANGED
@@ -16,7 +16,7 @@ interface CliOptions {
16
16
  dbIds?: string;
17
17
  collectionIds?: string;
18
18
  bucketIds?: string;
19
- wipe?: "all" | "docs" | "users";
19
+ wipe?: "all" | "storage" | "docs" | "users";
20
20
  wipeCollections?: boolean;
21
21
  generate?: boolean;
22
22
  import?: boolean;
@@ -181,7 +181,8 @@ async function main() {
181
181
  collections: parsedArgv.collectionIds?.split(","),
182
182
  doBackup: parsedArgv.backup,
183
183
  wipeDatabase: parsedArgv.wipe === "all" || parsedArgv.wipe === "docs",
184
- wipeDocumentStorage: parsedArgv.wipe === "all",
184
+ wipeDocumentStorage:
185
+ parsedArgv.wipe === "all" || parsedArgv.wipe === "storage",
185
186
  wipeUsers: parsedArgv.wipe === "all" || parsedArgv.wipe === "users",
186
187
  generateSchemas: parsedArgv.generate,
187
188
  importData: parsedArgv.import,
@@ -218,19 +219,29 @@ async function main() {
218
219
  options.wipeUsers ||
219
220
  options.wipeCollections
220
221
  ) {
221
- if (options.wipeDatabase && options.databases) {
222
- for (const db of options.databases) {
223
- await controller.wipeDatabase(db);
222
+ if (parsedArgv.wipe === "all") {
223
+ if (options.databases) {
224
+ for (const db of options.databases) {
225
+ await controller.wipeDatabase(db, true); // true to wipe associated buckets
226
+ }
224
227
  }
225
- }
226
- if (options.wipeDocumentStorage && parsedArgv.bucketIds) {
227
- for (const bucketId of parsedArgv.bucketIds.split(",")) {
228
- await controller.wipeDocumentStorage(bucketId);
228
+ await controller.wipeUsers();
229
+ } else if (parsedArgv.wipe === "docs") {
230
+ if (options.databases) {
231
+ for (const db of options.databases) {
232
+ await controller.wipeBucketFromDatabase(db);
233
+ }
229
234
  }
230
- }
231
- if (options.wipeUsers) {
235
+ if (parsedArgv.bucketIds) {
236
+ for (const bucketId of parsedArgv.bucketIds.split(",")) {
237
+ await controller.wipeDocumentStorage(bucketId);
238
+ }
239
+ }
240
+ } else if (parsedArgv.wipe === "users") {
232
241
  await controller.wipeUsers();
233
242
  }
243
+
244
+ // Handle specific collection wipes
234
245
  if (options.wipeCollections && options.databases) {
235
246
  for (const db of options.databases) {
236
247
  const dbCollections = await fetchAllCollections(
@@ -136,13 +136,29 @@ export class ImportController {
136
136
  (db) => db.$id === targetDb.$id
137
137
  );
138
138
 
139
- const sourceBucketId = updatedDbConfig?.bucket?.$id ||
140
- (this.config.documentBucketId &&
141
- `${this.config.documentBucketId}_${updatedDb.$id.toLowerCase().trim().replace(" ", "")}`);
142
-
143
- const targetBucketId = targetDbConfig?.bucket?.$id ||
144
- (this.config.documentBucketId &&
145
- `${this.config.documentBucketId}_${targetDb.$id.toLowerCase().trim().replace(" ", "")}`);
139
+ const allBuckets = await this.storage.listBuckets([Query.limit(1000)]);
140
+ const bucketsWithDbIdInThem = allBuckets.buckets.filter(bucket => bucket.name.toLowerCase().includes(updatedDb.$id.toLowerCase()));
141
+ const configuredUpdatedBucketId = `${this.config.documentBucketId}_${updatedDb.$id.toLowerCase().trim().replace(" ", "")}`;
142
+ const configuredTargetBucketId = `${this.config.documentBucketId}_${targetDb.$id.toLowerCase().trim().replace(" ", "")}`;
143
+
144
+ let sourceBucketId: string | undefined;
145
+ let targetBucketId: string | undefined;
146
+
147
+ if (bucketsWithDbIdInThem.find(bucket => bucket.$id === configuredUpdatedBucketId)) {
148
+ sourceBucketId = configuredUpdatedBucketId;
149
+ } else if (bucketsWithDbIdInThem.find(bucket => bucket.$id === configuredTargetBucketId)) {
150
+ targetBucketId = configuredTargetBucketId;
151
+ }
152
+
153
+ if (!sourceBucketId) {
154
+ sourceBucketId = updatedDbConfig?.bucket?.$id ||
155
+ bucketsWithDbIdInThem[0]?.$id;
156
+ }
157
+
158
+ if (!targetBucketId) {
159
+ targetBucketId = targetDbConfig?.bucket?.$id ||
160
+ bucketsWithDbIdInThem[0]?.$id;
161
+ }
146
162
 
147
163
  if (sourceBucketId && targetBucketId) {
148
164
  await transferStorageLocalToLocal(
@@ -64,15 +64,20 @@ export const transferStorageLocalToLocal = async (
64
64
  file.name
65
65
  );
66
66
  console.log(`Creating file: ${file.name}`);
67
- tryAwaitWithRetry(
68
- async () =>
67
+ try {
68
+ await tryAwaitWithRetry(
69
+ async () =>
69
70
  await storage.createFile(
70
71
  toBucketId,
71
72
  file.$id,
72
73
  fileToCreate,
73
74
  file.$permissions
74
75
  )
75
- );
76
+ );
77
+ } catch (error: any) {
78
+ // File already exists, so we can skip it
79
+ continue;
80
+ }
76
81
  numberOfFiles++;
77
82
  }
78
83
  } else {
@@ -104,15 +109,20 @@ export const transferStorageLocalToLocal = async (
104
109
  new Uint8Array(fileData),
105
110
  file.name
106
111
  );
107
- await tryAwaitWithRetry(
108
- async () =>
112
+ try {
113
+ await tryAwaitWithRetry(
114
+ async () =>
109
115
  await storage.createFile(
110
116
  toBucketId,
111
117
  file.$id,
112
118
  fileToCreate,
113
119
  file.$permissions
114
120
  )
115
- );
121
+ );
122
+ } catch (error: any) {
123
+ // File already exists, so we can skip it
124
+ continue;
125
+ }
116
126
  numberOfFiles++;
117
127
  }
118
128
  }
@@ -167,15 +177,20 @@ export const transferStorageLocalToRemote = async (
167
177
  new Uint8Array(fileData),
168
178
  file.name
169
179
  );
170
- await tryAwaitWithRetry(
171
- async () =>
180
+ try {
181
+ await tryAwaitWithRetry(
182
+ async () =>
172
183
  await remoteStorage.createFile(
173
184
  toBucketId,
174
185
  file.$id,
175
186
  fileToCreate,
176
187
  file.$permissions
177
188
  )
178
- );
189
+ );
190
+ } catch (error: any) {
191
+ // File already exists, so we can skip it
192
+ continue;
193
+ }
179
194
  numberOfFiles++;
180
195
  }
181
196
  console.log(
@@ -179,10 +179,34 @@ export class UtilsController {
179
179
  );
180
180
  }
181
181
 
182
- async wipeDatabase(database: Models.Database) {
182
+ async wipeDatabase(database: Models.Database, wipeBucket: boolean = false) {
183
183
  await this.init();
184
184
  if (!this.database) throw new Error("Database not initialized");
185
- return await wipeDatabase(this.database, database.$id);
185
+ await wipeDatabase(this.database, database.$id);
186
+ if (wipeBucket) {
187
+ await this.wipeBucketFromDatabase(database);
188
+ }
189
+ }
190
+
191
+ async wipeBucketFromDatabase(database: Models.Database) {
192
+ // Check configured bucket in database config
193
+ const configuredBucket = this.config?.databases?.find(db => db.$id === database.$id)?.bucket;
194
+ if (configuredBucket?.$id) {
195
+ await this.wipeDocumentStorage(configuredBucket.$id);
196
+ }
197
+
198
+ // Also check for document bucket ID pattern
199
+ if (this.config?.documentBucketId) {
200
+ const documentBucketId = `${this.config.documentBucketId}_${database.$id.toLowerCase().trim().replace(/\s+/g, "")}`;
201
+ try {
202
+ await this.wipeDocumentStorage(documentBucketId);
203
+ } catch (error: any) {
204
+ // Ignore if bucket doesn't exist
205
+ if (error?.type !== 'storage_bucket_not_found') {
206
+ throw error;
207
+ }
208
+ }
209
+ }
186
210
  }
187
211
 
188
212
  async wipeCollection(database: Models.Database, collection: Models.Collection) {
package/src/appwrite.zip DELETED
Binary file