Add basic getAllBooks API

This commit is contained in:
Amy 2021-11-15 23:45:32 -05:00
parent 12d2ce1a0d
commit 3f3eee108c
1 changed files with 26 additions and 0 deletions

26
pages/api/books.ts Normal file
View File

@ -0,0 +1,26 @@
import { NextApiRequest, NextApiResponse } from "next";
import sqlite3 from "sqlite3";
const DATABASE_PATH = "catalogue.db";
export default async function getAllBooks(
request: NextApiRequest,
response: NextApiResponse
) {
const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY);
database.all(
"SELECT title, authors, isbn FROM books",
(error, rows: SimpleBook[]) => {
response.status(200).json(rows);
}
);
database.close();
}
export interface SimpleBook {
authors: string;
isbn: string;
title: string;
}