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.
Files changed (109) hide show
  1. package/README.md +162 -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 +224 -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 +289 -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
@@ -1,219 +1,118 @@
1
- import { Databases, ID, Query, Storage, type Models } from "node-appwrite";
2
- import { createOrUpdateAttribute } from "./attributes.js";
3
- import {
4
- createOrUpdateCollections,
5
- generateSchemas,
6
- wipeDatabase,
7
- } from "./collections.js";
8
- import { getMigrationCollectionSchemas } from "./backup.js";
9
- import { areCollectionNamesSame, toCamelCase } from "../utils/index.js";
10
- import {
11
- backupDatabase,
12
- initOrGetBackupStorage,
13
- initOrGetDocumentStorage,
14
- wipeDocumentStorage,
15
- } from "./storage.js";
16
- import { type AppwriteConfig } from "appwrite-utils";
17
- import type { SetupOptions } from "../utilsController.js";
18
- import { nameToIdMapping } from "./queue.js";
19
- import { UsersController } from "./users.js";
20
-
21
- export const setupMigrationDatabase = async (config: AppwriteConfig) => {
22
- // Create the migrations database if needed
23
- console.log("---------------------------------");
24
- console.log("Starting Migrations Setup");
25
- console.log("---------------------------------");
26
- const database = new Databases(config.appwriteClient!);
27
- let db: Models.Database | null = null;
28
- const dbCollections: Models.Collection[] = [];
29
- const migrationCollectionsSetup = getMigrationCollectionSchemas();
30
- try {
31
- db = await database.get("migrations");
32
- console.log("Migrations database found");
33
- } catch (e) {
34
- db = await database.create("migrations", "Migrations", true);
35
- console.log("Migrations database created");
36
- }
37
- if (db) {
38
- const collectionsPulled = await database.listCollections(db.$id, [
39
- Query.limit(25),
40
- ]);
41
- dbCollections.push(...collectionsPulled.collections);
42
- }
43
- console.log(`Collections in migrations database: ${dbCollections.length}`);
44
-
45
- // Iterate over each key in the migrationCollectionsSetup object
46
- for (const [collectionName, { collection, attributes }] of Object.entries(
47
- migrationCollectionsSetup
48
- )) {
49
- const collectionId = toCamelCase(collectionName); // Convert name to toCamelCase for the ID
50
- let collectionFound: Models.Collection | null = null;
51
- try {
52
- collectionFound = await database.getCollection(db.$id, collectionId);
53
- } catch (e) {
54
- console.log(`Collection not found: ${collectionId}`);
55
- }
56
- if (!collectionFound) {
57
- // Create the collection with the provided configuration
58
- collectionFound = await database.createCollection(
59
- db.$id,
60
- collectionId,
61
- collectionName,
62
- undefined,
63
- collection.documentSecurity,
64
- collection.enabled
65
- );
66
- }
67
- for (const attribute of attributes) {
68
- await createOrUpdateAttribute(
69
- database,
70
- db.$id,
71
- collectionFound,
72
- attribute
73
- );
74
- }
75
- }
76
- console.log("---------------------------------");
77
- console.log("Migrations Setup Complete");
78
- console.log("---------------------------------");
79
- };
80
-
81
- export const ensureDatabasesExist = async (config: AppwriteConfig) => {
82
- const database = new Databases(config.appwriteClient);
83
- const databasesToEnsure = config.databases;
84
- databasesToEnsure.push({
85
- $id: "migrations",
86
- name: "Migrations",
87
- });
88
- const dbNames = databasesToEnsure.map((db) => db.name);
89
-
90
- const existingDatabases = await database.list([Query.equal("name", dbNames)]);
91
-
92
- for (const db of databasesToEnsure) {
93
- if (!existingDatabases.databases.some((d) => d.name === db.name)) {
94
- await database.create(db.$id || ID.unique(), db.name, true);
95
- console.log(`${db.name} database created`);
96
- }
97
- }
98
- };
99
-
100
- export const wipeOtherDatabases = async (
101
- database: Databases,
102
- config: AppwriteConfig
103
- ) => {
104
- const databasesToKeep = config.databases.map((db) =>
105
- db.name.toLowerCase().trim().replace(" ", "")
106
- );
107
- databasesToKeep.push("migrations");
108
- console.log(`Databases to keep: ${databasesToKeep.join(", ")}`);
109
- const allDatabases = await database.list([Query.limit(500)]);
110
- for (const db of allDatabases.databases) {
111
- if (
112
- !databasesToKeep.includes(db.name.toLowerCase().trim().replace(" ", ""))
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
+ };