Skip to content

BrainDB

BrainDB is a library that allows you to treat your content as a database. It can be used to:

Related:

Installation

  1. Install dependencies

    Terminal window
    pnpm add @braindb/core @braindb/astro
  2. Configure Astro

    astro.config.mjs
    import { brainDbAstro } from "@braindb/astro";
    export default defineConfig({
    integrations: [brainDbAstro()],
    });

Without plugin

If you need to use BrainDB without the plugin, you can do it like this:

src/lib/braindb.mjs
import { slug as githubSlug } from "github-slugger";
import path from "node:path";
import process from "node:process";
import { BrainDB } from "@braindb/core";
// slug implementation according to Astro
// see astro/packages/astro/src/content/utils.ts
function generateSlug(filePath) {
const withoutFileExt = filePath.replace(
new RegExp(path.extname(filePath) + "$"),
""
);
const rawSlugSegments = withoutFileExt.split(path.sep);
const slug = rawSlugSegments
// Slugify each route segment to handle capitalization and spaces.
// Note: using `slug` instead of `new Slugger()` means no slug deduping.
.map((segment) => githubSlug(segment))
.join("/")
.replace(/\/index$/, "");
return slug;
}
function slugToUrl(slug) {
if (!slug.startsWith("/")) slug = "/" + slug;
if (!slug.endsWith("/")) slug = slug + "/";
return slug;
}
const start = new Date().getTime();
export const bdb = new BrainDB({
root: path.resolve(process.cwd(), "src/content/docs"),
url: (filePath, frontmatter) => {
if (frontmatter.slug !== undefined) return slugToUrl(frontmatter.slug);
return slugToUrl(generateSlug(filePath));
},
git: process.cwd(),
});
bdb.start();