libsql stuff

main
Ethan Niser 2023-09-14 23:39:42 -05:00
parent f50e0afe1b
commit 38f842dcda
7 changed files with 45 additions and 4 deletions

5
.gitignore vendored

@ -168,4 +168,7 @@ dist
.yarn/install-state.gz .yarn/install-state.gz
.pnp.\* .pnp.\*
tsconfig.tsbuildinfo tsconfig.tsbuildinfo
local.sqlite
local.sqlite-shm
local.sqlite-wal

Binary file not shown.

@ -24,10 +24,11 @@
}, },
"dependencies": { "dependencies": {
"@bogeychan/elysia-logger": "0.0.9", "@bogeychan/elysia-logger": "0.0.9",
"@elysiajs/cron": "^0.6.0",
"@elysiajs/static": "^0.6.0", "@elysiajs/static": "^0.6.0",
"@elysiajs/swagger": "^0.6.2", "@elysiajs/swagger": "^0.6.2",
"@kitajs/html": "^2.1.2", "@kitajs/html": "^2.1.2",
"@libsql/client": "^0.3.4", "@libsql/client": "0.3.5-pre.4",
"@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",
"drizzle-orm": "^0.28.6", "drizzle-orm": "^0.28.6",

@ -6,6 +6,7 @@ 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(),
NODE_ENV: z.enum(["development", "production"]), NODE_ENV: z.enum(["development", "production"]),
COOKIE_SECRET: z.string().min(1), COOKIE_SECRET: z.string().min(1),
}, },

@ -2,10 +2,11 @@ import { Elysia } from "elysia";
// import { logger } from "@bogeychan/elysia-logger"; // import { logger } from "@bogeychan/elysia-logger";
// import pretty from "pino-pretty"; // import pretty from "pino-pretty";
import { config } from "../config"; import { config } from "../config";
import { db } from "../db"; import { client, db } from "../db";
import "@kitajs/html/register"; import "@kitajs/html/register";
import "@kitajs/html/htmx"; import "@kitajs/html/htmx";
import { auth } from "../auth"; import { auth } from "../auth";
// import { cron } from "@elysiajs/cron";
// const stream = pretty({ // const stream = pretty({
// colorize: true, // colorize: true,
@ -24,6 +25,21 @@ export const ctx = new Elysia({
// stream, // stream,
// }) // })
// ) // )
// .use(
// cron({
// name: "heartbeat",
// pattern: "*/1 * * * * *",
// run() {
// if (config.env.SYNC_URL) {
// const now = performance.now();
// console.log("Syncing database...");
// void client.sync().then(() => {
// console.log(`Database synced in ${performance.now() - now}ms`);
// });
// }
// },
// })
// )
.decorate("db", db) .decorate("db", db)
.decorate("config", config) .decorate("config", config)
.decorate("auth", auth) .decorate("auth", auth)

@ -3,7 +3,7 @@ import { ctx } from "../context";
import { insertTodoSchema, todos } from "../db/schema/todos"; import { insertTodoSchema, todos } from "../db/schema/todos";
import { TodoItem, TodoForm, TodoList } from "../components/todos"; import { TodoItem, TodoForm, TodoList } from "../components/todos";
import { db } from "../db"; import { client, db } from "../db";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
export const todosController = new Elysia({ export const todosController = new Elysia({
@ -11,7 +11,9 @@ export const todosController = new Elysia({
}) })
.use(ctx) .use(ctx)
.get("/", async () => { .get("/", async () => {
const now = performance.now();
const data = await db.select().from(todos).limit(10); const data = await db.select().from(todos).limit(10);
console.log("queried in", performance.now() - now);
return <TodoList todos={data} />; return <TodoList todos={data} />;
}) })
.post( .post(
@ -36,6 +38,10 @@ export const todosController = new Elysia({
throw new Error("Todo not found"); throw new Error("Todo not found");
} }
client.sync();
console.log("returning");
return <TodoItem {...newTodo} />; return <TodoItem {...newTodo} />;
}, },
{ {
@ -65,14 +71,25 @@ export const todosController = new Elysia({
sub: "Subscribe to Ethan", sub: "Subscribe to Ethan",
}; };
const now = performance.now();
const [newTodo] = await db const [newTodo] = await db
.insert(todos) .insert(todos)
.values({ content: content[body.content] }) .values({ content: content[body.content] })
.returning(); .returning();
console.log("inserted in", performance.now() - now);
if (!newTodo) { if (!newTodo) {
throw new Error("Todo not found"); throw new Error("Todo not found");
} }
// const now2 = performance.now();
// client.sync().then(() => {
// console.log("synced in", performance.now() - now2);
// console.log("total time", performance.now() - now);
// });
console.log("returning");
console.log("total time", performance.now() - now);
return <TodoItem {...newTodo} />; return <TodoItem {...newTodo} />;
}, },

@ -6,6 +6,9 @@ import { config } from "../config";
export const client = createClient({ export const client = createClient({
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();
export const db = drizzle(client, { schema, logger: true }); export const db = drizzle(client, { schema, logger: true });