Module

x/kysely_postgrs_js_dialect/deps.ts>kysely.DeleteQueryBuilder#using

Kysely dialect for PostgreSQL using the Postgres.js client.
method kysely.DeleteQueryBuilder.prototype.using
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";
const { DeleteQueryBuilder } = kysely;

Adds a using clause to the query.

This clause allows adding additional tables to the query for filtering/returning only. Usually a non-standard syntactic-sugar alternative to a where with a sub-query.

Examples:

await db
  .deleteFrom('pet')
  .using('person')
  .whereRef('pet.owner_id', '=', 'person.id')
  .where('person.first_name', '=', 'Bob')
  .executeTakeFirstOrThrow()

The generated SQL (PostgreSQL):

delete from "pet"
using "person"
where "pet"."owner_id" = "person"."id"
  and "person"."first_name" = $1

On supported databases such as MySQL, this clause allows using joins, but requires at least one of the tables after the from keyword to be also named after the using keyword. See also {@link innerJoin}, {@link leftJoin}, {@link rightJoin} and {@link fullJoin}.

await db
  .deleteFrom('pet')
  .using('pet')
  .leftJoin('person', 'person.id', 'pet.owner_id')
  .where('person.first_name', '=', 'Bob')
  .executeTakeFirstOrThrow()

The generated SQL (MySQL):

delete from `pet`
using `pet`
left join `person` on `person`.`id` = `pet`.`owner_id`
where `person`.`first_name` = ?

You can also chain multiple invocations of this method, or pass an array to a single invocation to name multiple tables.

await db
  .deleteFrom('toy')
  .using(['pet', 'person'])
  .whereRef('toy.pet_id', '=', 'pet.id')
  .whereRef('pet.owner_id', '=', 'person.id')
  .where('person.first_name', '=', 'Bob')
  .returning('pet.name')
  .executeTakeFirstOrThrow()

The generated SQL (PostgreSQL):

delete from "toy"
using "pet", "person"
where "toy"."pet_id" = "pet"."id"
  and "pet"."owner_id" = "person"."id"
  and "person"."first_name" = $1
returning "pet"."name"

Type Parameters

TE extends TableExpression<DB, keyof DB>

Parameters

tables: TE[]

Returns

DeleteQueryBuilder<From<DB, TE>, FromTables<DB, TB, TE>, O>

Type Parameters

TE extends TableExpression<DB, keyof DB>

Returns

DeleteQueryBuilder<From<DB, TE>, FromTables<DB, TB, TE>, O>