From 78c0fdfb7562503108886e2a631ec36d1d035106 Mon Sep 17 00:00:00 2001 From: Hare Date: Fri, 6 Sep 2024 22:25:56 +0900 Subject: [PATCH] 2024-09-06 22:25:56 --- article/tech/obsidian-livesync.md | 10 ---------- article/tech/sveltekit-db-build-error.md | 10 +++++----- 2 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 article/tech/obsidian-livesync.md diff --git a/article/tech/obsidian-livesync.md b/article/tech/obsidian-livesync.md deleted file mode 100644 index b01083c..0000000 --- a/article/tech/obsidian-livesync.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -title: Obsidian LiveSync -released_at: null -updated_at: null -tags: [] -image: /uploads/ -publish: public hidden ---- - diff --git a/article/tech/sveltekit-db-build-error.md b/article/tech/sveltekit-db-build-error.md index 3ae1cfd..5404e0b 100644 --- a/article/tech/sveltekit-db-build-error.md +++ b/article/tech/sveltekit-db-build-error.md @@ -43,13 +43,13 @@ SvelteKitはページをプリレンダリングするために、ビルド時 リクエスト時に初めてモジュールが評価される場合でも、ページの`+page.server.ts`からモジュールを利用していると、モジュールの初期化によって接続が要求されエラーとなります。 ## だめな実装 -```ts: database.ts +```ts : database.ts import { PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_DB, } from '$env/static/private' const connectionString = `postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}`; const pool = new Pool({ connectionString }); export default async () => { return await pool.connect();} ``` -```ts: +page.server.ts +```ts : +page.server.ts import PG from '$lib/server/database'; const db = await PG() await db.query("hogehoge...") @@ -63,7 +63,7 @@ https://kit.svelte.dev/docs/hooks ここでは、プールを使った実装で、プールを取得するモジュールを定義し、そのプールを返す関数としてexportします。 hookを用いてリクエスト時にデータを渡してやることで、load関数から`locals.db`としてpoolにアクセスすることができます。 -```ts: lib/server/database/get_connection.ts +```ts : lib/server/database/get_connection.ts import pg from 'pg'; const { Pool } = pg; @@ -75,7 +75,7 @@ export const getConnection = () => pool; // DB初期化の処理 ``` -```ts: hook.server.ts +```ts : hook.server.ts import type { Handle } from '@sveltejs/kit' import { getConnection } from '$lib/server/database/get_connection' @@ -89,7 +89,7 @@ export const handle: Handle = async ({ event, resolve }) => { } ``` -```ts: +page.server.ts +```ts : +page.server.ts export const load: PageServerLoad = async ({ params, locals }) => { // locals.dbでアクセス const client = locals.db.connect();