mirror of
https://github.com/bookorbit/bookorbit.git
synced 2026-11-03 00:11:37 -06:00
fix(metadata): handle large Hardcover libraries
Bind book IDs as a PostgreSQL array so import previews do not exceed the wire-protocol parameter limit for large libraries. Fixes #1196
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
||||
import { Permission } from '@bookorbit/types';
|
||||
import { PgDialect } from 'drizzle-orm/pg-core';
|
||||
|
||||
import * as schema from '../../db/schema';
|
||||
import { HardcoverRepository } from './hardcover.repository';
|
||||
@@ -126,6 +127,17 @@ describe('HardcoverRepository', () => {
|
||||
expect(bookStateQuery.findMany).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('findBookStatesByBookIds binds large ID lists as one PostgreSQL array parameter', async () => {
|
||||
const { repo, bookStateQuery } = makeRepository();
|
||||
const bookIds = Array.from({ length: 65_535 }, (_, index) => index + 1);
|
||||
|
||||
await repo.findBookStatesByBookIds(7, bookIds);
|
||||
|
||||
const config = bookStateQuery.findMany.mock.calls[0]![0] as { where: Parameters<PgDialect['sqlToQuery']>[0] };
|
||||
const query = new PgDialect().sqlToQuery(config.where);
|
||||
expect(query.params).toEqual([7, bookIds]);
|
||||
});
|
||||
|
||||
it('upsertBookState inserts or updates per-book state', async () => {
|
||||
const { repo, db, bookStateInsert, bookStateRow } = makeRepository();
|
||||
db.insert.mockReset();
|
||||
|
||||
@@ -130,7 +130,7 @@ export class HardcoverRepository {
|
||||
async findBookStatesByBookIds(userId: number, bookIds: number[]): Promise<HardcoverBookState[]> {
|
||||
if (bookIds.length === 0) return [];
|
||||
return this.db.query.hardcoverBookState.findMany({
|
||||
where: and(eq(schema.hardcoverBookState.userId, userId), inArray(schema.hardcoverBookState.bookId, bookIds)),
|
||||
where: and(eq(schema.hardcoverBookState.userId, userId), sql`${schema.hardcoverBookState.bookId} = any(${sql.param(bookIds)}::integer[])`),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user