2024-09-06 22:25:56
This commit is contained in:
parent
9d15398a7c
commit
78c0fdfb75
|
@ -1,10 +0,0 @@
|
||||||
---
|
|
||||||
title: Obsidian LiveSync
|
|
||||||
released_at: null
|
|
||||||
updated_at: null
|
|
||||||
tags: []
|
|
||||||
image: /uploads/
|
|
||||||
publish: public hidden
|
|
||||||
---
|
|
||||||
<script>
|
|
||||||
</script>
|
|
|
@ -43,13 +43,13 @@ SvelteKitはページをプリレンダリングするために、ビルド時
|
||||||
リクエスト時に初めてモジュールが評価される場合でも、ページの`+page.server.ts`からモジュールを利用していると、モジュールの初期化によって接続が要求されエラーとなります。
|
リクエスト時に初めてモジュールが評価される場合でも、ページの`+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 });
|
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();}
|
export default async () => { return await pool.connect();}
|
||||||
```
|
```
|
||||||
|
|
||||||
```ts: +page.server.ts
|
```ts : +page.server.ts
|
||||||
import PG from '$lib/server/database';
|
import PG from '$lib/server/database';
|
||||||
const db = await PG()
|
const db = await PG()
|
||||||
await db.query("hogehoge...")
|
await db.query("hogehoge...")
|
||||||
|
@ -63,7 +63,7 @@ https://kit.svelte.dev/docs/hooks
|
||||||
|
|
||||||
ここでは、プールを使った実装で、プールを取得するモジュールを定義し、そのプールを返す関数としてexportします。
|
ここでは、プールを使った実装で、プールを取得するモジュールを定義し、そのプールを返す関数としてexportします。
|
||||||
hookを用いてリクエスト時にデータを渡してやることで、load関数から`locals.db`としてpoolにアクセスすることができます。
|
hookを用いてリクエスト時にデータを渡してやることで、load関数から`locals.db`としてpoolにアクセスすることができます。
|
||||||
```ts: lib/server/database/get_connection.ts
|
```ts : lib/server/database/get_connection.ts
|
||||||
import pg from 'pg';
|
import pg from 'pg';
|
||||||
const { Pool } = pg;
|
const { Pool } = pg;
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ export const getConnection = () => pool;
|
||||||
// DB初期化の処理
|
// DB初期化の処理
|
||||||
```
|
```
|
||||||
|
|
||||||
```ts: hook.server.ts
|
```ts : hook.server.ts
|
||||||
import type { Handle } from '@sveltejs/kit'
|
import type { Handle } from '@sveltejs/kit'
|
||||||
import { getConnection } from '$lib/server/database/get_connection'
|
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 }) => {
|
export const load: PageServerLoad = async ({ params, locals }) => {
|
||||||
// locals.dbでアクセス
|
// locals.dbでアクセス
|
||||||
const client = locals.db.connect();
|
const client = locals.db.connect();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user