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

Custom Builder Functions #1579

Open
mtom55 opened this issue Jul 15, 2016 · 3 comments
Open

Custom Builder Functions #1579

mtom55 opened this issue Jul 15, 2016 · 3 comments

Comments

@mtom55
Copy link

mtom55 commented Jul 15, 2016

I'd like to add some custom functions to knex which I have to use all the time but i'm finding it hard to get them to work.

They are:
knex.selectOne
knex.selectZeroOrOne

Instead of doing first, selectOne does not apply a limit and instead throws an error if it gets more than one record back. This allows the author to catch particular errors particular early.

I started with:

knex.client.QueryBuilder.prototype.selectOne = function (columns) { 
    const args = new Array(arguments.length);
    for (let i = 0; i < args.length; i++) {
      args[i] = arguments[i];
    }
    this.select.apply(this, args);
    this._method = 'first';
    //this.limit(1);
    return this;
}

but obviously the check on how many records get returned has to happen after the results have come back

Would you be able to point me in the right direction?

@tgriesser
Copy link
Member

Just FYI This code won't work in 0.12, you'll need to require knex/lib/query/builder directly to modify that prototype.

For 1.0 I'll plan to define a standard way to customize the query builder chain like you're trying to do above, and with the addition of "hooks", you'll be able to easily setup post-result transforms.

@thetutlage
Copy link
Contributor

@tgriesser I have waiting for this feature for while and start modifying the QueryBuilder prototype. It will be so great to have an interface to extend the query builder.

👍

@miketdonahue
Copy link

@tgriesser - How far are we away from 1.0?

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

No branches or pull requests

4 participants