module.exports = function(model) { return { index: (q) => { return model.query( (qb) => { const limit = q.limit || 100 const offset = q.offset || 0 const orderBy = q.orderBy || 'id desc' if (limit) { delete q.limit } 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) => { 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() }, } }