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,25 +1,44 @@
1
- import { Databases, Query, type Models } from "node-appwrite";
2
-
3
- export const fetchAllDatabases = async (
4
- database: Databases
5
- ): Promise<Models.Database[]> => {
6
- const databases = await database.list([Query.limit(25)]);
7
- const allDatabases = databases.databases;
8
- let lastDatabaseId = allDatabases[allDatabases.length - 1].$id;
9
- if (databases.databases.length < 25) {
10
- return allDatabases;
11
- } else {
12
- while (lastDatabaseId) {
13
- const databases = await database.list([
14
- Query.limit(25),
15
- Query.cursorAfter(lastDatabaseId),
16
- ]);
17
- allDatabases.push(...databases.databases);
18
- if (databases.databases.length < 25) {
19
- break;
20
- }
21
- lastDatabaseId = databases.databases[databases.databases.length - 1].$id;
22
- }
23
- }
24
- return allDatabases;
25
- };
1
+ import {
2
+ Client,
3
+ Databases,
4
+ IndexType,
5
+ Query,
6
+ type Models,
7
+ } from "node-appwrite";
8
+ import {
9
+ getAppwriteClient,
10
+ tryAwaitWithRetry,
11
+ } from "../utils/helperFunctions.js";
12
+ import {
13
+ transferDocumentsBetweenDbsLocalToLocal,
14
+ transferDocumentsBetweenDbsLocalToRemote,
15
+ } from "./collections.js";
16
+ import { createOrUpdateAttribute } from "./attributes.js";
17
+ import { parseAttribute } from "appwrite-utils";
18
+
19
+ export const fetchAllDatabases = async (
20
+ database: Databases
21
+ ): Promise<Models.Database[]> => {
22
+ const databases = await tryAwaitWithRetry(
23
+ async () => await database.list([Query.limit(25)])
24
+ );
25
+ const allDatabases = databases.databases;
26
+ if (allDatabases.length === 0) return [];
27
+ let lastDatabaseId = allDatabases[allDatabases.length - 1].$id;
28
+ if (databases.databases.length < 25) {
29
+ return allDatabases;
30
+ } else {
31
+ while (lastDatabaseId) {
32
+ const databases = await database.list([
33
+ Query.limit(25),
34
+ Query.cursorAfter(lastDatabaseId),
35
+ ]);
36
+ allDatabases.push(...databases.databases);
37
+ if (databases.databases.length < 25) {
38
+ break;
39
+ }
40
+ lastDatabaseId = databases.databases[databases.databases.length - 1].$id;
41
+ }
42
+ }
43
+ return allDatabases;
44
+ };
@@ -1,92 +1,92 @@
1
- import type {
2
- AppwriteConfig,
3
- Attribute,
4
- RelationshipAttribute,
5
- } from "appwrite-utils";
6
-
7
- // Helper function to categorize collections based on relationship sides
8
- export const categorizeCollectionByRelationshipSide = (
9
- attributes: Attribute[]
10
- ): "parent" | "mixed" | "child" => {
11
- let hasParent = false;
12
- let hasChild = false;
13
-
14
- for (const attr of attributes) {
15
- if (attr.type === "relationship") {
16
- if (attr.side === "parent") {
17
- hasParent = true;
18
- } else if (attr.side === "child") {
19
- hasChild = true;
20
- }
21
- }
22
- }
23
-
24
- if (hasParent && hasChild) return "mixed";
25
- if (hasParent) return "parent";
26
- return "child";
27
- };
28
-
29
- // Helper function to get all dependencies of a collection
30
- export const getDependencies = (attributes: Attribute[]): string[] => {
31
- return attributes
32
- .filter(
33
- (attr) =>
34
- attr.type === "relationship" && attr.relatedCollection !== undefined
35
- )
36
- .map((attr) => (attr as RelationshipAttribute).relatedCollection);
37
- };
38
-
39
- // Function to sort collections based on dependencies and relationship sides
40
- export const sortCollections = (
41
- configCollections: AppwriteConfig["collections"]
42
- ): AppwriteConfig["collections"] => {
43
- if (!configCollections) {
44
- return [];
45
- }
46
- // Categorize collections based on their relationship sides
47
- const parentCollections = configCollections.filter(
48
- ({ attributes }) =>
49
- categorizeCollectionByRelationshipSide(attributes) === "parent"
50
- );
51
- const mixedCollections = configCollections.filter(
52
- ({ attributes }) =>
53
- categorizeCollectionByRelationshipSide(attributes) === "mixed"
54
- );
55
- const childCollections = configCollections.filter(
56
- ({ attributes }) =>
57
- categorizeCollectionByRelationshipSide(attributes) === "child"
58
- );
59
-
60
- // Sort mixedCollections to ensure parents are processed before children within the mixed category
61
- // This might involve more sophisticated logic if you need to order mixed collections based on specific parent-child relationships
62
- mixedCollections.sort((a, b) => {
63
- // Example sorting logic for mixed collections; adjust based on your specific needs
64
- const aDependencies = getDependencies(a.attributes).length;
65
- const bDependencies = getDependencies(b.attributes).length;
66
- return aDependencies - bDependencies;
67
- });
68
-
69
- // Combine them back into a single array with the desired order
70
- // Children first because they have no dependencies and parents will create the relationship if it's twoWay
71
- return [...childCollections, ...parentCollections, ...mixedCollections];
72
- };
73
-
74
- // Function to sort attributes within a collection based on relationship sides
75
- export const sortAttributesByRelationshipSide = (
76
- attributes: Attribute[]
77
- ): Attribute[] => {
78
- // Separate attributes into parent and child based on their relationship side
79
- const parentAttributes = attributes.filter(
80
- (attr) => attr.type === "relationship" && attr.side === "parent"
81
- );
82
- const childAttributes = attributes.filter(
83
- (attr) => attr.type === "relationship" && attr.side === "child"
84
- );
85
- const otherAttributes = attributes.filter(
86
- (attr) => attr.type !== "relationship"
87
- );
88
-
89
- // Combine them back into a single array with child attributes first, then other attributes, and parent attributes last
90
- // as parent attributes will create the relationship with the child if needed
91
- return [...childAttributes, ...otherAttributes, ...parentAttributes];
92
- };
1
+ import type {
2
+ AppwriteConfig,
3
+ Attribute,
4
+ RelationshipAttribute,
5
+ } from "appwrite-utils";
6
+
7
+ // Helper function to categorize collections based on relationship sides
8
+ export const categorizeCollectionByRelationshipSide = (
9
+ attributes: Attribute[]
10
+ ): "parent" | "mixed" | "child" => {
11
+ let hasParent = false;
12
+ let hasChild = false;
13
+
14
+ for (const attr of attributes) {
15
+ if (attr.type === "relationship") {
16
+ if (attr.side === "parent") {
17
+ hasParent = true;
18
+ } else if (attr.side === "child") {
19
+ hasChild = true;
20
+ }
21
+ }
22
+ }
23
+
24
+ if (hasParent && hasChild) return "mixed";
25
+ if (hasParent) return "parent";
26
+ return "child";
27
+ };
28
+
29
+ // Helper function to get all dependencies of a collection
30
+ export const getDependencies = (attributes: Attribute[]): string[] => {
31
+ return attributes
32
+ .filter(
33
+ (attr) =>
34
+ attr.type === "relationship" && attr.relatedCollection !== undefined
35
+ )
36
+ .map((attr) => (attr as RelationshipAttribute).relatedCollection);
37
+ };
38
+
39
+ // Function to sort collections based on dependencies and relationship sides
40
+ export const sortCollections = (
41
+ configCollections: AppwriteConfig["collections"]
42
+ ): AppwriteConfig["collections"] => {
43
+ if (!configCollections) {
44
+ return [];
45
+ }
46
+ // Categorize collections based on their relationship sides
47
+ const parentCollections = configCollections.filter(
48
+ ({ attributes }) =>
49
+ categorizeCollectionByRelationshipSide(attributes) === "parent"
50
+ );
51
+ const mixedCollections = configCollections.filter(
52
+ ({ attributes }) =>
53
+ categorizeCollectionByRelationshipSide(attributes) === "mixed"
54
+ );
55
+ const childCollections = configCollections.filter(
56
+ ({ attributes }) =>
57
+ categorizeCollectionByRelationshipSide(attributes) === "child"
58
+ );
59
+
60
+ // Sort mixedCollections to ensure parents are processed before children within the mixed category
61
+ // This might involve more sophisticated logic if you need to order mixed collections based on specific parent-child relationships
62
+ mixedCollections.sort((a, b) => {
63
+ // Example sorting logic for mixed collections; adjust based on your specific needs
64
+ const aDependencies = getDependencies(a.attributes).length;
65
+ const bDependencies = getDependencies(b.attributes).length;
66
+ return aDependencies - bDependencies;
67
+ });
68
+
69
+ // Combine them back into a single array with the desired order
70
+ // Children first because they have no dependencies and parents will create the relationship if it's twoWay
71
+ return [...childCollections, ...parentCollections, ...mixedCollections];
72
+ };
73
+
74
+ // Function to sort attributes within a collection based on relationship sides
75
+ export const sortAttributesByRelationshipSide = (
76
+ attributes: Attribute[]
77
+ ): Attribute[] => {
78
+ // Separate attributes into parent and child based on their relationship side
79
+ const parentAttributes = attributes.filter(
80
+ (attr) => attr.type === "relationship" && attr.side === "parent"
81
+ );
82
+ const childAttributes = attributes.filter(
83
+ (attr) => attr.type === "relationship" && attr.side === "child"
84
+ );
85
+ const otherAttributes = attributes.filter(
86
+ (attr) => attr.type !== "relationship"
87
+ );
88
+
89
+ // Combine them back into a single array with child attributes first, then other attributes, and parent attributes last
90
+ // as parent attributes will create the relationship with the child if needed
91
+ return [...childAttributes, ...otherAttributes, ...parentAttributes];
92
+ };
@@ -0,0 +1,40 @@
1
+ import type { Databases, Models } from "node-appwrite";
2
+ import type { OperationCreate } from "../storage/schemas.js";
3
+ import { tryAwaitWithRetry } from "appwrite-utils";
4
+ import { ulid } from "ulidx";
5
+
6
+ export const logOperation = async (
7
+ db: Databases,
8
+ dbId: string,
9
+ operationDetails: OperationCreate,
10
+ operationId?: string
11
+ ): Promise<Models.Document> => {
12
+ try {
13
+ let operation;
14
+ if (operationId) {
15
+ // Update existing operation log
16
+ operation = await tryAwaitWithRetry(
17
+ async () =>
18
+ await db.updateDocument(
19
+ "migrations",
20
+ "currentOperations",
21
+ operationId,
22
+ operationDetails
23
+ )
24
+ );
25
+ } else {
26
+ // Create new operation log
27
+ operation = await db.createDocument(
28
+ "migrations",
29
+ "currentOperations",
30
+ ulid(),
31
+ operationDetails
32
+ );
33
+ }
34
+ console.log(`Operation logged: ${operation.$id}`);
35
+ return operation;
36
+ } catch (error) {
37
+ console.error(`Error logging operation: ${error}`);
38
+ throw error;
39
+ }
40
+ };