Skip to content

Commit

Permalink
fix: update method handle use case where values are falsy
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 28, 2020
1 parent 1fd882a commit 232f16a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
2 changes: 1 addition & 1 deletion npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
July 28th 2020, 6:09:30 am
July 28th 2020, 6:32:45 am
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
*/
public update(column: any, value?: any, returning?: string[]): this {
this.ensureCanPerformWrites()
if (!value && !returning) {
if (value === undefined && returning === undefined) {
this.knexQuery.update(this.resolveKey(column, true))
} else if (!returning) {
this.knexQuery.update(this.resolveKey(column, true), value)
} else if (returning === undefined) {
this.knexQuery.update(this.resolveKey(column), value)
} else {
this.knexQuery.update(this.resolveKey(column), value, returning)
}
Expand Down
114 changes: 114 additions & 0 deletions test/database/query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8421,3 +8421,117 @@ test.group('Query Builder | event', (group) => {
await connection.disconnect()
})
})

test.group('Query Builder | update', (group) => {
group.before(async () => {
await setup()
})

group.after(async () => {
await cleanup()
})

test('update columns by defining object', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

let db = getQueryBuilder(getQueryClient(connection))
const { sql, bindings } = db.from('users').update({ account_status: 'active' }).toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.update({ account_status: 'active' })
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.update({ account_status: 'active' })
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.update({ my_account_status: 'active' })
.toSQL()

assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('update columns by defining key-value pair', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

let db = getQueryBuilder(getQueryClient(connection))
const { sql, bindings } = db.from('users').update('account_status', 'active').toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.update('account_status', 'active')
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.update('account_status', 'active')
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.update('my_account_status', 'active')
.toSQL()

console.log(resolverSql, resolverBindings)
assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})

test('handle use case where update value is false or 0', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

let db = getQueryBuilder(getQueryClient(connection))
const { sql, bindings } = db.from('users').update('account_status', 0).toSQL()

const { sql: knexSql, bindings: knexBindings } = connection
.client!.from('users')
.update('account_status', 0)
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

db = getQueryBuilder(getQueryClient(connection))
db.keysResolver = (key) => `my_${key}`

const { sql: resolverSql, bindings: resolverBindings } = db
.from('users')
.update('is_active', false)
.toSQL()

const { sql: knexResolverSql, bindings: knexResolverBindings } = connection
.client!.from('users')
.update('my_is_active', false)
.toSQL()

console.log(resolverSql, resolverBindings)
assert.equal(resolverSql, knexResolverSql)
assert.deepEqual(resolverBindings, knexResolverBindings)

await connection.disconnect()
})
})

0 comments on commit 232f16a

Please sign in to comment.