From 13462b5f8acbf62440d0810730d6c3be83aaf33a Mon Sep 17 00:00:00 2001 From: Akis Date: Fri, 6 Jan 2023 21:23:14 +0200 Subject: [PATCH] fix --- src/lib/db.ts | 26 ---------------- src/lib/server/db.ts | 30 +++++++++++++++++++ src/routes/+page.server.ts | 2 +- .../admin/announcements/+page.server.ts | 2 +- 4 files changed, 32 insertions(+), 28 deletions(-) delete mode 100644 src/lib/db.ts create mode 100644 src/lib/server/db.ts diff --git a/src/lib/db.ts b/src/lib/db.ts deleted file mode 100644 index 318ed83..0000000 --- a/src/lib/db.ts +++ /dev/null @@ -1,26 +0,0 @@ -import knex from "knex"; -import { env } from "$env/dynamic/private"; - -const db = knex({ - client: "pg", - connection: { - host: String(env.DB_HOST), - port: Number(env.DB_PORT), - user: String(env.DB_USERNAME), - password: String(env.DB_PASSWORD), - database: "website" - } -}) - -if (! await db.schema.hasTable("Announcements")) { - await db.schema.createTable("Announcements", (table) => { - table.increments("id"); - table.text("title").notNullable(); - table.string("severity").notNullable(); - table.string("author").notNullable(); - table.string("link").nullable(); - table.bigInteger("created").notNullable(); - }); -} - -export default db; \ No newline at end of file diff --git a/src/lib/server/db.ts b/src/lib/server/db.ts new file mode 100644 index 0000000..d5a4e23 --- /dev/null +++ b/src/lib/server/db.ts @@ -0,0 +1,30 @@ +import knex from "knex"; +import type { Knex } from "knex"; +import { env } from "$env/dynamic/private"; +import { building } from "$app/environment"; + +export let db: Knex; + +if (!building) { + db = knex({ + client: "pg", + connection: { + host: String(env.DB_HOST), + port: Number(env.DB_PORT), + user: String(env.DB_USERNAME), + password: String(env.DB_PASSWORD), + database: "website" + } + }) + + if (! await db.schema.hasTable("Announcements")) { + await db.schema.createTable("Announcements", (table) => { + table.increments("id"); + table.text("title").notNullable(); + table.string("severity").notNullable(); + table.string("author").notNullable(); + table.string("link").nullable(); + table.bigInteger("created").notNullable(); + }); + } +} \ No newline at end of file diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 98b5cc1..67ff49c 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,7 +1,7 @@ import type { PageServerLoad } from "./$types"; import { compile } from "mdsvex"; import sanitizeHtml from "sanitize-html"; -import db from "$lib/db"; +import { db } from "$lib/server/db"; export const load: PageServerLoad = async () => { diff --git a/src/routes/admin/announcements/+page.server.ts b/src/routes/admin/announcements/+page.server.ts index 0605b4b..a833d3e 100644 --- a/src/routes/admin/announcements/+page.server.ts +++ b/src/routes/admin/announcements/+page.server.ts @@ -1,7 +1,7 @@ import type { Actions } from "./$types"; import Joi from "joi"; import { fail } from "@sveltejs/kit"; -import db from "$lib/db"; +import { db } from "$lib/server/db"; export const actions: Actions = { add: async ({ request, locals }) => {