-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (95 loc) · 2.15 KB
/
index.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
import { Schema, normalize } from './normalizer'
const data = [
{
id: 'bruce',
name: 'Bruce Wayne',
occupation: 'Playboy Billionaire',
enemy: {
id: 'ghul',
name: "Ra's al Ghul",
occupation: 'Trainer',
location: {
id: 'unknown',
name: '???',
},
},
location: [
{
id: 'gotham',
name: 'Gotham',
},
{
id: 'unknown',
name: '???',
},
],
},
{
id: 'alfred',
name: 'Alfred J. Pennyworth',
occupation: 'Butler',
location: {
id: 'gotham',
name: 'Gotham',
},
},
{
id: 'ghul',
name: "Ra's al Ghul",
occupation: 'Trainer',
location: {
id: 'unknown',
name: '???',
},
},
];
const Location = new Schema('location');
const Character = new Schema('character');
Character.relation('location', Location);
Character.relation('enemy', Character);
const { result, entities: clerk } = normalize(data, Character)
console.log(clerk, result);
console.log(clerk.character.bruce);
console.log(clerk.character.bruce.location);
console.log(clerk.character.bruce.location[1]);
console.log(clerk.location.gotham);
console.log(clerk.character.bruce.location[0] === clerk.location.gotham);
console.log(clerk.location.unknown);
console.log(clerk.character.bruce.enemy);
clerk.character.bruce.enemy.occupation = 'Killer Ninja';
console.log(clerk.character.bruce.enemy);
console.log(clerk.character.ghul);
clerk.character.bruce.enemy = [
{
id: 'ghul',
name: "Ra's al Ghul",
occupation: 'Killer Ninja',
location: {
id: 'unknown',
name: '???',
},
},
{
id: 'bane',
name: "Bane",
occupation: 'League of Assassins',
location: {
id: 'unknown',
name: '???',
},
},
];
console.log(clerk.character.bruce);
console.log(clerk.character.bruce.enemy);
console.log(clerk.character.bruce.enemy[1]);
console.log(clerk.character.bruce.enemy[1] === clerk.character.bane);
clerk.character.bruce.enemy[0] = {
id: 'joker',
name: "Joker",
occupation: 'Psychopath',
location: {
id: 'unknown',
name: '???',
},
};
console.log(clerk.character.bruce.enemy);