网页
综合
更新时间 2026-11-14 16:26:20
Getting started | Kysely
自动蜘蛛
kysely.dev
正文约 4600 字
Minimum supported version 5.4.
For improved compilation performance, use version 5.9 or later.
You must enable strict mode in your tsconfig.json file's compilerOptions:
Kysely can be installed using any of the following package managers:
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:
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:
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:
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:
Deno is a secure runtime for JavaScript and TypeScript.
Run the following command in your terminal:
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:
For production apps, it is recommended to automatically generate your Database interface by introspecting your production database or Prisma schemas. Generated types might differ in naming convention, internal order, etc. Find out more at "Generating types".
Kysely only deals with types in the TypeScript level. The runtime JavaScript types are decided by the underlying third-party driver such as pg or mysql2 and it's up to you to select the correct TypeScript types in the database interface. Kysely never touches the runtime output types in any way. Find out more at "Data types".
For Kysely's query compilation and execution to work, it needs to understand your database's SQL specification and how to communicate with it. This requires a Dialect implementation.There are 5 built-in dialects for PostgreSQL, MySQL, Microsoft SQL Server (MSSQL), SQLite, and PGlite. Additionally, the community has implemented several dialects to choose from. Find out more at "Dialects".
A Dialect implementation usually requires a database driver library as a peer dependency. Let's install it using the same package manager command from before:
Kysely's built-in PostgreSQL dialect uses the "pg" driver library under the hood. Please refer to its official documentation for configuration options.
Run the following command in your terminal:
Kysely's built-in MySQL dialect uses the "mysql2" driver library under the hood. Please refer to its official documentation for configuration options.
Run the following command in your terminal:
Kysely's built-in Microsoft SQL Server (MSSQL) dialect uses the "tedious" driver library under the hood. Please refer to its official documentation for configuration options.
Additionally, Kysely's Microsoft SQL Server (MSSQL) dialect uses the "tarn" resource pool package for connection pooling. Please refer to its official documentation for configuration options.
Run the following command in your terminal:
Kysely's built-in SQLite dialect uses the "better-sqlite3" driver library under the hood. Please refer to its official documentation for configuration options.
Run the following command in your terminal:
Kysely's built-in PGlite dialect uses the "@electric-sql/pglite" driver library under the hood. Please refer to its official documentation for configuration options.
Run the following command in your terminal:
I use a different package manager (not npm)
Let's create a Kysely instance using the built-in PostgresDialect dialect:
I use a different package manager (not npm)
I use a different dialect (not PostgreSQL)
When Kysely is responsible for closing its connections, pool, or database instance, call await db.destroy() when you are done using it to release those resources.
If you share the pool or database instance with other consumers or manage its lifecycle elsewhere, skip db.destroy(). Let the application close it once all consumers, including Kysely, have finished. No separate cleanup of the Kysely instance is needed.
Kysely initializes its driver lazily. Calling db.destroy() before initialization has started does nothing, even if you have used the supplied pool or database instance independently.
Let's implement the person repository:
I use a different dialect (not PostgreSQL)
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。