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
@@ -1,37 +1,40 @@
|
|
1
|
-
import { indexSchema, type Index } from "appwrite-utils";
|
2
|
-
import { Databases, Query, type Models } from "node-appwrite";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
index.
|
22
|
-
index.
|
23
|
-
index.
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
1
|
+
import { indexSchema, type Index } from "appwrite-utils";
|
2
|
+
import { Databases, IndexType, Query, type Models } from "node-appwrite";
|
3
|
+
import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
4
|
+
// import {}
|
5
|
+
|
6
|
+
export const createOrUpdateIndex = async (
|
7
|
+
dbId: string,
|
8
|
+
db: Databases,
|
9
|
+
collectionId: string,
|
10
|
+
index: Index
|
11
|
+
) => {
|
12
|
+
const existingIndex = await db.listIndexes(dbId, collectionId, [
|
13
|
+
Query.equal("key", index.key),
|
14
|
+
]);
|
15
|
+
if (existingIndex.total > 0) {
|
16
|
+
await db.deleteIndex(dbId, collectionId, existingIndex.indexes[0].key);
|
17
|
+
}
|
18
|
+
const newIndex = await db.createIndex(
|
19
|
+
dbId,
|
20
|
+
collectionId,
|
21
|
+
index.key,
|
22
|
+
index.type as IndexType,
|
23
|
+
index.attributes,
|
24
|
+
index.orders
|
25
|
+
);
|
26
|
+
return newIndex;
|
27
|
+
};
|
28
|
+
|
29
|
+
export const createOrUpdateIndexes = async (
|
30
|
+
dbId: string,
|
31
|
+
db: Databases,
|
32
|
+
collectionId: string,
|
33
|
+
indexes: Index[]
|
34
|
+
) => {
|
35
|
+
for (const index of indexes) {
|
36
|
+
await tryAwaitWithRetry(
|
37
|
+
async () => await createOrUpdateIndex(dbId, db, collectionId, index)
|
38
|
+
);
|
39
|
+
}
|
40
|
+
};
|
@@ -1,16 +1,29 @@
|
|
1
|
-
import winston from "winston";
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
1
|
+
import winston from "winston";
|
2
|
+
import fs from "fs";
|
3
|
+
import path from "path";
|
4
|
+
|
5
|
+
// Ensure the logs directory exists
|
6
|
+
const logDir = path.join(process.cwd(), "zlogs");
|
7
|
+
if (!fs.existsSync(logDir)) {
|
8
|
+
fs.mkdirSync(logDir);
|
9
|
+
}
|
10
|
+
|
11
|
+
export const logger = winston.createLogger({
|
12
|
+
level: "debug",
|
13
|
+
format: winston.format.json({ space: 2 }),
|
14
|
+
defaultMeta: { service: "appwrite-utils-cli" },
|
15
|
+
transports: [
|
16
|
+
new winston.transports.File({
|
17
|
+
filename: path.join(logDir, "error.log"),
|
18
|
+
level: "error",
|
19
|
+
}),
|
20
|
+
new winston.transports.File({
|
21
|
+
filename: path.join(logDir, "warn.log"),
|
22
|
+
level: "warn",
|
23
|
+
}),
|
24
|
+
new winston.transports.File({
|
25
|
+
filename: path.join(logDir, "info.log"),
|
26
|
+
level: "info",
|
27
|
+
}),
|
28
|
+
],
|
29
|
+
});
|
@@ -1,201 +1,207 @@
|
|
1
|
-
import { ID, Query, type Databases } from "node-appwrite";
|
2
|
-
import { BatchSchema, OperationSchema, type Operation } from "./backup.js";
|
3
|
-
import { AttributeMappingsSchema } from "appwrite-utils";
|
4
|
-
import { z } from "zod";
|
5
|
-
import { logger } from "./logging.js";
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
*
|
10
|
-
*
|
11
|
-
* @
|
12
|
-
* @
|
13
|
-
* @property {
|
14
|
-
* @property {
|
15
|
-
* @property {string
|
16
|
-
* @property {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
"
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
"
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
Query.equal("
|
81
|
-
Query.
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
"
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
);
|
122
|
-
|
123
|
-
if (operations.documents.length > 0) {
|
124
|
-
return OperationSchema.parse(operations.documents[0]); // Assuming the first document is the operation we want
|
125
|
-
} else {
|
126
|
-
// Create a new operation document
|
127
|
-
const op = await
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
currentBatchLength
|
186
|
-
currentBatchItemCount
|
187
|
-
|
188
|
-
//
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
1
|
+
import { ID, Query, type Databases } from "node-appwrite";
|
2
|
+
import { BatchSchema, OperationSchema, type Operation } from "./backup.js";
|
3
|
+
import { AttributeMappingsSchema } from "appwrite-utils";
|
4
|
+
import { z } from "zod";
|
5
|
+
import { logger } from "./logging.js";
|
6
|
+
import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Object that contains the context for an action that needs to be executed after import
|
10
|
+
* Used in the afterImportActionsDefinitions
|
11
|
+
* @type {ContextObject}
|
12
|
+
* @typedef {Object} ContextObject
|
13
|
+
* @property {string} collectionId - The ID of the collection
|
14
|
+
* @property {any} finalItem - The final item that was imported
|
15
|
+
* @property {string} action - The name of the action
|
16
|
+
* @property {string[]} params - The parameters for the action
|
17
|
+
* @property {Object} context - The context object for the action (all the data of this specific item)
|
18
|
+
*/
|
19
|
+
export const ContextObject = z.object({
|
20
|
+
dbId: z.string(),
|
21
|
+
collectionId: z.string(),
|
22
|
+
finalItem: z.any(),
|
23
|
+
attributeMappings: AttributeMappingsSchema,
|
24
|
+
context: z.any(),
|
25
|
+
});
|
26
|
+
|
27
|
+
export type ContextObject = z.infer<typeof ContextObject>;
|
28
|
+
|
29
|
+
export const createOrFindAfterImportOperation = async (
|
30
|
+
database: Databases,
|
31
|
+
collectionId: string,
|
32
|
+
context: ContextObject
|
33
|
+
) => {
|
34
|
+
let operation = await findOrCreateOperation(
|
35
|
+
database,
|
36
|
+
collectionId,
|
37
|
+
"afterImportAction"
|
38
|
+
);
|
39
|
+
if (!operation.batches) {
|
40
|
+
operation.batches = [];
|
41
|
+
}
|
42
|
+
|
43
|
+
// Directly create a new batch for the context without checking for an existing batch
|
44
|
+
const contextData = JSON.stringify(context);
|
45
|
+
// Create a new batch with the contextData
|
46
|
+
const newBatchId = await addBatch(database, contextData);
|
47
|
+
// Update the operation with the new batch's $id
|
48
|
+
operation.batches = [...operation.batches, newBatchId];
|
49
|
+
await database.updateDocument(
|
50
|
+
"migrations",
|
51
|
+
"currentOperations",
|
52
|
+
operation.$id,
|
53
|
+
{ batches: operation.batches }
|
54
|
+
);
|
55
|
+
};
|
56
|
+
|
57
|
+
export const addBatch = async (database: Databases, data: string) => {
|
58
|
+
const batch = await database.createDocument(
|
59
|
+
"migrations",
|
60
|
+
"batches",
|
61
|
+
ID.unique(),
|
62
|
+
{
|
63
|
+
data,
|
64
|
+
processed: false,
|
65
|
+
}
|
66
|
+
);
|
67
|
+
return batch.$id;
|
68
|
+
};
|
69
|
+
|
70
|
+
export const getAfterImportOperations = async (
|
71
|
+
database: Databases,
|
72
|
+
collectionId: string
|
73
|
+
) => {
|
74
|
+
let lastDocumentId: string | undefined;
|
75
|
+
const allOperations = [];
|
76
|
+
let total = 0;
|
77
|
+
|
78
|
+
do {
|
79
|
+
const query = [
|
80
|
+
Query.equal("collectionId", collectionId),
|
81
|
+
Query.equal("operationType", "afterImportAction"),
|
82
|
+
Query.limit(100),
|
83
|
+
];
|
84
|
+
|
85
|
+
if (lastDocumentId) {
|
86
|
+
query.push(Query.cursorAfter(lastDocumentId));
|
87
|
+
}
|
88
|
+
|
89
|
+
const operations = await database.listDocuments(
|
90
|
+
"migrations",
|
91
|
+
"currentOperations",
|
92
|
+
query
|
93
|
+
);
|
94
|
+
total = operations.total; // Update total with the latest fetch
|
95
|
+
allOperations.push(...operations.documents);
|
96
|
+
|
97
|
+
if (operations.documents.length > 0 && operations.documents.length >= 100) {
|
98
|
+
lastDocumentId =
|
99
|
+
operations.documents[operations.documents.length - 1].$id;
|
100
|
+
}
|
101
|
+
} while (allOperations.length < total);
|
102
|
+
|
103
|
+
const allOps = allOperations.map((op) => OperationSchema.parse(op));
|
104
|
+
return allOps;
|
105
|
+
};
|
106
|
+
|
107
|
+
export const findOrCreateOperation = async (
|
108
|
+
database: Databases,
|
109
|
+
collectionId: string,
|
110
|
+
operationType: string,
|
111
|
+
additionalQueries?: string[]
|
112
|
+
) => {
|
113
|
+
const operations = await tryAwaitWithRetry(
|
114
|
+
async () =>
|
115
|
+
await database.listDocuments("migrations", "currentOperations", [
|
116
|
+
Query.equal("collectionId", collectionId),
|
117
|
+
Query.equal("operationType", operationType),
|
118
|
+
Query.equal("status", "pending"),
|
119
|
+
...(additionalQueries || []),
|
120
|
+
])
|
121
|
+
);
|
122
|
+
|
123
|
+
if (operations.documents.length > 0) {
|
124
|
+
return OperationSchema.parse(operations.documents[0]); // Assuming the first document is the operation we want
|
125
|
+
} else {
|
126
|
+
// Create a new operation document
|
127
|
+
const op = await tryAwaitWithRetry(
|
128
|
+
async () =>
|
129
|
+
await database.createDocument(
|
130
|
+
"migrations",
|
131
|
+
"currentOperations",
|
132
|
+
ID.unique(),
|
133
|
+
{
|
134
|
+
operationType,
|
135
|
+
collectionId,
|
136
|
+
status: "pending",
|
137
|
+
batches: [],
|
138
|
+
progress: 0,
|
139
|
+
total: 0,
|
140
|
+
error: "",
|
141
|
+
}
|
142
|
+
)
|
143
|
+
);
|
144
|
+
|
145
|
+
return OperationSchema.parse(op);
|
146
|
+
}
|
147
|
+
};
|
148
|
+
|
149
|
+
export const updateOperation = async (
|
150
|
+
database: Databases,
|
151
|
+
operationId: string,
|
152
|
+
updateFields: any
|
153
|
+
) => {
|
154
|
+
await tryAwaitWithRetry(
|
155
|
+
async () =>
|
156
|
+
await database.updateDocument(
|
157
|
+
"migrations",
|
158
|
+
"currentOperations",
|
159
|
+
operationId,
|
160
|
+
updateFields
|
161
|
+
)
|
162
|
+
);
|
163
|
+
};
|
164
|
+
|
165
|
+
// Actual max 1073741824
|
166
|
+
export const maxDataLength = 1073741820;
|
167
|
+
export const maxBatchItems = 25;
|
168
|
+
|
169
|
+
export const splitIntoBatches = (data: any[]): any[][] => {
|
170
|
+
let batches = [];
|
171
|
+
let currentBatch: any[] = [];
|
172
|
+
let currentBatchLength = 0;
|
173
|
+
let currentBatchItemCount = 0;
|
174
|
+
|
175
|
+
data.forEach((item, index) => {
|
176
|
+
const itemLength = JSON.stringify(item).length;
|
177
|
+
if (itemLength > maxDataLength) {
|
178
|
+
console.log(
|
179
|
+
item,
|
180
|
+
`Large item found at index ${index} with length ${itemLength}:`
|
181
|
+
);
|
182
|
+
}
|
183
|
+
// Check if adding the current item would exceed the max length or max items per batch
|
184
|
+
if (
|
185
|
+
currentBatchLength + itemLength >= maxDataLength ||
|
186
|
+
currentBatchItemCount >= maxBatchItems
|
187
|
+
) {
|
188
|
+
// If so, start a new batch
|
189
|
+
batches.push(currentBatch);
|
190
|
+
currentBatch = [item];
|
191
|
+
currentBatchLength = itemLength;
|
192
|
+
currentBatchItemCount = 1; // Reset item count for the new batch
|
193
|
+
} else {
|
194
|
+
// Otherwise, add the item to the current batch
|
195
|
+
currentBatch.push(item);
|
196
|
+
currentBatchLength += itemLength;
|
197
|
+
currentBatchItemCount++;
|
198
|
+
}
|
199
|
+
});
|
200
|
+
|
201
|
+
// Don't forget to add the last batch if it's not empty
|
202
|
+
if (currentBatch.length > 0) {
|
203
|
+
batches.push(currentBatch);
|
204
|
+
}
|
205
|
+
|
206
|
+
return batches;
|
207
|
+
};
|