/
User.ts
152 lines (141 loc) Β· 3.87 KB
/
User.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { UserType } from 'nocodb-sdk';
import { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import Noco from '../Noco';
import extractProps from '../meta/helpers/extractProps';
import NocoCache from '../cache/NocoCache';
export default class User implements UserType {
id: number;
/** @format email */
email: string;
password?: string;
salt?: string;
firstname: string;
lastname: string;
username?: string;
refresh_token?: string;
invite_token?: string;
invite_token_expires?: number | Date;
reset_password_expires?: number | Date;
reset_password_token?: string;
email_verification_token?: string;
email_verified: boolean;
roles?: string;
token_version?: string;
constructor(data: User) {
Object.assign(this, data);
}
public static async insert(user: Partial<User>, ncMeta = Noco.ncMeta) {
const insertObj = extractProps(user, [
'id',
'email',
'password',
'salt',
'firstname',
'lastname',
'username',
'refresh_token',
'invite_token',
'invite_token_expires',
'reset_password_expires',
'reset_password_token',
'email_verification_token',
'email_verified',
'roles',
'token_version'
]);
const { id } = await ncMeta.metaInsert2(
null,
null,
MetaTable.USERS,
insertObj
);
await NocoCache.del(CacheScope.INSTANCE_META);
return this.get(id, ncMeta);
}
public static async update(id, user: Partial<User>, ncMeta = Noco.ncMeta) {
const updateObj = extractProps(user, [
'email',
'password',
'salt',
'firstname',
'lastname',
'username',
'refresh_token',
'invite_token',
'invite_token_expires',
'reset_password_expires',
'reset_password_token',
'email_verification_token',
'email_verified',
'roles',
'token_version'
]);
// get existing cache
const keys = [
// update user:<id>
`${CacheScope.USER}:${id}`,
// update user:<email>
`${CacheScope.USER}:${user.email}`
];
for (const key of keys) {
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
}
// as <projectId> is unknown, delete user:<email>___<projectId> in cache
await NocoCache.delAll(CacheScope.USER, `${user.email}___*`);
// set meta
return await ncMeta.metaUpdate(null, null, MetaTable.USERS, updateObj, id);
}
public static async getByEmail(email, ncMeta = Noco.ncMeta) {
let user =
email &&
(await NocoCache.get(
`${CacheScope.USER}:${email}`,
CacheGetType.TYPE_OBJECT
));
if (!user) {
user = await ncMeta.metaGet2(null, null, MetaTable.USERS, {
email
});
await NocoCache.set(`${CacheScope.USER}:${email}`, user);
}
return user;
}
static async isFirst(ncMeta = Noco.ncMeta) {
const isFirst = !(await NocoCache.getAll(`${CacheScope.USER}:*`))?.length;
if (isFirst)
return !(await ncMeta.metaGet2(null, null, MetaTable.USERS, {}));
return false;
}
static async count(ncMeta = Noco.ncMeta) {
return (
await ncMeta
.knex(MetaTable.USERS)
.count('id', { as: 'count' })
.first()
)?.count;
}
static async get(userId, ncMeta = Noco.ncMeta) {
let user =
userId &&
(await NocoCache.get(
`${CacheScope.USER}:${userId}`,
CacheGetType.TYPE_OBJECT
));
if (!user) {
user = await ncMeta.metaGet2(null, null, MetaTable.USERS, userId);
await NocoCache.set(`${CacheScope.USER}:${userId}`, user);
}
return user;
}
static async getByRefreshToken(refresh_token, ncMeta = Noco.ncMeta) {
const user = await ncMeta.metaGet2(null, null, MetaTable.USERS, {
refresh_token
});
return user;
}
}