func/work.js

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.loadAuthorNames = loadAuthorNames;
  6. /*
  7. * Copyright (C) 2022 Shivam Awasthi
  8. * Some parts adapted from bookbrainz-site
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /**
  25. * @param {ORM} orm - the BookBrainz ORM, initialized during app setup
  26. * @param {array} workBBIDs - the array containing the BBIDs of the works contained in the edition
  27. * @returns {Object} - Returns an array of objects containing the authorAlias, authorBBID of each work in an edition
  28. */
  29. async function loadAuthorNames(orm, workBBIDs) {
  30. if (!workBBIDs.length) {
  31. return [];
  32. }
  33. const sqlQuery = `select
  34. author.bbid as authorBBID,
  35. alias."name" as authorAlias,
  36. work.bbid as workBBID
  37. from
  38. bookbrainz.work as work
  39. -- Get Authors related to Work (relationship type 8, Author wrote Work)
  40. left join bookbrainz.relationship_set as workRelSet on
  41. workRelSet.id = work.relationship_set_id
  42. left join bookbrainz.relationship_set__relationship as workRelSetRel on
  43. workRelSetRel.set_id = workRelSet.id
  44. inner join bookbrainz.relationship as workRel on
  45. workRel.type_id = 8
  46. and workRel.id = workRelSetRel.relationship_id
  47. left join bookbrainz.author as author on
  48. author.bbid = workRel.source_bbid
  49. and author.master is true
  50. -- Get defaultAlias of the Authors
  51. left join bookbrainz.alias on
  52. alias.id = author.default_alias_id
  53. where
  54. work.master is true
  55. and work.data_id is not null
  56. and work.bbid in ${`(${workBBIDs.map(bbid => `'${bbid}'`).join(', ')})`}`;
  57. const queryResults = await orm.bookshelf.knex.raw(sqlQuery);
  58. return queryResults.rows;
  59. }