-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
146 lines (124 loc) · 3.72 KB
/
helper.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const mysql = require('mysql2/promise');
const bluebird = require('bluebird');
const dbConfig = require('./dbconfig')
const DOC_TABLE= dbConfig.DOC_TABLE;
const DOCUMENT_TYPE_TABLE = dbConfig.DOCUMENT_TYPE_TABLE;
let sqlConnection = null;
module.exports = {
getDocuments,
getDocument,
addDocument,
editDocumentType,
deleteDocument,
getDocumentTypes,
connect,
}
async function connect() {
sqlConnection = await mysql.createPool({
host:dbConfig.HOST,
user: dbConfig.USER,
password:dbConfig.PASSWORD,
database: dbConfig.DB,
Promise: bluebird,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
}
async function getDocuments() {
try {
if(!sqlConnection) // check if connection already exits, if yes no need to connect
await connect();
const [rows] = await sqlConnection.query(`SELECT ${DOC_TABLE}.id,
${DOC_TABLE}.name,
${DOCUMENT_TYPE_TABLE}.NAME AS documentType,
${DOCUMENT_TYPE_TABLE}.description AS description,
${DOCUMENT_TYPE_TABLE}.id AS typeId
FROM documents
LEFT JOIN documenttype
ON ${DOC_TABLE}.type = ${DOCUMENT_TYPE_TABLE}.id `);
return rows;
}
catch (error) {
console.log(error);
throw new Error('Failed to get the documents', 500);
}
}
async function getDocumentTypes() {
try {
if(!sqlConnection)
await connect();
const [rows]= await sqlConnection.query(`SELECT * from documentType`);
return rows;
}
catch (error) {
console.log(error);
throw new Error('Failed to get the documents', 500);
}
}
async function editDocumentType(documentType) {
try {
if(!sqlConnection)
await connect();
const [rows]= await sqlConnection.execute(`UPDATE ${DOCUMENT_TYPE_TABLE} SET NAME = ?, description = ? where id =?`,[documentType.name, documentType.description, documentType.id]
);
return rows;
}
catch (error) {
console.log(error);
throw new Error('Failed to get the documents', 500);
}
}
async function deleteDocument(documentId) {
try {
if(!sqlConnection)
await connect();
const id = !Number.isNaN(documentId) ? documentId : null;
if(!id) {
throw new Error('Invalid Document Id, Please check the value.', 500);
}
const [row] = await sqlConnection.execute(
'DELETE FROM documents WHERE `id` = ?',
[id])
return row;
}
catch (error) {
console.log(error);
throw new Error('Failed to get the document, please make sure document exits.', 500);
}
}
async function getDocument(documentId) {
try {
if(!sqlConnection)
await connect();
const id = !Number.isNaN(documentId) ? documentId : null;
if(!id) {
throw new Error('Invalid Document Id, Please check the value.', 500);
}
const [row] = await sqlConnection.execute(
'SELECT * FROM documents WHERE `id` = ?',
[id])
if(row && row.length){
return row[0];
} return []
}
catch (error) {
console.log(error);
throw new Error('Failed to get the document, please make sure document exits.', 500);
}
}
async function addDocument(document) {
try {
if(!sqlConnection)
await connect();
const [row] = await sqlConnection.query('INSERT INTO documents (name, type ) VALUES(?,?)', [
document.name,
document.type,
]);
return row;
}
catch (err) {
console.log(err);
throw new Error('Failed to add the document, please make sure document attributes are correct.', 500);
}
}