-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground-network.mongodb.js
More file actions
142 lines (112 loc) · 3.03 KB
/
Copy pathplayground-network.mongodb.js
File metadata and controls
142 lines (112 loc) · 3.03 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
/* global use, db, ObjectId */
// CrewGraph network playground
// Update the database name below if your Atlas connection string uses a different one.
// Replace the placeholder crew id before running.
use("test");
const crews = db.getCollection("crews");
const productions = db.getCollection("productions");
const credits = db.getCollection("credits");
const crewIdString = "69b854ae056fd4f6db836b75";
if (crewIdString === "PASTE_CREW_ID_HERE") {
throw new Error("Replace PASTE_CREW_ID_HERE with a real crew _id from the crews collection.");
}
if (!/^[a-fA-F0-9]{24}$/.test(crewIdString)) {
throw new Error("crewIdString must be a valid 24-character MongoDB ObjectId.");
}
const crewId = new ObjectId(crewIdString);
const center = crews.findOne(
{ _id: crewId },
{ name: 1, role: 1, avatar: 1, createdAt: 1 }
);
if (!center) {
throw new Error(`No crew member found for id ${crewIdString}`);
}
const baseCredits = credits.find({ crewId }).toArray();
const productionIds = [...new Set(baseCredits.map((credit) => String(credit.productionId)))].map(
(id) => new ObjectId(id)
);
const nodes = [
{
id: String(center._id),
type: "crew",
label: center.name,
role: center.role,
avatar: center.avatar,
center: true
}
];
const edges = [];
if (productionIds.length === 0) {
({
center,
nodes,
edges
});
}
const productionDocs = productions
.find(
{ _id: { $in: productionIds } },
{ title: 1, year: 1, tmdbId: 1 }
)
.toArray();
const networkCredits = credits.find({ productionId: { $in: productionIds } }).toArray();
const collaboratorIds = [
...new Set(networkCredits.map((credit) => String(credit.crewId)))
].map((id) => new ObjectId(id));
const collaboratorDocs = crews
.find(
{ _id: { $in: collaboratorIds } },
{ name: 1, role: 1, avatar: 1 }
)
.toArray();
const productionMap = new Map(
productionDocs.map((production) => [String(production._id), production])
);
const crewMap = new Map(
collaboratorDocs.map((crew) => [String(crew._id), crew])
);
for (const production of productionDocs) {
nodes.push({
id: String(production._id),
type: "production",
label: production.title,
year: production.year,
tmdbId: production.tmdbId
});
}
for (const crew of collaboratorDocs) {
const id = String(crew._id);
if (id === String(center._id)) {
continue;
}
nodes.push({
id,
type: "crew",
label: crew.name,
role: crew.role,
avatar: crew.avatar,
center: false
});
}
const edgeMap = new Map();
for (const credit of networkCredits) {
const crew = crewMap.get(String(credit.crewId));
const production = productionMap.get(String(credit.productionId));
if (!crew || !production) {
continue;
}
const edgeKey = `${credit.crewId}-${credit.productionId}-${credit.role}`;
edgeMap.set(edgeKey, {
source: String(credit.crewId),
target: String(credit.productionId),
role: credit.role
});
}
edges.push(...edgeMap.values());
const payload = {
center,
nodes,
edges
};
console.log(JSON.stringify(payload, null, 2));
payload;