-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcreate-parent-records.js
120 lines (105 loc) · 3.04 KB
/
create-parent-records.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Creates a `parent` with `nrChildren` children and each `child` has `nrFriends` friends.
* If `nrFriends > 0` then each child also has a `bestFriend` and a `secondBestFriend`.
*/
let parentFixtureId = 1;
let childFixtureId = 1;
module.exports = function createParentRecords(nrParents = 1, nrChildren, nrFriends) {
const fullPayload = {
data: [],
included: [],
};
for (let i = 0; i < nrParents; i++) {
const payload = createParentPayload(`${parentFixtureId++}`, nrChildren, nrFriends);
if (nrParents === 1) {
return payload;
}
fullPayload.data.push(payload.data);
fullPayload.included.push(...payload.included);
}
return fullPayload;
};
function createParentPayload(parentId = '1', nrChildren = 0, nrFriends = 0) {
const PARENT = createJsonApiResource('parent', parentId, {
parentName: 'Scott',
});
const ALL_FRIENDS = new Array(nrChildren * nrFriends).fill(null).map((i) => {
const child = createJsonApiResource('child', `${childFixtureId++}`, {
childName: `Not Scott's child ${i + 1}`,
});
child.relationships = {
parent: {
data: { type: 'parent', id: `${parentFixtureId++}` },
},
};
return child;
});
let friendIndex = 0;
const ALL_CHILDREN = new Array(nrChildren).fill(null).map((i) => {
const child = createJsonApiResource('child', `${childFixtureId++}`, {
childName: `Scott child ${i + 1}`,
});
child.relationships = {
parent: {
data: { type: 'parent', id: parentId },
},
};
const childIdentifier = extractIdentifiers(child);
if (nrFriends > 0) {
const bestFriend = ALL_FRIENDS[friendIndex];
child.relationships.bestFriend = {
data: extractIdentifiers(bestFriend),
};
bestFriend.relationships.bestFriend = {
data: childIdentifier,
};
const otherFriends = [];
child.relationships.friends = {
data: otherFriends,
};
for (let i = 0; i < nrFriends; i++) {
const friend = ALL_FRIENDS[friendIndex + i];
friend.relationships.friends = {
data: [childIdentifier],
};
otherFriends.push(extractIdentifiers(friend));
}
}
if (nrFriends > 1) {
const secondBestFriend = ALL_FRIENDS[friendIndex + 1];
child.relationships.secondBestFriend = {
data: extractIdentifiers(secondBestFriend),
};
secondBestFriend.relationships.secondBestFriend = {
data: childIdentifier,
};
}
friendIndex += nrFriends;
return child;
});
PARENT.relationships = {
children: {
data: extractIdentifiers(ALL_CHILDREN),
},
};
const payload = {
data: PARENT,
included: [].concat(ALL_CHILDREN, ALL_FRIENDS),
};
return payload;
}
function extractIdentifiers(objOrArr) {
if (Array.isArray(objOrArr)) {
return objOrArr.map((o) => {
return { id: o.id, type: o.type };
});
}
return { id: objOrArr.id, type: objOrArr.type };
}
function createJsonApiResource(type, id, attributes) {
return {
type,
id,
attributes,
};
}