idek just sync it

main
Ethan Niser 2023-09-19 23:34:03 -05:00
parent d530f4f178
commit 342d294dbc
13 changed files with 59 additions and 106 deletions

Binary file not shown.

@ -13,7 +13,8 @@
"uno:dev": "bunx --bun unocss --watch", "uno:dev": "bunx --bun unocss --watch",
"typecheck": "bunx --bun tsc", "typecheck": "bunx --bun tsc",
"format:check": "prettier --check .", "format:check": "prettier --check .",
"format": "prettier --write . --list-different" "format": "prettier --write . --list-different",
"reset-cache": "rm -rf /home/whatplan/.bun/install/cache && rm -rf node_modules && bun i"
}, },
"devDependencies": { "devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^4.1.0", "@ianvs/prettier-plugin-sort-imports": "^4.1.0",
@ -22,7 +23,7 @@
"@unocss/transformer-variant-group": "^0.55.7", "@unocss/transformer-variant-group": "^0.55.7",
"bun-types": "latest", "bun-types": "latest",
"concurrently": "^8.2.1", "concurrently": "^8.2.1",
"drizzle-kit": "^0.19.13", "drizzle-kit": "latest",
"prettier": "^3.0.3", "prettier": "^3.0.3",
"prettier-plugin-tailwindcss": "^0.5.4", "prettier-plugin-tailwindcss": "^0.5.4",
"typescript": "^5.2.2", "typescript": "^5.2.2",
@ -34,11 +35,11 @@
"@elysiajs/static": "^0.6.0", "@elysiajs/static": "^0.6.0",
"@elysiajs/swagger": "^0.6.2", "@elysiajs/swagger": "^0.6.2",
"@iconify-json/logos": "^1.1.37", "@iconify-json/logos": "^1.1.37",
"@libsql/client": "0.3.5-pre.4", "@libsql/client": "0.3.5-pre.8",
"@lucia-auth/adapter-sqlite": "^2.0.0", "@lucia-auth/adapter-sqlite": "^2.0.0",
"@t3-oss/env-core": "^0.6.1", "@t3-oss/env-core": "^0.6.1",
"@tlscipher/holt": "^1.0.4", "@tlscipher/holt": "^1.0.4",
"beth-stack": "0.0.18", "beth-stack": "0.0.20",
"drizzle-orm": "^0.28.6", "drizzle-orm": "^0.28.6",
"drizzle-typebox": "^0.1.1", "drizzle-typebox": "^0.1.1",
"elysia": "0.7.0-beta.1", "elysia": "0.7.0-beta.1",

@ -1,6 +1,6 @@
import { libsql } from "@lucia-auth/adapter-sqlite"; import { libsql } from "@lucia-auth/adapter-sqlite";
import { lucia } from "lucia"; import { lucia } from "lucia";
import { web } from "lucia/middleware"; import { elysia, web } from "lucia/middleware";
import { config } from "../config"; import { config } from "../config";
import { client } from "../db"; import { client } from "../db";

@ -6,9 +6,10 @@ const env = createEnv({
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]), LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]),
DATABASE_URL: z.string().min(1), DATABASE_URL: z.string().min(1),
DATABASE_AUTH_TOKEN: z.string().min(1), DATABASE_AUTH_TOKEN: z.string().min(1),
// SYNC_URL: z.string().optional(), SYNC_URL: z.string().optional(),
NODE_ENV: z.enum(["development", "production"]), NODE_ENV: z.enum(["development", "production"]),
COOKIE_SECRET: z.string().min(1), COOKIE_SECRET: z.string().min(1),
TURSO_API_TOKEN: z.string().min(1),
}, },
runtimeEnv: process.env, runtimeEnv: process.env,
}); });

