blob: f772704b8c46e69d0a7d76e9d768c9bd0f364783 (
plain)
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
|
module.exports = function(model) {
return {
index: (q) => {
return model.query( (qb) => {
let limit = q.limit
const offset = q.offset || 0
const orderBy = q.orderBy || 'id desc'
if ('limit' in q) {
delete q.limit
}
if (!limit && limit !== 0) {
limit = 100
}
if (q.offset) {
delete q.offset
}
delete q.orderBy
if (Object.keys(q).length > 0) qb.where(q)
if (orderBy) {
const ob = orderBy.split(" ")
const ob_field = ob[0] || 'id'
const ob_dir = ob[1] || 'desc'
qb.orderBy(ob_field, ob_dir)
}
if (limit) qb.limit( limit )
if (offset) qb.offset( offset )
// console.log(qb)
return qb
}).fetchAll()
},
show: (id, field='id') => {
return new model({[field]: id}).fetch()
},
show_ids: (ids) => {
return model.query( (qb) => {
qb.whereIn('id', ids)
return qb
}).fetchAll()
},
create: (data) => {
return new model(data).save()
},
update: (id, data) => {
return new model({'id': id}).save(data)
},
destroy: (id) => {
return new model({'id': id}).destroy()
},
}
}
|