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
Comments
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 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 |
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. |
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.
The text was updated successfully, but these errors were encountered: