From 232f16aef61cf5cbc5ca723eb06d2ae163a7c54b Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Tue, 28 Jul 2020 12:02:42 +0530 Subject: [PATCH] fix: update method handle use case where values are falsy --- npm-audit.html | 2 +- src/Database/QueryBuilder/Database.ts | 6 +- test/database/query-builder.spec.ts | 114 ++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/npm-audit.html b/npm-audit.html index b556f097..33239d10 100644 --- a/npm-audit.html +++ b/npm-audit.html @@ -55,7 +55,7 @@
- July 28th 2020, 6:09:30 am + July 28th 2020, 6:32:45 am

Last updated

diff --git a/src/Database/QueryBuilder/Database.ts b/src/Database/QueryBuilder/Database.ts index d3952095..1e3ef47a 100644 --- a/src/Database/QueryBuilder/Database.ts +++ b/src/Database/QueryBuilder/Database.ts @@ -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) } diff --git a/test/database/query-builder.spec.ts b/test/database/query-builder.spec.ts index c93ddf59..94220716 100644 --- a/test/database/query-builder.spec.ts +++ b/test/database/query-builder.spec.ts @@ -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() + }) +})