-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Allow chaining of increment, decrement, and update #2740
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
map, | ||
omitBy, | ||
reduce, | ||
has, | ||
} from 'lodash'; | ||
import uuid from 'uuid'; | ||
|
||
|
@@ -538,21 +539,6 @@ assign(QueryCompiler.prototype, { | |
} | ||
}, | ||
|
||
// Compile the "counter". | ||
counter() { | ||
const { counter } = this.single; | ||
const toUpdate = {}; | ||
toUpdate[counter.column] = this.client.raw( | ||
this.formatter.wrap(counter.column) + | ||
' ' + | ||
(counter.symbol || '+') + | ||
' ' + | ||
counter.amount | ||
); | ||
this.single.update = toUpdate; | ||
return this.update(); | ||
}, | ||
|
||
// On Clause | ||
// ------ | ||
|
||
|
@@ -736,7 +722,35 @@ assign(QueryCompiler.prototype, { | |
}, | ||
|
||
// "Preps" the update. | ||
_prepUpdate(data) { | ||
_prepUpdate(data = {}) { | ||
const { counter = [] } = this.single; | ||
const grouped = groupBy(counter, 'column'); | ||
|
||
for (const column in grouped) { | ||
//Skip? | ||
if (has(data, column)) { | ||
//Needed? | ||
this.client.logger.warn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most warns have to do with feature support by different SQL engines; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its bold to assume calling No strong opinion on what should happen here, but definitely some form of message must be spit out of knex instead of silently ignoring. I opted for warn over error due to the argument above, but not 100% against changing this. |
||
`increment/decrement called for a column that has already been specified in main .update() call. Ignoring increment/decrement and using value from .update() call.` | ||
); | ||
continue; | ||
} | ||
|
||
let value = reduce( | ||
grouped[column], | ||
(memo, item) => memo + item.amount, | ||
0 | ||
); | ||
|
||
const symbol = value < 0 ? '-' : '+'; | ||
|
||
if (symbol === '-') { | ||
value = -value; | ||
} | ||
|
||
data[column] = this.client.raw(`?? ${symbol} ?`, [column, value]); | ||
} | ||
|
||
data = omitBy(data, isUndefined); | ||
const vals = []; | ||
const columns = Object.keys(data); | ||
|
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.
Could you explain what this endpoint might be used for?
My reaction is that it feels unnecessary
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 follows the same principals as
.clearSelect()
,.clearWhere()
,.clearOrder()
.I've yet to find a real life use-case for these functions myself, but for consistency I added one here as well. Suppose someone has found a use for the other clear-functions, else they wouldn't exist.
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.
@wubzz
we use it like this with objection.js for example: