Remove the database code, focus on flat file blog.
This commit is contained in:
parent
e0adc0e684
commit
a036cb76a2
BIN
db/blog.sqlite
BIN
db/blog.sqlite
Binary file not shown.
110
src/db/init.ts
110
src/db/init.ts
@ -1,110 +0,0 @@
|
|||||||
import { PostFileService } from '@blog/services/post-file';
|
|
||||||
import { db } from '@blog/services/database';
|
|
||||||
|
|
||||||
const createPostSql: string = `
|
|
||||||
CREATE TABLE IF NOT EXISTS posts (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
title TEXT NOT NULL,
|
|
||||||
slug TEXT NOT NULL UNIQUE,
|
|
||||||
is_draft INTEGER DEFAULT 0,
|
|
||||||
description TEXT,
|
|
||||||
published_date DATE,
|
|
||||||
raw_content TEXT
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
|
|
||||||
const createAuthorSql: string = `
|
|
||||||
CREATE TABLE IF NOT EXISTS authors (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
name TEXT,
|
|
||||||
email TEXT
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
|
|
||||||
const createTagsSql: string = `
|
|
||||||
CREATE TABLE IF NOT EXISTS tags (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
tag TEXT UNIQUE
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
|
|
||||||
const createPostsTagsSql: string = `
|
|
||||||
CREATE TABLE IF NOT EXISTS 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 IF NOT EXISTS 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(): Promise<void> {
|
|
||||||
const postService = await PostFileService.create();
|
|
||||||
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 createPostHtmlCacheSql: string = `
|
|
||||||
INSERT INTO post_html_cache (post_id, content)
|
|
||||||
VALUES ($posId, $content);`;
|
|
||||||
|
|
||||||
for (const [ slug, post ] of postService.getPosts().entries()) {
|
|
||||||
const postQuery = db.prepare(createPostSql);
|
|
||||||
const { lastInsertRowId } = postQuery.run({
|
|
||||||
$title: post.meta.title,
|
|
||||||
$slug: slug,
|
|
||||||
$description: post.meta.description,
|
|
||||||
$is_draft: post.meta.draft ?? false,
|
|
||||||
$published_date: post.meta.date.toString(),
|
|
||||||
$raw_content: post.content
|
|
||||||
});
|
|
||||||
|
|
||||||
const htmlCacheQuery = db.prepare(createPostHtmlCacheSql);
|
|
||||||
htmlCacheQuery.run({ $postId: lastInsertRowId, $content: post.html })
|
|
||||||
|
|
||||||
console.log({ message: "Added " + post.meta.title + " to the database." });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
await setupDb();
|
|
||||||
await readPostsToDatabase();
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
.then(() => process.exit(0))
|
|
||||||
.catch(e => {
|
|
||||||
console.log(e);
|
|
||||||
throw e;
|
|
||||||
});
|
|
@ -1,4 +0,0 @@
|
|||||||
import { Database } from 'bun:sqlite';
|
|
||||||
|
|
||||||
const db = new Database(":memory:");
|
|
||||||
export default db;
|
|
@ -1,3 +0,0 @@
|
|||||||
import { Database } from "bun:sqlite";
|
|
||||||
import { SQLITE_DATABASE_FILE } from "@blog/config";
|
|
||||||
export const db = new Database(SQLITE_DATABASE_FILE);
|
|
@ -1,29 +0,0 @@
|
|||||||
import { Post } from '@blog/models/Post';
|
|
||||||
import { db } from '@blog/services/database';
|
|
||||||
|
|
||||||
export class PostRepository {
|
|
||||||
public async getPost(id: number): Promise<Post> {
|
|
||||||
const queryString = `
|
|
||||||
SELECT title, slug, is_draft, description, published_date, raw_content
|
|
||||||
FROM posts
|
|
||||||
WHERE id = $id
|
|
||||||
`;
|
|
||||||
const query = db.query(queryString);
|
|
||||||
const results = query.get({ $id: id });
|
|
||||||
|
|
||||||
return {
|
|
||||||
meta: {
|
|
||||||
title: results.title,
|
|
||||||
slug: results.slug,
|
|
||||||
description: results.description,
|
|
||||||
date: new Date(results.published_date),
|
|
||||||
draft: results.is_draft,
|
|
||||||
},
|
|
||||||
content: results.raw_content,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getPostBySlug(slug: string): Post {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user