appwrite-utils-cli 0.0.285 → 0.9.0

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.
Files changed (109) hide show
  1. package/README.md +122 -96
  2. package/dist/collections/attributes.d.ts +4 -0
  3. package/dist/collections/attributes.js +224 -0
  4. package/dist/collections/indexes.d.ts +4 -0
  5. package/dist/collections/indexes.js +27 -0
  6. package/dist/collections/methods.d.ts +16 -0
  7. package/dist/collections/methods.js +216 -0
  8. package/dist/databases/methods.d.ts +6 -0
  9. package/dist/databases/methods.js +33 -0
  10. package/dist/interactiveCLI.d.ts +19 -0
  11. package/dist/interactiveCLI.js +555 -0
  12. package/dist/main.js +227 -62
  13. package/dist/migrations/afterImportActions.js +37 -40
  14. package/dist/migrations/appwriteToX.d.ts +26 -25
  15. package/dist/migrations/appwriteToX.js +42 -6
  16. package/dist/migrations/attributes.js +21 -20
  17. package/dist/migrations/backup.d.ts +93 -87
  18. package/dist/migrations/collections.d.ts +6 -0
  19. package/dist/migrations/collections.js +149 -20
  20. package/dist/migrations/converters.d.ts +2 -18
  21. package/dist/migrations/converters.js +13 -2
  22. package/dist/migrations/dataLoader.d.ts +276 -161
  23. package/dist/migrations/dataLoader.js +535 -292
  24. package/dist/migrations/databases.js +8 -2
  25. package/dist/migrations/helper.d.ts +3 -0
  26. package/dist/migrations/helper.js +21 -0
  27. package/dist/migrations/importController.d.ts +5 -2
  28. package/dist/migrations/importController.js +125 -88
  29. package/dist/migrations/importDataActions.d.ts +9 -1
  30. package/dist/migrations/importDataActions.js +15 -3
  31. package/dist/migrations/indexes.js +3 -2
  32. package/dist/migrations/logging.js +20 -8
  33. package/dist/migrations/migrationHelper.d.ts +9 -4
  34. package/dist/migrations/migrationHelper.js +6 -5
  35. package/dist/migrations/openapi.d.ts +1 -1
  36. package/dist/migrations/openapi.js +33 -18
  37. package/dist/migrations/queue.js +3 -2
  38. package/dist/migrations/relationships.d.ts +2 -2
  39. package/dist/migrations/schemaStrings.js +53 -41
  40. package/dist/migrations/setupDatabase.d.ts +2 -4
  41. package/dist/migrations/setupDatabase.js +24 -105
  42. package/dist/migrations/storage.d.ts +3 -1
  43. package/dist/migrations/storage.js +110 -16
  44. package/dist/migrations/transfer.d.ts +30 -0
  45. package/dist/migrations/transfer.js +337 -0
  46. package/dist/migrations/users.d.ts +2 -1
  47. package/dist/migrations/users.js +78 -43
  48. package/dist/schemas/authUser.d.ts +2 -2
  49. package/dist/storage/methods.d.ts +15 -0
  50. package/dist/storage/methods.js +207 -0
  51. package/dist/storage/schemas.d.ts +687 -0
  52. package/dist/storage/schemas.js +175 -0
  53. package/dist/utils/getClientFromConfig.d.ts +4 -0
  54. package/dist/utils/getClientFromConfig.js +16 -0
  55. package/dist/utils/helperFunctions.d.ts +11 -1
  56. package/dist/utils/helperFunctions.js +38 -0
  57. package/dist/utils/retryFailedPromises.d.ts +2 -0
  58. package/dist/utils/retryFailedPromises.js +21 -0
  59. package/dist/utils/schemaStrings.d.ts +13 -0
  60. package/dist/utils/schemaStrings.js +403 -0
  61. package/dist/utils/setupFiles.js +110 -61
  62. package/dist/utilsController.d.ts +40 -22
  63. package/dist/utilsController.js +164 -84
  64. package/package.json +13 -15
  65. package/src/collections/attributes.ts +483 -0
  66. package/src/collections/indexes.ts +53 -0
  67. package/src/collections/methods.ts +331 -0
  68. package/src/databases/methods.ts +47 -0
  69. package/src/init.ts +64 -64
  70. package/src/interactiveCLI.ts +767 -0
  71. package/src/main.ts +292 -83
  72. package/src/migrations/afterImportActions.ts +553 -490
  73. package/src/migrations/appwriteToX.ts +237 -174
  74. package/src/migrations/attributes.ts +483 -422
  75. package/src/migrations/backup.ts +205 -205
  76. package/src/migrations/collections.ts +545 -300
  77. package/src/migrations/converters.ts +161 -150
  78. package/src/migrations/dataLoader.ts +1615 -1304
  79. package/src/migrations/databases.ts +44 -25
  80. package/src/migrations/dbHelpers.ts +92 -92
  81. package/src/migrations/helper.ts +40 -0
  82. package/src/migrations/importController.ts +448 -384
  83. package/src/migrations/importDataActions.ts +315 -307
  84. package/src/migrations/indexes.ts +40 -37
  85. package/src/migrations/logging.ts +29 -16
  86. package/src/migrations/migrationHelper.ts +207 -201
  87. package/src/migrations/openapi.ts +83 -70
  88. package/src/migrations/queue.ts +118 -119
  89. package/src/migrations/relationships.ts +324 -324
  90. package/src/migrations/schemaStrings.ts +472 -460
  91. package/src/migrations/setupDatabase.ts +118 -219
  92. package/src/migrations/storage.ts +538 -358
  93. package/src/migrations/transfer.ts +608 -0
  94. package/src/migrations/users.ts +362 -285
  95. package/src/migrations/validationRules.ts +63 -63
  96. package/src/schemas/authUser.ts +23 -23
  97. package/src/setup.ts +8 -8
  98. package/src/storage/methods.ts +371 -0
  99. package/src/storage/schemas.ts +205 -0
  100. package/src/types.ts +9 -9
  101. package/src/utils/getClientFromConfig.ts +17 -0
  102. package/src/utils/helperFunctions.ts +181 -127
  103. package/src/utils/index.ts +2 -2
  104. package/src/utils/loadConfigs.ts +59 -59
  105. package/src/utils/retryFailedPromises.ts +27 -0
  106. package/src/utils/schemaStrings.ts +473 -0
  107. package/src/utils/setupFiles.ts +228 -182
  108. package/src/utilsController.ts +325 -194
  109. package/tsconfig.json +37 -37
