forked from BrianWangila/NodeJS-CRUD-PostgresSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerchant_model.js
More file actions
73 lines (66 loc) · 1.98 KB
/
merchant_model.js
File metadata and controls
73 lines (66 loc) · 1.98 KB
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
require('dotenv').config();
const Pool = require('pg').Pool
const pool = new Pool({
user: process.env.USER,
host: process.env.HOST,
database: process.env.DATABASE,
password: process.env.PASSWORD,
port: process.env.PORT,
connectionLimit: 10,
user: "postgres",
host: "factsDB.abcdefg.eu-west-2.rds.amazonaws.com",
database: "database_usuario",
password: "Tecnologia123*",
port: 5432,
debug: false
});
const getMerchants = () => {
return new Promise(function(resolve, reject) {
pool.query('SELECT * FROM merchants ORDER BY id ASC', (error, results) => {
if (error) {
reject(error)
}
resolve(results.rows);
})
})
}
const createMerchant = (body) => {
return new Promise(function(resolve, reject) {
const { name, email } = body
pool.query('INSERT INTO merchants (name, email) VALUES ($1, $2) RETURNING *', [name, email], (error, results) => {
if (error) {
reject(error)
}
resolve(`A new merchant has been added added: ${results.rows[0]}`)
})
})
}
const updateMerchant = (params, body) => {
return new Promise (function(resolve, reject) {
const id = parseInt(params)
const { name, email } = body
pool.query('UPDATE merchants SET name=$2, email=$3 WHERE id = $1', [id, name, email], (error, results) => {
if (error) {
reject(error)
}
resolve(`Merchant update with ID: ${id}`)
})
})
}
const deleteMerchant = (params) => {
return new Promise(function(resolve, reject) {
const id = parseInt(params)
pool.query('DELETE FROM merchants WHERE id = $1', [id], (error, results) => {
if (error) {
reject(error)
}
resolve(`Merchant deleted with ID: ${id}`)
})
})
}
module.exports = {
getMerchants,
createMerchant,
updateMerchant,
deleteMerchant,
}