-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
57 lines (50 loc) · 1.84 KB
/
utils.js
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
module.exports = {
dbSetter(query, alter, options = {}) {
return new Promise((resolve) => {
if (["string", "number"].includes(typeof query)) {
query = { id: query.toString() };
}
if (!alter) resolve(null);
if (!options.upsert) options.upsert = true;
if (["guilds", "sv_meta"].includes(this.cat)) options.upsert = false;
return resolve(this.updateOne(query, alter, options).lean().exec());
});
},
async dbChecker(query) {
if (["string", "number"].includes(typeof query)) {
query = { id: query.toString() };
}
if (!alter) resolve(false);
else resolve(true);
},
dbGetter(query, project) {
return new Promise((resolve) => {
if (["string", "number"].includes(typeof query)) {
query = { id: query.toString() };
}
if (!project) project = { _id: 0 };
return this.findOne(query, project).lean().then((data) => {
try{
if (!data && !!this.cat && PLX[this.cat].size) return this.new(PLX[this.cat].find((u) => u.id === query.id)).then(resolve);
if (data === null) return resolve(null);// return resolve( this.new(PLX.users.find(u=>u.id === query.id)) );
}catch(err){
}
return resolve(data?._doc || data);
});
});
},
dbGetterFull(query, project, avoidNew) {
return new Promise(async (resolve) => {
if (["string", "number"].includes(typeof query)) {
query = { id: query.toString() };
}
if (!project) project = { _id: 0 };
const data = await this.findOne(query, project);
if (!avoidNew){
if (!data && !!this.cat) return resolve( await this.new(PLX[this.cat].find((u) => u.id === query.id)));
if (data === null) return resolve( await this.new(PLX.users.find(u=>u.id === query.id||query)) );
}
return resolve(data);
});
},
}