-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
180 lines (147 loc) · 4.07 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const express = require("express");
const { OGM } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");
const bodyParser = require("body-parser");
const {
NEO4J_URI = "bolt://localhost:7687",
NEO4J_USER = "admin",
NEO4J_PASSWORD = "password",
} = process.env;
const HTTP_PORT = process.env.HTTP_PORT ? Number(process.env.HTTP_PORT) : 4000;
const typeDefs = `
type Movie {
movieId: ID
title: ID
imdbRating: Float
genres: [Genre] @relationship(type: "HAS_GENRE", direction: "OUT")
}
type Genre {
name: String
}
`;
const driver = neo4j.driver(
NEO4J_URI,
neo4j.auth.basic(NEO4J_USER, NEO4J_PASSWORD)
);
const ogm = new OGM({ typeDefs, driver });
const Movie = ogm.model("Movie");
const Genre = ogm.model("Genre");
const app = express();
app.use(bodyParser.json());
function handler(fn) {
return async (req, res) => {
try {
await fn(req, res);
} catch (error) {
return res
.status(500)
.json({ status: "error", error: error.message });
}
};
}
app.get(
"/movie",
handler(async (req, res) => {
const where = {
...(req.query.search
? { title_REGEX: `(?i).*${req.query.search}.*` }
: {}),
};
const options = {
...(req.query.sort ? { sort: req.query.sort.split(",") } : {}),
...(req.query.limit ? { limit: Number(req.query.limit) } : {}),
...(req.query.skip ? { skip: Number(req.query.skip) } : {}),
};
const movies = await Movie.find({ where, options });
return res.json(movies);
})
);
app.post(
"/movie",
handler(async (req, res) => {
const movie = req.body.movie;
const created = await Movie.create({ input: [movie] });
return res.json(created.movies[0]);
})
);
app.put(
"/movie/:id",
handler(async (req, res) => {
const updated = await Movie.update({
where: { movieId: req.params.id },
update: req.body.movie,
});
if (!updated.movies[0]) {
return res.status(404).end();
}
return res.json(updated.movies[0]);
})
);
app.delete(
"/movie/:id",
handler(async (req, res) => {
const { nodesDeleted } = await Movie.delete({
where: { movieId: req.params.id },
});
if (!nodesDeleted) {
return res.status(404).end();
}
return res.status(200).end();
})
);
app.get(
"/genre",
handler(async (req, res) => {
const where = {
...(req.query.search
? { name_REGEX: `(?i).*${req.query.search}.*` }
: {}),
};
const options = {
...(req.query.sort ? { sort: req.query.sort.split(",") } : {}),
...(req.query.limit ? { limit: Number(req.query.limit) } : {}),
...(req.query.skip ? { skip: Number(req.query.skip) } : {}),
};
const genres = await Genre.find({ where, options });
return res.json(genres);
})
);
app.post(
"/genre",
handler(async (req, res) => {
const genre = req.body.genre;
const created = await Genre.create({ input: [genre] });
return res.json(created.genres[0]);
})
);
app.put(
"/genre/:id",
handler(async (req, res) => {
const updated = await Genre.update({
where: { name: req.params.name },
update: req.body.genre,
});
if (!updated.genres[0]) {
return res.status(404);
}
return res.json(updated.genres[0]);
})
);
app.delete(
"/genre/:name",
handler(async (req, res) => {
const { nodesDeleted } = await Genre.delete({
where: { name: req.params.name },
});
if (!nodesDeleted) {
return res.status(404).end();
}
return res.status(200).end();
})
);
async function main() {
await driver.verifyConnectivity();
await app.listen(HTTP_PORT);
console.log(`Online @ ${HTTP_PORT}`);
}
main();