bknd logo
Runtimes

Deno

Run bknd inside Deno

Installation

To get started with Deno and bknd you can either install the package manually, and follow the descriptions below, or use the CLI starter:

CLI Starter

Create a new Deno CLI starter project by running the following command:

deno run npm:bknd create -i deno

Manual

Deno is fully supported as a runtime for bknd. If you plan to solely use the API, the setup is pretty straightforward.

main.ts
import { createAdapterApp } from "npm:bknd/adapter";

const app = await createAdapterApp({
   connection: {
      url: "file:data.db",
   },
});

export default {
   fetch: app.fetch,
};

Serve the Admin UI

In order to also serve the static assets of the admin UI, you have 3 choices:

  1. Use the serveStaticViaImport function to serve the static assets from the bknd package directly. Requires unstable raw-imports, but it's the easiest way to serve the static assets.
  2. Copy the static assets to your local project and use Hono's serveStatic middleware.
  3. Use the adminOptions.assetsPath property to point to a remote address with the static assets.

serveStaticViaImport

The serveStaticViaImport function is a middleware that serves the static assets from the bknd package directly using dynamic raw imports. It requires the unstable raw-imports feature to be enabled. You can enable it by adding the following to your deno.json:

deno.json
{
   "unstable": ["raw-imports"]
}

Or by using the --unstable-raw-imports flag when running your script. Now create a main.ts file to serve the API and static assets:

main.ts
import { createRuntimeApp, serveStaticViaImport } from "bknd/adapter";

const app = await createRuntimeApp({
   connection: {
      url: "file:data.db",
   },
   serveStatic: serveStaticViaImport()
});

export default {
   fetch: app.fetch,
};

In case you don't want to point your bknd dependency to the latest version, either add an imports section to your deno.json file:

deno.json
{
   "imports": {
      "bknd": "npm:bknd@<VERSION>"
   }
}

Or specify the package with the version specified to the serveStaticViaImport function:

const app = await createRuntimeApp({
   serveStatic: serveStaticViaImport({
      package: "bknd@<VERSION>", 
   }),
});

Replace <VERSION> with the version you want to use.

serveStatic from local files

You can also serve the static assets from your local project by using Hono's serveStatic middleware. You can do so by copying the static assets to your local project and using the serveStatic middleware. First, you have to copy the static assets, by running the following command:

deno run npm:bknd copy-assets --out public

This will copy the static assets to the public directory and then serve them from there:

main.ts
import { createRuntimeApp, serveStatic } from "bknd/adapter";
import { serveStatic } from "npm:hono/deno";

const app = await createRuntimeApp({
   connection: {
      url: "file:data.db",
   },
   serveStatic: serveStatic({
      root: "./public",
   }),
});

export default {
   fetch: app.fetch,
};

adminOptions.assetsPath

You can also use the adminOptions.assetsPath property to point to a remote address with the static assets. This is useful in case none of the other methods work for you.

main.ts
import { createRuntimeApp } from "bknd/adapter";

const app = await createRuntimeApp({
   connection: {
      url: "file:data.db",
   },
   adminOptions: {
      assetsPath: "https://...",
   },
});

export default {
   fetch: app.fetch,
};