Drizzle, Kysely and others
Use any database library through deps. Drizzle and Kysely examples, with Postgres error mapping.
Nothing in hono-bento is Prisma-specific. Any client works, as long as:
- the db package exports a
Databasetype and acreateDatabase(url); deps.tsregisters{ db: Database };mapErrorincludes a mapper for the library's errors.
For anything on Postgres, mapPostgresError from hono-bento/db maps the
driver's SQLSTATE codes, wrapped or not.
Drizzle
import { boolean, pgTable, text, timestamp } from 'drizzle-orm/pg-core';
export const todos = pgTable('todos', {
id: text('id').primaryKey(),
title: text('title').notNull(),
done: boolean('done').notNull().default(false),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
export type TodoRow = typeof todos.$inferSelect;import { drizzle } from 'drizzle-orm/node-postgres';
import * as schema from './schema';
export function createDatabase(url: string) {
return drizzle({ connection: url, schema });
}
export type Database = ReturnType<typeof createDatabase>;Drizzle queries need the table objects at runtime (eq(todos.id, id)), so
export the schema from its own entry point. Features import the schema; only
server.ts and tests import the client:
{
"exports": {
".": "./src/client.ts",
"./schema": "./src/schema.ts"
}
}import type { Database } from '@todos/db';
import { type TodoRow, todos } from '@todos/db/schema';
import { eq } from 'drizzle-orm';
import { HttpError } from 'hono-bento';
export const store = {
async update(db: Database, id: string, input: TodoUpdate): Promise<Todo> {
const [row] = await db.update(todos).set(input).where(eq(todos.id, id)).returning();
// Drizzle returns no rows rather than throwing, so check here.
if (!row) throw HttpError.notFound(`No todo found with id '${id}'.`);
return toTodo(row);
},
};import { mapPostgresError } from 'hono-bento/db';
mountSpecs(app, specs, { strategies, authorizer, deps: () => ({ db }), mapError: mapPostgresError });hono-bento check allows only type imports from the database package. Let
store.ts import the table definitions as values in hono-bento.config.ts:
db: { package: '@todos/db', allowValues: ['@todos/db/schema'] }.
Kysely
Kysely is typed by an interface, so features import only types, exactly as with Prisma:
import { type Generated, Kysely, PostgresDialect } from 'kysely';
import { Pool } from 'pg';
export interface Tables {
todo: { id: string; title: string; done: Generated<boolean>; created_at: Generated<Date> };
}
export type Database = Kysely<Tables>;
export function createDatabase(url: string): Database {
return new Kysely<Tables>({ dialect: new PostgresDialect({ pool: new Pool({ connectionString: url }) }) });
}export const store = {
async get(db: Database, id: string): Promise<Todo | undefined> {
const row = await db.selectFrom('todo').selectAll().where('id', '=', id).executeTakeFirst();
return row ? toTodo(row) : undefined;
},
};Use mapPostgresError here too.
Other databases
For another database, write a mapper of your own. A mapper is a function from
the thrown error to an HttpError, or undefined to leave the error alone:
import { HttpError, type ErrorMapper } from 'hono-bento';
export const mapSqliteError: ErrorMapper = (error) => {
const code = (error as { code?: unknown }).code;
if (code === 'SQLITE_CONSTRAINT_UNIQUE') {
return new HttpError({
type: 'conflict',
code: 'unique_violation',
message: 'A record with the same unique value already exists.',
});
}
return undefined;
};Keep messages generic: never echo the SQL, constraint names or values back to the client.