blog.hareworks.net/src/routes/+page.server.ts

47 lines
1.1 KiB
TypeScript

import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import PG from '$lib/server/database';
let data: {
recent: {
title: string,
image: string,
date: string,
link: string,
tags: string[]
}[],
tags: string[],
updated: string,
} = {
recent: [],
tags: [],
updated: "",
};
export const load: PageServerLoad = async ({ params }) => {
const db = await PG();
await db.begin();
try {
const recent_articles = await db.query(
"SELECT * FROM article WHERE publish = 'public' ORDER BY updated_at DESC LIMIT 6"
);
data.recent = recent_articles.rows.map((row) => ({
title: row.title,
image: row.image,
date: row.updated_at.toISOString().slice(0, 10),
link: `/article/${row.category}/${row.id}`,
tags: row.tags,
}));
const tags = await db.query("SELECT * FROM tag ORDER BY ref_count DESC LIMIT 20");
data.tags = tags.rows.map((row) => row.name);
} catch (e) {
await db.rollback();
throw error(500, e as Error);
} finally {
await db.release();
}
data.updated = data.recent[0].date;
return data;
}