Pash
Pash

Reputation: 392

How to fetch all rows from objection.js

I am using Knex.js & Objection.js, I want to display country drop down so I need all rows but it was only returning me 10 rows. How do I override the pagination functionality?

Is there any params I can set in url localhost:3031/countires?pages='All', there should be some easy way. I am using "knex": "^0.21.1", "objection": "^1.6.11" version. default.js contain

"paginate": {
    "default": 10,
    "max": 2000
  },

For time being I just made the following changes in src\serivices\countries\countires.services.js

module.exports = function (app) {
  const options = {
    Model: createModel(app),
    //paginate: app.get("paginate"),
    paginate: {
      default: 0,
      max: 0,
    },
  };

  // Initialize our service with any options it requires
  app.use("/countries", new Countries(options, app));

Upvotes: 0

Views: 1626

Answers (2)

Pash
Pash

Reputation: 392

I found the solution need to add application or service before get hook in my case I added to /src/app.hooks.js

module.exports = {
  before: {
    find: [
      (hook) => {
        if (hook.params.query && hook.params.query.$paginate) {
          hook.params.paginate =
            hook.params.query.$paginate === "false" ||
            hook.params.query.$paginate === false;
          delete hook.params.query.$paginate;
        }
        return hook;
      },
    ],
 }
}

How to call it from Postman :

GET http://localhost:3031/institutes?$paginate=false // all rows
GET http://localhost:3031/institutes //Using pagination

Nuxt.js

async mounted() {
    this.countries = await this.$api.countries.find({ $paginate: false });
},

vue.js

this.countries = await this.$axios.get('http://localhost:3031/countries?$paginate=false');

Upvotes: 0

jshehane
jshehane

Reputation: 88

If I could see your objection query for this route it would be helpful. But you could most likely set a value in paginate like this:

paginate = {
  isPage: false,
  default:10,
  max:2000
}

Then in your query you can have an if statement to check if the isPage value is true or false. And only limit your responses if you have that value set to true. Something like this:

const countries = paginate.isPage
  ? await Country.query()
    .limit(paginate.default)
  : await Country.query()

If you need more help just reply to me and I'll respond as son as I can!

Upvotes: 1

Related Questions