-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
schema.js
49 lines (42 loc) · 962 Bytes
/
schema.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
import { Entity } from '@data-client/endpoint';
class BaseEntity extends Entity {
id = 0;
pk() {
return `${this.id}`;
}
}
class User extends BaseEntity {
static process(value, parent, key) {
switch (key) {
case 'author':
return { ...value, posts: [parent.id] };
case 'commenter':
return { ...value, comments: [parent.id] };
default:
return { ...value };
}
}
static merge(entityA, entityB) {
return {
...entityA,
...entityB,
posts: [...(entityA.posts || []), ...(entityB.posts || [])],
comments: [...(entityA.comments || []), ...(entityB.comments || [])],
};
}
}
class Comment extends BaseEntity {
static schema = {
commenter: User,
};
static process(value, parent, key) {
return { ...value, post: parent.id };
}
}
class Post extends BaseEntity {
static schema = {
author: User,
comments: [Comment],
};
}
export default [Post];