Skip to content

Commit b0fb1f9

Browse files
committed
Fix #19 + prepare test for #20
1 parent f74c907 commit b0fb1f9

16 files changed

+236
-41
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This adapter just add a light serialize and deserialize layer over DSHttpAdapter
2626
| js-data | >= 3.0.0-rc.7 |
2727
| js-data-http | >= 3.0.0-rc.2 |
2828

29-
By design it is not compatible with JS-Data v2, for a full integration on v2, you could use: [js-data-jsonapi](https://github.com/BlairAllegroTech/js-data-jsonapi).
29+
By design it is not compatible with JS-Data v2, for a full integration on v2, you should use: [js-data-jsonapi](https://github.com/BlairAllegroTech/js-data-jsonapi).
3030

3131
## Quick Start
3232

dist/js-data-jsonapi-light.js

+10-28
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.

src/deserializer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export function jsonApiDeserialize(mapper:Mapper, res:any, opts:any){
7676
// For `hasOne`, like it's the remote object which should include
7777
// the key, we don't do anything.
7878
if (relation.type === 'belongsTo') {
79-
item.attributes[relation.localKey] = link.id;
79+
if (!relation.foreignKey) {
80+
this.warn(WARNING.NO_FOREIGN_KEY, relation);
81+
} else {
82+
item.attributes[relation.foreignKey] = link.id;
83+
}
8084
}
8185

8286
if (itemsIndexed[link.type] && itemsIndexed[link.type][link.id]) {

src/serializer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function jsonApiSerialize (mapper:any, data:any, opts:any){
4545
continue;
4646
}
4747

48-
// Relation that can be in data are only belongsTo since it has a localKey
48+
// Relation that can be in data are only belongsTo
4949
relationships[relation.localField] = {
5050
data: {
5151
type: relation.relation,

src/strings.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const WARNING = {
1010
NO_RESSOURCE: (type:string) => { return `Can\'t find resource '${type}'` },
1111
RELATION_UNKNOWN: 'Unknown relation',
1212
WRONG_RELATION_ARRAY_EXPECTED: 'Wrong relation somewhere, array expected',
13-
WRONG_RELATION_OBJECT_EXPECTED: 'Wrong relation somewhere, object expected'
13+
WRONG_RELATION_OBJECT_EXPECTED: 'Wrong relation somewhere, object expected',
14+
NO_FOREIGN_KEY: 'No `foreignKey` on this relation. Be careful `localKey` doesn\'t exist anymore on JSData v3 and has been replaced with `foreignKey`. `belongsTo` relations must have a `foreignKey`.'
1415
}

src/utils.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ export function mapperCacheRelationByField (mapper:any):void {
44
mapper.relationByFieldId = {};
55
for (let i = 0, l = (mapper.relationList || []).length; i < l; i++ ) {
66
let field:string = mapper.relationList[i].localField;
7-
let key:string = mapper.relationList[i].localKey;
87

9-
if (key) {
10-
mapper.relationByFieldId[key] = mapper.relationList[i];
8+
if (mapper.relationList[i].type === 'belongsTo) {
9+
let key:string = mapper.relationList[i].foreignKey;
10+
if (!relation.foreignKey) {
11+
this.warn(WARNING.NO_FOREIGN_KEY, relation);
12+
} else {
13+
mapper.relationByFieldId[key] = mapper.relationList[i];
14+
}
1115
}
1216

1317
if (field) {

test/api/articles.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"meta": {
3+
"description": "Have a belongsTo but no included data."
4+
},
25
"links": {
36
"self": "api/articles.json"
47
},

test/api/categories.json

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"meta": {
3+
"description": "Nested structure."
4+
},
5+
"links": {
6+
"self": "api/categories.json"
7+
},
8+
"data": [
9+
{
10+
"type": "Category",
11+
"id": "437add6a-a40f-4cf0-9c27-fd7bb4c9ef71",
12+
"attributes": {
13+
"name": "Level I - Root category"
14+
},
15+
"relationships": {
16+
"parent": {
17+
"links": {
18+
"related": "api/categories/437add6a-a40f-4cf0-9c27-fd7bb4c9ef71/parent.json"
19+
}
20+
},
21+
"children": {
22+
"links": {
23+
"related": "api/categories/437add6a-a40f-4cf0-9c27-fd7bb4c9ef71/children.json"
24+
}
25+
}
26+
},
27+
"links": {
28+
"self": "api/categories/437add6a-a40f-4cf0-9c27-fd7bb4c9ef71.json"
29+
}
30+
},
31+
{
32+
"type": "Category",
33+
"id": "dbb8d464-8229-4865-86c3-48ac90ae1029",
34+
"attributes": {
35+
"name": "Cat I.A"
36+
},
37+
"relationships": {
38+
"parent": {
39+
"links": {
40+
"related": "api/categories/dbb8d464-8229-4865-86c3-48ac90ae1029/parent.json"
41+
},
42+
"data": {
43+
"id": "437add6a-a40f-4cf0-9c27-fd7bb4c9ef71",
44+
"type": "Category"
45+
}
46+
},
47+
"children": {
48+
"links": {
49+
"related": "api/categories/dbb8d464-8229-4865-86c3-48ac90ae1029/children.json"
50+
}
51+
}
52+
},
53+
"links": {
54+
"self": "api/categories/dbb8d464-8229-4865-86c3-48ac90ae1029.json"
55+
}
56+
},
57+
{
58+
"type": "Category",
59+
"id": "eecff649-5ad2-4e42-8d1a-f1e5d55926bd",
60+
"attributes": {
61+
"name": "Cat I.B"
62+
},
63+
"relationships": {
64+
"parent": {
65+
"links": {
66+
"related": "api/categories/eecff649-5ad2-4e42-8d1a-f1e5d55926bd/parent.json"
67+
},
68+
"data": {
69+
"id": "437add6a-a40f-4cf0-9c27-fd7bb4c9ef71",
70+
"type": "Category"
71+
}
72+
},
73+
"children": {
74+
"links": {
75+
"related": "api/categories/eecff649-5ad2-4e42-8d1a-f1e5d55926bd/children.json"
76+
}
77+
}
78+
},
79+
"links": {
80+
"self": "api/categories/eecff649-5ad2-4e42-8d1a-f1e5d55926bd.json"
81+
}
82+
},
83+
{
84+
"type": "Category",
85+
"id": "c829339e-7b24-4fc8-b6ff-b605a2020fb3",
86+
"attributes": {
87+
"name": "Cat I.B.a"
88+
},
89+
"relationships": {
90+
"parent": {
91+
"links": {
92+
"related": "api/categories/c829339e-7b24-4fc8-b6ff-b605a2020fb3/parent.json"
93+
},
94+
"data": {
95+
"id": "eecff649-5ad2-4e42-8d1a-f1e5d55926bd",
96+
"type": "Category"
97+
}
98+
},
99+
"children": {
100+
"links": {
101+
"related": "api/categories/c829339e-7b24-4fc8-b6ff-b605a2020fb3/children.json"
102+
}
103+
}
104+
},
105+
"links": {
106+
"self": "api/categories/c829339e-7b24-4fc8-b6ff-b605a2020fb3.json"
107+
}
108+
}
109+
]
110+
}

test/resources/article.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const Article = window.store.defineMapper('Article', {
66
belongsTo: {
77
'User': {
88
localField: 'author',
9-
localKey: 'authorId'
9+
foreignKey: 'authorId'
1010
}
1111
}
1212
// ,

test/resources/category.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as JSData from 'js-data'
2+
3+
export const Category = window.store.defineMapper('Category', {
4+
endpoint: 'categories',
5+
relations: {
6+
belongsTo: {
7+
'Category': {
8+
localField: 'parent',
9+
foreignKey: 'parentId'
10+
}
11+
},
12+
hasMany: {
13+
'Category': {
14+
localField: 'children',
15+
foreignKey: 'parentId'
16+
}
17+
}
18+
}
19+
})

test/resources/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './article';
2+
export * from './category';
23
export * from './user';
34
export * from './user-group';
45
export * from './user-profile';

test/resources/user-favorite.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export const UserFavorite = window.store.defineMapper('UserFavorite', {
55
belongsTo: {
66
'Article': {
77
localField: 'article',
8-
localKey: 'articleId'
8+
foreignKey: 'articleId'
99
},
1010
'User': {
1111
localField: 'user',
12-
localKey: 'userId'
12+
foreignKey: 'userId'
1313
}
1414
}
1515
}

test/resources/user-profile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const UserProfile = window.store.defineMapper('UserProfile', {
66
belongsTo: {
77
'User': {
88
localField: 'user',
9-
localKey: 'userId'
9+
foreignKey: 'userId'
1010
}
1111
}
1212
}

test/resources/user.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const User = window.store.defineMapper('User',{
2323
belongsTo: {
2424
'UserGroup': {
2525
localField: 'group',
26-
localKey: 'groupId'
26+
foreignKey: 'groupId'
2727
}
2828
}
2929
}

test/unit/relations/nested.spec.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { expect } from 'chai';
2+
import { store } from '../../ds';
3+
import * as Resources from '../../resources'
4+
5+
describe('relations/nested', () => {
6+
describe('when data are fetched, relations should persist and be assigned if data is there', () => {
7+
const
8+
CATEGORY_LENGTH = 4,
9+
ROOT_CATEGORY = {
10+
ID: '437add6a-a40f-4cf0-9c27-fd7bb4c9ef71',
11+
CHILDREN: [
12+
'dbb8d464-8229-4865-86c3-48ac90ae1029',
13+
'eecff649-5ad2-4e42-8d1a-f1e5d55926bd'
14+
]
15+
},
16+
CATEGORIES_ASCENDING_PATH = [
17+
'c829339e-7b24-4fc8-b6ff-b605a2020fb3',
18+
'eecff649-5ad2-4e42-8d1a-f1e5d55926bd',
19+
'437add6a-a40f-4cf0-9c27-fd7bb4c9ef71'
20+
],
21+
CATEGORIES_NAME = {
22+
"437add6a-a40f-4cf0-9c27-fd7bb4c9ef71": "Level I - Root category",
23+
"dbb8d464-8229-4865-86c3-48ac90ae1029": "Cat I.A",
24+
"eecff649-5ad2-4e42-8d1a-f1e5d55926bd": "Cat I.B",
25+
"c829339e-7b24-4fc8-b6ff-b605a2020fb3": "Cat I.B.a"
26+
}
27+
;
28+
29+
let categories:Array<any>;
30+
31+
beforeEach(() => {
32+
return store.findAll('Category').then((datas:Array<any>) => {
33+
categories = datas
34+
})
35+
});
36+
37+
afterEach(() => {
38+
categories = null
39+
});
40+
41+
it('should return an array of correct length', () => {
42+
expect(store.getAll('Category')).to.be.an('array').and.to.have.lengthOf(CATEGORY_LENGTH);
43+
});
44+
45+
it('should be able to ascend in the nested tree to first ancestor', () => {
46+
let category = store.get('Category', CATEGORIES_ASCENDING_PATH[CATEGORIES_ASCENDING_PATH.length - 1]);
47+
let i = CATEGORIES_ASCENDING_PATH.length - 1; while (i--) {
48+
expect(category.parentId).to.equal(CATEGORIES_ASCENDING_PATH[i]);
49+
expect(category).to.have.property('parent');
50+
expect(category.parent).to.exist;
51+
expect(category.parent.id).to.equal(CATEGORIES_ASCENDING_PATH[i]);
52+
expect(category.parent.name).to.equal(CATEGORIES_NAME[CATEGORIES_ASCENDING_PATH[i]]);
53+
54+
category = category.parent
55+
}
56+
57+
expect(category.parent).to.be.null;
58+
});
59+
60+
it('should correctly retain children', () => {
61+
let rootCategory = store.get('Category', ROOT_CATEGORY.ID);
62+
expect(rootCategory).to.have.property('children');
63+
expect(rootCategory.children).to.exist;
64+
expect(rootCategory.children).to.be.an('array').and.to.have.lengthOf(ROOT_CATEGORY.CHILDREN.length);
65+
66+
let i = rootCategory.children.length; while (i--) {
67+
expect(ROOT_CATEGORY.CHILDREN).to.include(rootCategory.children[i].id);
68+
}
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)