From a5b00f3bb27400408539372565cf935bf0ef6ad3 Mon Sep 17 00:00:00 2001 From: Hare Date: Tue, 20 Aug 2024 23:13:27 +0900 Subject: [PATCH] feat: add a database pool and initialize table --- src/lib/database.ts | 18 ------- src/lib/index.ts | 1 - src/lib/server/database.ts | 82 +++++++++++++++++++++++++++++ src/lib/server/database/tablectl.ts | 30 +++++++++++ src/routes/+page.server.ts | 2 +- 5 files changed, 113 insertions(+), 20 deletions(-) delete mode 100644 src/lib/database.ts create mode 100644 src/lib/server/database.ts create mode 100644 src/lib/server/database/tablectl.ts diff --git a/src/lib/database.ts b/src/lib/database.ts deleted file mode 100644 index afbc561..0000000 --- a/src/lib/database.ts +++ /dev/null @@ -1,18 +0,0 @@ -import pg from 'pg'; -const { Pool } = pg; - -import { - DB_USER, - DB_PASS, - DB_HOST, - PG_PORT, - DB_DATABASE, -} from '$env/static/private' -const connectionString = `postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:${PG_PORT}/${DB_DATABASE}`; - -const pool = new Pool({ - connectionString: connectionString, - max: 2, -}); - -export default pool; diff --git a/src/lib/index.ts b/src/lib/index.ts index 856f2b6..e69de29 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1 +0,0 @@ -// place files you want to import through the `$lib` alias in this folder. diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts new file mode 100644 index 0000000..0950f79 --- /dev/null +++ b/src/lib/server/database.ts @@ -0,0 +1,82 @@ +import pg from 'pg'; +const { Pool } = pg; +import * as tc from './database/tablectl'; + +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}`; + +export const pool = new Pool({ + connectionString: connectionString, + max: 2, +}); + +// initialize +// import fs from 'fs'; +// type ArticleFileItem = { +// path: string, +// id: string, +// } +const schemas: tc.TableSchema[] = [ + { + name: 'article', + columns: [ + { + name: 'id', + type: 'serial', + constraint: 'primary key', + }, + { + name: 'title', + type: 'text', + constraint: 'not null', + }, + { + name: 'content', + type: 'text', + constraint: 'not null', + }, + ], + }, + { + name: 'fuga', + columns: [ + { + name: 'id', + type: 'serial', + constraint: 'primary key', + }, + { + name: 'name', + type: 'text', + constraint: 'not null', + }, + { + name: 'email', + type: 'text', + constraint: 'not null', + }, + { + name: 'password', + type: 'text', + constraint: 'not null', + }, + ], + }, +]; +(async () => { + for (const schema of schemas) { + const exists = await tc.is_table_exists(pool, schema.name); + if (!exists) { + console.log(`Creating table ${schema.name}`); + await tc.create_table(pool, schema); + } else { + console.log(`Table ${schema.name} already exists`); + } + } +})(); \ No newline at end of file diff --git a/src/lib/server/database/tablectl.ts b/src/lib/server/database/tablectl.ts new file mode 100644 index 0000000..c602b95 --- /dev/null +++ b/src/lib/server/database/tablectl.ts @@ -0,0 +1,30 @@ +import pg from 'pg'; + +export type TableSchema = { + name: string, + columns: { + name: string, + type: string, + constraint: string, + }[], +} + +export const is_table_exists = async (pool: pg.Pool, name: string) => { + const query = `select * from information_schema.tables where table_name = '${name}'`; + const res = await pool.query(query); + return res.rows.length > 0; +} + +export const create_table = async (pool: pg.Pool, schema: TableSchema) => { + const { name, columns } = schema; + const columnStr = columns.map(c => `${c.name} ${c.type} ${c.constraint}`).join(', '); + const query = `create table ${name} (${columnStr})`; + try { + await pool.query(query); + } catch (err) { + console.log(query); + console.error(err); + } +} + + diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index a8dae8b..cf7ca20 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; // import index from '$lib/index'; import type { Content } from '$lib/article'; -import pool from '$lib/database'; +import { pool } from '$lib/server/database'; let data: { recent: Content[],