diff --git a/lib/books.ts b/lib/books.ts new file mode 100644 index 0000000..0ba0964 --- /dev/null +++ b/lib/books.ts @@ -0,0 +1,30 @@ +import sqlite3 from "sqlite3"; + +const DATABASE_PATH = "catalogue.db"; + +export async function getAllBooks() { + const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); + + const books = new Promise((resolve, reject) => { + database.all( + "SELECT id, title, authors, isbn FROM books WHERE deleted = 0", + (error: Error | null, rows: SimpleBook[]) => { + if (error) { + reject(error); + } + resolve(rows); + } + ); + }); + + database.close(); + + return books; +} + +export interface SimpleBook { + id: number; + title: string | null; + authors: string | null; + isbn: string | null; +} diff --git a/pages/index.tsx b/pages/index.tsx index cf73651..46c3461 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,8 +1,29 @@ -import type { NextPage } from "next"; import React from "react"; -const Home: NextPage = () => { - return
I am a book
; -}; +import { getAllBooks, SimpleBook } from "../lib/books"; -export default Home; +export default function Home(props: Props) { + return ( + + ); +} + +interface Props { + books: SimpleBook[]; +} + +export async function getServerSideProps() { + return { + props: { + books: await getAllBooks(), + }, + }; +}