Registry / database / kysely-orm

kysely-orm

JSON →
library0.9.7jsnpmunverified

A lightweight, type-safe ORM wrapper around the Kysely query builder, providing a model-based layer with automatic transaction management via Node.js AsyncLocalStorage. v0.9.7 (released 2023) works only with Kysely ~0.24.0 and Node >=16.2.0. Key differentiators: no separate transaction object needed, per-model operations (insert, findOne), and built-in afterCommit hooks. Replaces manual Kysely usage with a simpler, Rails-like active record pattern while preserving full type safety through TypeScript generics.

npm install kysely-orm
INSTALL
IMPORT
SIG · KYSELY-ORM
K
kysely-orm
databasejavascriptv0.9.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Database
import { Database } from 'kysely-orm'
const { Database } = require('kysely-orm')
ESM-only package; requires Node >=16.2.0. CommonJS require will fail because the package does not export a CJS build.
model
class User extends db.model('users', 'id')
class User extends Model { }
model() is a method on the Database instance, not a separate import. The table name and primary key are passed as arguments.
transaction
db.transaction(async () => { ... })
import { transaction } from 'kysely-orm'
transaction is a method on Database or model classes, not a named export. Model.transaction is an alias for db.transaction.

Creates a Database instance with Postgres dialect, defines a User model, and inserts a new user.

import { Database } from 'kysely-orm'; import { PostgresDialect } from 'kysely'; import { Pool } from 'pg'; import type { Generated } from 'kysely'; interface Users { id: Generated<number>; name: string; } interface DB { users: Users; } const db = new Database<DB>({ dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL ?? '' }), }), }); class User extends db.model('users', 'id') {} export async function createUser(name: string) { return User.insert({ name }); }
Debug
Known issues
breakingRequires Node >=16.2.0 due to use of AsyncLocalStorage. Node 14 or lower will cause runtime errors.
fix
Upgrade Node to 16.2.0 or higher.
affects: >=0.9.0
gotchamodel() is a method call on the Database instance, not a class import. Common mistake: trying to import a 'Model' class.
fix
Use 'extends db.model('tablename', 'primaryKey')'.
affects: >=0.9.0
gotchaTransactions use AsyncLocalStorage; callback-based code breaks transaction context unless wrapped with AsyncResource.bind or promisify.
fix
Wrap callbacks in util.promisify or AsyncResource.bind to preserve transaction context.
affects: >=0.9.0
deprecatedAs of 2024, Kysely itself provides a similar model-like layer in higher versions; this plugin may not be needed with Kysely >=0.26.
fix
Consider using Kysely's built-in typed select/insert helpers or a maintained alternative.
affects: >=0.9.0
Errors
Common errors & fixes
Cannot find module 'kysely-orm' or its corresponding type declarations.
Installed in a CJS project or using TypeScript with strict ESM settings without proper type resolution.
fix
Ensure package.json has 'type': 'module' and tsconfig.json has 'moduleResolution': 'node16' or 'bundler'.
TypeError: db.model is not a function
Trying to call model() before the Database instance is fully initialized, or using a different import path.
fix
Verify 'import { Database } from 'kysely-orm'' and that the constructor is called with a valid dialect.
Type 'Database<DB>' is not generic.
Missing generic type parameter on Database construction.
fix
Add the DB type: 'new Database<DB>({ dialect: ... })'.
Upgrade
Version history
0.9.7latest on npm
Audit
Dependencies
kyselyrequiredPeer dependency — core query builder this ORM wraps
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
kysely-orm — npm install kysely-orm · libregistry