appwrite-utils-cli 0.0.273 → 0.0.275

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 (77) hide show
  1. package/README.md +5 -39
  2. package/dist/init.d.ts +2 -0
  3. package/dist/init.js +57 -0
  4. package/dist/main.js +62 -100
  5. package/dist/migrations/afterImportActions.d.ts +1 -4
  6. package/dist/migrations/afterImportActions.js +1 -0
  7. package/dist/migrations/appwriteToX.d.ts +46 -46
  8. package/dist/migrations/appwriteToX.js +6 -2
  9. package/dist/migrations/attributes.d.ts +1 -1
  10. package/dist/migrations/attributes.js +98 -70
  11. package/dist/migrations/backup.d.ts +240 -240
  12. package/dist/migrations/backup.js +1 -1
  13. package/dist/migrations/collections.d.ts +1 -1
  14. package/dist/migrations/collections.js +5 -5
  15. package/dist/migrations/converters.d.ts +9 -127
  16. package/dist/migrations/converters.js +1 -504
  17. package/dist/migrations/dataLoader.d.ts +470 -453
  18. package/dist/migrations/dataLoader.js +19 -1
  19. package/dist/migrations/dbHelpers.d.ts +1 -1
  20. package/dist/migrations/dbHelpers.js +3 -0
  21. package/dist/migrations/importController.d.ts +1 -1
  22. package/dist/migrations/importController.js +4 -7
  23. package/dist/migrations/importDataActions.d.ts +4 -6
  24. package/dist/migrations/importDataActions.js +6 -4
  25. package/dist/migrations/indexes.d.ts +1 -1
  26. package/dist/migrations/indexes.js +1 -1
  27. package/dist/migrations/migrationHelper.d.ts +29 -29
  28. package/dist/migrations/migrationHelper.js +1 -1
  29. package/dist/migrations/openapi.d.ts +1 -1
  30. package/dist/migrations/openapi.js +4 -1
  31. package/dist/migrations/queue.d.ts +1 -1
  32. package/dist/migrations/relationships.d.ts +5 -5
  33. package/dist/migrations/relationships.js +3 -0
  34. package/dist/migrations/schemaStrings.d.ts +2 -2
  35. package/dist/migrations/schemaStrings.js +93 -8
  36. package/dist/migrations/setupDatabase.d.ts +1 -1
  37. package/dist/migrations/setupDatabase.js +1 -1
  38. package/dist/migrations/storage.d.ts +1 -1
  39. package/dist/migrations/users.d.ts +1 -1
  40. package/dist/schemas/authUser.d.ts +12 -10
  41. package/dist/types.d.ts +0 -5
  42. package/dist/types.js +0 -2
  43. package/dist/utils/helperFunctions.d.ts +2 -3
  44. package/dist/utils/loadConfigs.d.ts +13 -0
  45. package/dist/utils/loadConfigs.js +47 -0
  46. package/dist/utils/setupFiles.d.ts +1 -0
  47. package/dist/utils/setupFiles.js +98 -223
  48. package/dist/utilsController.d.ts +1 -3
  49. package/dist/utilsController.js +14 -18
  50. package/package.json +9 -2
  51. package/src/init.ts +64 -0
  52. package/src/main.ts +73 -98
  53. package/src/migrations/afterImportActions.ts +1 -5
  54. package/src/migrations/appwriteToX.ts +6 -2
  55. package/src/migrations/attributes.ts +198 -145
  56. package/src/migrations/backup.ts +1 -1
  57. package/src/migrations/collections.ts +6 -12
  58. package/src/migrations/converters.ts +1 -540
  59. package/src/migrations/dataLoader.ts +19 -2
  60. package/src/migrations/dbHelpers.ts +4 -1
  61. package/src/migrations/importController.ts +5 -15
  62. package/src/migrations/importDataActions.ts +10 -14
  63. package/src/migrations/indexes.ts +1 -1
  64. package/src/migrations/migrationHelper.ts +1 -1
  65. package/src/migrations/openapi.ts +4 -1
  66. package/src/migrations/queue.ts +1 -1
  67. package/src/migrations/relationships.ts +4 -1
  68. package/src/migrations/schemaStrings.ts +106 -9
  69. package/src/migrations/setupDatabase.ts +1 -1
  70. package/src/migrations/storage.ts +1 -1
  71. package/src/migrations/users.ts +1 -1
  72. package/src/types.ts +0 -5
  73. package/src/utils/helperFunctions.ts +2 -3
  74. package/src/utils/loadConfigs.ts +55 -0
  75. package/src/utils/setupFiles.ts +114 -225
  76. package/src/utilsController.ts +27 -35
  77. package/src/migrations/schema.ts +0 -748
