diegohaz/querymen

"limit" Paging error when using "Schema"

wesias7 opened this issue ยท 7 comments

const querySchema = new QuerymenSchema({
  keywords: { search: true },
  joinedPlatform: { type: String, paths:['joined'], elementMatch: 'platform' },
  joinedGroup: { type: String, paths:['joined'], elementMatch: 'group' },
  joinedGrade: { type: String, paths:['joined'], elementMatch: 'grade' },
  createdAtDate: { type: Date, paths: ['createdAt'], duration: 'date', },
  status: status.type, 
  isAdult: status.isAdult, 
  activatedToEmail: activatedToEmail.type,
  activatedToMobile: activatedToMobile.type,
  activatedToIdentify: activatedToIdentify.type,
  activatedToBankAccount: activatedToBankAccount.type,
  allowMailing: allowMailing.type,
  allowSms: allowSms.type,
  page: { max: 100000 },
  limit: { max: 100000 }
})
querySchema.parser('search', (search, value, path, operator) => {
  if (!value) { return value }
  const regValue = new RegExp(value, "ig")
  if (search) {
    if(value*1 >= 0) {
      value = { $or: [{ mobile: regValue },{ accountNo: value*1 }] }
    } else {
      value = { $or: [{ email: regValue },{ name: regValue },{ realName: regValue },{ mobile: regValue },{ accountId: regValue }] }
    }
  }
  return value
})
querySchema.parser('elementMatch', (elementMatch, value, path, operator) => {
  if (!value) { return value }
  if (elementMatch) {
    value = { [path]: { $elemMatch: { [elementMatch]: value }, } }
  }
  return value
})
querySchema.parser('duration', (duration, value, path, operator) => {
  if (!value) { return value }
  if (duration == 'date') { 
    value = { [path]: { $gte: new Date(value).setHours(0,0,0,0), $lte: new Date(value).setHours(23,59,59,999), } }
  }
  return value
})
router.get('/',
  //token({ required: true, roles: ['admin'] }),
  query(querySchema),
  index)

then vscode debugs I saw an error like this.

{ query: {},
  select: {},
  cursor: { skip: 30, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=2 200 1360.531 ms - -
{ query: {},
  select: {},
  cursor: { skip: 60, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=3 200 25.560 ms - -
{ query: {},
  select: {},
  cursor: { skip: 0, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=1 200 27.345 ms - -
{ query: {},
  select: {},
  cursor: { skip: 30, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=2 200 27.026 ms - -
{ query: {},
  select: {},
  cursor: { skip: 60, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=3 200 26.904 ms - -
{ query: {},
  select: {},
  cursor: { skip: 90, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=4 200 30.536 ms - -
{ query: {},
  select: {},
  cursor: { skip: 120, limit: 15, sort: { createdAt: -1 } } }

Why is it an error here?

in querymen/index.js

    if (schema && schema.options && schema.options.near) {
      _schema = schema instanceof Schema
        ? _.clone(schema)
        : new Schema(schema, options)
    } else {
      _schema = schema instanceof Schema
        ? _.cloneDeep()
        : new Schema(schema, options)
    }

after

export function middleware (schema, options) {
  return function (req, res, next) {
    let _schema = schema instanceof Schema ? schema : new Schema(schema, options)

    _schema.validate(req.query, (err) => {
      if (err) {
        req.querymen = { error: err }
        res.status(400)
        return next(err.message)
      }

      req.querymen = _schema.parse()
      req.querymen.schema = _schema
      next()
    })
  }
}

This is how it works.

GET /?limit=15&page=3 200 27.436 ms - -
{ query: {},
  select: {},
  cursor: { skip: 0, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=1 200 24.773 ms - -
{ query: {},
  select: {},
  cursor: { skip: 15, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=2 200 25.139 ms - -
{ query: {},
  select: {},
  cursor: { skip: 30, limit: 15, sort: { createdAt: -1 } } }
GET /?limit=15&page=3 200 24.126 ms - -
{ query: {},
  select: {},
  cursor: { skip: 45, limit: 15, sort: { createdAt: -1 } } }

Why did this happen?

npm install --save https://github.com/wesias7/querymen.git#master
  1. Enable "schema" support. (this binds)
  2. Query overwrite is supported. (index.js)
  3. Processing "bind" of "this" to "constructor". (querymen-schema.js and querymen-param.js)
  4. Add missing args values (req.query in index.js)

@diegohaz @jorgeluisrezende @chemitaxis
"Schema" didn't work. I've solved it, but please check.

@wesias7 Are you going to maintain it ? or it's gonna be forgotten in few months ?

I think a better option is moving to new package: https://github.com/diegohaz/schm

@wesias7 Are you going to maintain it ? or it's gonna be forgotten in few months ?

I need to patch "Schema bind set this."
You can find out if you check my repository.
I'm still using it well.

I think a better option is moving to new package: https://github.com/diegohaz/schm

I checked. It's amazing.
But look at the patched content of my repository.
'This bind' is 'Schema'.I can see what you've done with js.

I hope this part is also patched on "querymen."
I want to see Diegohaz.