appwrite-utils-cli 0.9.2 → 0.9.4

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/src/main.ts CHANGED
@@ -33,6 +33,7 @@ interface CliOptions {
33
33
  remoteEndpoint?: string;
34
34
  remoteProjectId?: string;
35
35
  remoteApiKey?: string;
36
+ setup?: boolean;
36
37
  }
37
38
 
38
39
  type ParsedArgv = ArgumentsCamelCase<CliOptions>;
@@ -145,18 +146,34 @@ const argv = yargs(hideBin(process.argv))
145
146
  type: "string",
146
147
  description: "Set the remote Appwrite API key for transfers",
147
148
  })
149
+ .option("setup", {
150
+ type: "boolean",
151
+ description: "Setup the project with example data",
152
+ })
148
153
  .help()
149
154
  .parse();
150
155
 
151
156
  async function main() {
152
157
  const parsedArgv = (await argv) as ParsedArgv;
153
- const controller = new UtilsController(process.cwd());
154
- await controller.init();
158
+ let controller: UtilsController | undefined;
155
159
 
156
160
  if (parsedArgv.it) {
157
- const cli = new InteractiveCLI(process.cwd(), controller);
161
+ try {
162
+ controller = new UtilsController(process.cwd());
163
+ await controller.init();
164
+ } catch (error: any) {
165
+ // If it fails, that means there's no config, more than likely
166
+ console.log(
167
+ "No config found, you probably need to create the setup files"
168
+ );
169
+ }
170
+ const cli = new InteractiveCLI(process.cwd());
158
171
  await cli.run();
159
172
  } else {
173
+ if (!controller) {
174
+ controller = new UtilsController(process.cwd());
175
+ await controller.init();
176
+ }
160
177
  // Handle non-interactive mode with the new options
161
178
  const options: SetupOptions = {
162
179
  databases: parsedArgv.dbIds
@@ -1,40 +1,40 @@
1
- import type { Databases, Models } from "node-appwrite";
2
- import type { OperationCreate } from "../storage/schemas.js";
3
- import { tryAwaitWithRetry } from "appwrite-utils";
4
- import { ulid } from "ulidx";
5
-
6
- export const logOperation = async (
7
- db: Databases,
8
- dbId: string,
9
- operationDetails: OperationCreate,
10
- operationId?: string
11
- ): Promise<Models.Document> => {
12
- try {
13
- let operation;
14
- if (operationId) {
15
- // Update existing operation log
16
- operation = await tryAwaitWithRetry(
17
- async () =>
18
- await db.updateDocument(
19
- "migrations",
20
- "currentOperations",
21
- operationId,
22
- operationDetails
23
- )
24
- );
25
- } else {
26
- // Create new operation log
27
- operation = await db.createDocument(
28
- "migrations",
29
- "currentOperations",
30
- ulid(),
31
- operationDetails
32
- );
33
- }
34
- console.log(`Operation logged: ${operation.$id}`);
35
- return operation;
36
- } catch (error) {
37
- console.error(`Error logging operation: ${error}`);
38
- throw error;
39
- }
40
- };
1
+ import type { Databases, Models } from "node-appwrite";
2
+ import type { OperationCreate } from "../storage/schemas.js";
3
+ import { tryAwaitWithRetry } from "appwrite-utils";
4
+ import { ulid } from "ulidx";
5
+
6
+ export const logOperation = async (
7
+ db: Databases,
8
+ dbId: string,
9
+ operationDetails: OperationCreate,
10
+ operationId?: string
11
+ ): Promise<Models.Document> => {
12
+ try {
13
+ let operation;
14
+ if (operationId) {
15
+ // Update existing operation log
16
+ operation = await tryAwaitWithRetry(
17
+ async () =>
18
+ await db.updateDocument(
19
+ "migrations",
20
+ "currentOperations",
21
+ operationId,
22
+ operationDetails
23
+ )
24
+ );
25
+ } else {
26
+ // Create new operation log
27
+ operation = await db.createDocument(
28
+ "migrations",
29
+ "currentOperations",
30
+ ulid(),
31
+ operationDetails
32
+ );
33
+ }
34
+ console.log(`Operation logged: ${operation.$id}`);
35
+ return operation;
36
+ } catch (error) {
37
+ console.error(`Error logging operation: ${error}`);
38
+ throw error;
39
+ }
40
+ };
@@ -14,7 +14,7 @@ export const setupMigrationDatabase = async (config: AppwriteConfig) => {
14
14
  console.log("Starting Migrations Setup");
15
15
  console.log("---------------------------------");
16
16
  const database = new Databases(config.appwriteClient!);
17
- let db: Models.Database | null = null;
17
+ let db: Models.Database | undefined;
18
18
  const migrationCollectionsSetup = getMigrationCollectionSchemas();
19
19
 
20
20
  try {
@@ -31,36 +31,69 @@ export const setupMigrationDatabase = async (config: AppwriteConfig) => {
31
31
  console.log("Migrations database created");
32
32
  }
33
33
 
34
+ if (!db) {
35
+ console.error("Failed to create or retrieve the migrations database");
36
+ return;
37
+ }
38
+
34
39
  for (const [collectionName, { collection, attributes }] of Object.entries(
35
40
  migrationCollectionsSetup
36
41
  )) {
37
42
  const collectionId = toCamelCase(collectionName);
38
- let collectionFound: Models.Collection | null = null;
43
+ let collectionFound: Models.Collection | undefined;
39
44
  try {
40
45
  collectionFound = await tryAwaitWithRetry(
41
- async () => await database.getCollection(db!.$id, collectionId)
46
+ async () => await database.getCollection(db.$id, collectionId),
47
+ undefined,
48
+ true
42
49
  );
50
+ console.log(`Collection found: ${collectionId}`);
43
51
  } catch (e) {
44
52
  console.log(`Collection not found: ${collectionId}`);
45
- collectionFound = await tryAwaitWithRetry(
46
- async () =>
47
- await database.createCollection(
48
- db!.$id,
49
- collectionId,
50
- collectionName,
51
- undefined,
52
- collection.documentSecurity,
53
- collection.enabled
54
- )
55
- );
53
+ try {
54
+ collectionFound = await tryAwaitWithRetry(
55
+ async () =>
56
+ await database.createCollection(
57
+ db.$id,
58
+ collectionId,
59
+ collectionName,
60
+ undefined,
61
+ collection.documentSecurity,
62
+ collection.enabled
63
+ ),
64
+ undefined,
65
+ true
66
+ );
67
+ console.log(`Collection created: ${collectionId}`);
68
+ } catch (createError) {
69
+ console.error(
70
+ `Failed to create collection: ${collectionId}`,
71
+ createError
72
+ );
73
+ continue;
74
+ }
56
75
  }
76
+
77
+ if (!collectionFound) {
78
+ console.error(`Failed to create or retrieve collection: ${collectionId}`);
79
+ continue;
80
+ }
81
+
57
82
  for (const attribute of attributes) {
58
- await createOrUpdateAttribute(
59
- database,
60
- db!.$id,
61
- collectionFound!,
62
- attribute
63
- );
83
+ try {
84
+ await createOrUpdateAttribute(
85
+ database,
86
+ db.$id,
87
+ collectionFound,
88
+ attribute
89
+ );
90
+ console.log(`Attribute created/updated: ${attribute.key}`);
91
+ } catch (attrError) {
92
+ console.error(
93
+ `Failed to create/update attribute: ${attribute.key}`,
94
+ attrError
95
+ );
96
+ }
64
97
  }
65
98
  }
66
99
  console.log("---------------------------------");
@@ -80,7 +113,7 @@ export const ensureDatabasesExist = async (config: AppwriteConfig) => {
80
113
  (d) => d.name.toLowerCase().trim().replace(" ", "") === "migrations"
81
114
  );
82
115
  if (existingDatabases.databases.length !== 0 && migrationsDatabase) {
83
- console.log("Wiping all databases except migrations");
116
+ console.log("Creating all databases except migrations");
84
117
  databasesToEnsure.push(migrationsDatabase);
85
118
  }
86
119