@@ -0,0 +1,175 @@
1
+ import { z } from "zod";
2
+ import { attributeSchema, parseAttribute, CollectionCreateSchema, } from "appwrite-utils";
3
+ export const BackupSchema = z.object({
4
+ $id: z.string(),
5
+ $createdAt: z.string(),
6
+ $updatedAt: z.string(),
7
+ database: z.string(),
8
+ collections: z.array(z.string()),
9
+ documents: z
10
+ .array(z.object({
11
+ collectionId: z.string(),
12
+ data: z.string(),
13
+ }))
14
+ .default([]),
15
+ });
16
+ export const BackupCreateSchema = BackupSchema.omit({
17
+ $id: true,
18
+ $createdAt: true,
19
+ $updatedAt: true,
20
+ });
21
+ export const BatchSchema = z.object({
22
+ $id: z.string(),
23
+ $createdAt: z.string(),
24
+ $updatedAt: z.string(),
25
+ data: z.string().describe("The serialized data for this batch"),
26
+ processed: z
27
+ .boolean()
28
+ .default(false)
29
+ .describe("Whether the batch has been processed"),
30
+ });
31
+ export const BatchCreateSchema = BatchSchema.omit({
32
+ $id: true,
33
+ $createdAt: true,
34
+ $updatedAt: true,
35
+ });
36
+ export const OperationSchema = z.object({
37
+ $id: z.string(),
38
+ $createdAt: z.string(),
39
+ $updatedAt: z.string(),
40
+ operationType: z.string(),
41
+ collectionId: z.string(),
42
+ data: z.any(),
43
+ batches: z.array(z.string()).default([]).optional(),
44
+ progress: z.number(),
45
+ total: z.number(),
46
+ error: z.string(),
47
+ status: z
48
+ .enum([
49
+ "pending",
50
+ "ready",
51
+ "in_progress",
52
+ "completed",
53
+ "error",
54
+ "cancelled",
55
+ ])
56
+ .default("pending"),
57
+ });
58
+ export const OperationCreateSchema = OperationSchema.omit({
59
+ $id: true,
60
+ $createdAt: true,
61
+ $updatedAt: true,
62
+ });
63
+ export const getMigrationCollectionSchemas = () => {
64
+ const currentOperationsAttributes = [
65
+ parseAttribute({
66
+ key: "operationType",
67
+ type: "string",
68
+ error: "Invalid Operation Type",
69
+ size: 50,
70
+ required: true,
71
+ array: false,
72
+ xdefault: null,
73
+ }),
74
+ attributeSchema.parse({
75
+ key: "collectionId",
76
+ type: "string",
77
+ error: "Invalid Collection Id",
78
+ size: 50,
79
+ array: false,
80
+ xdefault: null,
81
+ }),
82
+ attributeSchema.parse({
83
+ key: "batches",
84
+ type: "string",
85
+ error: "Invalid Batches",
86
+ size: 1073741824,
87
+ array: true,
88
+ }),
89
+ attributeSchema.parse({
90
+ key: "data",
91
+ type: "string",
92
+ error: "Invalid Data",
93
+ size: 1073741824,
94
+ }),
95
+ attributeSchema.parse({
96
+ key: "progress",
97
+ type: "integer",
98
+ error: "Invalid Progress",
99
+ required: true,
100
+ array: false,
101
+ }),
102
+ attributeSchema.parse({
103
+ key: "total",
104
+ type: "integer",
105
+ error: "Invalid Total",
106
+ required: true,
107
+ array: false,
108
+ }),
109
+ attributeSchema.parse({
110
+ key: "error",
111
+ type: "string",
112
+ error: "Operation Error",
113
+ required: false,
114
+ array: false,
115
+ }),
116
+ attributeSchema.parse({
117
+ key: "status",
118
+ type: "enum",
119
+ elements: [
120
+ "pending",
121
+ "ready",
122
+ "in_progress",
123
+ "completed",
124
+ "error",
125
+ "cancelled",
126
+ ],
127
+ error: "Invalid Status",
128
+ array: false,
129
+ xdefault: "pending",
130
+ }),
131
+ ];
132
+ const currentOperationsConfig = CollectionCreateSchema.parse({
133
+ name: "CurrentOperations",
134
+ enabled: true,
135
+ documentSecurity: false,
136
+ attributes: [],
137
+ indexes: [],
138
+ });
139
+ const batchesAttributes = [
140
+ attributeSchema.parse({
141
+ key: "data",
142
+ type: "string",
143
+ size: 1073741824,
144
+ error: "Invalid Data",
145
+ required: true,
146
+ array: false,
147
+ }),
148
+ attributeSchema.parse({
149
+ key: "processed",
150
+ type: "boolean",
151
+ error: "Invalid Processed",
152
+ required: true,
153
+ array: false,
154
+ xdefault: false,
155
+ }),
156
+ ];
157
+ const batchesConfig = CollectionCreateSchema.parse({
158
+ name: "Batches",
159
+ enabled: true,
160
+ documentSecurity: false,
161
+ attributes: [],
162
+ indexes: [],
163
+ });
164
+ const toReturn = {
165
+ CurrentOperations: {
166
+ collection: currentOperationsConfig,
167
+ attributes: currentOperationsAttributes,
168
+ },
169
+ Batches: {
170
+ collection: batchesConfig,
171
+ attributes: batchesAttributes,
172
+ },
173
+ };
174
+ return toReturn;
175
+ };
@@ -0,0 +1,4 @@
1
+ import { type AppwriteConfig } from "appwrite-utils";
2
+ import { Client } from "node-appwrite";
3
+ export declare const getClientFromConfig: (config: AppwriteConfig) => Client | undefined;
4
+ export declare const getClient: (endpoint: string, project: string, key: string) => Client;
@@ -0,0 +1,16 @@
1
+ import {} from "appwrite-utils";
2
+ import { Client } from "node-appwrite";
3
+ export const getClientFromConfig = (config) => {
4
+ let appwriteClient;
5
+ if (!config.appwriteClient) {
6
+ appwriteClient = new Client()
7
+ .setEndpoint(config.appwriteEndpoint)
8
+ .setProject(config.appwriteProject)
9
+ .setKey(config.appwriteKey);
10
+ config.appwriteClient = appwriteClient;
11
+ }
12
+ return appwriteClient;
13
+ };
14
+ export const getClient = (endpoint, project, key) => {
15
+ return new Client().setEndpoint(endpoint).setProject(project).setKey(key);
16
+ };
@@ -1,4 +1,4 @@
1
- import type { Models } from "node-appwrite";
1
+ import { Client, type Models } from "node-appwrite";
2
2
  import type { CollectionImportData } from "../migrations/dataLoader.js";
