Skip to content

Commit 5a39315

Browse files
committed
Fix #23 - Add option
1 parent c857947 commit 5a39315

File tree

6 files changed

+102
-48
lines changed

6 files changed

+102
-48
lines changed

dist/js-data-jsonapi-light.js

+29-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data-jsonapi-light.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-data-jsonapi-light",
3-
"version": "1.0.0-alpha.6",
3+
"version": "1.0.0-alpha.7",
44
"description": "JsonApi adapter serializer/dezerializer light.",
55
"main": "./dist/js-data-jsonapi-light.js",
66
"author": {

src/serializer.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,31 @@ export function jsonApiSerialize (mapper:any, data:any, opts:any){
4040

4141
// @todo For the moment sending hasMany items is not supported and maybe
4242
// shouldn't be supported for security reasons (cf. JSON Api Spec)
43-
for (let key in data) {
44-
let relation:any = mapper.relationByFieldId[key];
45-
46-
// No relations means a simple attribute
47-
if (!relation) {
48-
attributes[key] = data[key];
49-
continue;
50-
}
43+
if (opts.forceRelationshipsInAttributes !== true) {
44+
for (let key in data) {
45+
let relation:any = mapper.relationByFieldId[key];
46+
47+
// No relations means a simple attribute
48+
if (!relation) {
49+
attributes[key] = data[key];
50+
continue;
51+
}
5152

52-
// Relation that can be in data are only belongsTo
53-
relationships[relation.localField] = {
54-
data: {
55-
type: relation.relation,
56-
id: data[key]
53+
// Relation that can be in data are only belongsTo
54+
relationships[relation.localField] = {
55+
data: {
56+
type: relation.relation,
57+
id: data[key]
58+
}
5759
}
5860
}
61+
} else {
62+
attributes = data;
5963
}
6064

65+
6166
// Only include relationships if needed
62-
if (Object.keys(relationships).length) {
67+
if (Object.keys(relationships).length !== 0) {
6368
output.data.relationships = relationships;
6469
}
6570

test/unit/crud/update.spec.ts

+49-5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ describe('UPDATE', () => {
6161
}
6262
}
6363
}
64+
},
65+
TMP_AUTHOR:any = {
66+
ID: "01ac757a-1cac-4a10-a6f1-ca4b7a91c4a8",
67+
EMAIL: "else@example.com"
6468
};
6569

6670
let reqGet:any = null;
@@ -142,7 +146,51 @@ describe('UPDATE', () => {
142146
})
143147
});
144148

145-
it('the request should send relation when a record is saved.', () => {
149+
it('the request should send only changed relation in `relationships` when a record is saved.', () => {
150+
return store.find('Article', ID).then((record) => {
151+
// When id, record is not considered New
152+
const otherAuthor = store.add('User', {
153+
id: TMP_AUTHOR.ID,
154+
email: TMP_AUTHOR.email
155+
});
156+
record.authorId = otherAuthor.id;
157+
return record.save();
158+
}).then((data) => {
159+
expect(reqPatch.body.data).to.be.an('object');
160+
expect(reqPatch.body.data.type).to.equal(MAPPER_NAME);
161+
expect(reqPatch.body.data.id).to.equal(ID);
162+
expect(reqPatch.body.data.attributes).to.be.undefined;
163+
expect(reqPatch.body.data.relationships).to.be.an('object');
164+
expect(reqPatch.body.data.relationships.author).to.be.an('object');
165+
expect(reqPatch.body.data.relationships.author.data).to.deep.equal({
166+
type: "User",
167+
id: TMP_AUTHOR.ID
168+
});
169+
})
170+
});
171+
172+
it('the request should send changed relation as attributes when a record is saved with option `forceRelationshipsInAttributes`.', () => {
173+
return store.find('Article', ID).then((record) => {
174+
// When record has id, it is not considered New
175+
const otherAuthor = store.add('User', {
176+
id: TMP_AUTHOR.ID,
177+
email: TMP_AUTHOR.email
178+
});
179+
record.title = UPDATE_PARAMS.title;
180+
record.author = otherAuthor;
181+
return record.save({ forceRelationshipsInAttributes: true });
182+
}).then((data) => {
183+
expect(reqPatch.body.data).to.be.an('object');
184+
expect(reqPatch.body.data.type).to.equal(MAPPER_NAME);
185+
expect(reqPatch.body.data.id).to.equal(ID);
186+
expect(reqPatch.body.data.attributes).to.deep.equal({
187+
title: UPDATE_PARAMS.title,
188+
authorId: TMP_AUTHOR.ID
189+
})
190+
})
191+
});
192+
193+
it('the request should send all attributes and relationships when record is saved with option `replace`', () =>{
146194
return store.update('Article', ID, UPDATE_PARAMS, {
147195
forceReplace: true
148196
}).then((data) => {
@@ -162,10 +210,6 @@ describe('UPDATE', () => {
162210
expect(data.title).to.equal(UPDATE_PARAMS.title)
163211
expect(data.content).to.equal(FIND_RESPONSE.data.attributes.content)
164212
})
165-
});
166-
167-
it('the request should send all attributes and relationships when record is saved with option `replace`', () =>{
168-
return true;
169213
})
170214
})
171215

test/unit/relations/nested.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ describe.skip('relations/nested', () => {
66
describe('when data are fetched, relations should persist and be assigned if data is there', () => {
77
const
88
CATEGORY_LENGTH = 4,
9-
ROOT_CATEGORY = {
9+
ROOT_CATEGORY:any = {
1010
ID: '437add6a-a40f-4cf0-9c27-fd7bb4c9ef71',
1111
CHILDREN: [
1212
'dbb8d464-8229-4865-86c3-48ac90ae1029',
1313
'eecff649-5ad2-4e42-8d1a-f1e5d55926bd'
1414
]
1515
},
16-
CATEGORIES_ASCENDING_PATH = [
16+
CATEGORIES_ASCENDING_PATH:[ string ] = [
1717
'c829339e-7b24-4fc8-b6ff-b605a2020fb3',
1818
'eecff649-5ad2-4e42-8d1a-f1e5d55926bd',
1919
'437add6a-a40f-4cf0-9c27-fd7bb4c9ef71'
2020
],
21-
CATEGORIES_NAME = {
21+
CATEGORIES_NAME:any = {
2222
"437add6a-a40f-4cf0-9c27-fd7bb4c9ef71": "Level I - Root category",
2323
"dbb8d464-8229-4865-86c3-48ac90ae1029": "Cat I.A",
2424
"eecff649-5ad2-4e42-8d1a-f1e5d55926bd": "Cat I.B",

0 commit comments

Comments
 (0)