Getting started
Installation
Kysely can be installed using any of the following package managers:
- npm
- pnpm
- Yarn
- Deno
- Bun
npm is the default package manager for Node.js, and to where Kysely is published.
Your project is using npm if it has a package-lock.json
file in its root folder.
Run the following command in your terminal:
npm install kysely
pnpm is a fast, disk space efficient package manager for Node.js.
Your project is using pnpm if it has a pnpm-lock.yaml
file in its root folder.
Run the following command in your terminal:
pnpm install kysely
Yarn is a fast, reliable and secure dependency manager for Node.js.
Your project is using Yarn if it has a yarn.lock
file in its root folder.
Run the following command in your terminal:
yarn add kysely
Deno is a secure runtime for JavaScript and TypeScript.
Your root deno.json
's "imports" field should include the following dependencies:
{
"imports": {
"kysely": "npm:kysely@^0.25.0"
}
}
Bun is a new JavaScript runtime built for speed, with a native bundler, transpiler, test runner, and npm-compatible package manager baked-in.
Run the following command in your terminal:
bun install kysely
Types
For Kysely's type-safety and autocompletion to work, it needs to know your
database structure. This requires a TypeScript Database
interface, that contains table names as keys and table schema interfaces as
values.
Let's define our first database interface:
import { ColumnType, Generated, Insertable, Selectable, Updatable } from 'kysely'
export interface Database {
person: PersonTable
pet: PetTable
}
export interface PersonTable {
// Columns that are generated by the database should be marked
// using the `Generated` type. This way they are automatically
// made optional in inserts and updates.
id: Generated<number>
first_name: string
gender: 'man' | 'woman' | 'other'
// If the column is nullable in the database, make its type nullable.
// Don't use optional properties. Optionality is always determined
// automatically by Kysely.
last_name: string | null
// You can specify a different type for each operation (select, insert and
// update) using the `ColumnType<SelectType, InsertType, UpdateType>`
// wrapper. Here we define a column `created_at` that is selected as
// a `Date`, can optionally be provided as a `string` in inserts and
// can never be updated:
created_at: ColumnType<Date, string | undefined, never>
}
// You should not use the table schema interfaces directly. Instead, you should
// use the `Selectable`, `Insertable` and `Updateable` wrappers. These wrappers
// make sure that the correct types are used in each operation.
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
export interface PetTable {
id: Generated<number>
name: string
owner_id: number
species: 'dog' | 'cat'
}
export type Pet = Selectable<PetTable>
export type NewPet = Insertable<PetTable>
export type EditedPet = Updateable<PetTable>
For production apps, it is recommended to automatically generate your Database
interface by introspecting your production database or Prisma schemas. Find out more at
"Generating types".