3
3
  import type { ConfigCollection } from "appwrite-utils";
4
4
  export declare const toPascalCase: (str: string) => string;
@@ -35,3 +35,13 @@ export declare const getFileViewUrl: (endpoint: string, projectId: string, bucke
35
35
  */
36
36
  export declare const getFileDownloadUrl: (endpoint: string, projectId: string, bucketId: string, fileId: string, jwt?: Models.Jwt) => string;
37
37
  export declare const finalizeByAttributeMap: (appwriteFolderPath: string, collection: ConfigCollection, item: CollectionImportData["data"][number]) => Promise<any>;
38
+ export declare let numTimesFailedTotal: number;
39
+ /**
40
+ * Tries to execute the given createFunction and retries up to 5 times if it fails.
41
+ *
42
+ * @param {() => Promise<any>} createFunction - The function to be executed.
43
+ * @param {number} [attemptNum=0] - The number of attempts made so far (default: 0).
44
+ * @return {Promise<any>} - A promise that resolves to the result of the createFunction or rejects with an error if it fails after 5 attempts.
45
+ */
46
+ export declare const tryAwaitWithRetry: <T>(createFunction: () => Promise<T>, attemptNum?: number, throwError?: boolean) => Promise<T>;
47
+ export declare const getAppwriteClient: (endpoint: string, projectId: string, apiKey: string) => Client;
@@ -1,3 +1,4 @@
1
+ import { AppwriteException, Client, } from "node-appwrite";
1
2
  import fs from "node:fs";
2
3
  import path from "node:path";
3
4
  export const toPascalCase = (str) => {
@@ -78,3 +79,40 @@ export const finalizeByAttributeMap = async (appwriteFolderPath, collection, ite
78
79
  ...item.finalData,
79
80
  });
80
81
  };
