mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
optimized local api instantiation to prepare for nextjs app router
This commit is contained in:
68
examples/nextjs/src/bknd.ts
Normal file
68
examples/nextjs/src/bknd.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { App, type LocalApiOptions } from "bknd";
|
||||
import { type NextjsBkndConfig, getApp as getBkndApp } from "bknd/adapter/nextjs";
|
||||
import { boolean, em, entity, text } from "bknd/data";
|
||||
import { secureRandomString } from "bknd/utils";
|
||||
|
||||
// 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 config = {
|
||||
// we can use any libsql config, and if omitted, uses in-memory
|
||||
connection: {
|
||||
url: "http://localhost:8080"
|
||||
},
|
||||
// an initial config is only applied if the database is empty
|
||||
initialConfig: {
|
||||
data: schema.toJSON(),
|
||||
// we're enabling auth ...
|
||||
auth: {
|
||||
enabled: true,
|
||||
jwt: {
|
||||
secret: secureRandomString(64)
|
||||
}
|
||||
}
|
||||
},
|
||||
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: "ds@bknd.io",
|
||||
password: "12345678"
|
||||
});
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
} as const satisfies NextjsBkndConfig;
|
||||
|
||||
export async function getApp() {
|
||||
return await getBkndApp(config);
|
||||
}
|
||||
|
||||
export async function getApi(options?: LocalApiOptions) {
|
||||
const app = await getApp();
|
||||
return await app.getApi(options);
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
import { App } from "bknd";
|
||||
import { config as bkndConfig } from "@/bknd";
|
||||
import { serve } from "bknd/adapter/nextjs";
|
||||
import { boolean, em, entity, text } from "bknd/data";
|
||||
import { secureRandomString } from "bknd/utils";
|
||||
|
||||
export const config = {
|
||||
runtime: "edge",
|
||||
@@ -12,57 +10,9 @@ export const config = {
|
||||
unstable_allowDynamic: ["**/*.js"]
|
||||
};
|
||||
|
||||
// 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 serve({
|
||||
// we can use any libsql config, and if omitted, uses in-memory
|
||||
connection: {
|
||||
url: "http://localhost:8080"
|
||||
cleanRequest: {
|
||||
searchParams: ["route"]
|
||||
},
|
||||
// an initial config is only applied if the database is empty
|
||||
initialConfig: {
|
||||
data: schema.toJSON(),
|
||||
// we're enabling auth ...
|
||||
auth: {
|
||||
enabled: true,
|
||||
jwt: {
|
||||
secret: secureRandomString(64)
|
||||
}
|
||||
}
|
||||
},
|
||||
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: "ds@bknd.io",
|
||||
password: "12345678"
|
||||
});
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
...bkndConfig
|
||||
});
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { withApi } from "bknd/adapter/nextjs";
|
||||
import { getApi } from "@/bknd";
|
||||
import type { InferGetServerSidePropsType } from "next";
|
||||
|
||||
export const getServerSideProps = withApi(async (context) => {
|
||||
const { data = [] } = await context.api.data.readMany("todos");
|
||||
const user = context.api.getUser();
|
||||
export const getServerSideProps = async () => {
|
||||
const api = await getApi();
|
||||
const { data = [] } = await api.data.readMany("todos");
|
||||
const user = api.getUser();
|
||||
|
||||
return { props: { data, user } };
|
||||
});
|
||||
};
|
||||
|
||||
export default function Home({
|
||||
data,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { App } from "bknd";
|
||||
import { App, type LocalApiOptions } from "bknd";
|
||||
import { registerLocalMediaAdapter } from "bknd/adapter/node";
|
||||
import { type RemixBkndConfig, getApp as getBkndApp } from "bknd/adapter/remix";
|
||||
import { boolean, em, entity, text } from "bknd/data";
|
||||
@@ -76,17 +76,7 @@ export async function getApp(args?: { request: Request }) {
|
||||
return await getBkndApp(config, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* If args are given, it will use authentication details from the request
|
||||
* @param args
|
||||
*/
|
||||
export async function getApi(args?: { request: Request }) {
|
||||
const app = await getApp(args);
|
||||
if (args) {
|
||||
const api = app.getApi(args.request);
|
||||
await api.verifyAuth();
|
||||
return api;
|
||||
}
|
||||
|
||||
return app.getApi();
|
||||
export async function getApi(options?: LocalApiOptions) {
|
||||
const app = await getApp();
|
||||
return await app.getApi(options);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user