diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-07-19 00:50:05 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-07-19 00:50:05 +0200 |
| commit | 64e8c03dea044752bf3f2f228462721fe565f950 (patch) | |
| tree | 41b48b67f69979bfc97be166129ee41c8dcb0c7f /lib/db/crud.js | |
| parent | 11a70bc347587219b2ec7b63cf4a6ff69bb4199b (diff) | |
refactor all the worker stuff
Diffstat (limited to 'lib/db/crud.js')
| -rw-r--r-- | lib/db/crud.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/db/crud.js b/lib/db/crud.js new file mode 100644 index 0000000..fb32690 --- /dev/null +++ b/lib/db/crud.js @@ -0,0 +1,40 @@ +module.exports = function(model) { + return { + index: (q) => { + return model.query( (qb) => { + const limit = q.limit || 100 + const offset = q.offset || 0 + if (limit) { + delete q.limit + } + if (q.offset) { + delete q.offset + } + if (Object.keys(q).length > 0) qb.where(q) + qb.orderBy("id", "desc") + if (limit) qb.limit( limit ) + if (offset) qb.offset( offset ) + // console.log(qb) + return qb + }).fetchAll() + }, + show: (id) => { + return new model({'id': 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(data) + }, + } +}
\ No newline at end of file |
