-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
62 lines (60 loc) · 1.84 KB
/
db.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
const spicedPg = require("spiced-pg");
const db = spicedPg(
process.env.DATABASE_URL || "postgres:leo@localhost/imgboard"
);
module.exports = {
getImages: () =>
db.query(
`SELECT url, title, description, id, (
SELECT id FROM images
ORDER BY id ASC
LIMIT 1
) AS "lowestId" FROM images
ORDER BY id DESC
LIMIT 12`
),
getMore: (lastId) =>
db.query(
`SELECT url, title, description, id, (
SELECT id FROM images
ORDER BY id ASC
LIMIT 1
) AS "lowestId" FROM images
WHERE id < $1
ORDER BY id DESC
LIMIT 12`,
[lastId]
),
addImage: (url, { username, title, description }) =>
db.query(
`INSERT INTO images (url, username, title, description)
VALUES ($1, $2, $3, $4)
RETURNING id, url`,
[url, username, title, description]
),
getImage: (imgId) =>
db.query(
`SELECT * FROM (
SELECT id, url, username, title, description, created_at,
LAG(id) over (ORDER BY id ASC) AS prev_id,
LEAD(id) over (ORDER BY id ASC) AS next_id
FROM images
) AS x
WHERE id = $1`,
[imgId]
),
getComments: (imgId) =>
db.query(
`SELECT * FROM comments
WHERE image_id = $1
ORDER BY created_at DESC`,
[imgId]
),
addComment: ({ id, username, content }) =>
db.query(
`INSERT INTO comments (image_id, username, content)
VALUES ($1, $2, $3)
RETURNING image_id`,
[id, username, content]
),
};