Update author finding and creation for books

This commit is contained in:
Nicholas Wallace 2026-05-11 19:44:25 -07:00
parent bf76128203
commit 94bd8f4110
2 changed files with 10 additions and 15 deletions

View file

@ -465,9 +465,8 @@ class Book extends Model {
/** @type {typeof import('./BookAuthor')} */ /** @type {typeof import('./BookAuthor')} */
const bookAuthorModel = this.sequelize.models.bookAuthor const bookAuthorModel = this.sequelize.models.bookAuthor
const authorsCleaned = authors.map((a) => a.toLowerCase()).filter((a) => a) const authorsRemoved = this.authors.filter((au) => !authors.some((authorName) => authorModel.isAuthorNameMatch(authorName, au.name)))
const authorsRemoved = this.authors.filter((au) => !authorsCleaned.includes(au.name.toLowerCase())) const newAuthorNames = authors.filter((a) => !this.authors.some((au) => authorModel.isAuthorNameMatch(au.name, a)))
const newAuthorNames = authors.filter((a) => !this.authors.some((au) => au.name.toLowerCase() === a.toLowerCase()))
for (const author of authorsRemoved) { for (const author of authorsRemoved) {
await bookAuthorModel.removeByIds(author.id, this.id) await bookAuthorModel.removeByIds(author.id, this.id)
@ -481,6 +480,7 @@ class Book extends Model {
const authorsAdded = [] const authorsAdded = []
for (const authorName of newAuthorNames) { for (const authorName of newAuthorNames) {
const { author, created } = await authorModel.findOrCreateByNameAndLibrary(authorName, libraryId) const { author, created } = await authorModel.findOrCreateByNameAndLibrary(authorName, libraryId)
if (!author) continue
await bookAuthorModel.create({ bookId: this.id, authorId: author.id }) await bookAuthorModel.create({ bookId: this.id, authorId: author.id })
if (created) { if (created) {
SocketAuthority.emitter('author_added', author.toOldJSON()) SocketAuthority.emitter('author_added', author.toOldJSON())

View file

@ -221,23 +221,18 @@ class BookScanner {
if (key === 'authors') { if (key === 'authors') {
// Check for authors added // Check for authors added
for (const authorName of bookMetadata.authors) { for (const authorName of bookMetadata.authors) {
if (!media.authors.some((au) => au.name === authorName)) { if (!media.authors.some((au) => Database.authorModel.isAuthorNameMatch(au.name, authorName))) {
const existingAuthorId = await Database.getAuthorIdByName(libraryItemData.libraryId, authorName) const { author, created } = await Database.authorModel.findOrCreateByNameAndLibrary(authorName, libraryItemData.libraryId)
if (existingAuthorId) { if (!created) {
await Database.bookAuthorModel.create({ await Database.bookAuthorModel.create({
bookId: media.id, bookId: media.id,
authorId: existingAuthorId authorId: author.id
}) })
libraryScan.addLog(LogLevel.DEBUG, `Updating book "${bookMetadata.title}" added author "${authorName}"`) libraryScan.addLog(LogLevel.DEBUG, `Updating book "${bookMetadata.title}" added author "${authorName}"`)
authorsUpdated = true authorsUpdated = true
} else { } else {
const newAuthor = await Database.authorModel.create({ await media.addAuthor(author)
name: authorName, Database.addAuthorToFilterData(libraryItemData.libraryId, author.name, author.id)
lastFirst: Database.authorModel.getLastFirst(authorName),
libraryId: libraryItemData.libraryId
})
await media.addAuthor(newAuthor)
Database.addAuthorToFilterData(libraryItemData.libraryId, newAuthor.name, newAuthor.id)
libraryScan.addLog(LogLevel.DEBUG, `Updating book "${bookMetadata.title}" added new author "${authorName}"`) libraryScan.addLog(LogLevel.DEBUG, `Updating book "${bookMetadata.title}" added new author "${authorName}"`)
authorsUpdated = true authorsUpdated = true
} }
@ -245,7 +240,7 @@ class BookScanner {
} }
// Check for authors removed // Check for authors removed
for (const author of media.authors) { for (const author of media.authors) {
if (!bookMetadata.authors.includes(author.name)) { if (!bookMetadata.authors.some((authorName) => Database.authorModel.isAuthorNameMatch(authorName, author.name))) {
await author.bookAuthor.destroy() await author.bookAuthor.destroy()
libraryScan.addLog(LogLevel.DEBUG, `Updating book "${bookMetadata.title}" removed author "${author.name}"`) libraryScan.addLog(LogLevel.DEBUG, `Updating book "${bookMetadata.title}" removed author "${author.name}"`)
authorsUpdated = true authorsUpdated = true