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.
- package/README.md +122 -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 +227 -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 +292 -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,219 +1,118 @@
|
|
1
|
-
import { Databases,
|
2
|
-
import { createOrUpdateAttribute } from "./attributes.js";
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
import {
|
10
|
-
import {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
81
|
-
|
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
|
-
await database.delete(db.$id);
|
115
|
-
console.log(`Deleted database: ${db.name}`);
|
116
|
-
}
|
117
|
-
}
|
118
|
-
};
|
119
|
-
|
120
|
-
export const startSetup = async (
|
121
|
-
database: Databases,
|
122
|
-
storage: Storage,
|
123
|
-
config: AppwriteConfig,
|
124
|
-
setupOptions: SetupOptions,
|
125
|
-
appwriteFolderPath: string
|
126
|
-
) => {
|
127
|
-
await setupMigrationDatabase(config);
|
128
|
-
|
129
|
-
if (config.enableBackups) {
|
130
|
-
await initOrGetBackupStorage(storage);
|
131
|
-
if (setupOptions.wipeDocumentStorage) {
|
132
|
-
if (setupOptions.runProd) {
|
133
|
-
await initOrGetDocumentStorage(
|
134
|
-
storage,
|
135
|
-
config,
|
136
|
-
config.databases[0].name
|
137
|
-
);
|
138
|
-
await wipeDocumentStorage(storage, config, config.databases[0].name);
|
139
|
-
}
|
140
|
-
if (setupOptions.runStaging) {
|
141
|
-
await initOrGetDocumentStorage(
|
142
|
-
storage,
|
143
|
-
config,
|
144
|
-
config.databases[1].name
|
145
|
-
);
|
146
|
-
await wipeDocumentStorage(storage, config, config.databases[1].name);
|
147
|
-
}
|
148
|
-
if (setupOptions.runDev) {
|
149
|
-
await initOrGetDocumentStorage(
|
150
|
-
storage,
|
151
|
-
config,
|
152
|
-
config.databases[2].name
|
153
|
-
);
|
154
|
-
await wipeDocumentStorage(storage, config, config.databases[2].name);
|
155
|
-
}
|
156
|
-
}
|
157
|
-
}
|
158
|
-
if (config.enableWipeOtherDatabases) {
|
159
|
-
await wipeOtherDatabases(database, config);
|
160
|
-
}
|
161
|
-
if (setupOptions.wipeUsers) {
|
162
|
-
const usersController = new UsersController(config, database);
|
163
|
-
console.log("Wiping users");
|
164
|
-
await usersController.wipeUsers();
|
165
|
-
console.log("Users wiped");
|
166
|
-
}
|
167
|
-
await ensureDatabasesExist(config);
|
168
|
-
|
169
|
-
const databaseNames = config.databases.map((db) => db.name);
|
170
|
-
|
171
|
-
// Move to here so it always runs if it's set to true
|
172
|
-
if (setupOptions.generateSchemas) {
|
173
|
-
await generateSchemas(config, appwriteFolderPath);
|
174
|
-
}
|
175
|
-
|
176
|
-
for (const db of config.databases) {
|
177
|
-
// Determine if the current database should be processed based on the setup options
|
178
|
-
const processDatabase =
|
179
|
-
(setupOptions.runProd &&
|
180
|
-
areCollectionNamesSame(db.name, databaseNames[0])) ||
|
181
|
-
(setupOptions.runStaging &&
|
182
|
-
areCollectionNamesSame(db.name, databaseNames[1])) ||
|
183
|
-
(setupOptions.runDev &&
|
184
|
-
areCollectionNamesSame(db.name, databaseNames[2]));
|
185
|
-
if (!processDatabase) {
|
186
|
-
continue;
|
187
|
-
} else {
|
188
|
-
await initOrGetDocumentStorage(storage, config, db.name);
|
189
|
-
}
|
190
|
-
console.log(`---------------------------------`);
|
191
|
-
console.log(`Starting setup for database: ${db.name}`);
|
192
|
-
console.log(`---------------------------------`);
|
193
|
-
let deletedCollections:
|
194
|
-
| { collectionId: string; collectionName: string }[]
|
195
|
-
| undefined;
|
196
|
-
|
197
|
-
if (setupOptions.wipeDatabases && processDatabase) {
|
198
|
-
if (config.enableBackups && setupOptions.doBackup) {
|
199
|
-
await backupDatabase(database, db.$id, storage);
|
200
|
-
}
|
201
|
-
deletedCollections = await wipeDatabase(database, db.$id);
|
202
|
-
}
|
203
|
-
|
204
|
-
if (processDatabase) {
|
205
|
-
await createOrUpdateCollections(
|
206
|
-
database,
|
207
|
-
db.$id,
|
208
|
-
config,
|
209
|
-
deletedCollections
|
210
|
-
);
|
211
|
-
}
|
212
|
-
|
213
|
-
deletedCollections = undefined;
|
214
|
-
nameToIdMapping.clear();
|
215
|
-
console.log(`---------------------------------`);
|
216
|
-
console.log(`Finished setup for database: ${db.name}`);
|
217
|
-
console.log(`---------------------------------`);
|
218
|
-
}
|
219
|
-
};
|
1
|
+
import { Databases, Query, type Models } from "node-appwrite";
|
2
|
+
import { createOrUpdateAttribute } from "./attributes.js";
|
3
|
+
import { getMigrationCollectionSchemas } from "./backup.js";
|
4
|
+
import {
|
5
|
+
areCollectionNamesSame,
|
6
|
+
toCamelCase,
|
7
|
+
tryAwaitWithRetry,
|
8
|
+
} from "../utils/index.js";
|
9
|
+
import { type AppwriteConfig } from "appwrite-utils";
|
10
|
+
import { ulid } from "ulidx";
|
11
|
+
|
12
|
+
export const setupMigrationDatabase = async (config: AppwriteConfig) => {
|
13
|
+
console.log("---------------------------------");
|
14
|
+
console.log("Starting Migrations Setup");
|
15
|
+
console.log("---------------------------------");
|
16
|
+
const database = new Databases(config.appwriteClient!);
|
17
|
+
let db: Models.Database | null = null;
|
18
|
+
const migrationCollectionsSetup = getMigrationCollectionSchemas();
|
19
|
+
|
20
|
+
try {
|
21
|
+
db = await tryAwaitWithRetry(
|
22
|
+
async () => await database.get("migrations"),
|
23
|
+
undefined,
|
24
|
+
true
|
25
|
+
);
|
26
|
+
console.log("Migrations database found");
|
27
|
+
} catch (e) {
|
28
|
+
db = await tryAwaitWithRetry(
|
29
|
+
async () => await database.create("migrations", "Migrations", true)
|
30
|
+
);
|
31
|
+
console.log("Migrations database created");
|
32
|
+
}
|
33
|
+
|
34
|
+
for (const [collectionName, { collection, attributes }] of Object.entries(
|
35
|
+
migrationCollectionsSetup
|
36
|
+
)) {
|
37
|
+
const collectionId = toCamelCase(collectionName);
|
38
|
+
let collectionFound: Models.Collection | null = null;
|
39
|
+
try {
|
40
|
+
collectionFound = await tryAwaitWithRetry(
|
41
|
+
async () => await database.getCollection(db!.$id, collectionId)
|
42
|
+
);
|
43
|
+
} catch (e) {
|
44
|
+
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
|
+
);
|
56
|
+
}
|
57
|
+
for (const attribute of attributes) {
|
58
|
+
await createOrUpdateAttribute(
|
59
|
+
database,
|
60
|
+
db!.$id,
|
61
|
+
collectionFound!,
|
62
|
+
attribute
|
63
|
+
);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
console.log("---------------------------------");
|
67
|
+
console.log("Migrations Setup Complete");
|
68
|
+
console.log("---------------------------------");
|
69
|
+
};
|
70
|
+
|
71
|
+
export const ensureDatabasesExist = async (config: AppwriteConfig) => {
|
72
|
+
const database = new Databases(config.appwriteClient!);
|
73
|
+
const databasesToEnsure = config.databases;
|
74
|
+
|
75
|
+
const existingDatabases = await tryAwaitWithRetry(
|
76
|
+
async () => await database.list([Query.limit(500)])
|
77
|
+
);
|
78
|
+
|
79
|
+
const migrationsDatabase = existingDatabases.databases.find(
|
80
|
+
(d) => d.name.toLowerCase().trim().replace(" ", "") === "migrations"
|
81
|
+
);
|
82
|
+
if (existingDatabases.databases.length !== 0 && migrationsDatabase) {
|
83
|
+
console.log("Wiping all databases except migrations");
|
84
|
+
databasesToEnsure.push(migrationsDatabase);
|
85
|
+
}
|
86
|
+
|
87
|
+
for (const db of databasesToEnsure) {
|
88
|
+
if (!existingDatabases.databases.some((d) => d.name === db.name)) {
|
89
|
+
await tryAwaitWithRetry(
|
90
|
+
async () => await database.create(db.$id || ulid(), db.name, true)
|
91
|
+
);
|
92
|
+
console.log(`${db.name} database created`);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
};
|
96
|
+
|
97
|
+
export const wipeOtherDatabases = async (
|
98
|
+
database: Databases,
|
99
|
+
databasesToKeep: Models.Database[]
|
100
|
+
) => {
|
101
|
+
console.log(`Databases to keep: ${databasesToKeep.join(", ")}`);
|
102
|
+
const allDatabases = await tryAwaitWithRetry(
|
103
|
+
async () => await database.list([Query.limit(500)])
|
104
|
+
);
|
105
|
+
const migrationsDatabase = allDatabases.databases.find(
|
106
|
+
(d) => d.name.toLowerCase().trim().replace(" ", "") === "migrations"
|
107
|
+
);
|
108
|
+
if (allDatabases.databases.length !== 0 && migrationsDatabase) {
|
109
|
+
console.log("Wiping all databases except migrations");
|
110
|
+
databasesToKeep.push(migrationsDatabase);
|
111
|
+
}
|
112
|
+
for (const db of allDatabases.databases) {
|
113
|
+
if (!databasesToKeep.some((d) => d.name === db.name)) {
|
114
|
+
await tryAwaitWithRetry(async () => await database.delete(db.$id));
|
115
|
+
console.log(`Deleted database: ${db.name}`);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
};
|