-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-postgres.js
More file actions
38 lines (26 loc) · 987 Bytes
/
database-postgres.js
File metadata and controls
38 lines (26 loc) · 987 Bytes
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
import { randomUUID } from 'node:crypto'
import { sql } from './db.js';
export class DatabasePostgres {
#videos = new Map();
async read(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 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}`;
}
}