appwrite-utils-cli 0.0.34 → 0.0.35

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
@@ -86,6 +86,7 @@ This setup ensures that developers have robust tools at their fingertips to mana
86
86
 
87
87
  ### Changelog
88
88
 
89
+ - 0.0.35: Added update collection if it exists and permissions or such are different (`documentSecurity` and `enabled`), also added a check for `fetch failed` errors to retry them with recursion, not sure how well that will work out, but we're gonna try it! It will still fail after 5 tries, but hopefully that gives Appwrite some time to figure it's stuff out
89
90
  - 0.0.34: Fixed the `bin` section of the package.json, apparently you can't use `node` to run it
90
91
  - 0.0.33: Fixed `idMappings`, if you are importing data and use the `idMappings` functionality, you can set a `fieldToSet` based on the value of a `sourceField` in the current imported items data (whether it's in the final data or the original), in order to match another field in another collection. So if you had a store, and it had items and the items have a Region ID for instance. You can then, in your regionId of the items, setup an `idMapping` that will allow you to map the value of the `targetField` based on the value of the `targetFieldToMatch` in the `targetCollection`. Sounds complex, but it's very useful. Like psuedo-relationship resolution, without the relationships.
91
92
  - 0.0.29: If you use the `description` variable in an attribute and collection, it'll add that description to the generated schemas. This assumes you have `zod-to-openpi`
@@ -163,7 +163,7 @@ export const createOrUpdateCollections = async (database, databaseId, config, de
163
163
  usedIds.add(collectionId); // Mark this ID as used
164
164
  // Create the collection with the determined ID
165
165
  try {
166
- collectionToUse = await database.createCollection(databaseId, collectionId, collection.name, permissions, collection.documentSecurity, collection.enabled);
166
+ collectionToUse = await database.createCollection(databaseId, collectionId, collection.name, permissions, collection.documentSecurity ?? false, collection.enabled ?? true);
167
167
  collection.$id = collectionToUse.$id;
168
168
  nameToIdMapping.set(collection.name, collectionToUse.$id);
169
169
  }
@@ -174,6 +174,12 @@ export const createOrUpdateCollections = async (database, databaseId, config, de
174
174
  }
175
175
  else {
176
176
  console.log(`Collection ${collection.name} exists, using it`);
177
+ if (collection.enabled !== collectionToUse.enabled ||
178
+ collection.documentSecurity !== collectionToUse.documentSecurity ||
179
+ collection.$permissions.length !== collectionToUse.$permissions.length) {
180
+ console.log(`Updating collection: ${collectionToUse.$id}`);
181
+ await database.updateCollection(databaseId, collectionToUse.$id, collection.name, permissions, collection.documentSecurity ?? false, collection.enabled ?? true);
182
+ }
177
183
  }
178
184
  // Update attributes and indexes for the collection
179
185
  console.log("Creating Attributes");
@@ -9,7 +9,7 @@ export declare class UsersController {
9
9
  wipeUsers(): Promise<void>;
10
10
  getAllUsers(): Promise<Models.User<Models.Preferences>[]>;
11
11
  createUsersAndReturn(items: AuthUserCreate[]): Promise<Models.User<Models.Preferences>[]>;
12
- createUserAndReturn(item: AuthUserCreate): Promise<Models.User<Models.Preferences>>;
12
+ createUserAndReturn(item: AuthUserCreate, numAttempts?: number): Promise<Models.User<Models.Preferences>>;
13
13
  createAndCheckForUserAndReturn(item: AuthUserCreate): Promise<Models.User<Models.Preferences> | undefined>;
14
14
  getUserIdByEmailOrPhone(email?: string, phone?: string): Promise<string | undefined>;
15
15
  }
@@ -1,4 +1,4 @@
1
- import { Databases, ID, Query, Users } from "node-appwrite";
1
+ import { AppwriteException, Databases, ID, Query, Users, } from "node-appwrite";
2
2
  import { AuthUserSchema, } from "../schemas/authUser.js";
3
3
  import _ from "lodash";
4
4
  import { logger } from "./logging.js";
@@ -96,7 +96,7 @@ export class UsersController {
96
96
  const users = await Promise.all(items.map((item) => this.createUserAndReturn(item)));
97
97
  return users;
98
98
  }
99
- async createUserAndReturn(item) {
99
+ async createUserAndReturn(item, numAttempts) {
100
100
  try {
101
101
  const user = await this.users.create(item.userId || ID.unique(), item.email || undefined, item.phone && item.phone.length < 15 && item.phone.startsWith("+")
102
102
  ? item.phone
@@ -112,6 +112,16 @@ export class UsersController {
112
112
  return user;
113
113
  }
114
114
  catch (e) {
115
+ if (e instanceof AppwriteException) {
116
+ if (e.message.includes("fetch failed")) {
117
+ const numberOfAttempts = numAttempts || 0;
118
+ if (numberOfAttempts > 5) {
119
+ throw e;
120
+ }
121
+ const user = await this.createUserAndReturn(item, numberOfAttempts + 1);
122
+ return user;
123
+ }
124
+ }
115
125
  if (e instanceof Error) {
116
126
  logger.error("FAILED CREATING USER: ", e.message);
117
127
  }
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.0.34",
4
+ "version": "0.0.35",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -216,8 +216,8 @@ export const createOrUpdateCollections = async (
216
216
  collectionId,
217
217
  collection.name,
218
218
  permissions,
219
- collection.documentSecurity,
220
- collection.enabled
219
+ collection.documentSecurity ?? false,
220
+ collection.enabled ?? true
221
221
  );
222
222
  collection.$id = collectionToUse.$id;
223
223
  nameToIdMapping.set(collection.name, collectionToUse.$id);
@@ -229,6 +229,21 @@ export const createOrUpdateCollections = async (
229
229
  }
230
230
  } else {
231
231
  console.log(`Collection ${collection.name} exists, using it`);
232
+ if (
233
+ collection.enabled !== collectionToUse.enabled ||
234
+ collection.documentSecurity !== collectionToUse.documentSecurity ||
235
+ collection.$permissions.length !== collectionToUse.$permissions.length
236
+ ) {
237
+ console.log(`Updating collection: ${collectionToUse.$id}`);
238
+ await database.updateCollection(
239
+ databaseId,
240
+ collectionToUse.$id,
241
+ collection.name,
242
+ permissions,
243
+ collection.documentSecurity ?? false,
244
+ collection.enabled ?? true
245
+ );
246
+ }
232
247
  }
233
248
 
234
249
  // Update attributes and indexes for the collection
@@ -1,5 +1,12 @@
1
1
  import type { AppwriteConfig, ConfigCollection } from "appwrite-utils";
2
- import { Databases, ID, Query, Users, type Models } from "node-appwrite";
2
+ import {
3
+ AppwriteException,
4
+ Databases,
5
+ ID,
6
+ Query,
7
+ Users,
8
+ type Models,
9
+ } from "node-appwrite";
3
10
  import {
4
11
  AuthUserSchema,
5
12
  type AuthUser,
@@ -108,7 +115,7 @@ export class UsersController {
108
115
  return users;
109
116
  }
110
117
 
111
- async createUserAndReturn(item: AuthUserCreate) {
118
+ async createUserAndReturn(item: AuthUserCreate, numAttempts?: number) {
112
119
  try {
113
120
  const user = await this.users.create(
114
121
  item.userId || ID.unique(),
@@ -129,6 +136,17 @@ export class UsersController {
129
136
  }
130
137
  return user;
131
138
  } catch (e) {
139
+ if (e instanceof AppwriteException) {
140
+ if (e.message.includes("fetch failed")) {
141
+ const numberOfAttempts = numAttempts || 0;
142
+ if (numberOfAttempts > 5) {
143
+ throw e;
144
+ }
145
+ const user: Models.User<Models.Preferences> =
146
+ await this.createUserAndReturn(item, numberOfAttempts + 1);
147
+ return user;
148
+ }
149
+ }
132
150
  if (e instanceof Error) {
133
151
  logger.error("FAILED CREATING USER: ", e.message);
134
152
  }