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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add withRecursive() #2889
Add withRecursive() #2889
Conversation
f403332
to
48d21f1
Compare
src/query/builder.js
Outdated
@@ -45,6 +45,22 @@ function Builder(client) { | |||
} | |||
inherits(Builder, EventEmitter); | |||
|
|||
const validateWithArgs = function(alias, statement, method) { | |||
if (typeof alias !== 'string') { | |||
throw new Error(method + '() first argument must be a string'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use template string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was simply refactoring this to be used in both with*
methods. I've noticed template aren't used in most of this codebase and I assumed this was support older JS interpreters. If you're saying template strings are fine I'll update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well yeah, support for older JS interpreters was a thing previously, but this year we dropped support for Node.js < 6, so it's good time to start moving towards modern JS syntax :)
}, | ||
|
||
// Helper for compiling any advanced `withRecursive` queries. | ||
withRecursiveWrapped(alias, query) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why separate this from withRecursive
if it just passes same params after validation? Also, should it be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just mirroring the pattern established by with
and withWrapped
.
src/query/compiler.js
Outdated
while (++i < withs.length) { | ||
const stmt = withs[i]; | ||
if (stmt.recursive) recursive = 'recursive '; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's preferable to always use {}
src/query/compiler.js
Outdated
while (++i < withs.length) { | ||
const stmt = withs[i]; | ||
if (stmt.recursive) recursive = 'recursive '; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a bit redundant, you are resetting string multiple times and constructing string at this time looks kinda premature. Might be cleaner to set boolean flag instead and do something like
const withBlock = isRecursive ? : 'with recursive ' : 'with '
later on
Thank you for your contribution! Could you also create a PR in https://github.com/knex/documentation? |
Comments addressed and docs PR created knex/documentation#144 |
LGTM! @elhigu, what do you think? |
Yoo @caseywebdev good to see you man! Looks good to me, thanks for the patch! |
Hey Tim! Happy to help 馃憤馃徏 Thanks for the merge 馃巻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only thing to comment from me would be to have integration test making sure that those queries are actually doing what they are meant to do (verifies that written syntax won't get broken even in the future). Looks great though 馃憤
* Add withRecursive()
Closes #1755, an alternative to #2218
I needed
WITH RECURSIVE
in a project recently so I monkey patched this in and it works great 馃憣, figured I'd help out the upstream.I noticed the other PR tried to handle the
UNION ALL
automatically for the recursive section, but in my case I actually wanted just aUNION
. I think simply having this method add theRECURSIVE
keyword is the way to go since it's trivial to do the.union(...)
or.unionAll(...)
or whatever one needs in the query section of the.withRecursive()
method.Future improvements might include a
raw
version, but I think getting this added in its simplest form is an easy win.