appwrite-utils-cli 0.9.984 → 0.9.990

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,
@@ -173,7 +173,7 @@ async function main() {
173
173
  options.wipeCollections) {
174
174
  if (options.wipeDatabase && options.databases) {
175
175
  for (const db of options.databases) {
176
- await controller.wipeDatabase(db);
176
+ await controller.wipeDatabase(db, options.wipeDocumentStorage);
177
177
  }
178
178
  }
179
179
  if (options.wipeDocumentStorage && parsedArgv.bucketIds) {
@@ -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
  }
@@ -90,7 +96,13 @@ export const transferStorageLocalToRemote = async (localStorage, endpoint, proje
90
96
  for (const file of allFromFiles) {
91
97
  const fileData = await tryAwaitWithRetry(async () => await localStorage.getFileDownload(file.bucketId, file.$id));
92
98
  const fileToCreate = InputFile.fromBuffer(new Uint8Array(fileData), file.name);
93
- await tryAwaitWithRetry(async () => await remoteStorage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
99
+ try {
100
+ await tryAwaitWithRetry(async () => await remoteStorage.createFile(toBucketId, file.$id, fileToCreate, file.$permissions));
101
+ }
102
+ catch (error) {
103
+ // File already exists, so we can skip it
104
+ continue;
105
+ }
94
106
  numberOfFiles++;
95
107
  }
96
108
  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.990",
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,7 @@ 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: parsedArgv.wipe === "all" || parsedArgv.wipe === "storage",
185
185
  wipeUsers: parsedArgv.wipe === "all" || parsedArgv.wipe === "users",
186
186
  generateSchemas: parsedArgv.generate,
187
187
  importData: parsedArgv.import,
@@ -220,7 +220,7 @@ async function main() {
220
220
  ) {
221
221
  if (options.wipeDatabase && options.databases) {
222
222
  for (const db of options.databases) {
223
- await controller.wipeDatabase(db);
223
+ await controller.wipeDatabase(db, options.wipeDocumentStorage);
224
224
  }
225
225
  }
226
226
  if (options.wipeDocumentStorage && parsedArgv.bucketIds) {
@@ -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 {
@@ -167,15 +172,20 @@ export const transferStorageLocalToRemote = async (
167
172
  new Uint8Array(fileData),
168
173
  file.name
169
174
  );
170
- await tryAwaitWithRetry(
171
- async () =>
175
+ try {
176
+ await tryAwaitWithRetry(
177
+ async () =>
172
178
  await remoteStorage.createFile(
173
179
  toBucketId,
174
180
  file.$id,
175
181
  fileToCreate,
176
182
  file.$permissions
177
183
  )
178
- );
184
+ );
185
+ } catch (error: any) {
186
+ // File already exists, so we can skip it
187
+ continue;
188
+ }
179
189
  numberOfFiles++;
180
190
  }
181
191
  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