updated examples: astro, nextjs, remix, bun, node

This commit is contained in:
dswbx
2024-12-23 16:50:26 +01:00
parent a17fd2df67
commit 70e42a02d7
31 changed files with 319 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
{
"_variables": {
"lastUpdateCheck": 1732785435939
"lastUpdateCheck": 1734966049246
}
}

View File

@@ -21,4 +21,5 @@ pnpm-debug.log*
.DS_Store
# jetbrains setting folder
.idea/
.idea/
*.db

View File

@@ -14,7 +14,7 @@ export const prerender = false;
<body>
<Admin
withProvider={{ user }}
config={{ basepath: "/admin", color_scheme: "dark" }}
config={{ basepath: "/admin", color_scheme: "dark", logo_return_path: "/../" }}
client:only
/>
</body>

View File

@@ -1,12 +1,70 @@
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";
export const prerender = false;
// since we're running in node, we can register the local media adapter
registerLocalMediaAdapter();
export const ALL = serve({
// we can use any libsql config, and if omitted, uses in-memory
connection: {
type: "libsql",
config: {
url: "file:test.db"
}
},
// an initial config is only applied if the database is empty
initialConfig: {
// the em() function makes it easy to create an initial schema
data: em({
todos: entity("todos", {
title: text(),
done: boolean()
})
}).toJSON(),
// we're enabling auth ...
auth: {
enabled: true,
jwt: {
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: "ds@bknd.io",
password: "12345678"
});
},
"sync"
);
}
});

Binary file not shown.

View File

@@ -38,3 +38,4 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts
!test.db

View File

@@ -1,5 +1,7 @@
import { withApi } from "bknd/adapter/nextjs";
import type { InferGetServerSidePropsType } from "next";
import dynamic from "next/dynamic";
import { withApi } from "bknd/adapter/nextjs";
import "bknd/dist/styles.css";
const Admin = dynamic(() => import("bknd/ui").then((mod) => mod.Admin), {
@@ -14,7 +16,11 @@ export const getServerSideProps = withApi(async (context) => {
};
});
export default function AdminPage() {
export default function AdminPage({
user
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
if (typeof document === "undefined") return null;
return <Admin withProvider config={{ basepath: "/admin" }} />;
return (
<Admin withProvider={{ user }} config={{ basepath: "/admin", logo_return_path: "/../" }} />
);
}

View File

@@ -1,7 +1,7 @@
import { serve } from "bknd/adapter/nextjs";
export const config = {
runtime: "experimental-edge",
runtime: "edge",
// add a matcher for bknd dist to allow dynamic otherwise build may fail.
// inside this repo it's '../../app/dist/index.js', outside probably inside node_modules
// see https://github.com/vercel/next.js/issues/51401

View File

@@ -3,3 +3,4 @@ node_modules
/.cache
/build
.env
*.db

View File

@@ -35,8 +35,9 @@ export const loader = async (args: LoaderFunctionArgs) => {
// add api to the context
args.context.api = api;
await api.verifyAuth();
return {
user: api.getAuthState().user
user: api.getAuthState()?.user
};
};

View File

@@ -7,7 +7,7 @@ export const meta: MetaFunction = () => {
export const loader = async (args: LoaderFunctionArgs) => {
const api = args.context.api;
const user = api.getAuthState().user;
const user = (await api.getVerifiedAuthState()).user;
const { data } = await api.data.readMany("todos");
return { data, user };
};

View File

@@ -3,6 +3,7 @@ import "bknd/dist/styles.css";
export default adminPage({
config: {
basepath: "/admin"
basepath: "/admin",
logo_return_path: "/../"
}
});

View File

@@ -1,11 +1,70 @@
import { App } from "bknd";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { serve } from "bknd/adapter/remix";
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
registerLocalMediaAdapter();
const handler = serve({
// we can use any libsql config, and if omitted, uses in-memory
connection: {
type: "libsql",
config: {
url: "file:test.db"
}
},
// an initial config is only applied if the database is empty
initialConfig: {
// the em() function makes it easy to create an initial schema
data: em({
todos: entity("todos", {
title: text(),
done: boolean()
})
}).toJSON(),
// we're enabling auth ...
auth: {
enabled: true,
jwt: {
issuer: "bknd-remix-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: "ds@bknd.io",
password: "12345678"
});
},
"sync"
);
}
});

View File

@@ -12,17 +12,17 @@
"typecheck": "tsc"
},
"dependencies": {
"@remix-run/node": "^2.14.0",
"@remix-run/react": "^2.14.0",
"@remix-run/serve": "^2.14.0",
"@remix-run/node": "^2.15.2",
"@remix-run/react": "^2.15.2",
"@remix-run/serve": "^2.15.2",
"bknd": "workspace:*",
"isbot": "^4.1.0",
"isbot": "^5.1.18",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"remix-utils": "^7.7.0"
"remix-utils": "^8.0.0"
},
"devDependencies": {
"@remix-run/dev": "^2.14.0",
"@remix-run/dev": "^2.15.2",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"typescript": "^5.1.6",

Binary file not shown.