ManyToManyFindOperation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const RelationFindOperation = require('../../RelationFindOperation');
  2. const { getTempColumn } = require('../../../utils/tmpColumnUtils');
  3. const { uniqBy } = require('../../../utils/objectUtils');
  4. class ManyToManyFindOperation extends RelationFindOperation {
  5. constructor(name, opt) {
  6. super(name, opt);
  7. this.ownerJoinColumnAlias = new Array(this.relation.joinTableOwnerProp.size);
  8. for (let i = 0, l = this.ownerJoinColumnAlias.length; i < l; ++i) {
  9. this.ownerJoinColumnAlias[i] = getTempColumn(i);
  10. }
  11. }
  12. onBuild(builder) {
  13. const relatedModelClass = this.relation.relatedModelClass;
  14. const joinTableOwnerProp = this.relation.joinTableOwnerProp;
  15. const ownerProp = this.relation.ownerProp;
  16. const ids = new Array(this.owners.length);
  17. for (let i = 0, l = this.owners.length; i < l; ++i) {
  18. ids[i] = ownerProp.getProps(this.owners[i]);
  19. }
  20. if (!builder.has(builder.constructor.SelectSelector)) {
  21. const table = builder.tableRefFor(relatedModelClass);
  22. // If the user hasn't specified a select clause, select the related model's columns.
  23. // If we don't do this we also get the join table's columns.
  24. builder.select(`${table}.*`);
  25. // Also select all extra columns.
  26. for (let i = 0, l = this.relation.joinTableExtras.length; i < l; ++i) {
  27. const extra = this.relation.joinTableExtras[i];
  28. const joinTable = this.relation.joinTable;
  29. builder.select(`${joinTable}.${extra.joinTableCol} as ${extra.aliasCol}`);
  30. }
  31. }
  32. this.relation.findQuery(builder, {
  33. ownerIds: uniqBy(ids, join)
  34. });
  35. // We must select the owner join columns so that we know for which owner model the related
  36. // models belong to after the requests.
  37. for (let i = 0, l = joinTableOwnerProp.size; i < l; ++i) {
  38. const joinTableOwnerRef = joinTableOwnerProp.ref(builder, i);
  39. const propName = relatedModelClass.columnNameToPropertyName(this.ownerJoinColumnAlias[i]);
  40. builder.select(joinTableOwnerRef.as(this.ownerJoinColumnAlias[i]));
  41. // Mark them to be omitted later.
  42. this.omitProps.push(propName);
  43. }
  44. this.selectMissingJoinColumns(builder);
  45. }
  46. onAfter2(builder, related) {
  47. const isOneToOne = this.relation.isOneToOne();
  48. if (this.assignResultToOwner) {
  49. const ownerProp = this.relation.ownerProp;
  50. const relatedByOwnerId = new Map();
  51. for (let i = 0, l = related.length; i < l; ++i) {
  52. const rel = related[i];
  53. const key = rel.$propKey(this.ownerJoinColumnAlias);
  54. let arr = relatedByOwnerId.get(key);
  55. if (!arr) {
  56. arr = [];
  57. relatedByOwnerId.set(key, arr);
  58. }
  59. arr.push(rel);
  60. }
  61. for (let i = 0, l = this.owners.length; i < l; ++i) {
  62. const own = this.owners[i];
  63. const key = ownerProp.propKey(own);
  64. const related = relatedByOwnerId.get(key);
  65. if (isOneToOne) {
  66. own[this.relationProperty] = (related && related[0]) || null;
  67. } else {
  68. own[this.relationProperty] = related || [];
  69. }
  70. }
  71. }
  72. return related;
  73. }
  74. }
  75. function join(arr) {
  76. return arr.join();
  77. }
  78. module.exports = ManyToManyFindOperation;