|
@@ -409,32 +409,52 @@ class DbSearcher {
|
|
|
if (rows.length == ids.length)
|
|
|
return rows;
|
|
|
|
|
|
+ //далее восстановим книги из book в <from>_book
|
|
|
const idsSet = new Set(rows.map(r => r.id));
|
|
|
|
|
|
+ //недостающие
|
|
|
+ const tableIds = [];
|
|
|
for (const id of ids) {
|
|
|
- if (!idsSet.has(id)) {
|
|
|
- const bookIds = await db.select({
|
|
|
- table: from,
|
|
|
- where: `@@id(${db.esc(id)})`
|
|
|
- });
|
|
|
-
|
|
|
- if (!bookIds.length)
|
|
|
- continue;
|
|
|
+ if (!idsSet.has(id))
|
|
|
+ tableIds.push(id);
|
|
|
+ }
|
|
|
|
|
|
- let books = await db.select({
|
|
|
- table: 'book',
|
|
|
- where: `@@id(${db.esc(bookIds[0].bookIds)})`
|
|
|
- });
|
|
|
+ const tableRows = await db.select({
|
|
|
+ table: from,
|
|
|
+ where: `@@id(${db.esc(tableIds)})`
|
|
|
+ });
|
|
|
|
|
|
- if (!books.length)
|
|
|
- continue;
|
|
|
+ //список недостающих bookId
|
|
|
+ const bookIds = [];
|
|
|
+ for (const row of tableRows) {
|
|
|
+ for (const bookId of row.bookIds)
|
|
|
+ bookIds.push(bookId);
|
|
|
+ }
|
|
|
|
|
|
- rows.push({id, name: bookIds[0].name, books});
|
|
|
+ //выбираем книги
|
|
|
+ const books = await db.select({
|
|
|
+ table: 'book',
|
|
|
+ where: `@@id(${db.esc(bookIds)})`
|
|
|
+ });
|
|
|
|
|
|
- await db.insert({table: bookTable, ignore: true, rows});
|
|
|
+ const booksMap = new Map();
|
|
|
+ for (const book of books)
|
|
|
+ booksMap.set(book.id, book);
|
|
|
+
|
|
|
+ //распределяем
|
|
|
+ for (const row of tableRows) {
|
|
|
+ const books = [];
|
|
|
+ for (const bookId of row.bookIds) {
|
|
|
+ const book = booksMap.get(bookId);
|
|
|
+ if (book)
|
|
|
+ books.push(book);
|
|
|
}
|
|
|
+
|
|
|
+ rows.push({id: row.id, name: row.name, books});
|
|
|
}
|
|
|
|
|
|
+ await db.insert({table: bookTable, ignore: true, rows});
|
|
|
+
|
|
|
return rows;
|
|
|
}
|
|
|
|