From 9fb0ad7c0c17dcdbaa3b410024738924b8eade61 Mon Sep 17 00:00:00 2001 From: MaximeMRF Date: Wed, 11 Oct 2023 20:28:25 +0800 Subject: [PATCH] feat(QueryBuilder/where): add null type to where clause --- adonis-typings/querybuilder.ts | 8 +++++-- test/database/query-builder.spec.ts | 35 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/adonis-typings/querybuilder.ts b/adonis-typings/querybuilder.ts index 8dd6747c..4e292aba 100644 --- a/adonis-typings/querybuilder.ts +++ b/adonis-typings/querybuilder.ts @@ -141,8 +141,12 @@ declare module '@ioc:Adonis/Lucid/Database' { /** * Key-value pair. The value can also be a subquery */ - (key: string | RawQuery, value: StrictValues | ChainableContract): Builder - (key: string | RawQuery, operator: string, value: StrictValues | ChainableContract): Builder + (key: string | RawQuery, value: StrictValues | ChainableContract | null): Builder + ( + key: string | RawQuery, + operator: string, + value: StrictValues | ChainableContract | null + ): Builder } /** diff --git a/test/database/query-builder.spec.ts b/test/database/query-builder.spec.ts index ddd22d8a..9ecb3ca1 100644 --- a/test/database/query-builder.spec.ts +++ b/test/database/query-builder.spec.ts @@ -467,6 +467,41 @@ test.group('Query Builder | where', (group) => { await connection.disconnect() }) + test('add where clause with a null comparison', async ({ assert }) => { + const connection = new Connection('primary', getConfig(), app.logger) + connection.connect() + + let db = getQueryBuilder(getQueryClient(connection, app)) + const { sql, bindings } = db.from('users').where('username', null).toSQL() + + const { sql: knexSql, bindings: knexBindings } = connection + .client!.from('users') + .where('username', null) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + + /** + * Using keys resolver + */ + db = getQueryBuilder(getQueryClient(connection, app)) + db.keysResolver = (key) => `my_${key}` + const { sql: resolverSql, bindings: resolverBindings } = db + .from('users') + .where('username', null) + .toSQL() + + const { sql: knexResolverSql, bindings: knexResolverBindings } = connection + .client!.from('users') + .where('my_username', null) + .toSQL() + + assert.equal(resolverSql, knexResolverSql) + assert.deepEqual(resolverBindings, knexResolverBindings) + await connection.disconnect() + }) + test('wrap where clause to its own group', async ({ assert }) => { const connection = new Connection('primary', getConfig(), app.logger) connection.connect()