The central configuration file to extend bknd should be placed in the root of your project, so that the CLI can automatically pick it up. It allows to:
Here is an example of a configuration file that specifies a database connection, registers a plugin, add custom routes using Hono and performs a Kysely query.
import type { BkndConfig } from "bknd/adapter";import { showRoutes } from "bknd/plugins";export default { connection: { url: process.env.DB_URL ?? "file:data.db", }, onBuilt: async (app) => { // `app.server` is a Hono instance const hono = app.server; hono.get("/hello", (c) => c.text("Hello World")); // for complex queries, you can use Kysely directly const db = app.connection.kysely; hono.get("/custom_query", async (c) => { return c.json(await db.selectFrom("pages").selectAll().execute()); }); }, options: { plugins: [showRoutes()], },} satisfies BkndConfig;
The connection property is the main connection object to the database. It can be either an object with libsql config or the actual Connection class.
// uses the default SQLite connection depending on the runtimeconst connection = { url: "<url>" };// the same as above, but more explicitimport { sqlite } from "bknd/adapter/sqlite";const connection = sqlite({ url: "<url>" });// Node.js SQLite, default on Node.jsimport { nodeSqlite } from "bknd/adapter/node";const connection = nodeSqlite({ url: "<url>" });// Bun SQLite, default on Bunimport { bunSqlite } from "bknd/adapter/bun";const connection = bunSqlite({ url: "<url>" });// LibSQL, default on Cloudflareimport { libsql } from "bknd";const connection = libsql({ url: "<url>" });
See a full list of available connections in the Database section. Alternatively, you can pass an instance of a Connection class directly, see Custom Connection as a reference.
If the connection object is omitted, the app will try to use an in-memory database.
As configuration, you can either pass a partial configuration object or a complete one
with a version number. The version number is used to automatically migrate the configuration up
to the latest version upon boot (db mode only). The default configuration looks like this:
The app property is a function that returns a CreateAppConfig object. It allows accessing the adapter specific environment variables. This is especially useful when using the Cloudflare Workers runtime, where the environment variables are only available inside the request handler.
The beforeBuild property is an async function that is called before the app is built. It allows to modify the app instance that may influence the build process.
import type { BkndConfig } from "bknd/adapter";export default { beforeBuild: async (app: App) => { // do something that has to happen before the app is built },};
The onBuilt property is an async function that is called after the app has been built. It allows to hook into the app after it has been built. This is useful for defining event listeners or register custom routes, as both the event manager and the server are recreated during the build process.
import { type App, AppEvents } from "bknd";import type { BkndConfig } from "bknd/adapter";export default { onBuilt: async (app: App) => { console.log("App built", app.version()); // registering an event listener app.emgr.onEvent(AppEvents.AppRequest, (event) => { console.log("Request received", event.request.url); }); // registering a custom route app.server.get("/hello", (c) => c.text("Hello World")); },};
The plugins property is an array of functions that are called after the app has been built,
but before its event is emitted. This is useful for adding custom routes or other functionality.
A simple plugin that adds a custom route looks like this:
Since each plugin has full access to the app instance, it can add routes, modify the database
structure, add custom middlewares, respond to or add events, etc. Plugins are very powerful, so
make sure to only run trusted ones.
The seed function will only be executed on app's first boot in "db" mode. If a configuration already exists in the database, or in "code" mode, it will not be executed.
The seed property is a function that is called when the app is booted for the first time. It is used to seed the database with initial data. The function is passed a ModuleBuildContext object:
Runtime adapters need additional configuration to serve static assets for the admin UI.
export type RuntimeBkndConfig<Args = any> = BkndConfig<Args> & { // the path to the dist folder to serve static assets for the admin UI distPath?: string; // custom middleware to serve static assets for the admin UI serveStatic?: MiddlewareHandler | [string, MiddlewareHandler]; // options for the admin UI adminOptions?: AdminControllerOptions | false;};
Framework adapters may need additional configuration based on the framework's requirements. For example, the NextjsBkndConfig type extends the BkndConfig type with the following additional properties:
type NextjsEnv = NextApiRequest["env"];export type NextjsBkndConfig<Env = NextjsEnv> = FrameworkBkndConfig<Env> & { cleanRequest?: { searchParams?: string[] };};
Next.js adds the mounted path to the request object, so that the cleanRequest property can be used to remove the mounted path from the request URL. See other frameworks for more information on how to configure them.
The configuration file is automatically picked up if you're using the CLI. This allows interacting with your application using the bknd command. For example, you can run the following command in the root of your project to start an instance:
npx bknd run
When serving your application, you need to make sure to import the contents of your configuration file. If you're using Next.js for example, it's recommended to follow these steps:
create a bknd.config.ts file in the root of your project which defines the connection to the database, adds event listeners and custom routes.
create a bknd.ts file inside your app folder which exports helper functions to instantiate the bknd instance and retrieve the API.
create a catch-all route file at src/api/[[...bknd]]/route.ts which serves the bknd API.
This way, your application and the CLI are using the same configuration.