Start adding some database code
This commit is contained in:
parent
a25655f28b
commit
eeca7eaece
95
src/db/init.ts
Normal file
95
src/db/init.ts
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
import { readdir } from 'node:fs/promises';
|
||||||
|
import { openPostMarkdownFile, parsePostMetadata } from '@blog/post/post-reader';
|
||||||
|
import { PostMeta } from '@blog/model/PostMeta';
|
||||||
|
import db from '@blog/db/memory';
|
||||||
|
|
||||||
|
const createPostSql: string = `
|
||||||
|
CREATE TABLE posts (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
slug TEXT NOT NULL,
|
||||||
|
is_draft INTEGER DEFAULT 0,
|
||||||
|
description TEXT,
|
||||||
|
published_date DATE,
|
||||||
|
raw_content TEXT
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
|
||||||
|
const createAuthorSql: string = `
|
||||||
|
CREATE TABLE authors (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
name TEXT,
|
||||||
|
email TEXT
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
|
||||||
|
const createTagsSql: string = `
|
||||||
|
CREATE TABLE tags (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
tag TEXT UNIQUE
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
|
||||||
|
const createPostsTagsSql: string = `
|
||||||
|
CREATE TABLE posts_tags (
|
||||||
|
tag_id TEXT,
|
||||||
|
post_id INTEGER,
|
||||||
|
|
||||||
|
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
|
||||||
|
const createPostHtmlCache = `
|
||||||
|
CREATE TABLE post_html_cache (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
post_id INTEGER,
|
||||||
|
content TEXT,
|
||||||
|
|
||||||
|
FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
`;
|
||||||
|
|
||||||
|
export async function setupDb(): Promise<void> {
|
||||||
|
console.log({ message: "Setting up the Database..." });
|
||||||
|
console.log({ message: "Creating the Posts table..." });
|
||||||
|
db.query(createPostSql).run();
|
||||||
|
|
||||||
|
console.log({ message: "Creating Authors table..." });
|
||||||
|
db.query(createAuthorSql).run();
|
||||||
|
|
||||||
|
console.log({ message: "Creating Tags table..." });
|
||||||
|
db.query(createTagsSql).run();
|
||||||
|
|
||||||
|
console.log({ message: "Creating Post and Tags relationships..." });
|
||||||
|
db.query(createPostsTagsSql).run();
|
||||||
|
|
||||||
|
console.log({ message: "Creating HTML cache table..." });
|
||||||
|
db.query(createPostHtmlCache).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function readPostsToDatabase(postPath: string): Promise<void> {
|
||||||
|
const posts = new Array();
|
||||||
|
const createPostSql: string = `
|
||||||
|
INSERT INTO posts (title, slug, description, is_draft, published_date, raw_content)
|
||||||
|
VALUES ($title, $slug, $description, $is_draft, $published_date, $raw_content);
|
||||||
|
`;
|
||||||
|
|
||||||
|
const fileList = await readdir(postPath);
|
||||||
|
for (const file of fileList) {
|
||||||
|
const contents: string = await openPostMarkdownFile(postPath + "/" + file);
|
||||||
|
const post = parsePostMetadata(contents);
|
||||||
|
|
||||||
|
const postQuery = db.prepare(createPostSql);
|
||||||
|
postQuery.run({
|
||||||
|
$title: post.meta.title,
|
||||||
|
$slug: file,
|
||||||
|
$description: post.meta.description,
|
||||||
|
$is_draft: post.meta.draft ?? false,
|
||||||
|
$published_date: post.meta.date.toString(),
|
||||||
|
$raw_content: post.content
|
||||||
|
});
|
||||||
|
console.log({ message: "Added " + post.meta.title + " to the database." });
|
||||||
|
}
|
||||||
|
}
|
4
src/db/memory.ts
Normal file
4
src/db/memory.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import { Database } from 'bun:sqlite';
|
||||||
|
|
||||||
|
const db = new Database(":memory:");
|
||||||
|
export default db;
|
@ -4,6 +4,7 @@ import { Page } from '@blog/templates/Page';
|
|||||||
import home from '@blog/routes/home';
|
import home from '@blog/routes/home';
|
||||||
import posts from '@blog/routes/posts';
|
import posts from '@blog/routes/posts';
|
||||||
import type { SiteMeta } from '@blog/model/SiteMeta';
|
import type { SiteMeta } from '@blog/model/SiteMeta';
|
||||||
|
import { setupDb, readPostsToDatabase } from '@blog/db/init';
|
||||||
|
|
||||||
declare module 'hono' {
|
declare module 'hono' {
|
||||||
interface ContextRenderer {
|
interface ContextRenderer {
|
||||||
@ -11,10 +12,13 @@ declare module 'hono' {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = new Hono();
|
async function main() {
|
||||||
|
console.log({ message: "Starting the Blog application..." });
|
||||||
|
const app = new Hono();
|
||||||
|
|
||||||
// Render the JSX views
|
// Render the JSX views
|
||||||
app.get(
|
console.log({ message: "Bootstrapping the view layer..." });
|
||||||
|
app.get(
|
||||||
'*',
|
'*',
|
||||||
jsxRenderer(
|
jsxRenderer(
|
||||||
({ children, meta }) => {
|
({ children, meta }) => {
|
||||||
@ -24,12 +28,22 @@ app.get(
|
|||||||
docType: true
|
docType: true
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// read all posts
|
// Bootstrap the Database
|
||||||
// create listing of posts
|
console.log({ message: "Bootstrapping the database..." });
|
||||||
|
setupDb();
|
||||||
|
await readPostsToDatabase(__dirname + "/../posts");
|
||||||
|
|
||||||
app.route('/', home);
|
// read all posts
|
||||||
app.route('/posts', posts);
|
// create listing of posts
|
||||||
|
|
||||||
|
console.log({ message: "Bootstrapping the routes..." });
|
||||||
|
app.route('/', home);
|
||||||
|
app.route('/posts', posts);
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default main();
|
||||||
|
|
||||||
export default app;
|
|
||||||
|
@ -31,7 +31,6 @@ export function parsePostMetadata(post: string): PostMetaWithRawContent {
|
|||||||
|
|
||||||
export async function getPostList(path: string): Promise<PostMeta[]> {
|
export async function getPostList(path: string): Promise<PostMeta[]> {
|
||||||
const postList: PostMeta[] = new Array<PostMeta>();
|
const postList: PostMeta[] = new Array<PostMeta>();
|
||||||
console.log(path);
|
|
||||||
const dir = await readdir(path);
|
const dir = await readdir(path);
|
||||||
|
|
||||||
if (!dir.length) {
|
if (!dir.length) {
|
||||||
|
Loading…
Reference in New Issue
Block a user