-
Notifications
You must be signed in to change notification settings - Fork 0
/
databasePostgres.js
50 lines (35 loc) · 1.23 KB
/
databasePostgres.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
import { randomUUID } from "node:crypto"
import { sql } from "./db.js"
export class DatabasePostgres {
#videos = new Map()
async list(search) {
let videos
if (search) {
videos = await sql`select * from videos where title ilike ${'%' + search + '%'}`
} else {
videos = await sql`select * from videos`
}
return videos
}
async listByID(searchID) {
let videos
if (searchID) {
videos = await sql`select * from videos where id ilike ${'%' + searchID + '%'}`
} else {
videos = await sql`select * from videos`
}
return videos
}
async create(video) {
const videoID = randomUUID()
const { title, description, duration } = video
await sql `insert into videos (id, title, description, duration) values (${videoID}, ${title}, ${description}, ${duration})`
}
async update(id, video) {
const { title, description, duration } = video
await sql `update videos set title = ${title}, description = ${description}, duration = ${duration} where id = ${id}`
}
async delete(id) {
await sql `delete from videos where id = ${id}`
}
}