appwrite-utils-cli 0.0.1
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 +80 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +74 -0
- package/dist/migrations/afterImportActions.d.ts +12 -0
- package/dist/migrations/afterImportActions.js +196 -0
- package/dist/migrations/attributes.d.ts +4 -0
- package/dist/migrations/attributes.js +158 -0
- package/dist/migrations/backup.d.ts +621 -0
- package/dist/migrations/backup.js +159 -0
- package/dist/migrations/collections.d.ts +16 -0
- package/dist/migrations/collections.js +207 -0
- package/dist/migrations/converters.d.ts +179 -0
- package/dist/migrations/converters.js +575 -0
- package/dist/migrations/dbHelpers.d.ts +5 -0
- package/dist/migrations/dbHelpers.js +54 -0
- package/dist/migrations/importController.d.ts +44 -0
- package/dist/migrations/importController.js +312 -0
- package/dist/migrations/importDataActions.d.ts +44 -0
- package/dist/migrations/importDataActions.js +219 -0
- package/dist/migrations/indexes.d.ts +4 -0
- package/dist/migrations/indexes.js +18 -0
- package/dist/migrations/logging.d.ts +2 -0
- package/dist/migrations/logging.js +14 -0
- package/dist/migrations/migrationHelper.d.ts +18 -0
- package/dist/migrations/migrationHelper.js +66 -0
- package/dist/migrations/queue.d.ts +13 -0
- package/dist/migrations/queue.js +79 -0
- package/dist/migrations/relationships.d.ts +90 -0
- package/dist/migrations/relationships.js +209 -0
- package/dist/migrations/schema.d.ts +3142 -0
- package/dist/migrations/schema.js +485 -0
- package/dist/migrations/schemaStrings.d.ts +12 -0
- package/dist/migrations/schemaStrings.js +261 -0
- package/dist/migrations/setupDatabase.d.ts +7 -0
- package/dist/migrations/setupDatabase.js +151 -0
- package/dist/migrations/storage.d.ts +8 -0
- package/dist/migrations/storage.js +241 -0
- package/dist/migrations/users.d.ts +11 -0
- package/dist/migrations/users.js +114 -0
- package/dist/migrations/validationRules.d.ts +43 -0
- package/dist/migrations/validationRules.js +42 -0
- package/dist/schemas/authUser.d.ts +62 -0
- package/dist/schemas/authUser.js +17 -0
- package/dist/setup.d.ts +2 -0
- package/dist/setup.js +5 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +5 -0
- package/dist/utils/configSchema.json +742 -0
- package/dist/utils/helperFunctions.d.ts +34 -0
- package/dist/utils/helperFunctions.js +72 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/setupFiles.d.ts +2 -0
- package/dist/utils/setupFiles.js +276 -0
- package/dist/utilsController.d.ts +30 -0
- package/dist/utilsController.js +106 -0
- package/package.json +34 -0
- package/src/main.ts +77 -0
- package/src/migrations/afterImportActions.ts +300 -0
- package/src/migrations/attributes.ts +315 -0
- package/src/migrations/backup.ts +189 -0
- package/src/migrations/collections.ts +303 -0
- package/src/migrations/converters.ts +628 -0
- package/src/migrations/dbHelpers.ts +89 -0
- package/src/migrations/importController.ts +509 -0
- package/src/migrations/importDataActions.ts +313 -0
- package/src/migrations/indexes.ts +37 -0
- package/src/migrations/logging.ts +15 -0
- package/src/migrations/migrationHelper.ts +100 -0
- package/src/migrations/queue.ts +119 -0
- package/src/migrations/relationships.ts +336 -0
- package/src/migrations/schema.ts +590 -0
- package/src/migrations/schemaStrings.ts +310 -0
- package/src/migrations/setupDatabase.ts +219 -0
- package/src/migrations/storage.ts +351 -0
- package/src/migrations/users.ts +148 -0
- package/src/migrations/validationRules.ts +63 -0
- package/src/schemas/authUser.ts +23 -0
- package/src/setup.ts +8 -0
- package/src/types.ts +14 -0
- package/src/utils/configSchema.json +742 -0
- package/src/utils/helperFunctions.ts +111 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/setupFiles.ts +295 -0
- package/src/utilsController.ts +173 -0
- package/tsconfig.json +37 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { Databases, ID, Query, Users } from "node-appwrite";
|
|
2
|
+
import { AuthUserSchema, } from "../schemas/authUser.js";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
export class UsersController {
|
|
5
|
+
config;
|
|
6
|
+
users;
|
|
7
|
+
static userFields = [
|
|
8
|
+
"email",
|
|
9
|
+
"name",
|
|
10
|
+
"password",
|
|
11
|
+
"phone",
|
|
12
|
+
"labels",
|
|
13
|
+
"prefs",
|
|
14
|
+
"userId",
|
|
15
|
+
"$createdAt",
|
|
16
|
+
"$updatedAt",
|
|
17
|
+
];
|
|
18
|
+
constructor(config, db) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
this.users = new Users(this.config.appwriteClient);
|
|
21
|
+
}
|
|
22
|
+
async wipeUsers() {
|
|
23
|
+
const users = await this.users.list([Query.limit(25)]);
|
|
24
|
+
const allUsers = users.users;
|
|
25
|
+
let lastDocumentId;
|
|
26
|
+
if (users.total > 25) {
|
|
27
|
+
lastDocumentId = users.users[users.users.length - 1].$id;
|
|
28
|
+
}
|
|
29
|
+
while (lastDocumentId) {
|
|
30
|
+
const moreUsers = await this.users.list([
|
|
31
|
+
Query.limit(25),
|
|
32
|
+
Query.cursorAfter(lastDocumentId),
|
|
33
|
+
]);
|
|
34
|
+
allUsers.push(...moreUsers.users);
|
|
35
|
+
lastDocumentId = moreUsers.users[moreUsers.users.length - 1].$id;
|
|
36
|
+
}
|
|
37
|
+
console.log("Deleting all users");
|
|
38
|
+
for (const user of allUsers) {
|
|
39
|
+
await this.users.delete(user.$id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async createUserAndReturn(item) {
|
|
43
|
+
console.log("Creating user with item", item);
|
|
44
|
+
// Attempt to find an existing user by email or phone.
|
|
45
|
+
let foundUsers = [];
|
|
46
|
+
if (item.email) {
|
|
47
|
+
const foundUsersByEmail = await this.users.list([
|
|
48
|
+
Query.equal("email", item.email),
|
|
49
|
+
]);
|
|
50
|
+
foundUsers = foundUsersByEmail.users;
|
|
51
|
+
}
|
|
52
|
+
if (item.phone) {
|
|
53
|
+
const foundUsersByPhone = await this.users.list([
|
|
54
|
+
Query.equal("phone", item.phone),
|
|
55
|
+
]);
|
|
56
|
+
foundUsers = foundUsers.length
|
|
57
|
+
? foundUsers.concat(foundUsersByPhone.users)
|
|
58
|
+
: foundUsersByPhone.users;
|
|
59
|
+
}
|
|
60
|
+
let userToReturn = foundUsers[0] || undefined;
|
|
61
|
+
if (!userToReturn) {
|
|
62
|
+
console.log("Creating user cause not found");
|
|
63
|
+
userToReturn = await this.users.create(item.userId || ID.unique(), item.email || undefined, item.phone && item.phone.length < 15 && item.phone.startsWith("+")
|
|
64
|
+
? item.phone
|
|
65
|
+
: undefined, item.password?.toLowerCase() || `changeMe${item.email}`.toLowerCase(), item.name || undefined);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
console.log("Updating user cause found");
|
|
69
|
+
// Update user details as necessary, ensuring email uniqueness if attempting an update.
|
|
70
|
+
if (item.email &&
|
|
71
|
+
item.email !== userToReturn.email &&
|
|
72
|
+
!_.isEmpty(item.email) &&
|
|
73
|
+
!_.isUndefined(item.email)) {
|
|
74
|
+
const emailExists = await this.users.list([
|
|
75
|
+
Query.equal("email", item.email),
|
|
76
|
+
]);
|
|
77
|
+
if (emailExists.users.length === 0) {
|
|
78
|
+
userToReturn = await this.users.updateEmail(userToReturn.$id, item.email);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
console.log("Email update skipped: Email already exists.");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (item.password) {
|
|
85
|
+
userToReturn = await this.users.updatePassword(userToReturn.$id, item.password.toLowerCase());
|
|
86
|
+
}
|
|
87
|
+
if (item.name && item.name !== userToReturn.name) {
|
|
88
|
+
userToReturn = await this.users.updateName(userToReturn.$id, item.name);
|
|
89
|
+
}
|
|
90
|
+
if (item.phone &&
|
|
91
|
+
item.phone !== userToReturn.phone &&
|
|
92
|
+
item.phone.length < 15 &&
|
|
93
|
+
item.phone.startsWith("+") &&
|
|
94
|
+
(_.isUndefined(userToReturn.phone) || _.isEmpty(userToReturn.phone))) {
|
|
95
|
+
const userFoundWithPhone = await this.users.list([
|
|
96
|
+
Query.equal("phone", item.phone),
|
|
97
|
+
]);
|
|
98
|
+
if (userFoundWithPhone.total === 0) {
|
|
99
|
+
userToReturn = await this.users.updatePhone(userToReturn.$id, item.phone);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (item.$createdAt && item.$updatedAt) {
|
|
104
|
+
console.log("$createdAt and $updatedAt are not yet supported, sorry about that!");
|
|
105
|
+
}
|
|
106
|
+
if (item.labels && item.labels.length) {
|
|
107
|
+
userToReturn = await this.users.updateLabels(userToReturn.$id, item.labels);
|
|
108
|
+
}
|
|
109
|
+
if (item.prefs && Object.keys(item.prefs).length) {
|
|
110
|
+
userToReturn = await this.users.updatePrefs(userToReturn.$id, item.prefs);
|
|
111
|
+
}
|
|
112
|
+
return userToReturn;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface ValidationRules {
|
|
2
|
+
[key: string]: (value: any, ...args: any[]) => boolean;
|
|
3
|
+
}
|
|
4
|
+
export declare const validationRules: {
|
|
5
|
+
isNumber: (value: any) => boolean;
|
|
6
|
+
isString: (value: any) => boolean;
|
|
7
|
+
isBoolean: (value: any) => boolean;
|
|
8
|
+
isArray: (value: any) => boolean;
|
|
9
|
+
isObject: (value: any) => boolean;
|
|
10
|
+
isNull: (value: any) => boolean;
|
|
11
|
+
isValidEmail: (value: string) => boolean;
|
|
12
|
+
isValidPhone: (value: string) => boolean;
|
|
13
|
+
isValidPassword: (value: string) => boolean;
|
|
14
|
+
isValidUrl: (value: string) => boolean;
|
|
15
|
+
isValidHex: (value: string) => boolean;
|
|
16
|
+
isValidHexColor: (value: string) => boolean;
|
|
17
|
+
isValidHexAlpha: (value: string) => boolean;
|
|
18
|
+
isValidDate: (value: string) => boolean;
|
|
19
|
+
isValidTime: (value: string) => boolean;
|
|
20
|
+
isNullish: (value: any) => boolean;
|
|
21
|
+
isUndefined: (value: any) => boolean;
|
|
22
|
+
isDefined: (value: any) => boolean;
|
|
23
|
+
isDate: (value: any) => boolean;
|
|
24
|
+
isEmpty: (value: any) => boolean;
|
|
25
|
+
isInteger: (value: any) => boolean;
|
|
26
|
+
isFloat: (value: any) => boolean;
|
|
27
|
+
isArrayLike: (value: any) => boolean;
|
|
28
|
+
isArrayLikeObject: (value: any) => boolean;
|
|
29
|
+
isFunction: (value: any) => boolean;
|
|
30
|
+
isLength: (value: any) => boolean;
|
|
31
|
+
isMap: (value: any) => boolean;
|
|
32
|
+
isSet: (value: any) => boolean;
|
|
33
|
+
isRegExp: (value: any) => boolean;
|
|
34
|
+
isSymbol: (value: any) => boolean;
|
|
35
|
+
isObjectLike: (value: any) => boolean;
|
|
36
|
+
isPlainObject: (value: any) => boolean;
|
|
37
|
+
isSafeInteger: (value: any) => boolean;
|
|
38
|
+
isTypedArray: (value: any) => boolean;
|
|
39
|
+
isEqual: (value: any, other: any) => boolean;
|
|
40
|
+
isMatch: (object: any, source: any) => boolean;
|
|
41
|
+
has: (object: any, path: string) => boolean;
|
|
42
|
+
get: (object: any, path: string, defaultValue: any) => any;
|
|
43
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
export const validationRules = {
|
|
3
|
+
isNumber: (value) => _.isNumber(value),
|
|
4
|
+
isString: (value) => _.isString(value),
|
|
5
|
+
isBoolean: (value) => _.isBoolean(value),
|
|
6
|
+
isArray: (value) => _.isArray(value),
|
|
7
|
+
isObject: (value) => _.isObject(value) && !_.isArray(value) && !_.isFunction(value),
|
|
8
|
+
isNull: (value) => _.isNull(value),
|
|
9
|
+
isValidEmail: (value) => value.match(/^[\w\-\.]+@([\w-]+\.)+[\w-]{2,}$/) !== null,
|
|
10
|
+
isValidPhone: (value) => value.match(/^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/im) !==
|
|
11
|
+
null,
|
|
12
|
+
isValidPassword: (value) => value.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/) !== null,
|
|
13
|
+
isValidUrl: (value) => value.match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/) !== null,
|
|
14
|
+
isValidHex: (value) => value.match(/^#([a-f0-9]{6}|[a-f0-9]{3})$/i) !== null,
|
|
15
|
+
isValidHexColor: (value) => value.match(/^#([a-f0-9]{6}|[a-f0-9]{3})$/i) !== null,
|
|
16
|
+
isValidHexAlpha: (value) => value.match(/^#([a-f0-9]{8}|[a-f0-9]{4})$/i) !== null,
|
|
17
|
+
isValidDate: (value) => value.match(/^\d{4}-\d{2}-\d{2}$/) !== null,
|
|
18
|
+
isValidTime: (value) => value.match(/^\d{2}:\d{2}(:\d{2})?$/) !== null,
|
|
19
|
+
isNullish: (value) => _.isNull(value) || _.isUndefined(value),
|
|
20
|
+
isUndefined: (value) => _.isUndefined(value),
|
|
21
|
+
isDefined: (value) => !_.isUndefined(value) && !_.isNull(value) && !_.isEmpty(value),
|
|
22
|
+
isDate: (value) => _.isDate(value),
|
|
23
|
+
isEmpty: (value) => _.isEmpty(value),
|
|
24
|
+
isInteger: (value) => _.isInteger(value),
|
|
25
|
+
isFloat: (value) => _.isNumber(value) && !_.isInteger(value),
|
|
26
|
+
isArrayLike: (value) => _.isArrayLike(value),
|
|
27
|
+
isArrayLikeObject: (value) => _.isArrayLikeObject(value),
|
|
28
|
+
isFunction: (value) => _.isFunction(value),
|
|
29
|
+
isLength: (value) => _.isLength(value),
|
|
30
|
+
isMap: (value) => _.isMap(value),
|
|
31
|
+
isSet: (value) => _.isSet(value),
|
|
32
|
+
isRegExp: (value) => _.isRegExp(value),
|
|
33
|
+
isSymbol: (value) => _.isSymbol(value),
|
|
34
|
+
isObjectLike: (value) => _.isObjectLike(value),
|
|
35
|
+
isPlainObject: (value) => _.isPlainObject(value),
|
|
36
|
+
isSafeInteger: (value) => _.isSafeInteger(value),
|
|
37
|
+
isTypedArray: (value) => _.isTypedArray(value),
|
|
38
|
+
isEqual: (value, other) => _.isEqual(value, other),
|
|
39
|
+
isMatch: (object, source) => _.isMatch(object, source),
|
|
40
|
+
has: (object, path) => _.has(object, path),
|
|
41
|
+
get: (object, path, defaultValue) => _.get(object, path, defaultValue),
|
|
42
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const AuthUserSchema: z.ZodObject<{
|
|
3
|
+
$id: z.ZodString;
|
|
4
|
+
$createdAt: z.ZodOptional<z.ZodString>;
|
|
5
|
+
$updatedAt: z.ZodOptional<z.ZodString>;
|
|
6
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
7
|
+
email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
8
|
+
phone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
9
|
+
prefs: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
|
|
10
|
+
labels: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
|
|
11
|
+
}, "strip", z.ZodTypeAny, {
|
|
12
|
+
$id: string;
|
|
13
|
+
prefs: Record<string, string>;
|
|
14
|
+
labels: string[];
|
|
15
|
+
$createdAt?: string | undefined;
|
|
16
|
+
$updatedAt?: string | undefined;
|
|
17
|
+
name?: string | null | undefined;
|
|
18
|
+
email?: string | null | undefined;
|
|
19
|
+
phone?: string | null | undefined;
|
|
20
|
+
}, {
|
|
21
|
+
$id: string;
|
|
22
|
+
$createdAt?: string | undefined;
|
|
23
|
+
$updatedAt?: string | undefined;
|
|
24
|
+
name?: string | null | undefined;
|
|
25
|
+
email?: string | null | undefined;
|
|
26
|
+
phone?: string | null | undefined;
|
|
27
|
+
prefs?: Record<string, string> | undefined;
|
|
28
|
+
labels?: string[] | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
export type AuthUser = z.infer<typeof AuthUserSchema>;
|
|
31
|
+
export declare const AuthUserCreateSchema: z.ZodObject<{
|
|
32
|
+
email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
33
|
+
$createdAt: z.ZodOptional<z.ZodString>;
|
|
34
|
+
$updatedAt: z.ZodOptional<z.ZodString>;
|
|
35
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
36
|
+
phone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
37
|
+
prefs: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
|
|
38
|
+
labels: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
|
|
39
|
+
userId: z.ZodOptional<z.ZodString>;
|
|
40
|
+
password: z.ZodOptional<z.ZodString>;
|
|
41
|
+
}, "strip", z.ZodTypeAny, {
|
|
42
|
+
prefs: Record<string, string>;
|
|
43
|
+
labels: string[];
|
|
44
|
+
email?: string | null | undefined;
|
|
45
|
+
$createdAt?: string | undefined;
|
|
46
|
+
$updatedAt?: string | undefined;
|
|
47
|
+
name?: string | null | undefined;
|
|
48
|
+
phone?: string | null | undefined;
|
|
49
|
+
userId?: string | undefined;
|
|
50
|
+
password?: string | undefined;
|
|
51
|
+
}, {
|
|
52
|
+
email?: string | null | undefined;
|
|
53
|
+
$createdAt?: string | undefined;
|
|
54
|
+
$updatedAt?: string | undefined;
|
|
55
|
+
name?: string | null | undefined;
|
|
56
|
+
phone?: string | null | undefined;
|
|
57
|
+
prefs?: Record<string, string> | undefined;
|
|
58
|
+
labels?: string[] | undefined;
|
|
59
|
+
userId?: string | undefined;
|
|
60
|
+
password?: string | undefined;
|
|
61
|
+
}>;
|
|
62
|
+
export type AuthUserCreate = z.infer<typeof AuthUserCreateSchema>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const AuthUserSchema = z.object({
|
|
3
|
+
$id: z.string(),
|
|
4
|
+
$createdAt: z.string().optional(),
|
|
5
|
+
$updatedAt: z.string().optional(),
|
|
6
|
+
name: z.string().nullish(),
|
|
7
|
+
email: z.string().nullish(),
|
|
8
|
+
phone: z.string().nullish(),
|
|
9
|
+
prefs: z.record(z.string()).optional().default({}),
|
|
10
|
+
labels: z.array(z.string()).optional().default([]),
|
|
11
|
+
});
|
|
12
|
+
export const AuthUserCreateSchema = AuthUserSchema.omit({
|
|
13
|
+
$id: true,
|
|
14
|
+
}).extend({
|
|
15
|
+
userId: z.string().optional(),
|
|
16
|
+
password: z.string().optional(),
|
|
17
|
+
});
|
package/dist/setup.d.ts
ADDED
package/dist/setup.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { AppwriteConfig } from "./migrations/schema.js";
|
|
2
|
+
export type { ConverterFunctions } from "./migrations/converters.js";
|
|
3
|
+
export type { ValidationRules } from "./migrations/validationRules.js";
|
|
4
|
+
export type { AfterImportActions } from "./migrations/afterImportActions.js";
|
|
5
|
+
export { type AuthUserCreate, AuthUserCreateSchema, type AuthUser, AuthUserSchema, } from "./schemas/authUser.js";
|
|
6
|
+
export { getFileViewUrl, getFileDownloadUrl } from "./utils/helperFunctions.js";
|
|
7
|
+
export { converterFunctions } from "./migrations/converters.js";
|
|
8
|
+
export { validationRules } from "./migrations/validationRules.js";
|
|
9
|
+
export { afterImportActions } from "./migrations/afterImportActions.js";
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AuthUserCreateSchema, AuthUserSchema, } from "./schemas/authUser.js";
|
|
2
|
+
export { getFileViewUrl, getFileDownloadUrl } from "./utils/helperFunctions.js";
|
|
3
|
+
export { converterFunctions } from "./migrations/converters.js";
|
|
4
|
+
export { validationRules } from "./migrations/validationRules.js";
|
|
5
|
+
export { afterImportActions } from "./migrations/afterImportActions.js";
|