-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path04-circular-references.ts
More file actions
150 lines (128 loc) · 3.98 KB
/
04-circular-references.ts
File metadata and controls
150 lines (128 loc) · 3.98 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Circular References Example
*
* This example demonstrates how to handle circular references
* using the use() method for lazy evaluation.
*/
import { Factory } from 'interface-forge';
interface Department {
employees: Employee[];
id: string;
manager: Employee;
name: string;
}
interface Employee {
department: Department;
id: string;
name: string;
subordinates: Employee[];
supervisor?: Employee;
}
interface Post {
author: User;
content: string;
id: string;
relatedPosts: Post[];
title: string;
}
interface User {
favoritePost?: Post;
id: string;
name: string;
posts: Post[];
}
const UserFactory: Factory<User> = new Factory<User>((faker) => ({
favoritePost: faker.use(() => PostFactory.build()),
id: faker.string.uuid(),
name: faker.person.fullName(),
posts: faker.use(() => PostFactory.batch(3)),
}));
const PostFactory: Factory<Post> = new Factory<Post>((faker) => ({
author: faker.use(() => UserFactory.build()),
content: faker.lorem.paragraphs(2),
id: faker.string.uuid(),
relatedPosts: faker.use(() => PostFactory.batch(2)),
title: faker.lorem.sentence(),
}));
const user = UserFactory.build();
console.log('User:', user.name);
console.log('Posts by user:', user.posts.length);
console.log('Favorite post:', user.favoritePost?.title);
const DepartmentFactory: Factory<Department> = new Factory<Department>(
(faker) => ({
employees: faker.use(() => EmployeeFactory.batch(5)),
id: faker.string.uuid(),
manager: faker.use(() => EmployeeFactory.build()),
name: faker.commerce.department(),
}),
);
const EmployeeFactory: Factory<Employee> = new Factory<Employee>((faker) => ({
department: faker.use(() => DepartmentFactory.build()),
id: faker.string.uuid(),
name: faker.person.fullName(),
subordinates: faker.use(() =>
EmployeeFactory.batch(faker.number.int({ max: 3, min: 0 })),
),
supervisor: faker.datatype.boolean({ probability: 0.8 })
? faker.use(() => EmployeeFactory.build())
: undefined,
}));
const LimitedUserFactory: Factory<User> = new Factory<User>(
(faker) => ({
favoritePost: undefined,
id: faker.string.uuid(),
name: faker.person.fullName(),
posts: faker.use(() => LimitedPostFactory.batch(2)),
}),
{ maxDepth: 3 },
);
const LimitedPostFactory: Factory<Post> = new Factory<Post>(
(faker) => ({
author: faker.use(() => LimitedUserFactory.build()),
content: faker.lorem.paragraphs(2),
id: faker.string.uuid(),
relatedPosts: faker.use(() => LimitedPostFactory.batch(1)),
title: faker.lorem.sentence(),
}),
{ maxDepth: 3 },
);
const limitedUser = LimitedUserFactory.build();
console.log('Limited depth user:', limitedUser);
const createUserWithOwnPosts = () => {
const user = UserFactory.build({ favoritePost: undefined, posts: [] });
const posts = PostFactory.batch(3, { author: user });
user.posts = posts;
[user.favoritePost] = posts;
return user;
};
const userWithOwnPosts = createUserWithOwnPosts();
console.log(
'User owns all posts:',
userWithOwnPosts.posts.every(
(post: Post) => post.author.id === userWithOwnPosts.id,
),
);
interface GraphNode {
connections: GraphNode[];
id: string;
value: number;
}
const GraphNodeFactory: Factory<GraphNode> = new Factory<GraphNode>(
(faker) => ({
connections: faker.use(() =>
GraphNodeFactory.batch(faker.number.int({ max: 3, min: 0 })),
),
id: faker.string.uuid(),
value: faker.number.int({ max: 100, min: 1 }),
}),
);
const graphRoot = new Factory<GraphNode>(
(faker) => ({
connections: faker.use(() => GraphNodeFactory.batch(3)),
id: faker.string.uuid(),
value: faker.number.int({ max: 100, min: 1 }),
}),
{ maxDepth: 2 },
).build();
console.log('Graph root node:', graphRoot.id);
console.log('Direct connections:', graphRoot.connections.length);