From bd9f8c9a60c28e8688c8833092a9d5e861ecc1ce Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Mon, 14 Feb 2022 00:44:36 -0500 Subject: [PATCH 1/6] Add query for one book --- lib/books.ts | 35 +++++++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/books.ts diff --git a/lib/books.ts b/lib/books.ts new file mode 100644 index 0000000..3144413 --- /dev/null +++ b/lib/books.ts @@ -0,0 +1,35 @@ +import sqlite3 from "sqlite3"; + +const DATABASE_PATH = "catalogue.db"; + +async function getBook(id: number) { + const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); + const sql = + "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated, deleted FROM books WHERE id = ? "; + + await database.get(sql, [id], (err: Error | null, book: DetailedBook) => { + if (err) { + return console.error(err.message); + } + console.log(book); + }); + + database.close(); +} + +export interface DetailedBook { + isbn: string | null; + lccn: string | null; + title: string; + authors: string | null; + edition: string | null; + publisher: string | null; + publish_year: string | null; + publish_month: string | null; + publish_location: string | null; + pages: string | null; + pagination: string | null; + weight: string | null; + last_updated: Date | null; + deleted: boolean | null; +} diff --git a/tsconfig.json b/tsconfig.json index a5ccdc7..5510e90 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ /* Basic Options */ "incremental": true, "target": "ES6", - "module": "esnext", + "module": "CommonJS", "moduleResolution": "node", "lib": ["dom", "dom.iterable", "esnext"], "sourceMap": true, -- 2.39.2 From 5587b88cf17b26f70b59cdf1bd106a2832453be5 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Tue, 15 Feb 2022 22:24:26 -0500 Subject: [PATCH 2/6] Fix type errors --- lib/books.ts | 32 ++++++++++++++++++++++++++------ pages/index.tsx | 21 ++++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/books.ts b/lib/books.ts index 3144413..252ac27 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -2,19 +2,39 @@ import sqlite3 from "sqlite3"; const DATABASE_PATH = "catalogue.db"; -async function getBook(id: number) { +export async function getBook(id: number): Promise { const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); + const sql = "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated, deleted FROM books WHERE id = ? "; - await database.get(sql, [id], (err: Error | null, book: DetailedBook) => { - if (err) { - return console.error(err.message); - } - console.log(book); + const book: Promise = new Promise((resolve, reject) => { + database.get(sql, [id], (err: Error | null, book: DetailedBook) => { + if (err) { + reject(err); + } + if (book) { + resolve(book); + } else { + reject(new Error("Not a valid id")); + } + }); }); + // .then((newBook) => { + // book = new Promise((resolve) => { + // resolve(newBook); + // }); + // }) + // .catch((err) => { + // console.error(err); + // }) + // .finally(() => { + // database.close(); + // }); database.close(); + + return book; } export interface DetailedBook { diff --git a/pages/index.tsx b/pages/index.tsx index cf73651..df94b40 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,8 +1,19 @@ -import type { NextPage } from "next"; import React from "react"; -const Home: NextPage = () => { - return
I am a book
; -}; +import { getBook, DetailedBook } from "../lib/books"; -export default Home; +export default function Home(props: Props) { + return

{props.book.title}

; +} + +interface Props { + book: DetailedBook; +} + +export async function getServerSideProps() { + return { + props: { + book: await getBook(2), + }, + }; +} -- 2.39.2 From 955c906f4ee4036d2b10a58cefe56c3db9b24329 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Tue, 22 Feb 2022 23:08:35 -0500 Subject: [PATCH 4/6] Fix type for title --- lib/books.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/books.ts b/lib/books.ts index 252ac27..cd4a8ed 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -40,7 +40,7 @@ export async function getBook(id: number): Promise { export interface DetailedBook { isbn: string | null; lccn: string | null; - title: string; + title: string | null; authors: string | null; edition: string | null; publisher: string | null; -- 2.39.2 From 0414c01776c06ae1ff7acca1103c5380650493c3 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 16 Mar 2022 18:13:50 -0400 Subject: [PATCH 5/6] PR feedback --- lib/books.ts | 13 +------------ pages/index.tsx | 2 +- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/books.ts b/lib/books.ts index cd4a8ed..c6ff592 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -6,7 +6,7 @@ export async function getBook(id: number): Promise { const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); const sql = - "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated, deleted FROM books WHERE id = ? "; + "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated, deleted FROM books WHERE id = ? AND deleted = 0"; const book: Promise = new Promise((resolve, reject) => { database.get(sql, [id], (err: Error | null, book: DetailedBook) => { @@ -20,17 +20,6 @@ export async function getBook(id: number): Promise { } }); }); - // .then((newBook) => { - // book = new Promise((resolve) => { - // resolve(newBook); - // }); - // }) - // .catch((err) => { - // console.error(err); - // }) - // .finally(() => { - // database.close(); - // }); database.close(); diff --git a/pages/index.tsx b/pages/index.tsx index df94b40..da1ed32 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -13,7 +13,7 @@ interface Props { export async function getServerSideProps() { return { props: { - book: await getBook(2), + book: await getBook(44), }, }; } -- 2.39.2 From 21985f8aa6c2c56498371f7e00aa3f2344cb352d Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Fri, 18 Mar 2022 16:42:52 -0400 Subject: [PATCH 6/6] Change SQL query --- lib/books.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/books.ts b/lib/books.ts index c9fea35..d75d334 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -6,7 +6,7 @@ export async function getBook(id: number): Promise { const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); const sql = - "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated, deleted FROM books WHERE id = ? AND deleted = 0"; + "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated FROM books WHERE id = ? AND deleted = 0"; const book: Promise = new Promise((resolve, reject) => { database.get(sql, [id], (err: Error | null, book: DetailedBook) => { -- 2.39.2