edwardhotchkiss / mongoose-paginate

Mongoose.js (Node.js & MongoDB) Document Query Pagination
MIT License
984 stars 216 forks source link

I want to change 'docs' to 'rows' #108

Open ilaipi opened 7 years ago

ilaipi commented 7 years ago

any way to do that? thanks

Jokero commented 7 years ago

Right now you can't do that in a general way. What do you think about this possible in the future solution (with transform function)? https://github.com/edwardhotchkiss/mongoose-paginate/issues/83#issuecomment-208747602

jdegger commented 7 years ago

Just in case you wanna still wanna do this

function pagination(query, options) {

  const self = this
  return new Promise(function(resolve, reject) {
    co(function*() {

      const result = yield self.paginate(query, options)

      // clean
      result.offset = options.offset
      result.data = result.docs
      delete result.docs

      // resolve
      resolve(result)

    }).then(resolve, reject)
  })

}

schema.statics.pagination = pagination

Is how I achieved it. Basically just proxying the result through another function

ndabAP commented 6 years ago

I'm interested in such function as well. My coding style enforces no abbreviations.

jdegger commented 6 years ago

ES6 version:

schema.statics.pagination = async (query, options) => {

  // run normal pagination function first
  const result = await this.paginate(query, options)

  // clean
  result.offset = options.offset;
  result.data = result.docs;
  delete result.docs;

  // resolve
  return result;

}
ndabAP commented 6 years ago

Great @jdegger, will try that!

zlocate commented 5 years ago

Maybe add init params for this?