make it buildable to db system #2

Merged
Hare merged 5 commits from develop into master 2024-08-28 00:15:50 +09:00
8 changed files with 109 additions and 87 deletions
Showing only changes of commit d3cc3145e0 - Show all commits

6
src/app.d.ts vendored
View File

@ -1,9 +1,13 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
import { Database } from 'pg';
declare global {
namespace App {
// interface Error {}
// interface Locals {}
interface Locals {
db: any;
}
// interface PageData {}
// interface PageState {}
// interface Platform {}

10
src/hooks.server.ts Normal file
View File

@ -0,0 +1,10 @@
import type { Handle } from '@sveltejs/kit'
import { getConnection } from '$lib/server/database/get_connection'
export const handle: Handle = async ({ event, resolve }) => {
const pg = getConnection();
event.locals.db = pg;
const result = await resolve(event);
return result;
}

View File

@ -1,21 +1,12 @@
import pg from 'pg';
const { Pool } = pg;
import { Pool } from 'pg';
import { getConnection } from './database/get_connection';
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 });
class Postgres {
export class Postgres {
client: pg.PoolClient | null = null;
public static async new() {
public static async new(pool: Pool) {
const pg = new Postgres();
pg.client = await pool.connect();
pg.client = await getConnection().connect();
return pg;
}
@ -40,8 +31,10 @@ class Postgres {
}
}
export default async () => { return await Postgres.new(); }
export default async (
pool: Pool
) => { return await Postgres.new(pool); };
import { building } from '$app/environment';
import init from '$lib/server/database/init_db';
await init();
if (!building) await init(await Postgres.new(getConnection()));

View File

@ -0,0 +1,15 @@
import pg from 'pg';
const { Pool } = pg;
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 const getConnection = () => pool;

View File

@ -1,10 +1,9 @@
// initialize
import PG from '$lib/server/database';
import type { Postgres } from '$lib/server/database';
import fs from 'fs';
import { compile } from 'mdsvex';
export default async function init() {
export default async function init(db: Postgres) {
// Create tables(when not exists)
const schemas = [
{
@ -73,9 +72,9 @@ export default async function init() {
}
];
const db = await PG();
try {
await db.begin();
try {
// Create tables
for (const schema of schemas) {
const res = await db.query(`select * from information_schema.tables where table_name = '${schema.name}'`)
if (res.rowCount == 0) {
@ -87,10 +86,6 @@ export default async function init() {
}
}
await db.commit();
} catch (err) {
console.error(err);
await db.rollback();
}
const articleFiles: ArticleFileItem[] = [];
function scanDir(path: string) {
@ -146,9 +141,14 @@ export default async function init() {
}
}
}
await db.commit();
} catch (err) {
console.error(err);
await db.rollback();
} finally {
await db.release();
}
}
type ArticleFileItem = {

View File

@ -18,8 +18,8 @@ let data: {
updated: "",
};
export const load: PageServerLoad = async ({ params }) => {
const db = await PG();
export const load: PageServerLoad = async ({ params, locals }) => {
const db = await PG(locals.db);
await db.begin();
try {
const recent_articles = await db.query(

View File

@ -3,11 +3,11 @@ import type { PageServerLoad } from './$types';
import PG from '$lib/server/database';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params }) => {
export const load: PageServerLoad = async ({ params, locals }) => {
const { category, id } = params;
console.log(id);
const db = await PG();
const db = await PG(locals.db);
await db.begin();
try {
const article = await db.query(

View File

@ -3,11 +3,11 @@ import type { PageServerLoad } from './$types';
import PG from '$lib/server/database';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params }) => {
export const load: PageServerLoad = async ({ params, locals }) => {
const { category, id, series } = params;
console.log(id);
const db = await PG();
const db = await PG(locals.db);
await db.begin();
try {
const article = await db.query(