feat: add a database pool and initialize table
This commit is contained in:
parent
2727a08b0f
commit
a5b00f3bb2
|
@ -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;
|
|
@ -1 +0,0 @@
|
|||
// place files you want to import through the `$lib` alias in this folder.
|
82
src/lib/server/database.ts
Normal file
82
src/lib/server/database.ts
Normal 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`);
|
||||
}
|
||||
}
|
||||
})();
|
30
src/lib/server/database/tablectl.ts
Normal file
30
src/lib/server/database/tablectl.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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[],
|
||||
|
|
Loading…
Reference in New Issue
Block a user