appwrite-utils-cli 0.0.249 → 0.0.250
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.
@@ -4,6 +4,14 @@ import { z } from "zod";
|
|
4
4
|
import { type Databases } from "node-appwrite";
|
5
5
|
export declare const CollectionImportDataSchema: z.ZodObject<{
|
6
6
|
collection: z.ZodOptional<z.ZodObject<Omit<{
|
7
|
+
/**
|
8
|
+
* Prepares user data by checking for duplicates based on email or phone, adding to a duplicate map if found,
|
9
|
+
* and then returning the transformed item without user-specific keys.
|
10
|
+
*
|
11
|
+
* @param item - The raw item to be processed.
|
12
|
+
* @param attributeMappings - The attribute mappings for the item.
|
13
|
+
* @returns The transformed item with user-specific keys removed.
|
14
|
+
*/
|
7
15
|
$id: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
8
16
|
$createdAt: z.ZodString;
|
9
17
|
$updatedAt: z.ZodString;
|
@@ -1558,6 +1566,7 @@ export declare class DataLoader {
|
|
1558
1566
|
private mergedUserMap;
|
1559
1567
|
private emailToUserIdMap;
|
1560
1568
|
private phoneToUserIdMap;
|
1569
|
+
userExistsMap: Map<string, boolean>;
|
1561
1570
|
constructor(appwriteFolderPath: string, importDataActions: ImportDataActions, database: Databases, config: AppwriteConfig);
|
1562
1571
|
getCollectionKey(name: string): string;
|
1563
1572
|
loadData(importDef: ImportDef): Promise<any[]>;
|
@@ -44,6 +44,7 @@ export class DataLoader {
|
|
44
44
|
// Maps to hold email and phone to user ID mappings for unique-ness in User Accounts
|
45
45
|
emailToUserIdMap = new Map();
|
46
46
|
phoneToUserIdMap = new Map();
|
47
|
+
userExistsMap = new Map();
|
47
48
|
// Constructor to initialize the DataLoader with necessary configurations
|
48
49
|
constructor(appwriteFolderPath, importDataActions, database, config) {
|
49
50
|
this.appwriteFolderPath = appwriteFolderPath;
|
@@ -155,6 +156,9 @@ export class DataLoader {
|
|
155
156
|
async getAllUsers() {
|
156
157
|
const users = new UsersController(this.config, this.database);
|
157
158
|
const allUsers = await users.getAllUsers();
|
159
|
+
for (const user of allUsers) {
|
160
|
+
this.userExistsMap.set(user.$id, true);
|
161
|
+
}
|
158
162
|
return allUsers;
|
159
163
|
}
|
160
164
|
// Main method to start the data loading process for a given database ID
|
@@ -97,14 +97,19 @@ export class ImportController {
|
|
97
97
|
console.log(`Processing user batch ${i + 1} of ${userBatchesAll.length}`);
|
98
98
|
const userBatchPromises = userBatches
|
99
99
|
.map((userBatch) => {
|
100
|
-
if (userBatch.finalData && userBatch) {
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
100
|
+
if (userBatch.finalData && userBatch.finalData.length > 0) {
|
101
|
+
const userId = userBatch.finalData.userId;
|
102
|
+
if (dataLoader.userExistsMap.has(userId)) {
|
103
|
+
if (!dataLoader.userExistsMap.get(userId)) {
|
104
|
+
return usersController
|
105
|
+
.createUserAndReturn(userBatch.finalData)
|
106
|
+
.then(() => console.log("Created user"))
|
107
|
+
.catch((error) => {
|
108
|
+
logger.error("Error creating user:", error, "\nUser data is ", userBatch.finalData);
|
109
|
+
throw error;
|
110
|
+
});
|
111
|
+
}
|
112
|
+
}
|
108
113
|
}
|
109
114
|
})
|
110
115
|
.flat();
|
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.
|
4
|
+
"version": "0.0.250",
|
5
5
|
"main": "src/main.ts",
|
6
6
|
"type": "module",
|
7
7
|
"repository": {
|
@@ -62,6 +62,7 @@ export class DataLoader {
|
|
62
62
|
// Maps to hold email and phone to user ID mappings for unique-ness in User Accounts
|
63
63
|
private emailToUserIdMap = new Map<string, string>();
|
64
64
|
private phoneToUserIdMap = new Map<string, string>();
|
65
|
+
userExistsMap = new Map<string, boolean>();
|
65
66
|
|
66
67
|
// Constructor to initialize the DataLoader with necessary configurations
|
67
68
|
constructor(
|
@@ -212,6 +213,9 @@ export class DataLoader {
|
|
212
213
|
async getAllUsers() {
|
213
214
|
const users = new UsersController(this.config, this.database);
|
214
215
|
const allUsers = await users.getAllUsers();
|
216
|
+
for (const user of allUsers) {
|
217
|
+
this.userExistsMap.set(user.$id, true);
|
218
|
+
}
|
215
219
|
return allUsers;
|
216
220
|
}
|
217
221
|
|
@@ -156,19 +156,24 @@ export class ImportController {
|
|
156
156
|
);
|
157
157
|
const userBatchPromises = userBatches
|
158
158
|
.map((userBatch) => {
|
159
|
-
if (userBatch.finalData && userBatch) {
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
"
|
166
|
-
error
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
159
|
+
if (userBatch.finalData && userBatch.finalData.length > 0) {
|
160
|
+
const userId = userBatch.finalData.userId;
|
161
|
+
if (dataLoader.userExistsMap.has(userId)) {
|
162
|
+
if (!dataLoader.userExistsMap.get(userId)) {
|
163
|
+
return usersController
|
164
|
+
.createUserAndReturn(userBatch.finalData)
|
165
|
+
.then(() => console.log("Created user"))
|
166
|
+
.catch((error) => {
|
167
|
+
logger.error(
|
168
|
+
"Error creating user:",
|
169
|
+
error,
|
170
|
+
"\nUser data is ",
|
171
|
+
userBatch.finalData
|
172
|
+
);
|
173
|
+
throw error;
|
174
|
+
});
|
175
|
+
}
|
176
|
+
}
|
172
177
|
}
|
173
178
|
})
|
174
179
|
.flat();
|