@project-karin/drizzle
v0.5.21
Published
Drizzle ORM integration plugin for Karin.
Readme
@project-karin/drizzle
Drizzle ORM integration plugin for Karin.
Installation
bun add @project-karin/drizzle drizzle-ormYou will also need a driver, for example postgres, mysql2, or @libsql/client.
Usage
Basic Setup (e.g. with Turso/LibSQL)
import { DrizzlePlugin } from "@project-karin/drizzle";
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";
// Initialize client and db
const client = createClient({
url: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN
});
const db = drizzle(client);
const app = await KarinFactory.create(adapter, {
plugins: [
new DrizzlePlugin({
db,
client, // Pass client to handle graceful shutdown
}),
],
});Lazy Initialization
You can pass a factory function to initialize the DB only when the plugin installs.
new DrizzlePlugin({
db: () => {
const client = createClient({ ... });
return drizzle(client);
},
// If you need to close the client, use onDisconnect with lazy init
onDisconnect: async (db) => {
// You might need to access the client from the db object if possible,
// or manage the client scope differently.
}
})Dependency Injection
Inject the database instance into your services.
import { InjectDrizzle } from "@project-karin/drizzle";
import { LibSQLDatabase } from "drizzle-orm/libsql";
import { users } from "./schema";
@Service()
class UserService {
constructor(@InjectDrizzle() private db: LibSQLDatabase) {}
async findAll() {
return this.db.select().from(users);
}
}Serverless Support
The plugin automatically detects serverless environments (AWS Lambda, Vercel, Cloudflare Workers, etc.) and skips the disconnection step to allow connection reuse (Warm Starts).
You can override this behavior:
new DrizzlePlugin({
db,
serverless: true // Force serverless mode
})Options
interface DrizzlePluginOptions {
db: any | (() => any);
client?: any;
onDisconnect?: (db: any, client?: any) => Promise<void> | void;
token?: string;
serverless?: boolean;
}