lux
lux copied to clipboard
Offer access to data gathered from queries in actions after they are executed.
I am trying to find a simple way to add pagination meta data to all my index actions, e.g. the total amount of entries and the current offset.
Other JSON API servers add this by default, e.g. jsonapi-server:
"meta": {
"page": {
"limit": 50,
"offset": 0,
"total": 4
}
}
In a discussion on Gitter, @zacharygolba has proposed the following interim solution:
export const addMetaData = model => (req, res, data) =>
model.count().then(total => ({
...data,
meta: {
total
},
}))
import { Controller } from 'lux-framework'
import User from '../models/user'
import addMetaData from '../utils/add-meta-data'
export default class UsersController extends Controller {
afterAction = [
addMetaData(User),
]
}
But this means that the query is run twice in order to determine the total amount of entries in addMetaData().
Ideally, there would be a way to access the data gathered from queries in actions after they are executed.
- Platform: Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
- Database: SQLite3
- Lux Version: 1.2.0
- Node Version: 7.10.0