From def34a860b4fb1d32f199161139e052ff3b78add Mon Sep 17 00:00:00 2001 From: Oleg Ivasenko Date: Fri, 13 Sep 2024 16:23:25 +0000 Subject: [PATCH 1/2] when checking if series/author is alread in DB, use case insensitive match only for ASCII names --- server/models/Author.js | 26 +++++++++++++++++++------- server/models/Series.js | 26 +++++++++++++++++++------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/server/models/Author.js b/server/models/Author.js index f3bbba57..7115a85e 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -53,14 +53,26 @@ class Author extends Model { * @returns {Promise} */ static async getByNameAndLibrary(authorName, libraryId) { - return this.findOne({ - where: [ - where(fn('lower', col('name')), authorName.toLowerCase()), - { - libraryId + const containsOnlyASCII = /^[\u0000-\u007f]*$/.test(authorName) + + // SQLite does not support lower with non-Unicode chars + if (!containsOnlyASCII) { + return this.findOne({ + where: { + name: authorName, + libraryId: libraryId } - ] - }) + }) + } else { + return this.findOne({ + where: [ + where(fn('lower', col('name')), authorName.toLowerCase()), + { + libraryId + } + ] + }) + } } /** diff --git a/server/models/Series.js b/server/models/Series.js index c57a1a11..9eab76b9 100644 --- a/server/models/Series.js +++ b/server/models/Series.js @@ -39,14 +39,26 @@ class Series extends Model { * @returns {Promise} */ static async getByNameAndLibrary(seriesName, libraryId) { - return this.findOne({ - where: [ - where(fn('lower', col('name')), seriesName.toLowerCase()), - { - libraryId + const containsOnlyASCII = /^[\u0000-\u007f]*$/.test(authorName) + + // SQLite does not support lower with non-Unicode chars + if (!containsOnlyASCII) { + return this.findOne({ + where: { + name: seriesName, + libraryId: libraryId } - ] - }) + }) + } else { + return this.findOne({ + where: [ + where(fn('lower', col('name')), seriesName.toLowerCase()), + { + libraryId + } + ] + }) + } } /** From 0af29a378a98417856e96dd7a6f296ae69095f4a Mon Sep 17 00:00:00 2001 From: Oleg Ivasenko Date: Fri, 13 Sep 2024 17:09:32 +0000 Subject: [PATCH 2/2] use asciiOnlyToLowerCase to match lower function behaviour of SQLite --- server/models/Author.js | 27 ++++++++------------------- server/models/Series.js | 27 ++++++++------------------- 2 files changed, 16 insertions(+), 38 deletions(-) diff --git a/server/models/Author.js b/server/models/Author.js index 7115a85e..40e7f75a 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -1,5 +1,6 @@ const { DataTypes, Model, where, fn, col } = require('sequelize') const parseNameString = require('../utils/parsers/parseNameString') +const { asciiOnlyToLowerCase } = require('../utils/index') class Author extends Model { constructor(values, options) { @@ -53,26 +54,14 @@ class Author extends Model { * @returns {Promise} */ static async getByNameAndLibrary(authorName, libraryId) { - const containsOnlyASCII = /^[\u0000-\u007f]*$/.test(authorName) - - // SQLite does not support lower with non-Unicode chars - if (!containsOnlyASCII) { - return this.findOne({ - where: { - name: authorName, - libraryId: libraryId + return this.findOne({ + where: [ + where(fn('lower', col('name')), asciiOnlyToLowerCase(authorName)), + { + libraryId } - }) - } else { - return this.findOne({ - where: [ - where(fn('lower', col('name')), authorName.toLowerCase()), - { - libraryId - } - ] - }) - } + ] + }) } /** diff --git a/server/models/Series.js b/server/models/Series.js index 9eab76b9..dc8d110f 100644 --- a/server/models/Series.js +++ b/server/models/Series.js @@ -1,6 +1,7 @@ const { DataTypes, Model, where, fn, col } = require('sequelize') const { getTitlePrefixAtEnd } = require('../utils/index') +const { asciiOnlyToLowerCase } = require('../utils/index') class Series extends Model { constructor(values, options) { @@ -39,26 +40,14 @@ class Series extends Model { * @returns {Promise} */ static async getByNameAndLibrary(seriesName, libraryId) { - const containsOnlyASCII = /^[\u0000-\u007f]*$/.test(authorName) - - // SQLite does not support lower with non-Unicode chars - if (!containsOnlyASCII) { - return this.findOne({ - where: { - name: seriesName, - libraryId: libraryId + return this.findOne({ + where: [ + where(fn('lower', col('name')), asciiOnlyToLowerCase(seriesName)), + { + libraryId } - }) - } else { - return this.findOne({ - where: [ - where(fn('lower', col('name')), seriesName.toLowerCase()), - { - libraryId - } - ] - }) - } + ] + }) } /**