This content is not available in your language yet.
Short and simple examples of how to write update queries.
import { Generated } from 'kysely' declare global { interface DB { person: PersonTable pet: PetTable } interface PersonTable { id: Generated<string> first_name: string last_name: string | null created_at: Generated<Date> age: number } interface PetTable { id: Generated<string> name: string owner_id: string species: 'cat' | 'dog' is_favorite: boolean }}
const result = await db .updateTable('person') .set({ first_name: 'Jennifer', last_name: 'Aniston' }) .where('id', '=', '1') .executeTakeFirst() console.log(result.numUpdatedRows)
UPDATE "person"SET "first_name" = $1, "last_name" = $2WHERE "id" = $3 -- Parameters-- [1] Jennifer-- [2] Aniston-- [3] 1
UPDATE `person`SET `first_name` = ?, `last_name` = ?WHERE `id` = ? -- Parameters-- [1] Jennifer-- [2] Aniston-- [3] 1
UPDATE "person"SET "first_name" = ?, "last_name" = ?WHERE "id" = ? -- Parameters-- [1] Jennifer-- [2] Aniston-- [3] 1
const result = await db .updateTable('person') .set((eb) => ({ age: eb('age', '+', 1), first_name: eb.selectFrom('pet').select('name').limit(1), last_name: 'updated', })) .where('id', '=', '1') .executeTakeFirst() console.log(result.numUpdatedRows)
UPDATE "person"SET "age" = "age" + $1, "first_name" = ( SELECT "name" FROM "pet" LIMIT $2 ), "last_name" = $3WHERE "id" = $4 -- Parameters-- [1] 1-- [2] 1-- [3] updated-- [4] 1
UPDATE `person`SET `age` = `age` + ?, `first_name` = ( SELECT `name` FROM `pet` LIMIT ? ), `last_name` = ?WHERE `id` = ? -- Parameters-- [1] 1-- [2] 1-- [3] updated-- [4] 1
UPDATE "person"SET "age" = "age" + ?, "first_name" = ( SELECT "name" FROM "pet" LIMIT ? ), "last_name" = ?WHERE "id" = ? -- Parameters-- [1] 1-- [2] 1-- [3] updated-- [4] 1