Registry / database / kysely-zod-sqlite

kysely-zod-sqlite

JSON →
library1.4.15jsnpmunverified

A flexible API for Cloudflare D1 and SQLite, combining Prisma-like simplicity with Kysely's powerful query builder and Zod runtime validation. Version 1.4.15, active development, ships TypeScript types. Key differentiators: Zod schema-driven model validation with JSON parsing, support for nested relations (one-to-one, one-to-many), local unit testing with Better-SQLite3, and remote worker-to-worker/service binding calls. Supports ESM and requires Node >=18.

npm install kysely-zod-sqlite
INSTALL
IMPORT
SIG · KYSELY-ZOD-SQLITE
K
kysely-zod-sqlite
databasejavascriptv1.4.15
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.

SqliteApi
import { SqliteApi } from 'kysely-zod-sqlite'
import SqliteApi from 'kysely-zod-sqlite'
Named export, not default. Use destructured import.
BetterSqlite3Driver
import { BetterSqlite3Driver } from 'kysely-zod-sqlite/driver/sqlite-driver'
import { BetterSqlite3Driver } from 'kysely-zod-sqlite'
Driver exports are under subpaths, not the main entry.
D1Driver
import { D1Driver } from 'kysely-zod-sqlite/driver/d1-driver'
import D1Driver from 'kysely-zod-sqlite/driver/d1-driver'
Named export, not default. For Cloudflare Workers/Pages.
zJsonObject
import { zJsonObject } from 'kysely-zod-sqlite'
Main entry export, used in Zod schemas.
zRelationOne
import { zRelationOne } from 'kysely-zod-sqlite'
Main entry export for defining relations.

Sets up a local SQLite database with Zod validation, Kysely query builder, and Prisma-like API for CRUD operations.

import { z } from 'zod'; import { SqliteApi, zJsonObject, zJsonSchema, zRelationOne, zBoolean, zDate } from 'kysely-zod-sqlite'; import { BetterSqlite3Driver } from 'kysely-zod-sqlite/driver/sqlite-driver'; import Database from 'better-sqlite3'; const userSchema = z.object({ id: z.string(), name: z.string(), email: z.string().optional(), data: zJsonObject(), config: zJsonSchema(z.object({ language: z.string() })), created_at: zDate(), updated_at: zDate(), isDelete: zBoolean(), }); const dbSchema = z.object({ users: userSchema }); type DbSchema = typeof dbSchema; class MyApi extends SqliteApi<DbSchema> { get users() { return this.table('users'); } } const db = new Database(':memory:'); const driver = new BetterSqlite3Driver(db, {}); const api = new MyApi({ schema: dbSchema, config: {}, kysely: new Kysely({ dialect: new SqliteDialect({ database: db }), }), }); async function main() { await api.users.create({ data: { id: '1', name: 'Alice', data: {}, config: { language: 'en' }, created_at: new Date(), updated_at: new Date(), isDelete: false } }); const user = await api.users.selectFirst({ where: { name: 'Alice' }, include: {} }); console.log(user); } main().catch(console.error);
Debug
Known issues
gotchaThe `BetterSqlite3Driver` requires `better-sqlite3` as an optional dependency. If not installed, import will fail.
fix
Run `npm install better-sqlite3` in your project.
affects: *
deprecatedPackage uses `createKyselySqlite` in examples, but as of v1.4 this function may be deprecated or moved. Use `new Kysely({ dialect: ... })` directly.
fix
Instantiate Kysely manually with a dialect (e.g., SqliteDialect for Better-SQLite3).
affects: <1.5
gotchaThe `FetchDriver` is for remote workers, not for local use. Using it locally will fail without a server.
fix
Use `BetterSqlite3Driver` for local development.
affects: *
breakingStarting from v1.0, all import paths changed: drivers are under subpaths like `kysely-zod-sqlite/driver/sqlite-driver`. Old imports from main entry break.
fix
Update imports: use `import { BetterSqlite3Driver } from 'kysely-zod-sqlite/driver/sqlite-driver'`.
affects: >=1.0
Errors
Common errors & fixes
Cannot find module 'better-sqlite3'
Missing optional dependency for local driver.
fix
Install better-sqlite3: `npm install better-sqlite3`.
Cannot read properties of undefined (reading 'selectFrom')
Accessing `api.ky` before initializing the Kysely instance.
fix
Ensure Kysely is properly passed to the Api constructor via the `kysely` option.
TypeError: Cannot import 'BetterSqlite3Driver' from 'kysely-zod-sqlite'
Importing from the wrong path.
fix
Use `import { BetterSqlite3Driver } from 'kysely-zod-sqlite/driver/sqlite-driver'`.
Upgrade
Version history
1.4.15latest on npm
Audit
Dependencies
kyselyrequiredpeer dependency for SQL query builder
zodrequiredpeer dependency for schema validation
better-sqlite3optionalrequired for local driver (BetterSqlite3Driver)
Agent activity
9 hits · last 30 days
node
6
Resources
kysely-zod-sqlite — npm install kysely-zod-sqlite · libregistry