Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer access to data gathered from queries in actions after they are executed. #710

Open
lehni opened this issue May 30, 2017 · 0 comments

Comments

@lehni
Copy link

lehni commented May 30, 2017

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants