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] 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,