-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
53 lines (48 loc) · 1.51 KB
/
model.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
const DB = require("./db");
const ValidationService = require("./validation");
class Model extends DB {
constructor(table, connection = null, schema = "public") {
super(table, connection, schema);
this.table = table;
this.schema = schema;
this.relations = {};
this.columns = {};
this.isAggregate = false;
}
getModelColumnsCommaSeperatedString(alias, select, extras) {
if (ValidationService.isObject(select)) {
let columns = Object.values(this.columns)
.filter((c) => !!select[c.column])
.map((c) => `${alias ? alias : this.table}.${c.column}`);
if (extras && ValidationService.isObject(extras)) {
columns = columns.concat(
Object.values(extras)
.filter((x) => typeof x === "function")
.map((x) => x(alias || this.table))
);
}
if (columns.length > 0) {
return columns.join(",");
}
}
if (extras && ValidationService.isObject(extras)) {
return Object.values(this.columns)
.map((c) => `${alias ? alias : this.table}.${c.column}`)
.concat(
Object.values(extras)
.filter((x) => typeof x === "function")
.map((x) => x(alias || this.table))
)
.join(",");
}
return Object.values(this.columns)
.map((c) => `${alias ? alias : this.table}.${c.column}`)
.join(",");
}
get columnsStrNoAlias() {
return Object.values(this.columns)
.map((c) => `${c.column}`)
.join(",");
}
}
module.exports = Model;