import sqlite3 from 'better-sqlite3' export interface IPost { slug: string date?: Date title: string image?: string tag?: string[] /** * cleaned markdown */ content: string excerptHtml: string contentHtml: string } export class Database { db: sqlite3.Database constructor(public filepath: string) { this.db = sqlite3(filepath) } public init(): void { this.db.exec(/* sql */ ` CREATE VIRTUAL TABLE q USING fts5 ( slug, [date] UNINDEXED, -- epoch milli title, [image] UNINDEXED, tag, -- space separated content, -- cleaned markdown excerptHtml UNINDEXED, contentHtml UNINDEXED ); `) } public insert(...ps: IPost[]): void { const stmt = this.db.prepare(/* sql */ ` INSERT INTO q (slug, [date], title, [image], tag, content, excerptHtml, contentHtml) VALUES (@slug, @date, @title, @image, @tag, @content, @excerptHtml, @contentHtml) `) this.db.transaction(() => { // eslint-disable-next-line array-callback-return ps.map((p) => { stmt.run({ ...p, date: p.date ? p.date.getTime() : undefined, tag: (p.tag || []).join(' '), }) }) })() } }