Skip to content

Commit 2359d7f

Browse files
authored
Merge pull request #31 from dynamiccast/relationship-fix
Fix many to many relationships not mentioned both ways
2 parents 3f1ce47 + 388107b commit 2359d7f

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

lib/api/blueprints/_util/actionUtil.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,37 @@ module.exports = {
113113
*/
114114
updateRelationship(recordToUpdate, relationshipName, relationships) {
115115

116-
// First let's remove all current relationships
117-
recordToUpdate[relationshipName].forEach((toDelete) => {
116+
var a = recordToUpdate[relationshipName];
117+
var b = relationships.data;
118+
119+
var toDelete = a.filter(function(current){
120+
return b.filter(function(current_b){
121+
return current_b.id == current.id;
122+
}).length == 0;
123+
});
124+
125+
var toAdd = b.filter(function(current){
126+
return a.filter(function(current_a){
127+
return current_a.id == current.id;
128+
}).length == 0;
129+
});
130+
131+
toDelete.forEach((toDelete) => {
118132
recordToUpdate[relationshipName].remove(toDelete.id);
119133
});
120134

135+
toAdd.forEach((toAdd) => {
136+
recordToUpdate[relationshipName].add(toAdd.id);
137+
});
138+
121139
return new Promise((resolve, reject) => {
122140
recordToUpdate.save((err) => {
123141
if (err) {
124142
return reject(err);
125143
}
126144

127-
relationships.data.forEach((relationship) => {
128-
recordToUpdate[relationshipName].add(relationship.id);
129-
});
130-
131-
recordToUpdate.save((err) => {
132-
if (err) {
133-
return reject(err);
134-
}
135-
136-
return resolve(null);
137-
});
145+
return resolve(null);
138146
});
139-
})
147+
});
140148
}
141149
};

lib/api/services/JsonApiService.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ module.exports = {
129129
serialize: function(modelName, data) {
130130

131131
var returnedValue = Serializer.serialize(modelName, data);
132+
133+
/*
134+
* To avoid the situation where many to many relationships are not described both ways
135+
* let's remove all included record's relationships
136+
* See https://github.com/danivek/json-api-serializer/issues/10 for more information
137+
*/
138+
if (returnedValue.included) {
139+
returnedValue.included.forEach((include) => {
140+
delete include.relationships;
141+
});
142+
}
132143
delete returnedValue.jsonapi; // Let's ignore the version for now
133144

134145
return returnedValue;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sails-json-api-blueprints",
3-
"version": "0.11.0",
3+
"version": "0.11.1",
44
"description": "Blueprints to turn a Sails.js API into a JSON API",
55
"main": "index.js",
66
"scripts": {

tests/dummy/test/integration/controllers/Relationships.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ describe("Has many through relationships", function() {
124124
"id": houseId,
125125
"attributes": {
126126
"city": "Paris"
127-
},
128-
"relationships": {
129-
"pets": {
130-
"data": []
131-
}
132127
}
133128
}]
134129
})

0 commit comments

Comments
 (0)