Skip to content

Commit

Permalink
Fix RelationOwner._getPropsFromModels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
coderNeos authored and koskimas committed Oct 9, 2020
1 parent 6ab1ff9 commit ea93c04
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/relations/RelationOwner.js
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/misc/#1627.js
Expand Up @@ -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);
});
});
};
20 changes: 19 additions & 1 deletion tests/unit/utils.js
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit ea93c04

Please sign in to comment.