@@ -1,174 +1,202 @@
1
1
  import { Query } from "node-appwrite";
2
- import { parseAttribute } from "./schema.js";
2
+ import { attributeSchema, parseAttribute, } from "appwrite-utils";
3
3
  import { nameToIdMapping, enqueueOperation } from "./queue.js";
4
4
  import _ from "lodash";
5
- const attributesSame = (a, b) => {
6
- // Direct type comparison for non-string types
7
- // Also check if the type IS string and has a format for either
8
- // That means the format is the type of the attribute
9
- if (a.type === b.type &&
10
- !((a.type === "string" && a.format) || (b.type === "string" && b.format))) {
11
- if (a.type === "relationship" && b.type === "relationship") {
12
- return (a.key === b.key &&
13
- a.relationType === b.relationType &&
14
- a.twoWay === b.twoWay &&
15
- a.twoWayKey === b.twoWayKey &&
16
- a.required === b.required);
17
- }
18
- return a.key === b.key && a.array === b.array && a.required === b.required;
19
- }
20
- if (a.type === "string" && a.format) {
21
- // @ts-expect-error
22
- a.type = a.format;
23
- }
24
- if (b.type === "string" && b.format) {
25
- // @ts-expect-error
26
- b.type = b.format;
27
- }
28
- // Handling string types with specific formats in Appwrite
29
- if (a.type === "string" && b.type === "string") {
30
- return (a.key === b.key &&
31
- a.format === b.format &&
32
- a.array === b.array &&
33
- a.required === b.required);
34
- }
35
- // Fallback to false if none of the above conditions are met
36
- return false;
5
+ const attributesSame = (databaseAttribute, configAttribute) => {
6
+ return (databaseAttribute.key == configAttribute.key &&
7
+ databaseAttribute.type == configAttribute.type &&
8
+ databaseAttribute.array == configAttribute.array);
37
9
  };
38
10
  export const createOrUpdateAttribute = async (db, dbId, collection, attribute) => {
39
11
  let action = "create";
40
12
  let foundAttribute;
13
+ const updateEnabled = false;
14
+ let finalAttribute = attribute;
41
15
  try {
42
- foundAttribute = await db.getAttribute(dbId, collection.$id, attribute.key);
43
- foundAttribute = parseAttribute(foundAttribute);
16
+ const collectionAttr = collection.attributes.find(
17
+ // @ts-expect-error
18
+ (attr) => attr.key === attribute.key);
19
+ foundAttribute = parseAttribute(collectionAttr);
44
20
  }
45
21
  catch (error) {
46
22
  foundAttribute = undefined;
47
23
  }
48
- let numSameAttributes = 0;
49
- if (foundAttribute && attributesSame(foundAttribute, attribute)) {
50
- numSameAttributes++;
51
- return;
24
+ if (foundAttribute &&
25
+ attributesSame(foundAttribute, attribute) &&
26
+ updateEnabled) {
27
+ // Check if mutable properties have changed and set action to "update" if necessary
28
+ const requiredChanged = "required" in foundAttribute && "required" in attribute
29
+ ? foundAttribute.required !== attribute.required
30
+ : false;
31
+ // const xdefaultChanged =
32
+ // "xdefault" in foundAttribute && "xdefault" in attribute
33
+ // ? foundAttribute.xdefault !== attribute.xdefault
34
+ // : false;
35
+ const onDeleteChanged = foundAttribute.type === "relationship" &&
36
+ attribute.type === "relationship" &&
37
+ "onDelete" in foundAttribute &&
38
+ "onDelete" in attribute
39
+ ? foundAttribute.onDelete !== attribute.onDelete
40
+ : false;
41
+ if (requiredChanged || onDeleteChanged) {
42
+ console.log(`Required changed: ${requiredChanged}\nOnDelete changed: ${onDeleteChanged}`);
43
+ console.log(`Found attribute: ${JSON.stringify(foundAttribute, null, 2)}`);
44
+ console.log(`Attribute: ${JSON.stringify(attribute, null, 2)}`);
45
+ finalAttribute = {
46
+ ...attribute,
47
+ ...foundAttribute,
48
+ };
49
+ action = "update";
50
+ }
51
+ else {
52
+ // If no properties that can be updated have changed, return early
53
+ return;
54
+ }
52
55
  }
53
- else if (foundAttribute && !attributesSame(foundAttribute, attribute)) {
54
- console.log(`Deleting attribute with same key ${attribute.key} -- ${foundAttribute.key} but different values -- ${JSON.stringify(attribute, null, 2)} -- ${JSON.stringify(foundAttribute, null, 2)}`);
56
+ else if (foundAttribute &&
57
+ !attributesSame(foundAttribute, attribute) &&
58
+ updateEnabled) {
59
+ console.log(`Deleting attribute with same key ${attribute.key} -- but different values -- ${JSON.stringify(attribute, null, 2)} -- ${JSON.stringify(foundAttribute, null, 2)}`);
55
60
  await db.deleteAttribute(dbId, collection.$id, attribute.key);
61
+ // After deletion, you might want to create the attribute anew
62
+ finalAttribute = attribute;
63
+ action = "create";
64
+ }
65
+ else if (!updateEnabled && foundAttribute) {
66
+ return;
56
67
  }
57
68
  // Relationship attribute logic with adjustments
58
69
  let collectionFoundViaRelatedCollection;
59
70
  let relatedCollectionId;
60
- if (attribute.type === "relationship") {
61
- if (nameToIdMapping.has(attribute.relatedCollection)) {
62
- relatedCollectionId = nameToIdMapping.get(attribute.relatedCollection);
71
+ if (finalAttribute.type === "relationship") {
72
+ if (nameToIdMapping.has(finalAttribute.relatedCollection)) {
73
+ relatedCollectionId = nameToIdMapping.get(finalAttribute.relatedCollection);
63
74
  try {
64
75
  collectionFoundViaRelatedCollection = await db.getCollection(dbId, relatedCollectionId);
65
76
  }
66
77
  catch (e) {
67
- console.log(`Collection not found: ${attribute.relatedCollection} when nameToIdMapping was set`);
78
+ console.log(`Collection not found: ${finalAttribute.relatedCollection} when nameToIdMapping was set`);
68
79
  collectionFoundViaRelatedCollection = undefined;
69
80
  }
70
81
  }
71
82
  else {
72
83
  const collectionsPulled = await db.listCollections(dbId, [
73
- Query.equal("name", attribute.relatedCollection),
84
+ Query.equal("name", finalAttribute.relatedCollection),
74
85
  ]);
75
86
  if (collectionsPulled.total > 0) {
76
87
  collectionFoundViaRelatedCollection = collectionsPulled.collections[0];
77
88
  relatedCollectionId = collectionFoundViaRelatedCollection.$id;
78
- nameToIdMapping.set(attribute.relatedCollection, relatedCollectionId);
89
+ nameToIdMapping.set(finalAttribute.relatedCollection, relatedCollectionId);
79
90
  }
80
91
  }
81
92
  if (!(relatedCollectionId && collectionFoundViaRelatedCollection)) {
82
- console.log(`Enqueueing operation for attribute: ${attribute.key}`);
93
+ console.log(`Enqueueing operation for attribute: ${finalAttribute.key}`);
83
94
  enqueueOperation({
84
95
  type: "attribute",
85
96
  collectionId: collection.$id,
86
97
  collection: collection,
87
98
  attribute,
88
- dependencies: [attribute.relatedCollection],
99
+ dependencies: [finalAttribute.relatedCollection],
89
100
  });
90
101
  return;
91
102
  }
92
103
  }
93
- switch (attribute.type) {
104
+ finalAttribute = attributeSchema.parse(finalAttribute);
105
+ switch (finalAttribute.type) {
94
106
  case "string":
95
107
  if (action === "create") {
96
- await db.createStringAttribute(dbId, collection.$id, attribute.key, attribute.size, attribute.required, attribute.xdefault || undefined, attribute.array, attribute.encrypted);
108
+ await db.createStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.size, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array || false, finalAttribute.encrypted);
97
109
  }
98
110
  else {
99
- await db.updateStringAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined);
111
+ await db.updateStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined);
100
112
  }
101
113
  break;
102
114
  case "integer":
103
115
  if (action === "create") {
104
- await db.createIntegerAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.min, attribute.max, attribute.xdefault || undefined, attribute.array);
116
+ if (finalAttribute.min &&
117
+ BigInt(finalAttribute.min) === BigInt(-9223372036854776000)) {
118
+ delete finalAttribute.min;
119
+ }
120
+ if (finalAttribute.max &&
121
+ BigInt(finalAttribute.max) === BigInt(9223372036854776000)) {
122
+ delete finalAttribute.max;
123
+ }
124
+ await db.createIntegerAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min, finalAttribute.max, finalAttribute.xdefault || undefined, finalAttribute.array);
105
125
  }
106
126
  else {
107
- await db.updateIntegerAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.min || 0, attribute.max || 2147483647, attribute.xdefault || undefined);
127
+ if (finalAttribute.min &&
128
+ BigInt(finalAttribute.min) === BigInt(-9223372036854776000)) {
129
+ delete finalAttribute.min;
130
+ }
131
+ if (finalAttribute.max &&
132
+ BigInt(finalAttribute.max) === BigInt(9223372036854776000)) {
133
+ delete finalAttribute.max;
134
+ }
135
+ await db.updateIntegerAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min || 0, finalAttribute.max || 2147483647, finalAttribute.xdefault || undefined);
108
136
  }
109
137
  break;
110
138
  case "float":
111
139
  if (action === "create") {
112
- await db.createFloatAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.min, attribute.max, attribute.xdefault || undefined, attribute.array);
140
+ await db.createFloatAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min, finalAttribute.max, finalAttribute.xdefault || undefined, finalAttribute.array);
113
141
  }
