Merge pull request #5354 from mikiher/scan-author-events
Some checks failed
CodeQL / Analyze (push) Has been cancelled
Build and Push Docker Image / build (push) Has been cancelled
Integration Test / build and test (push) Has been cancelled
Run Unit Tests / Run Unit Tests (push) Has been cancelled

Emit author socket events when book scans create or link authors
This commit is contained in:
advplyr 2026-07-13 19:27:31 -04:00 committed by GitHub
commit 59c7d0275c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 81 additions and 7 deletions

View file

@ -1,4 +1,4 @@
const { DataTypes, Model } = require('sequelize')
const { DataTypes, Model, fn, col } = require('sequelize')
class BookAuthor extends Model {
constructor(values, options) {
@ -37,6 +37,32 @@ class BookAuthor extends Model {
})
}
/**
* Get number of books for each author
*
* @param {string[]} authorIds
* @returns {Promise<Record<string, number>>}
*/
static async getCountsForAuthors(authorIds) {
if (!authorIds.length) return {}
const rows = await this.findAll({
attributes: ['authorId', [fn('COUNT', col('id')), 'count']],
where: {
authorId: authorIds
},
group: ['authorId'],
raw: true
})
/** @type {Record<string, number>} */
const counts = {}
for (const row of rows) {
counts[row.authorId] = Number(row.count)
}
return counts
}
/**
* Initialize model
* @param {import('../Database').sequelize} sequelize