Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/db/calibre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface CalibreBook {
has_cover: number;
description: string | null;
rating: number | null; // 1-5 stars (converted from Calibre's 0-10 scale)
total_pages: number | null;
}

export interface PaginationOptions {
Expand Down Expand Up @@ -111,6 +112,8 @@ export function getAllBooks(options?: PaginationOptions): CalibreBook[] {

const hasSeries = hasSeriesColumn || hasSeriesLinkTable;

const hasPagesLinkTable = tableNames.includes("books_pages_link");

const query = `
SELECT
b.id,
Expand All @@ -123,6 +126,7 @@ export function getAllBooks(options?: PaginationOptions): CalibreBook[] {
GROUP_CONCAT(DISTINCT a.name) as authors,
${hasPublisher ? 'p.name' : 'NULL'} as publisher,
${hasSeries ? 's.name' : 'NULL'} as series,
${hasPagesLinkTable ? "bpl.pages" : "NULL"} as total_pages,
GROUP_CONCAT(DISTINCT i.val) as isbn,
c.text as description,
r.rating as rating
Expand All @@ -136,6 +140,7 @@ export function getAllBooks(options?: PaginationOptions): CalibreBook[] {
? 'LEFT JOIN books_series_link bsl ON b.id = bsl.book LEFT JOIN series s ON bsl.series = s.id'
: ''
}
${hasPagesLinkTable ? 'LEFT JOIN books_pages_link bpl ON b.id = bpl.book' : ''}
LEFT JOIN identifiers i ON b.id = i.book AND i.type = 'isbn'
LEFT JOIN comments c ON b.id = c.book
LEFT JOIN books_ratings_link brl ON b.id = brl.book
Expand Down Expand Up @@ -174,6 +179,8 @@ export function getBookById(id: number): CalibreBook | undefined {

const hasSeries = hasSeriesColumn || hasSeriesLinkTable;

const hasPagesLinkTable = tableNames.includes("books_pages_link");

const query = `
SELECT
b.id,
Expand All @@ -186,6 +193,7 @@ export function getBookById(id: number): CalibreBook | undefined {
GROUP_CONCAT(DISTINCT a.name) as authors,
${hasPublisher ? 'p.name' : 'NULL'} as publisher,
${hasSeries ? 's.name' : 'NULL'} as series,
${hasPagesLinkTable ? "bpl.pages" : "NULL"} as total_pages,
GROUP_CONCAT(DISTINCT i.val) as isbn,
c.text as description,
r.rating as rating
Expand All @@ -199,6 +207,7 @@ export function getBookById(id: number): CalibreBook | undefined {
? 'LEFT JOIN books_series_link bsl ON b.id = bsl.book LEFT JOIN series s ON bsl.series = s.id'
: ''
}
${hasPagesLinkTable ? 'LEFT JOIN books_pages_link bpl ON b.id = bpl.book' : ''}
LEFT JOIN identifiers i ON b.id = i.book AND i.type = 'isbn'
LEFT JOIN comments c ON b.id = c.book
LEFT JOIN books_ratings_link brl ON b.id = brl.book
Expand Down Expand Up @@ -239,6 +248,8 @@ export function searchBooks(query: string): CalibreBook[] {

const hasSeries = hasSeriesColumn || hasSeriesLinkTable;

const hasPagesLinkTable = tableNames.includes("books_pages_link");

const searchQuery = `
SELECT
b.id,
Expand All @@ -251,6 +262,7 @@ export function searchBooks(query: string): CalibreBook[] {
GROUP_CONCAT(DISTINCT a.name) as authors,
${hasPublisher ? 'p.name' : 'NULL'} as publisher,
${hasSeries ? 's.name' : 'NULL'} as series,
${hasPagesLinkTable ? "bpl.pages" : "NULL"} as total_pages,
GROUP_CONCAT(DISTINCT i.val) as isbn,
c.text as description,
r.rating as rating
Expand All @@ -264,6 +276,7 @@ export function searchBooks(query: string): CalibreBook[] {
? 'LEFT JOIN books_series_link bsl ON b.id = bsl.book LEFT JOIN series s ON bsl.series = s.id'
: ''
}
${hasPagesLinkTable ? "LEFT JOIN books_pages_link bpl ON b.id = bpl.book" : ""}
LEFT JOIN identifiers i ON b.id = i.book AND i.type = 'isbn'
LEFT JOIN comments c ON b.id = c.book
LEFT JOIN books_ratings_link brl ON b.id = brl.book
Expand Down
1 change: 1 addition & 0 deletions lib/sync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export async function syncCalibreLibrary(
pubDate: calibreBook.pubdate ? new Date(calibreBook.pubdate) : undefined,
series: calibreBook.series || null,
seriesIndex: calibreBook.series_index || null,
totalPages: calibreBook.total_pages || null,
tags,
path: calibreBook.path,
description: calibreBook.description || undefined,
Expand Down