refactor: replace better-sqlite3 with bun:sqlite
This commit is contained in:
12
server.ts
12
server.ts
@ -1,12 +1,12 @@
|
|||||||
import { serve } from "bun";
|
import { serve } from "bun";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import Database from "better-sqlite3"; // @types/better-sqlite3 のインストールを忘れずに
|
import { Database } from "bun:sqlite"; // bun:sqlite は非同期が基本
|
||||||
|
|
||||||
const projectRoot = import.meta.dirname; // server.ts がプロジェクトルートにあると仮定
|
const projectRoot = import.meta.dirname; // server.ts がプロジェクトルートにあると仮定
|
||||||
|
|
||||||
const db = new Database(path.join(projectRoot, "data/podcast.db"));
|
const db = new Database(path.join(projectRoot, "data/podcast.db"));
|
||||||
db.exec(fs.readFileSync(path.join(projectRoot, "schema.sql"), "utf-8"));
|
await db.exec(fs.readFileSync(path.join(projectRoot, "schema.sql"), "utf-8"));
|
||||||
|
|
||||||
// 静的ファイルを提供するディレクトリのパス設定
|
// 静的ファイルを提供するディレクトリのパス設定
|
||||||
// services/tts.ts の出力先は ../static/podcast_audio であり、
|
// services/tts.ts の出力先は ../static/podcast_audio であり、
|
||||||
@ -30,8 +30,8 @@ serve({
|
|||||||
// API Routes
|
// API Routes
|
||||||
if (pathname === "/api/feeds") {
|
if (pathname === "/api/feeds") {
|
||||||
if (req.method === "GET") {
|
if (req.method === "GET") {
|
||||||
const rows = db
|
const rows = await db
|
||||||
.prepare("SELECT feed_url FROM processed_feed_items GROUP BY feed_url")
|
.query("SELECT feed_url FROM processed_feed_items GROUP BY feed_url")
|
||||||
.all() as { feed_url: string }[];
|
.all() as { feed_url: string }[];
|
||||||
return new Response(JSON.stringify(rows.map((r) => r.feed_url)), {
|
return new Response(JSON.stringify(rows.map((r) => r.feed_url)), {
|
||||||
status: 200,
|
status: 200,
|
||||||
@ -58,8 +58,8 @@ serve({
|
|||||||
|
|
||||||
if (pathname === "/api/episodes") {
|
if (pathname === "/api/episodes") {
|
||||||
if (req.method === "GET") {
|
if (req.method === "GET") {
|
||||||
const episodes = db
|
const episodes = await db
|
||||||
.prepare("SELECT * FROM episodes ORDER BY pubDate DESC")
|
.query("SELECT * FROM episodes ORDER BY pubDate DESC")
|
||||||
.all();
|
.all();
|
||||||
return new Response(JSON.stringify(episodes), {
|
return new Response(JSON.stringify(episodes), {
|
||||||
status: 200,
|
status: 200,
|
||||||
|
Reference in New Issue
Block a user