library/lib/books.ts

31 lines
655 B
TypeScript
Raw Normal View History

2021-11-15 23:45:32 -05:00
import sqlite3 from "sqlite3";
const DATABASE_PATH = "catalogue.db";
export async function getAllBooks() {
2021-11-15 23:45:32 -05:00
const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY);
const books = new Promise<SimpleBook[]>((resolve, reject) => {
2021-11-15 23:46:25 -05:00
database.all(
"SELECT id, title, authors, isbn FROM books WHERE deleted = 0",
2021-11-15 23:46:25 -05:00
(error: Error | null, rows: SimpleBook[]) => {
if (error) {
reject(error);
2021-11-15 23:46:25 -05:00
}
resolve(rows);
2021-11-15 23:46:25 -05:00
}
);
});
2021-11-15 23:45:32 -05:00
database.close();
return books;
2021-11-15 23:45:32 -05:00
}
export interface SimpleBook {
id: number;
title: string | null;
authors: string | null;
isbn: string | null;
2021-11-15 23:45:32 -05:00
}