feat: add a database pool and initialize table

This commit is contained in:
Keisuke Hirata 2024-08-20 23:13:27 +09:00
parent 2727a08b0f
commit a5b00f3bb2
5 changed files with 113 additions and 20 deletions

View File

@ -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;

View File

@ -1 +0,0 @@
// place files you want to import through the `$lib` alias in this folder.

View File

@ -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`);
}
}
})();

View File

@ -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);
}
}

View File

@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
// import index from '$lib/index'; // import index from '$lib/index';
import type { Content } from '$lib/article'; import type { Content } from '$lib/article';
import pool from '$lib/database'; import { pool } from '$lib/server/database';
let data: { let data: {
recent: Content[], recent: Content[],