diff --git a/.gitignore b/.gitignore index 44d1be4..183a072 100644 --- a/.gitignore +++ b/.gitignore @@ -177,4 +177,6 @@ dist # prisma prisma/**/migrations/ prisma/*.db -prisma/*-journal \ No newline at end of file +prisma/*-journal + +dev.db \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index ee733c6..d515b3a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,17 +4,15 @@ LABEL maintainer="BreizhHardware" # Add your application files ADD .env /app/.env -ADD prisma /app/prisma -ADD bun.lockb /app/bun.lockb ADD index.ts /app/index.ts ADD noDb.ts /app/noDb.ts -ADD package-lock.json /app/package-lock.json ADD package.json /app/package.json ADD tsconfig.json /app/tsconfig.json +ADD bun.lockb /app/bun.lock # Change directory and run bun commands WORKDIR /app RUN bun install -CMD ["bun", "dev"] EXPOSE 8080 +CMD ["bun", "dev:db"] diff --git a/bun.lockb b/bun.lockb index 13d32c4..c07e369 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/index.ts b/index.ts index 10fe085..7074419 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,8 @@ -import { PrismaClient } from '@prisma/client' -const prisma = new PrismaClient(); +import { Database } from "bun:sqlite"; + +const db = new Database(Bun.env.BUN_DB_FILE || "./bun.db"); + +db.query("CREATE TABLE IF NOT EXISTS Url (url TEXT)").run(); Bun.serve({ port: Bun.env.PORT || 8080, @@ -10,13 +13,13 @@ Bun.serve({ if (req.body) { const { u, password } = await req.json() as { u: string, password: string }; if (u && password === Bun.env.ADMIN_PASSWORD) { - const uu = await prisma.url.create({ - data: { - url: u - } - }) + const query = db.query("INSERT INTO Url (url) VALUES ($u) RETURNING *"); - return new Response(JSON.stringify({body: uu}), { + const results = query.get({ + $u: u, + }); + + return new Response(JSON.stringify({body: results}), { status: 200 }) } @@ -25,7 +28,7 @@ Bun.serve({ if (req.body) { const { password } = await req.json() as { password: string }; if (password === Bun.env.ADMIN_PASSWORD) { - await prisma.url.deleteMany(); + db.query("DELETE FROM Url").run(); return new Response(JSON.stringify({body: "ok"}), { status: 200 }) @@ -35,7 +38,7 @@ Bun.serve({ if (req.body) { const {password} = await req.json() as { password: string }; if (password === Bun.env.ADMIN_PASSWORD) { - const urls = await prisma.url.findMany(); + const urls = db.query("SELECT * FROM Url").all(); return new Response(JSON.stringify({body: urls}), { status: 200 }) @@ -45,11 +48,9 @@ Bun.serve({ if (req.body) { const { u, password } = await req.json() as { u: string, password: string }; if (u && password === Bun.env.ADMIN_PASSWORD) { - await prisma.url.deleteMany({ - where: { - url: u - } - }) + db.query("DELETE FROM Url WHERE url = $u").run({ + $u: u, + }); return new Response(JSON.stringify({body: u}), { status: 200 }) @@ -59,8 +60,7 @@ Bun.serve({ } else if (req.method === "GET") { // random - const urls = await prisma.url.findMany(); - + const urls = db.query("SELECT * FROM Url").all() as any[]; const urlRes = urls[Math.floor(Math.random() * urls.length)]; if (urlRes) { @@ -76,4 +76,8 @@ Bun.serve({ status: 400 }) } -}) \ No newline at end of file +}) + +process.on("exit", async () => { + db.close(); +}); \ No newline at end of file diff --git a/package.json b/package.json index d61c019..053acf6 100644 --- a/package.json +++ b/package.json @@ -3,15 +3,11 @@ "module": "index.ts", "type": "module", "devDependencies": { - "@types/bun": "^1.0.4", - "prisma": "^5.8.1" + "@types/bun": "^1.0.4" }, "peerDependencies": { "typescript": "^5.0.0" }, - "dependencies": { - "@prisma/client": "5.8.1" - }, "scripts": { "dev": "bun run noDb.ts", "dev:db": "bun run index.ts", diff --git a/prisma/schema.prisma b/prisma/schema.prisma deleted file mode 100644 index 1886007..0000000 --- a/prisma/schema.prisma +++ /dev/null @@ -1,18 +0,0 @@ -// This is your Prisma schema file, -// learn more about it in the docs: https://pris.ly/d/prisma-schema - -generator client { - provider = "prisma-client-js" -} - -datasource db { - provider = "sqlite" - url = env("DATABASE_URL") -} - -model Url { - id Int @id @default(autoincrement()) - url String - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt -} \ No newline at end of file