82
+ export let numTimesFailedTotal = 0;
83
+ /**
84
+ * Tries to execute the given createFunction and retries up to 5 times if it fails.
85
+ *
86
+ * @param {() => Promise<any>} createFunction - The function to be executed.
87
+ * @param {number} [attemptNum=0] - The number of attempts made so far (default: 0).
88
+ * @return {Promise<any>} - A promise that resolves to the result of the createFunction or rejects with an error if it fails after 5 attempts.
89
+ */
90
+ export const tryAwaitWithRetry = async (createFunction, attemptNum = 0, throwError = false) => {
91
+ try {
92
+ return await createFunction();
93
+ }
94
+ catch (error) {
95
+ if (error instanceof AppwriteException &&
96
+ (error.message.toLowerCase().includes("fetch failed") ||
97
+ error.message.toLowerCase().includes("server error"))) {
98
+ numTimesFailedTotal++;
99
+ console.log(`Fetch failed on attempt ${attemptNum}. Retrying...`);
100
+ if (attemptNum > 5) {
101
+ throw error;
102
+ }
103
+ return tryAwaitWithRetry(createFunction, attemptNum + 1);
104
+ }
105
+ if (throwError) {
106
+ throw error;
107
+ }
108
+ console.error("Error during retryAwait function: " + error);
109
+ // @ts-ignore
110
+ return Promise.resolve();
111
+ }
112
+ };
113
+ export const getAppwriteClient = (endpoint, projectId, apiKey) => {
114
+ return new Client()
115
+ .setEndpoint(endpoint)
116
+ .setProject(projectId)
117
+ .setKey(apiKey);
118
+ };
@@ -0,0 +1,2 @@
1
+ import type { Models } from "node-appwrite";
2
+ export declare const retryFailedPromises: (batch: Promise<Models.Document>[], maxRetries?: number) => Promise<PromiseSettledResult<Models.Document>[]>;
@@ -0,0 +1,21 @@
1
+ export const retryFailedPromises = async (batch, maxRetries = 3) => {
2
+ const results = await Promise.allSettled(batch);
3
+ const toRetry = [];
4
+ results.forEach((result, index) => {
5
+ if (result.status === "rejected") {
6
+ console.error("Promise rejected with reason:", result.reason);
7
+ if (maxRetries > 0) {
8
+ toRetry.push(batch[index]);
9
+ }
10
+ }
11
+ });
12
+ if (toRetry.length > 0) {
13
+ console.log(`Retrying ${toRetry.length} promises`);
14
+ return retryFailedPromises(toRetry, maxRetries - 1);
15
+ }
16
+ else {
17
+ return results
18
+ .filter((result) => result.status === "fulfilled")
19
+ .map((result) => result);
20
+ }
21
+ };
@@ -0,0 +1,13 @@
1
+ import type { AppwriteConfig, Attribute } from "appwrite-utils";
2
+ export declare class SchemaGenerator {
3
+ private relationshipMap;
4
+ private config;
5
+ private appwriteFolderPath;
6
+ constructor(config: AppwriteConfig, appwriteFolderPath: string);
7
+ updateTsSchemas(): void;
8
+ private extractRelationships;
9
+ private addRelationship;
10
+ generateSchemas(): void;
11
+ createSchemaString: (name: string, attributes: Attribute[]) => string;
12
+ typeToZod: (attribute: Attribute) => string;
13
+ }