73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { Database } from 'bun:sqlite';
|
|
import path from 'path';
|
|
|
|
// Initialize the database if it doesn't exist
|
|
const dbPath = path.join(process.cwd(), 'blog_metadata.sqlite');
|
|
const db = new Database(dbPath);
|
|
|
|
// Create the posts table if it doesn't exist
|
|
db.run(`
|
|
CREATE TABLE IF NOT EXISTS posts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
path TEXT UNIQUE NOT NULL,
|
|
title TEXT,
|
|
date TEXT,
|
|
author TEXT,
|
|
tags TEXT,
|
|
published INTEGER,
|
|
summary TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Load metadata from the blog post into a SQLite database
|
|
// This allows us to index and make queries against the metadata
|
|
// to support functions like search, filtering, and sorting
|
|
export function loadMetadata(filePath: string, data: { [key: string]: any }) {
|
|
if (!data) return;
|
|
|
|
try {
|
|
// Extract common fields
|
|
const { title, date, author, tags, published, summary } = data;
|
|
|
|
// Log the values for debugging
|
|
// console.log('Metadata values:', {
|
|
// filePath: typeof filePath,
|
|
// title: typeof title,
|
|
// date: typeof date,
|
|
// author: typeof author,
|
|
// tags: typeof tags,
|
|
// published: typeof published,
|
|
// summary: typeof summary
|
|
// });
|
|
|
|
// Convert all values to strings or null explicitly
|
|
const values = [
|
|
filePath ? String(filePath) : null,
|
|
title ? String(title) : null,
|
|
date ? String(date) : null,
|
|
author ? String(author) : null,
|
|
tags ? JSON.stringify(tags) : null,
|
|
published !== undefined ? (published ? 1 : 0) : null,
|
|
summary ? String(summary) : null
|
|
];
|
|
|
|
// Log the prepared values
|
|
// console.log('Prepared values:', values);
|
|
|
|
// Query to insert or replace metadata
|
|
const query = db.query(`
|
|
INSERT OR REPLACE INTO posts
|
|
(path, title, date, author, tags, published, summary, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
`);
|
|
|
|
query.run(...values);
|
|
|
|
// console.log(`Stored metadata for: ${filePath}`);
|
|
} catch (error) {
|
|
// console.error(`Failed to store metadata for ${filePath}:`, error);
|
|
}
|
|
}
|