Database
Choosing the right database configuration
In order to use bknd, you need to prepare access information to your database and potentially install additional dependencies. Connections to the database are managed using Kysely. Therefore, all its dialects are theoretically supported.
Currently supported and tested databases are:
- SQLite (embedded): Node.js SQLite, Bun SQLite, LibSQL, SQLocal
- SQLite (remote): Turso, Cloudflare D1
- Postgres: Vanilla Postgres, Supabase, Neon, Xata
By default, bknd will try to use a SQLite database in-memory. Depending on your runtime, a different SQLite implementation will be used.
Defining the connection
There are mainly 3 ways to define the connection to your database, when
- creating an app using
App.create()
orcreateApp()
- creating an app using a Framework or Runtime adapter
- starting a quick instance using the CLI
When creating an app using App.create()
or createApp()
, you can pass a connection object in the configuration object.
When using an adapter, or using the CLI, bknd will automatically try to use a SQLite implementation depending on the runtime:
You can also pass a connection instance to the connection
property to explictly use a specific connection.
If you’re using bknd.config.*
, you can specify the connection on the exported object.
Throughout the documentation, it is assumed you use bknd.config.ts
to define your connection.
SQLite
Using config object
When run with Node.js, a version of 22 (LTS) or higher is required. Please verify your version by running node -v
, and upgrade if necessary.
The sqlite
adapter is automatically resolved based on the runtime.
Runtime | Adapter | In-Memory | File | Remote |
---|---|---|---|---|
Node.js | node:sqlite | ✅ | ✅ | ❌ |
Bun | bun:sqlite | ✅ | ✅ | ❌ |
Cloudflare Worker/Browser/Edge | libsql | 🟠 | 🟠 | ✅ |
The bundled version of the libsql
connection only works with remote databases. However, you can pass in a Client
from @libsql/client
, see LibSQL for more details.
LibSQL
Turso offers a SQLite-fork called LibSQL that runs a server around your SQLite database. The edge-version of the adapter is included in the bundle (remote only):
If you wish to use LibSQL as file, in-memory or make use of Embedded Replicas, you have to pass in the Client
from @libsql/client
:
Cloudflare D1
Using the Cloudflare Adapter, you can choose to use a D1 database binding. To do so, you only need to add a D1 database to your wrangler.toml
and it’ll pick up automatically.
To manually specify which D1 database to take, you can specify it explicitly:
SQLocal
To use bknd with sqlocal
for a offline expierence, you need to install the @bknd/sqlocal
package. You can do so by running the following command:
This package uses sqlocal
under the hood. Consult the sqlocal documentation for connection options:
PostgreSQL
To use bknd with Postgres, you need to install the @bknd/postgres
package. You can do so by running the following command:
You can connect to your Postgres database using pg
or postgres
dialects. Additionally, you may also define your custom connection.
Using pg
To establish a connection to your database, you can use any connection options available on the pg
package.
Using postgres
To establish a connection to your database, you can use any connection options available on the postgres
package.
Using custom connection
Several Postgres hosting providers offer their own clients to connect to their database, e.g. suitable for serverless environments.
Example using @neondatabase/serverless
:
Example using @xata.io/client
:
Custom Connection
Creating a custom connection is as easy as extending the Connection
class and passing constructing a Kysely instance.
Initial Structure
To provide an initial database structure, you can pass initialConfig
to the creation of an app. This will only be used if there isn’t an existing configuration found in the database given. Here is a quick example:
The initial structure is only respected if the database is empty! If you made updates, ensure to delete the database first, or perform updates through the Admin UI.
Note that we didn’t add relational fields directly to the entity, but instead defined them afterwards. That is because the relations are managed outside the entity scope to have an unified expierence for all kinds of relations (e.g. many-to-many).
Defined relations are currently not part of the produced types for the structure. We’re working on that, but in the meantime, you can define them manually.
Type completion
To get type completion, there are two options:
- Use the CLI to generate the types
- If you have an initial structure created with the prototype functions, you can extend the
DB
interface with your own schema.
All entity related functions use the types defined in DB
from bknd/core
. To get type completion, you can extend that interface with your own schema:
The type completion is available for the API as well as all provided React hooks.
Seeding the database
To seed your database with initial data, you can pass a seed
function to the configuration. It
provides the ModuleBuildContext
as the first argument.
Note that the seed function will only be executed on app’s first boot. If a configuration already exists in the database, it will not be executed.