appwrite-utils-cli 0.0.286 → 0.9.2
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 +162 -96
- package/dist/collections/attributes.d.ts +4 -0
- package/dist/collections/attributes.js +224 -0
- package/dist/collections/indexes.d.ts +4 -0
- package/dist/collections/indexes.js +27 -0
- package/dist/collections/methods.d.ts +16 -0
- package/dist/collections/methods.js +216 -0
- package/dist/databases/methods.d.ts +6 -0
- package/dist/databases/methods.js +33 -0
- package/dist/interactiveCLI.d.ts +19 -0
- package/dist/interactiveCLI.js +555 -0
- package/dist/main.js +224 -62
- package/dist/migrations/afterImportActions.js +37 -40
- package/dist/migrations/appwriteToX.d.ts +26 -25
- package/dist/migrations/appwriteToX.js +42 -6
- package/dist/migrations/attributes.js +21 -20
- package/dist/migrations/backup.d.ts +93 -87
- package/dist/migrations/collections.d.ts +6 -0
- package/dist/migrations/collections.js +149 -20
- package/dist/migrations/converters.d.ts +2 -18
- package/dist/migrations/converters.js +13 -2
- package/dist/migrations/dataLoader.d.ts +276 -161
- package/dist/migrations/dataLoader.js +535 -292
- package/dist/migrations/databases.js +8 -2
- package/dist/migrations/helper.d.ts +3 -0
- package/dist/migrations/helper.js +21 -0
- package/dist/migrations/importController.d.ts +5 -2
- package/dist/migrations/importController.js +125 -88
- package/dist/migrations/importDataActions.d.ts +9 -1
- package/dist/migrations/importDataActions.js +15 -3
- package/dist/migrations/indexes.js +3 -2
- package/dist/migrations/logging.js +20 -8
- package/dist/migrations/migrationHelper.d.ts +9 -4
- package/dist/migrations/migrationHelper.js +6 -5
- package/dist/migrations/openapi.d.ts +1 -1
- package/dist/migrations/openapi.js +33 -18
- package/dist/migrations/queue.js +3 -2
- package/dist/migrations/relationships.d.ts +2 -2
- package/dist/migrations/schemaStrings.js +53 -41
- package/dist/migrations/setupDatabase.d.ts +2 -4
- package/dist/migrations/setupDatabase.js +24 -105
- package/dist/migrations/storage.d.ts +3 -1
- package/dist/migrations/storage.js +110 -16
- package/dist/migrations/transfer.d.ts +30 -0
- package/dist/migrations/transfer.js +337 -0
- package/dist/migrations/users.d.ts +2 -1
- package/dist/migrations/users.js +78 -43
- package/dist/schemas/authUser.d.ts +2 -2
- package/dist/storage/methods.d.ts +15 -0
- package/dist/storage/methods.js +207 -0
- package/dist/storage/schemas.d.ts +687 -0
- package/dist/storage/schemas.js +175 -0
- package/dist/utils/getClientFromConfig.d.ts +4 -0
- package/dist/utils/getClientFromConfig.js +16 -0
- package/dist/utils/helperFunctions.d.ts +11 -1
- package/dist/utils/helperFunctions.js +38 -0
- package/dist/utils/retryFailedPromises.d.ts +2 -0
- package/dist/utils/retryFailedPromises.js +21 -0
- package/dist/utils/schemaStrings.d.ts +13 -0
- package/dist/utils/schemaStrings.js +403 -0
- package/dist/utils/setupFiles.js +110 -61
- package/dist/utilsController.d.ts +40 -22
- package/dist/utilsController.js +164 -84
- package/package.json +13 -15
- package/src/collections/attributes.ts +483 -0
- package/src/collections/indexes.ts +53 -0
- package/src/collections/methods.ts +331 -0
- package/src/databases/methods.ts +47 -0
- package/src/init.ts +64 -64
- package/src/interactiveCLI.ts +767 -0
- package/src/main.ts +289 -83
- package/src/migrations/afterImportActions.ts +553 -490
- package/src/migrations/appwriteToX.ts +237 -174
- package/src/migrations/attributes.ts +483 -422
- package/src/migrations/backup.ts +205 -205
- package/src/migrations/collections.ts +545 -300
- package/src/migrations/converters.ts +161 -150
- package/src/migrations/dataLoader.ts +1615 -1304
- package/src/migrations/databases.ts +44 -25
- package/src/migrations/dbHelpers.ts +92 -92
- package/src/migrations/helper.ts +40 -0
- package/src/migrations/importController.ts +448 -384
- package/src/migrations/importDataActions.ts +315 -307
- package/src/migrations/indexes.ts +40 -37
- package/src/migrations/logging.ts +29 -16
- package/src/migrations/migrationHelper.ts +207 -201
- package/src/migrations/openapi.ts +83 -70
- package/src/migrations/queue.ts +118 -119
- package/src/migrations/relationships.ts +324 -324
- package/src/migrations/schemaStrings.ts +472 -460
- package/src/migrations/setupDatabase.ts +118 -219
- package/src/migrations/storage.ts +538 -358
- package/src/migrations/transfer.ts +608 -0
- package/src/migrations/users.ts +362 -285
- package/src/migrations/validationRules.ts +63 -63
- package/src/schemas/authUser.ts +23 -23
- package/src/setup.ts +8 -8
- package/src/storage/methods.ts +371 -0
- package/src/storage/schemas.ts +205 -0
- package/src/types.ts +9 -9
- package/src/utils/getClientFromConfig.ts +17 -0
- package/src/utils/helperFunctions.ts +181 -127
- package/src/utils/index.ts +2 -2
- package/src/utils/loadConfigs.ts +59 -59
- package/src/utils/retryFailedPromises.ts +27 -0
- package/src/utils/schemaStrings.ts +473 -0
- package/src/utils/setupFiles.ts +228 -182
- package/src/utilsController.ts +325 -194
- 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
|
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,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
|
+
}
|