refactored adapters to run test suites (#126)

* refactored adapters to run test suites

* fix bun version for tests

* added missing adapter tests and refactored examples to use `bknd.config.ts` where applicable
This commit is contained in:
dswbx
2025-04-01 11:43:11 +02:00
committed by GitHub
parent 36e4224b33
commit 3f26c45dd9
55 changed files with 1130 additions and 647 deletions

View File

@@ -0,0 +1,65 @@
import type { AstroBkndConfig } from "bknd/adapter/astro";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils";
// since we're running in node, we can register the local media adapter
const local = registerLocalMediaAdapter();
// the em() function makes it easy to create an initial schema
const schema = em({
todos: entity("todos", {
title: text(),
done: boolean(),
}),
});
// register your schema to get automatic type completion
type Database = (typeof schema)["DB"];
declare module "bknd/core" {
interface DB extends Database {}
}
export default {
// we can use any libsql config, and if omitted, uses in-memory
app: (env) => ({
connection: {
url: env.DB_URL ?? "file:data.db",
},
}),
// an initial config is only applied if the database is empty
initialConfig: {
data: schema.toJSON(),
// we're enabling auth ...
auth: {
enabled: true,
jwt: {
issuer: "bknd-astro-example",
secret: secureRandomString(64),
},
},
// ... and media
media: {
enabled: true,
adapter: local({
path: "./public",
}),
},
},
options: {
// the seed option is only executed if the database was empty
seed: async (ctx) => {
// create some entries
await ctx.em.mutator("todos").insertMany([
{ title: "Learn bknd", done: true },
{ title: "Build something cool", done: false },
]);
// and create a user
await ctx.app.module.auth.createUser({
email: "test@bknd.io",
password: "12345678",
});
},
},
} as const satisfies AstroBkndConfig;

View File

@@ -0,0 +1,23 @@
import type { AstroGlobal } from "astro";
import { getApp as getBkndApp } from "bknd/adapter/astro";
import config from "../bknd.config";
export { config };
export async function getApp() {
return await getBkndApp(config);
}
export async function getApi(
astro: AstroGlobal,
opts?: { mode: "static" } | { mode?: "dynamic"; verify?: boolean },
) {
const app = await getApp();
if (opts?.mode !== "static" && opts?.verify) {
const api = app.getApi({ headers: astro.request.headers });
await api.verifyAuth();
return api;
}
return app.getApi();
}

View File

@@ -2,9 +2,9 @@
import { Admin } from "bknd/ui";
import "bknd/dist/styles.css";
import { getApi } from "bknd/adapter/astro";
import { getApi } from "../../bknd";
const api = await getApi(Astro, { mode: "dynamic" });
const api = await getApi(Astro, { verify: true });
const user = api.getUser();
export const prerender = false;

View File

@@ -1,77 +1,6 @@
import type { APIContext } from "astro";
import { App } from "bknd";
import { serve } from "bknd/adapter/astro";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils";
import { config } from "../../bknd";
export const prerender = false;
// since we're running in node, we can register the local media adapter
registerLocalMediaAdapter();
// the em() function makes it easy to create an initial schema
const schema = em({
todos: entity("todos", {
title: text(),
done: boolean(),
}),
});
// register your schema to get automatic type completion
type Database = (typeof schema)["DB"];
declare module "bknd/core" {
interface DB extends Database {}
}
export const ALL = serve<APIContext>({
// we can use any libsql config, and if omitted, uses in-memory
connection: {
url: "file:data.db",
},
// an initial config is only applied if the database is empty
initialConfig: {
data: schema.toJSON(),
// we're enabling auth ...
auth: {
enabled: true,
jwt: {
issuer: "bknd-astro-example",
secret: secureRandomString(64),
},
},
// ... and media
media: {
enabled: true,
adapter: {
type: "local",
config: {
path: "./public",
},
},
},
},
options: {
// the seed option is only executed if the database was empty
seed: async (ctx) => {
await ctx.em.mutator("todos").insertMany([
{ title: "Learn bknd", done: true },
{ title: "Build something cool", done: false },
]);
},
},
// here we can hook into the app lifecycle events ...
beforeBuild: async (app) => {
app.emgr.onEvent(
App.Events.AppFirstBoot,
async () => {
// ... to create an initial user
await app.module.auth.createUser({
email: "test@bknd.io",
password: "12345678",
});
},
"sync",
);
},
});
export const ALL = serve<APIContext>(config);

View File

@@ -1,5 +1,5 @@
---
import { getApi } from "bknd/adapter/astro";
import { getApi } from "../bknd";
import Card from "../components/Card.astro";
import Layout from "../layouts/Layout.astro";

View File

@@ -1,8 +1,8 @@
---
import { getApi } from "bknd/adapter/astro";
import { getApi } from "../bknd";
import Card from "../components/Card.astro";
import Layout from "../layouts/Layout.astro";
const api = await getApi(Astro, { mode: "dynamic" });
const api = await getApi(Astro, { verify: true });
const { data } = await api.data.readMany("todos");
const user = api.getUser();