diff --git a/.gitignore b/.gitignore
index 463fc53..65392ed 100644
--- a/.gitignore
+++ b/.gitignore
@@ -168,4 +168,7 @@ dist
.yarn/install-state.gz
.pnp.\*
-tsconfig.tsbuildinfo
\ No newline at end of file
+tsconfig.tsbuildinfo
+local.sqlite
+local.sqlite-shm
+local.sqlite-wal
\ No newline at end of file
diff --git a/bun.lockb b/bun.lockb
index 5fd71ff..fad0b3f 100755
Binary files a/bun.lockb and b/bun.lockb differ
diff --git a/package.json b/package.json
index 080db02..a3161db 100644
--- a/package.json
+++ b/package.json
@@ -24,10 +24,11 @@
},
"dependencies": {
"@bogeychan/elysia-logger": "0.0.9",
+ "@elysiajs/cron": "^0.6.0",
"@elysiajs/static": "^0.6.0",
"@elysiajs/swagger": "^0.6.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",
"@t3-oss/env-core": "^0.6.1",
"drizzle-orm": "^0.28.6",
diff --git a/src/config/index.ts b/src/config/index.ts
index 47e14df..b2080da 100644
--- a/src/config/index.ts
+++ b/src/config/index.ts
@@ -6,6 +6,7 @@ const env = createEnv({
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]),
DATABASE_URL: z.string().min(1),
DATABASE_AUTH_TOKEN: z.string().min(1),
+ SYNC_URL: z.string().optional(),
NODE_ENV: z.enum(["development", "production"]),
COOKIE_SECRET: z.string().min(1),
},
diff --git a/src/context/index.ts b/src/context/index.ts
index 006fe76..9527a6f 100644
--- a/src/context/index.ts
+++ b/src/context/index.ts
@@ -2,10 +2,11 @@ import { Elysia } from "elysia";
// import { logger } from "@bogeychan/elysia-logger";
// import pretty from "pino-pretty";
import { config } from "../config";
-import { db } from "../db";
+import { client, db } from "../db";
import "@kitajs/html/register";
import "@kitajs/html/htmx";
import { auth } from "../auth";
+// import { cron } from "@elysiajs/cron";
// const stream = pretty({
// colorize: true,
@@ -24,6 +25,21 @@ export const ctx = new Elysia({
// 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("config", config)
.decorate("auth", auth)
diff --git a/src/controllers/todos.tsx b/src/controllers/todos.tsx
index 206cafa..96017f1 100644
--- a/src/controllers/todos.tsx
+++ b/src/controllers/todos.tsx
@@ -3,7 +3,7 @@ import { ctx } from "../context";
import { insertTodoSchema, todos } from "../db/schema/todos";
import { TodoItem, TodoForm, TodoList } from "../components/todos";
-import { db } from "../db";
+import { client, db } from "../db";
import { eq } from "drizzle-orm";
export const todosController = new Elysia({
@@ -11,7 +11,9 @@ export const todosController = new Elysia({
})
.use(ctx)
.get("/", async () => {
+ const now = performance.now();
const data = await db.select().from(todos).limit(10);
+ console.log("queried in", performance.now() - now);
return ;
})
.post(
@@ -36,6 +38,10 @@ export const todosController = new Elysia({
throw new Error("Todo not found");
}
+ client.sync();
+
+ console.log("returning");
+
return ;
},
{
@@ -65,14 +71,25 @@ export const todosController = new Elysia({
sub: "Subscribe to Ethan",
};
+ const now = performance.now();
const [newTodo] = await db
.insert(todos)
.values({ content: content[body.content] })
.returning();
+ console.log("inserted in", performance.now() - now);
+
if (!newTodo) {
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 ;
},
diff --git a/src/db/index.ts b/src/db/index.ts
index e65065a..a6c8752 100644
--- a/src/db/index.ts
+++ b/src/db/index.ts
@@ -6,6 +6,9 @@ import { config } from "../config";
export const client = createClient({
url: config.env.DATABASE_URL,
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 });