-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
61 lines (56 loc) · 1.51 KB
/
database.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
import pg from 'pg'
const Client = pg.Client
const client = new Client({
user: 'chetan',
host: 'localhost',
database: 'todolist',
password: '12345',
port: 5432
})
export function connectDataBase () {
client.connect((error) => {
if (error) {
console.log(error.message)
} else {
console.log('Connection successful..')
}
})
}
export async function getTodos () {
const todos = await client.query('SELECT * from todo ORDER BY id;')
return todos.rows
}
export async function insertTodo (todo) {
const addTodo = `INSERT INTO todo (name) VALUES ('${todo}');`// change
return await client.query(addTodo)
}
export async function updateTodo (id, property, value) {
const updateTodo = `UPDATE todo
SET ${property} ='${value}'
WHERE id=${id};`
return await client.query(updateTodo)
}
export async function deleteTodo (id) {
const deleteTodo = `DELETE FROM todo
WHERE id=${id};`
return await client.query(deleteTodo)
}
export async function deleteDone () {
const delDone = `DELETE FROM todo
WHERE checkbox = 'true';`
return await client.query(delDone)
}
export async function deleteAll () {
const delAll = 'DELETE FROM todo;'
return await client.query(delAll)
}
export async function showCompleted () {
const completed = `SELECT * FROM todo
WHERE checkbox = 'true' ORDER BY id;`
return await client.query(completed)
}
export async function pendingTodos () {
const pending = `SELECT * FROM todo
WHERE checkbox = 'false' ORDER BY id;`
return await client.query(pending)
}