forked from paularmstrong/normalizr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.ts
44 lines (39 loc) · 1022 Bytes
/
entity.ts
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
import { denormalize, normalize, schema } from '../index'
type User = {
id_str: string;
name: string;
};
type Tweet = {
id_str: string;
url: string;
user: User;
};
const data = {
/* ...*/
};
const user = new schema.Entity<User>(
'users',
{},
{ idAttribute: 'id_str', fallbackStrategy: (key) => ({ id_str: key, name: 'Unknown' }) }
);
const tweet = new schema.Entity(
'tweets',
{ user: user },
{
idAttribute: 'id_str',
// Apply everything from entityB over entityA, except for "favorites"
mergeStrategy: (entityA, entityB) => ({
...entityA,
...entityB,
favorites: entityA.favorites
}),
// Remove the URL field from the entity
processStrategy: (entity: Tweet, parent, key) => {
const { url, ...entityWithoutUrl } = entity;
return entityWithoutUrl;
}
}
);
const normalizedData = normalize(data, tweet);
const denormalizedData = denormalize(normalizedData.result, tweet, normalizedData.entities);
const isTweet = tweet.key === 'tweets';