114
142
  else {
115
- await db.updateFloatAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.min || 0, attribute.max || 2147483647, attribute.xdefault || undefined);
143
+ await db.updateFloatAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.min || 0, finalAttribute.max || 2147483647, finalAttribute.xdefault || undefined);
116
144
  }
117
145
  break;
118
146
  case "boolean":
119
147
  if (action === "create") {
120
- await db.createBooleanAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined, attribute.array);
148
+ await db.createBooleanAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array);
121
149
  }
122
150
  else {
123
- await db.updateBooleanAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined);
151
+ await db.updateBooleanAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || null);
124
152
  }
125
153
  break;
126
154
  case "datetime":
127
155
  if (action === "create") {
128
- await db.createDatetimeAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined, attribute.array);
156
+ await db.createDatetimeAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array);
129
157
  }
130
158
  else {
131
- await db.updateDatetimeAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined);
159
+ await db.updateDatetimeAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined);
132
160
  }
133
161
  break;
134
162
  case "email":
135
163
  if (action === "create") {
136
- await db.createEmailAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined, attribute.array);
164
+ await db.createEmailAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array);
137
165
  }
138
166
  else {
139
- await db.updateEmailAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined);
167
+ await db.updateEmailAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined);
140
168
  }
141
169
  break;