@ -1,5 +1,5 @@
import { logger } from "@bogeychan/elysia-logger"; import { logger } from "@bogeychan/elysia-logger";
// import { HoltLogger } from "@tlscipher/holt"; import { HoltLogger } from "@tlscipher/holt";
import { bethStack } from "beth-stack/elysia"; import { bethStack } from "beth-stack/elysia";
import { Elysia } from "elysia"; import { Elysia } from "elysia";
import pretty from "pino-pretty"; import pretty from "pino-pretty";
@ -28,6 +28,7 @@ export const ctx = new Elysia({
stream, stream,
}), }),
) )
.use(new HoltLogger().getLogger())
// .use( // .use(
// cron({ // cron({
// name: "heartbeat", // name: "heartbeat",
@ -43,10 +44,10 @@ export const ctx = new Elysia({
// }, // },
// }) // })
// ) // )
// .decorate("db", db) .decorate("db", () => db)
.decorate("config", config) .decorate("config", config)
.decorate("auth", auth); .decorate("auth", auth);
// .onStart(({ log }) => log.info("Server starting")) // .onStart(({ log }) => log.info("Server starting"));
// .onStop(({ log }) => log.info("Server stopping")) // .onStop(({ log }) => log.info("Server stopping"))
// .onRequest(({ log, request }) => { // .onRequest(({ log, request }) => {
// log.debug(`Request received: ${request.method}: ${request.url}`); // log.debug(`Request received: ${request.method}: ${request.url}`);

@ -5,5 +5,6 @@ import { todosController } from "./todos";
export const api = new Elysia({ export const api = new Elysia({
prefix: "/api", prefix: "/api",
name: "@controllers",
}).use(todosController); }).use(todosController);
// .use(authController); // .use(authController);

@ -7,6 +7,7 @@ import { insertTodoSchema, todos } from "../db/schema/todos";
export const todosController = new Elysia({ export const todosController = new Elysia({
prefix: "/todos", prefix: "/todos",
name: "@controllers/todos",
}) })
.use(ctx) .use(ctx)
.get("/", async () => { .get("/", async () => {

@ -3,12 +3,19 @@ import { drizzle } from "drizzle-orm/libsql";
import { config } from "../config"; import { config } from "../config";
import * as schema from "./schema"; import * as schema from "./schema";
export const client = createClient({ const remoteOptions = {
url: config.env.DATABASE_URL, url: config.env.DATABASE_URL,
authToken: config.env.DATABASE_AUTH_TOKEN, authToken: config.env.DATABASE_AUTH_TOKEN,
// syncUrl: config.env.SYNC_URL, };
});
// if (config.env.SYNC_URL) await client.sync(); // const localOptions = {
// url: "file:local.sqlite",
// authToken: config.env.DATABASE_AUTH_TOKEN,
// syncUrl: config.env.DATABASE_URL,
// };
export const client = createClient(remoteOptions);
// await client.sync();
export const db = drizzle(client, { schema, logger: true }); export const db = drizzle(client, { schema, logger: true });

@ -1,12 +1,15 @@
// import { swagger } from "@elysiajs/swagger";
import { staticPlugin } from "@elysiajs/static"; import { staticPlugin } from "@elysiajs/static";
// import { swagger } from "@elysiajs/swagger";
import { Elysia } from "elysia"; import { Elysia } from "elysia";
import { config } from "./config"; import { config } from "./config";
import { api } from "./controllers/*"; import { api } from "./controllers/*";
import { pages } from "./pages/*"; import { pages } from "./pages/*";
const app = new Elysia() const app = new Elysia({
name: "@app/app",
})
// .use(swagger()) // .use(swagger())
//@ts-expect-error
.use(staticPlugin()) .use(staticPlugin())
.use(api) .use(api)
.use(pages) .use(pages)

@ -4,6 +4,8 @@ import { profile } from "./profile";
// import { signin } from "./signin"; // import { signin } from "./signin";
export const authGroup = new Elysia() export const authGroup = new Elysia({
name: "@pages/auth/root",
})
// .use(signup).use(signin) // .use(signup).use(signin)
.use(profile); .use(profile);

@ -2,7 +2,9 @@ import Elysia from "elysia";
import { BaseHtml } from "../../components/base"; import { BaseHtml } from "../../components/base";
import { ctx } from "../../context"; import { ctx } from "../../context";
export const profile = new Elysia() export const profile = new Elysia({
name: "@pages/auth/profile",
})
.use(ctx) .use(ctx)
.get("/profile", async ({ auth, html, request }) => { .get("/profile", async ({ auth, html, request }) => {
const authRequest = auth.handleRequest(request); const authRequest = auth.handleRequest(request);

@ -2,4 +2,8 @@ import Elysia from "elysia";
import { authGroup } from "./(auth)/*"; import { authGroup } from "./(auth)/*";
import { index } from "./index"; import { index } from "./index";
export const pages = new Elysia().use(index).use(authGroup); export const pages = new Elysia({
name: "@pages/root",
})
.use(index)
.use(authGroup);

@ -1,98 +1,28 @@
import { persistedCache, revalidateTag } from "beth-stack/cache";
import { renderToStream, renderToString, Suspense } from "beth-stack/jsx";
import { Elysia } from "elysia"; import { Elysia } from "elysia";
import { BaseHtml } from "../components/base"; import { BaseHtml } from "../components/base";
import { ctx } from "../context"; import { ctx } from "../context";
const start = Date.now(); export const index = new Elysia({
name: "@pages/index",
const getTime = async () => (Date.now() - start) / 1000; })
const cachedGetTime = persistedCache(getTime, "getTime", {
tags: ["time"],
revalidate: 2,
});
export const index = new Elysia()
.use(ctx) .use(ctx)
.onRequest(({ request }) => { .get("/", async ({ html, db, log, config }) => {
const revalidate = request.headers.get("HX-Revalidate"); const start = performance.now();
if (revalidate) { // const todos = await db().query.todos.findMany();
const tags = JSON.parse(revalidate); log.info(`query took ${performance.now() - start}ms`);
if (!Array.isArray(tags)) {
return; const res = await fetch("https://api.turso.tech/v1/groups", {
} headers: {
tags.forEach((tag) => { Authorization: `Bearer ${config.env.TURSO_API_TOKEN}`,
if (typeof tag !== "string") { },
return;
}
revalidateTag(tag);
}); });
} const json = await res.text();
})
.get("/test", async ({ html, log }) => {
log.info("test");
const time = await cachedGetTime();
return html(() => <p>{time}</p>);
})
.get("/", async ({ html }) => {
return html(() => ( return html(() => (
<BaseHtml> <BaseHtml>
<h1>cache revalidates every two seconds</h1> <h1>hi!</h1>
<button hx-get="/test" hx-target="#foo" hx-swap="beforeend"> {/* <p safe>{JSON.stringify(todos)}</p> */}
click me to get time since start (cached) <p safe>{JSON.stringify(json)}</p>
</button>
<br />
<div>hot reload</div>
<div class="i-logos-apple w-lg"></div>
<br />
<button
hx-get="/test"
hx-target="#foo"
hx-swap="beforeend"
hx-revalidate="time"
>
click me to get time since start (revalidate now)
</button>
<div id="foo"></div>
</BaseHtml> </BaseHtml>
)); ));
})
.get("/test2", async ({ htmlStream }) => {
return htmlStream(() => <App2 />);
}); });
function wait(ms: number): Promise<number> {
return new Promise((resolve) =>
setTimeout(() => {
resolve(ms);
}, ms),
);
}
export async function Wait({ ms }: { ms: number }) {
const data = await wait(ms);
return <div>loaded in: {data}ms</div>;
}
const App2 = () => (
<BaseHtml>
<div>
<p>I am sent immediately</p>
<Suspense fallback={<div>Loading...</div>}>
<Wait ms={1000} />
<div>hello</div>
</Suspense>
<p>hey me too!</p>
<Suspense fallback={<div>loading 2...</div>}>
<Wait ms={2000} />
<div>hello two!</div>
<Suspense fallback={<div>loading 3...</div>}>
<Wait ms={3000} />
<div>hello three!</div>
</Suspense>
</Suspense>
</div>
</BaseHtml>
);