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

"limit" Paging error when using "Schema" #68

Open
wesias7 opened this issue Sep 10, 2018 · 7 comments
Open

"limit" Paging error when using "Schema" #68

wesias7 opened this issue Sep 10, 2018 · 7 comments

Comments

@wesias7
Copy link

wesias7 commented Sep 10, 2018

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?

@wesias7
Copy link
Author

wesias7 commented Sep 10, 2018

#69

@wesias7
Copy link
Author

wesias7 commented Sep 10, 2018

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)

@wesias7
Copy link
Author

wesias7 commented Sep 10, 2018

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

@roonie007
Copy link

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

@chemitaxis
Copy link
Collaborator

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

@wesias7
Copy link
Author

wesias7 commented Jan 13, 2019

@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.

@wesias7
Copy link
Author

wesias7 commented Jan 13, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants