import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.4/deps.ts";
const { DeleteQueryBuilder } = kysely;
Call func(this)
if condition
is true.
This method is especially handy with optional selects. Any returning
or returningAll
method calls add columns as optional fields to the output type when called inside
the func
callback. This is because we can't know if those selections were actually
made before running the code.
You can also call any other methods inside the callback.
Examples
async function deletePerson(id: number, returnLastName: boolean) {
return await db
.deleteFrom('person')
.where('id', '=', id)
.returning(['id', 'first_name'])
.$if(returnLastName, (qb) => qb.returning('last_name'))
.executeTakeFirstOrThrow()
}
Any selections added inside the if
callback will be added as optional fields to the
output type since we can't know if the selections were actually made before running
the code. In the example above the return type of the deletePerson
function is:
{
id: number
first_name: string
last_name?: string
}
Parameters
func: (qb: this) => DeleteQueryBuilder<any, any, O2>
Returns
O2 extends DeleteResult ? DeleteQueryBuilder<DB, TB, DeleteResult> : O2 extends O & infer E ? DeleteQueryBuilder<DB, TB, O & Partial<E>> : DeleteQueryBuilder<DB, TB, Partial<O2>>