142
170
  case "ip":
143
171
  if (action === "create") {
144
- await db.createIpAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined, attribute.array);
172
+ await db.createIpAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array);
145
173
  }
146
174
  else {
147
- await db.updateIpAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined);
175
+ await db.updateIpAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined);
148
176
  }
149
177
  break;
150
178
  case "url":
151
179
  if (action === "create") {
152
- await db.createUrlAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined, attribute.array);
180
+ await db.createUrlAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array);
153
181
  }
154
182
  else {
155
- await db.updateUrlAttribute(dbId, collection.$id, attribute.key, attribute.required, attribute.xdefault || undefined);
183
+ await db.updateUrlAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined);
156
184
  }
157
185
  break;
158
186
  case "enum":
159
187
  if (action === "create") {
160
- await db.createEnumAttribute(dbId, collection.$id, attribute.key, attribute.elements, attribute.required, attribute.xdefault || undefined, attribute.array);
188
+ await db.createEnumAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.elements, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array);
161
189
  }
162
190
  else {
163
- await db.updateEnumAttribute(dbId, collection.$id, attribute.key, attribute.elements, attribute.required, attribute.xdefault || undefined);
191
+ await db.updateEnumAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.elements, finalAttribute.required || false, finalAttribute.xdefault || undefined);
164
192
  }
165
193
  break;
166
194
  case "relationship":
167
195
  if (action === "create") {
168
- await db.createRelationshipAttribute(dbId, collection.$id, relatedCollectionId, attribute.relationType, attribute.twoWay, attribute.key, attribute.twoWayKey, attribute.onDelete);
196
+ await db.createRelationshipAttribute(dbId, collection.$id, relatedCollectionId, finalAttribute.relationType, finalAttribute.twoWay, finalAttribute.key, finalAttribute.twoWayKey, finalAttribute.onDelete);
169
197
  }
170
198
  else {
171
- await db.updateRelationshipAttribute(dbId, collection.$id, attribute.key, attribute.onDelete);
199
+ await db.updateRelationshipAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.onDelete);
172
200
  }
173
201
  break;
174
202
  default: