/
utils.js
83 lines (71 loc) · 2.32 KB
/
utils.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
var Connection = require('tedious').Connection;
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
const executeSQL = (context, verb, payload) => new Promise((resolve, reject) => {
var result = "";
const paramPayload = (payload != null) ? JSON.stringify(payload) : '';
//context.log(`Payload: ${JSON.stringify(payload)}`);
const connection = new Connection({
server: process.env["db_server"],
authentication: {
type: 'default',
options: {
userName: process.env["db_user"],
password: process.env["db_password"],
}
},
options: {
database: process.env["db_database"],
encrypt: true
}
});
// Use this logic to coonect to Azure SQL backend pools using managed indentity of an Azure VM
/* const connection = new Connection({
server: process.env["db_server"],
authentication: {
type: 'azure-active-directory-msi-vm',
},
options: {
database: process.env["db_database"],
encrypt: true,
port: 1433
}
});
// Use this logic to coonect to Azure SQL backend pools using managed indentity of an Azure App Service/Function Apps
const connection = new Connection({
server: process.env["db_server"],
authentication: {
type: 'azure-active-directory-msi-app-service',
},
options: {
database: process.env["db_database"],
encrypt: true,
port: 1433
}
});
*/
const request = new Request(`web.${verb}_todo`, (err) => {
if (err) {
reject(err);
} else {
if ((result == "" || result == null || result == "null")) result = "[]";
resolve(result);
}
});
request.addParameter('payload', TYPES.NVarChar, paramPayload, Infinity);
request.on('row', columns => {
columns.forEach(column => {
result += column.value;
});
});
connection.on('connect', err => {
if (err) {
reject(err);
}
else {
connection.callProcedure(request);
}
});
connection.connect();
});
exports.executeSQL = executeSQL;