appwrite-utils-cli 0.9.0 → 0.9.3

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,133 +33,147 @@ 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>;
39
40
 
40
41
  const argv = yargs(hideBin(process.argv))
41
- .command("migrate", "Run Appwrite migrations", (yargs) => {
42
- return yargs
43
- .option("it", {
44
- alias: ["interactive", "i"],
45
- type: "boolean",
46
- description: "Run in interactive mode",
47
- })
48
- .option("dbIds", {
49
- type: "string",
50
- description: "Comma-separated list of database IDs to operate on",
51
- })
52
- .option("collectionIds", {
53
- alias: ["collIds"],
54
- type: "string",
55
- description: "Comma-separated list of collection IDs to operate on",
56
- })
57
- .option("bucketIds", {
58
- type: "string",
59
- description: "Comma-separated list of bucket IDs to operate on",
60
- })
61
- .option("wipe", {
62
- choices: ["all", "docs", "users"] as const,
63
- description:
64
- "Wipe data (all: everything, docs: only documents, users: only user data)",
65
- })
66
- .option("generate", {
67
- type: "boolean",
68
- description: "Generate TypeScript schemas from database schemas",
69
- })
70
- .option("import", {
71
- type: "boolean",
72
- description: "Import data into your databases",
73
- })
74
- .option("backup", {
75
- type: "boolean",
76
- description: "Perform a backup of your databases",
77
- })
78
- .option("writeData", {
79
- type: "boolean",
80
- description: "Write converted imported data to file",
81
- })
82
- .option("push", {
83
- type: "boolean",
84
- description:
85
- "Push your local Appwrite config to your configured Appwrite Project",
86
- })
87
- .option("sync", {
88
- type: "boolean",
89
- description:
90
- "Synchronize by pulling your Appwrite config from your configured Appwrite Project",
91
- })
92
- .option("endpoint", {
93
- type: "string",
94
- description: "Set the Appwrite endpoint",
95
- })
96
- .option("projectId", {
97
- type: "string",
98
- description: "Set the Appwrite project ID",
99
- })
100
- .option("apiKey", {
101
- type: "string",
102
- description: "Set the Appwrite API key",
103
- })
104
- .option("transfer", {
105
- type: "boolean",
106
- description: "Transfer data between databases or collections",
107
- })
108
- .option("fromDbId", {
109
- alias: ["fromDb"],
110
- type: "string",
111
- description: "Set the source database ID for transfer",
112
- })
113
- .option("toDbId", {
114
- alias: ["toDb"],
115
- type: "string",
116
- description: "Set the destination database ID for transfer",
117
- })
118
- .option("fromCollectionId", {
119
- alias: ["fromCollId", "fromColl"],
120
- type: "string",
121
- description: "Set the source collection ID for transfer",
122
- })
123
- .option("toCollectionId", {
124
- alias: ["toCollId", "toColl"],
125
- type: "string",
126
- description: "Set the destination collection ID for transfer",
127
- })
128
- .option("fromBucketId", {
129
- alias: ["fromBucket"],
130
- type: "string",
131
- description: "Set the source bucket ID for transfer",
132
- })
133
- .option("toBucketId", {
134
- alias: ["toBucket"],
135
- type: "string",
136
- description: "Set the destination bucket ID for transfer",
137
- })
138
- .option("remoteEndpoint", {
139
- type: "string",
140
- description: "Set the remote Appwrite endpoint for transfers",
141
- })
142
- .option("remoteProjectId", {
143
- type: "string",
144
- description: "Set the remote Appwrite project ID for transfers",
145
- })
146
- .option("remoteApiKey", {
147
- type: "string",
148
- description: "Set the remote Appwrite API key for transfers",
149
- });
42
+ .option("it", {
43
+ alias: ["interactive", "i"],
44
+ type: "boolean",
45
+ description: "Run in interactive mode",
46
+ })
47
+ .option("dbIds", {
48
+ type: "string",
49
+ description: "Comma-separated list of database IDs to operate on",
50
+ })
51
+ .option("collectionIds", {
52
+ alias: ["collIds"],
53
+ type: "string",
54
+ description: "Comma-separated list of collection IDs to operate on",
55
+ })
56
+ .option("bucketIds", {
57
+ type: "string",
58
+ description: "Comma-separated list of bucket IDs to operate on",
59
+ })
60
+ .option("wipe", {
61
+ choices: ["all", "docs", "users"] as const,
62
+ description:
63
+ "Wipe data (all: everything, docs: only documents, users: only user data)",
64
+ })
65
+ .option("generate", {
66
+ type: "boolean",
67
+ description: "Generate TypeScript schemas from database schemas",
68
+ })
69
+ .option("import", {
70
+ type: "boolean",
71
+ description: "Import data into your databases",
72
+ })
73
+ .option("backup", {
74
+ type: "boolean",
75
+ description: "Perform a backup of your databases",
76
+ })
77
+ .option("writeData", {
78
+ type: "boolean",
79
+ description: "Write converted imported data to file",
80
+ })
81
+ .option("push", {
82
+ type: "boolean",
83
+ description:
84
+ "Push your local Appwrite config to your configured Appwrite Project",
85
+ })
86
+ .option("sync", {
87
+ type: "boolean",
88
+ description:
89
+ "Synchronize by pulling your Appwrite config from your configured Appwrite Project",
90
+ })
91
+ .option("endpoint", {
92
+ type: "string",
93
+ description: "Set the Appwrite endpoint",
94
+ })
95
+ .option("projectId", {
96
+ type: "string",
97
+ description: "Set the Appwrite project ID",
98
+ })
99
+ .option("apiKey", {
100
+ type: "string",
101
+ description: "Set the Appwrite API key",
102
+ })
103
+ .option("transfer", {
104
+ type: "boolean",
105
+ description: "Transfer data between databases or collections",
106
+ })
107
+ .option("fromDbId", {
108
+ alias: ["fromDb"],
109
+ type: "string",
110
+ description: "Set the source database ID for transfer",
111
+ })
112
+ .option("toDbId", {
113
+ alias: ["toDb"],
114
+ type: "string",
115
+ description: "Set the destination database ID for transfer",
116
+ })
117
+ .option("fromCollectionId", {
118
+ alias: ["fromCollId", "fromColl"],
119
+ type: "string",
120
+ description: "Set the source collection ID for transfer",
121
+ })
122
+ .option("toCollectionId", {
123
+ alias: ["toCollId", "toColl"],
124
+ type: "string",
125
+ description: "Set the destination collection ID for transfer",
126
+ })
127
+ .option("fromBucketId", {
128
+ alias: ["fromBucket"],
129
+ type: "string",
130
+ description: "Set the source bucket ID for transfer",
131
+ })
132
+ .option("toBucketId", {
133
+ alias: ["toBucket"],
134
+ type: "string",
135
+ description: "Set the destination bucket ID for transfer",
136
+ })
137
+ .option("remoteEndpoint", {
138
+ type: "string",
139
+ description: "Set the remote Appwrite endpoint for transfers",
140
+ })
141
+ .option("remoteProjectId", {
142
+ type: "string",
143
+ description: "Set the remote Appwrite project ID for transfers",
144
+ })
145
+ .option("remoteApiKey", {
146
+ type: "string",
147
+ description: "Set the remote Appwrite API key for transfers",
148
+ })
149
+ .option("setup", {
150
+ type: "boolean",
151
+ description: "Setup the project with example data",
150
152
  })
151
153
  .help()
152
154
  .parse();
153
155
 
154
156
  async function main() {
155
157
  const parsedArgv = (await argv) as ParsedArgv;
156
- const controller = new UtilsController(process.cwd());
157
- await controller.init();
158
+ let controller: UtilsController | undefined;
158
159
 
159
160
  if (parsedArgv.it) {
160
- 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());
161
171
  await cli.run();
162
172
  } else {
173
+ if (!controller) {
174
+ controller = new UtilsController(process.cwd());
175
+ await controller.init();
176
+ }
163
177
  // Handle non-interactive mode with the new options
164
178
  const options: SetupOptions = {
165
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
+ };