From ea93c045e9ac92470c78c426757a6e89c2a76119 Mon Sep 17 00:00:00 2001 From: Artem Shinkaruk Date: Wed, 7 Oct 2020 17:13:48 +0700 Subject: [PATCH] Fix RelationOwner._getPropsFromModels It would drop some unique props in case if they had buffer values. As the result some related models won't be fetched. This commit fixes the join function, which converts buffers to generate correct key for the prop. --- lib/relations/RelationOwner.js | 2 +- tests/integration/misc/#1627.js | 16 ++++++++++++++++ tests/unit/utils.js | 20 +++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/relations/RelationOwner.js b/lib/relations/RelationOwner.js index 53adb57bb..847637a6f 100644 --- a/lib/relations/RelationOwner.js +++ b/lib/relations/RelationOwner.js @@ -219,7 +219,7 @@ function containsNonNull(arr) { } function join(id) { - return id.join(','); + return id.map(x => (Buffer.isBuffer(x) ? x.toString('hex') : x)).join(','); } function isIdProp(relationProp) { diff --git a/tests/integration/misc/#1627.js b/tests/integration/misc/#1627.js index 3bd080e67..14643a6d4 100644 --- a/tests/integration/misc/#1627.js +++ b/tests/integration/misc/#1627.js @@ -90,5 +90,21 @@ module.exports = session => { expect(result).to.eql(inserted); }); + it('should fetch multiple relations correctly', async () => { + const ids = [ + Buffer.from('00000000000000000000000000007AAC', 'hex'), + Buffer.from('00000000000000000000000000007AAD', 'hex'), + Buffer.from('00000000000000000000000000007AAE', 'hex') + ]; + const graph = ids.map(id => ({ + id, + roles: [{ id: crypto.randomBytes(16) }] + })); + const inserted = await User.query().insertGraph(graph); + const result = await User.query() + .findByIds(ids) + .withGraphFetched('roles'); + expect(result).to.eql(inserted); + }); }); }; diff --git a/tests/unit/utils.js b/tests/unit/utils.js index f8468783c..f509d4f4d 100644 --- a/tests/unit/utils.js +++ b/tests/unit/utils.js @@ -12,7 +12,7 @@ const { const { range } = require('lodash'); const { compose, mixin } = require('../../lib/utils/mixin'); const { map } = require('../../lib/utils/promiseUtils'); -const { jsonEquals } = require('../../lib/utils/objectUtils'); +const { jsonEquals, uniqBy } = require('../../lib/utils/objectUtils'); describe('utils', () => { describe('mixin', () => { @@ -428,4 +428,22 @@ describe('utils', () => { ).to.equal(false); }); }); + describe('uniqBy', () => { + const items = [ + [Buffer.from('00000000000000000000000000007AAD', 'hex')], + [Buffer.from('00000000000000000000000000007AAE', 'hex')], + [Buffer.from('00000000000000000000000000007AAC', 'hex')] + ]; + it('should work with Buffer items', () => { + expect(uniqBy(items)).to.eql(items); + }); + it('should work with Buffer[] items', () => { + expect(uniqBy(items)).to.eql(items); + }); + it('should work with Buffer[] items with custom keyGetter function', () => { + expect( + uniqBy(items, item => item.map(x => (Buffer.isBuffer(x) ? x.toString('hex') : x)).join(',')) + ).to.eql(items); + }); + }); });