add sqlocal connection including example

This commit is contained in:
dswbx
2025-03-15 14:40:41 +01:00
parent 5697b7891a
commit 622a7b2b9a
22 changed files with 1047 additions and 56 deletions

View File

@@ -0,0 +1,71 @@
import { useEffect, useState } from "react";
import { App } from "bknd";
import { Admin } from "bknd/ui";
import { checksum } from "bknd/utils";
import { em, entity, text } from "bknd/data";
import { SQLocalConnection } from "@bknd/sqlocal";
import "bknd/dist/styles.css";
export default function () {
const [app, setApp] = useState<App | undefined>(undefined);
const [hash, setHash] = useState<string>("");
async function onBuilt(app: App) {
setApp(app);
setHash(await checksum(app.toJSON()));
}
useEffect(() => {
setup({
onBuilt,
})
.then((app) => console.log("setup", app?.version()))
.catch(console.error);
}, []);
if (!app) return null;
return (
// @ts-ignore
<Admin key={hash} withProvider={{ api: app.getApi() }} />
);
}
let initialized = false;
export async function setup(opts?: {
beforeBuild?: (app: App) => Promise<void>;
onBuilt?: (app: App) => Promise<void>;
}) {
if (initialized) return;
initialized = true;
const connection = new SQLocalConnection({
verbose: true,
});
const app = App.create({
connection,
initialConfig: {
data: em({
test: entity("test", {
name: text(),
}),
}).toJSON(),
},
});
if (opts?.onBuilt) {
app.emgr.onEvent(
App.Events.AppBuiltEvent,
async () => {
await opts.onBuilt?.(app);
},
"sync",
);
}
await opts?.beforeBuild?.(app);
await app.build({ sync: true });
return app;
}

View File

@@ -0,0 +1,9 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.tsx";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
</StrictMode>,
);

1
examples/react/src/vite-env.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
/// <reference types="vite/client" />