Skip to content

Commit

Permalink
feat(QueryBuilder/where): add null type to where clause (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeMRF committed Nov 20, 2023
1 parent 0fac231 commit 2790fe9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
35 changes: 35 additions & 0 deletions test/database/query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2790fe9

Please sign in to comment.