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

Change query to change status from string to integer #1313

Closed
hassan-jahan opened this issue Apr 28, 2019 · 2 comments
Closed

Change query to change status from string to integer #1313

hassan-jahan opened this issue Apr 28, 2019 · 2 comments

Comments

@hassan-jahan
Copy link

hassan-jahan commented Apr 28, 2019

Hi,

Thanks for great work! I'm looking for a way to create a plugin for status fields.

User.query().where('status', 'blocked')

which actually should be changed to:

User.query().where('status', 100)
This will change strings to integer so we will have more readable code and smaller (and faster) DB queries.

I know it's possible to use 'beforeUpdate' and 'beforeInsert' hooks but I couldn't find a way to change select queries.

@koskimas
Copy link
Collaborator

koskimas commented Apr 28, 2019

Unfortunately there's no way to do that except overriding the query builder methods yourself. Objection query builder is a superset of the knex query builder and simply calls the corresponding knex method for most of the query builder methods like where. That way, objection doesn't need to duplicate the knex API which would need to be tested and maintained. Knex doesn't have a hook system or a reflection API for doing things like this.

But you can override the objection query builder. Something like this:

class MyCustomQueryBuilder extends Model.QueryBuilder {
  where(...args) {
    if (args.length === 2 && args[0] === 'status') {
      args[1] = mapStatus(args[1])
    }
    return super.where(...args)
  }
}
class User extends Model {
  static get QueryBuilder() {
    return MyCustomQueryBuilder
  }
}

If you want to make this work in all situations, you need to override a bunch more *where methods, so this solution is not optimal.

@koskimas
Copy link
Collaborator

koskimas commented Apr 28, 2019

This will change strings to integer so we will have more readable code and smaller (and faster) DB queries.

I would just define them as strings in the db too. I challenge you to show any proof that there is a significant performance difference. If comparing two numbers takes 0.1 nanoseconds, comparing small strings can take 0.5 nanosecond or something like that. You can still do billions of those in a second. Those comparisons will never ever be a bottleneck. If you have this kind of queries, you would use an index anyway, which will take down the amount of comparisons by orders of magnitude.

Also the space you save is couple of bytes per row. Even if you have billions of rows, it's some gigabytes. At that point your database will be terabytes anyway, so it wouldn't really matter.

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

2 participants