Skip to content

Commit 1b1e8aa

Browse files
SkysplitSeyZ
authored andcommitted
Store included relations in object instead of an array
1 parent a2c77c5 commit 1b1e8aa

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

lib/deserializer-utils.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ var isFunction = require('lodash/isFunction');
44
var _find = require('lodash/find');
55
var _extend = require('lodash/extend');
66
var _transform = require('lodash/transform');
7+
var _merge = require('lodash/merge');
8+
var _get = require('lodash/get');
9+
var _set = require('lodash/set');
710
var Inflector = require('./inflector');
811

912
module.exports = function (jsonapi, data, opts) {
10-
var alreadyIncluded = [];
13+
var alreadyIncluded = {};
1114

1215
function isComplexType(obj) {
1316
return Array.isArray(obj) || isPlainObject(obj);
@@ -33,21 +36,20 @@ module.exports = function (jsonapi, data, opts) {
3336
type: relationshipData.type
3437
});
3538

36-
var includedObject = {
37-
to: {
38-
id: from.id,
39-
type: from.type,
40-
},
41-
from: Object.assign({}, relationshipData),
42-
relation: relationshipName,
43-
};
39+
var path = [
40+
from.type,
41+
from.id,
42+
relationshipName,
43+
relationshipData.type,
44+
relationshipData.id,
45+
]
4446

4547
// Check if the include is already processed (prevent circular
4648
// references).
47-
if (_find(alreadyIncluded, includedObject)) {
49+
if (_get(alreadyIncluded, path, false)) {
4850
return resolve(null);
4951
} else {
50-
alreadyIncluded.push(includedObject);
52+
_merge(alreadyIncluded, _set({}, path, true));
5153
}
5254

5355
if (included) {

0 commit comments

Comments
 (0)