appwrite-utils-cli 0.9.54 → 0.9.56

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 CHANGED
@@ -124,7 +124,9 @@ This updated CLI ensures that developers have robust tools at their fingertips t
124
124
 
125
125
  ## Changelog
126
126
 
127
- - 0.9.53: Added small delay (`100ms`) between collection deletions, reduced other delays from `1000` to `500/250ms`
127
+ - 0.9.56: Changed the updateAttribute so it doesn't always update attributes and hopefully fixed the required error
128
+ - 0.9.55: Updated to use `node-appwrite@14` for appwrite 1.6.0
129
+ - 0.9.54: Added small delay (`100ms`) between collection deletions, reduced other delays from `1000` to `500/250ms`
128
130
  - 0.9.53: Reduced delay, went too far
129
131
  - 0.9.52: Add delay after creating indexes, attributes, and others to prevent `fetch failed` errors during large-scale collection creation
130
132
  - 0.9.51: Fix transfer databases, remove "ensure duplicates" check
@@ -11,61 +11,45 @@ const attributesSame = (databaseAttribute, configAttribute) => {
11
11
  export const createOrUpdateAttribute = async (db, dbId, collection, attribute) => {
12
12
  let action = "create";
13
13
  let foundAttribute;
14
- const updateEnabled = false;
15
14
  let finalAttribute = attribute;
16
15
  try {
17
16
  const collectionAttr = collection.attributes.find(
18
- // @ts-expect-error
17
+ // @ts-ignore
19
18
  (attr) => attr.key === attribute.key);
20
19
  foundAttribute = parseAttribute(collectionAttr);
21
20
  }
22
21
  catch (error) {
23
22
  foundAttribute = undefined;
24
23
  }
25
- if (foundAttribute &&
26
- attributesSame(foundAttribute, attribute) &&
27
- updateEnabled) {
28
- // Check if mutable properties have changed and set action to "update" if necessary
24
+ if (foundAttribute) {
25
+ // Check if any properties have changed
29
26
  const requiredChanged = "required" in foundAttribute && "required" in attribute
30
27
  ? foundAttribute.required !== attribute.required
31
28
  : false;
32
- // const xdefaultChanged =
33
- // "xdefault" in foundAttribute && "xdefault" in attribute
34
- // ? foundAttribute.xdefault !== attribute.xdefault
35
- // : false;
29
+ const xdefaultChanged = "xdefault" in foundAttribute && "xdefault" in attribute
30
+ ? foundAttribute.xdefault !== attribute.xdefault
31
+ : false;
36
32
  const onDeleteChanged = foundAttribute.type === "relationship" &&
37
33
  attribute.type === "relationship" &&
38
34
  "onDelete" in foundAttribute &&
39
35
  "onDelete" in attribute
40
36
  ? foundAttribute.onDelete !== attribute.onDelete
41
37
  : false;
42
- if (requiredChanged || onDeleteChanged) {
43
- console.log(`Required changed: ${requiredChanged}\nOnDelete changed: ${onDeleteChanged}`);
38
+ if (requiredChanged || xdefaultChanged || onDeleteChanged) {
39
+ console.log(`Updating attribute: ${attribute.key}\nRequired changed: ${requiredChanged}\nDefault changed: ${xdefaultChanged}\nOnDelete changed: ${onDeleteChanged}`);
44
40
  console.log(`Found attribute: ${JSON.stringify(foundAttribute, null, 2)}`);
45
- console.log(`Attribute: ${JSON.stringify(attribute, null, 2)}`);
41
+ console.log(`New attribute: ${JSON.stringify(attribute, null, 2)}`);
46
42
  finalAttribute = {
47
- ...attribute,
48
43
  ...foundAttribute,
44
+ ...attribute,
49
45
  };
50
46
  action = "update";
51
47
  }
52
48
  else {
53
- // If no properties that can be updated have changed, return early
49
+ // If no properties have changed, return early
54
50
  return;
55
51
  }
56
52
  }
57
- else if (foundAttribute &&
58
- !attributesSame(foundAttribute, attribute) &&
59
- updateEnabled) {
60
- console.log(`Deleting attribute with same key ${attribute.key} -- but different values -- ${JSON.stringify(attribute, null, 2)} -- ${JSON.stringify(foundAttribute, null, 2)}`);
61
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
62
- // After deletion, you might want to create the attribute anew
63
- finalAttribute = attribute;
64
- action = "create";
65
- }
66
- else if (!updateEnabled && foundAttribute) {
67
- return;
68
- }
69
53
  // Relationship attribute logic with adjustments
70
54
  let collectionFoundViaRelatedCollection;
71
55
  let relatedCollectionId;
@@ -106,10 +90,14 @@ export const createOrUpdateAttribute = async (db, dbId, collection, attribute) =
106
90
  switch (finalAttribute.type) {
107
91
  case "string":
108
92
  if (action === "create") {
109
- await tryAwaitWithRetry(async () => await db.createStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.size, finalAttribute.required || false, finalAttribute.xdefault || undefined, finalAttribute.array || false, finalAttribute.encrypted));
93
+ await tryAwaitWithRetry(async () => await db.createStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.size, finalAttribute.required || false, finalAttribute.xdefault
94
+ ? `${finalAttribute.xdefault}`
95
+ : undefined, finalAttribute.array || false, finalAttribute.encrypted));
110
96
  }
111
97
  else {
112
- await tryAwaitWithRetry(async () => await db.updateStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault || undefined));
98
+ await tryAwaitWithRetry(async () => await db.updateStringAttribute(dbId, collection.$id, finalAttribute.key, finalAttribute.required || false, finalAttribute.xdefault
99
+ ? `${finalAttribute.xdefault}`
100
+ : undefined, finalAttribute.size));
113
101
  }
114
102
  break;
115
103
  case "integer":
@@ -8,10 +8,10 @@ export const createOrUpdateIndex = async (dbId, db, collectionId, index) => {
8
8
  let createIndex = false;
9
9
  let newIndex = null;
10
10
  if (existingIndex.total > 0 &&
11
- existingIndex.indexes.some((index) => (index.key === index.key &&
12
- index.type === index.type &&
13
- index.attributes === index.attributes) ||
14
- JSON.stringify(index) === JSON.stringify(index))) {
11
+ !existingIndex.indexes.some((existingIndex) => (existingIndex.key === index.key &&
12
+ existingIndex.type === index.type &&
13
+ existingIndex.attributes === index.attributes) ||
14
+ JSON.stringify(existingIndex) === JSON.stringify(index))) {
15
15
  await db.deleteIndex(dbId, collectionId, existingIndex.indexes[0].key);
16
16
  createIndex = true;
17
17
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "appwrite-utils-cli",
3
3
  "description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
4
- "version": "0.9.54",
4
+ "version": "0.9.56",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "lodash": "^4.17.21",
39
39
  "luxon": "^3.5.0",
40
40
  "nanostores": "^0.10.3",
41
- "node-appwrite": "^13.0.0",
41
+ "node-appwrite": "^14.1.0",
42
42
  "tsx": "^4.17.0",
43
43
  "ulidx": "^2.4.0",
44
44
  "winston": "^3.14.2",
@@ -27,11 +27,10 @@ export const createOrUpdateAttribute = async (
27
27
  ): Promise<void> => {
28
28
  let action = "create";
29
29
  let foundAttribute: Attribute | undefined;
30
- const updateEnabled = false;
31
30
  let finalAttribute: any = attribute;
32
31
  try {
33
32
  const collectionAttr = collection.attributes.find(
34
- // @ts-expect-error
33
+ // @ts-ignore
35
34
  (attr) => attr.key === attribute.key
36
35
  ) as unknown as any;
37
36
  foundAttribute = parseAttribute(collectionAttr);
@@ -39,21 +38,17 @@ export const createOrUpdateAttribute = async (
39
38
  foundAttribute = undefined;
40
39
  }
41
40
 
42
- if (
43
- foundAttribute &&
44
- attributesSame(foundAttribute, attribute) &&
45
- updateEnabled
46
- ) {
47
- // Check if mutable properties have changed and set action to "update" if necessary
41
+ if (foundAttribute) {
42
+ // Check if any properties have changed
48
43
  const requiredChanged =
49
44
  "required" in foundAttribute && "required" in attribute
50
45
  ? foundAttribute.required !== attribute.required
51
46
  : false;
52
47
 
53
- // const xdefaultChanged =
54
- // "xdefault" in foundAttribute && "xdefault" in attribute
55
- // ? foundAttribute.xdefault !== attribute.xdefault
56
- // : false;
48
+ const xdefaultChanged =
49
+ "xdefault" in foundAttribute && "xdefault" in attribute
50
+ ? foundAttribute.xdefault !== attribute.xdefault
51
+ : false;
57
52
 
58
53
  const onDeleteChanged =
59
54
  foundAttribute.type === "relationship" &&
@@ -63,43 +58,23 @@ export const createOrUpdateAttribute = async (
63
58
  ? foundAttribute.onDelete !== attribute.onDelete
64
59
  : false;
65
60
 
66
- if (requiredChanged || onDeleteChanged) {
61
+ if (requiredChanged || xdefaultChanged || onDeleteChanged) {
67
62
  console.log(
68
- `Required changed: ${requiredChanged}\nOnDelete changed: ${onDeleteChanged}`
63
+ `Updating attribute: ${attribute.key}\nRequired changed: ${requiredChanged}\nDefault changed: ${xdefaultChanged}\nOnDelete changed: ${onDeleteChanged}`
69
64
  );
70
65
  console.log(
71
66
  `Found attribute: ${JSON.stringify(foundAttribute, null, 2)}`
72
67
  );
73
- console.log(`Attribute: ${JSON.stringify(attribute, null, 2)}`);
68
+ console.log(`New attribute: ${JSON.stringify(attribute, null, 2)}`);
74
69
  finalAttribute = {
75
- ...attribute,
76
70
  ...foundAttribute,
71
+ ...attribute,
77
72
  };
78
73
  action = "update";
79
74
  } else {
80
- // If no properties that can be updated have changed, return early
75
+ // If no properties have changed, return early
81
76
  return;
82
77
  }
83
- } else if (
84
- foundAttribute &&
85
- !attributesSame(foundAttribute, attribute) &&
86
- updateEnabled
87
- ) {
88
- console.log(
89
- `Deleting attribute with same key ${
90
- attribute.key
91
- } -- but different values -- ${JSON.stringify(
92
- attribute,
93
- null,
94
- 2
95
- )} -- ${JSON.stringify(foundAttribute, null, 2)}`
96
- );
97
- await db.deleteAttribute(dbId, collection.$id, attribute.key);
98
- // After deletion, you might want to create the attribute anew
99
- finalAttribute = attribute;
100
- action = "create";
101
- } else if (!updateEnabled && foundAttribute) {
102
- return;
103
78
  }
104
79
 
105
80
  // Relationship attribute logic with adjustments
@@ -158,7 +133,9 @@ export const createOrUpdateAttribute = async (
158
133
  finalAttribute.key,
159
134
  finalAttribute.size,
160
135
  finalAttribute.required || false,
161
- (finalAttribute.xdefault as string) || undefined,
136
+ finalAttribute.xdefault
137
+ ? `${finalAttribute.xdefault}`
138
+ : undefined,
162
139
  finalAttribute.array || false,
163
140
  finalAttribute.encrypted
164
141
  )
@@ -171,7 +148,10 @@ export const createOrUpdateAttribute = async (
171
148
  collection.$id,
172
149
  finalAttribute.key,
173
150
  finalAttribute.required || false,
174
- (finalAttribute.xdefault as string) || undefined
151
+ finalAttribute.xdefault
152
+ ? `${finalAttribute.xdefault}`
153
+ : undefined,
154
+ finalAttribute.size
175
155
  )
176
156
  );
177
157
  }
@@ -15,12 +15,12 @@ export const createOrUpdateIndex = async (
15
15
  let newIndex: Models.Index | null = null;
16
16
  if (
17
17
  existingIndex.total > 0 &&
18
- existingIndex.indexes.some(
19
- (index) =>
20
- (index.key === index.key &&
21
- index.type === index.type &&
22
- index.attributes === index.attributes) ||
23
- JSON.stringify(index) === JSON.stringify(index)
18
+ !existingIndex.indexes.some(
19
+ (existingIndex) =>
20
+ (existingIndex.key === index.key &&
21
+ existingIndex.type === index.type &&
22
+ existingIndex.attributes === index.attributes) ||
23
+ JSON.stringify(existingIndex) === JSON.stringify(index)
24
24
  )
25
25
  ) {
26
26
  await db.deleteIndex(dbId, collectionId, existingIndex.indexes[0].key);