-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.js
More file actions
47 lines (40 loc) · 1.3 KB
/
connection.js
File metadata and controls
47 lines (40 loc) · 1.3 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
const fs = require('fs')
const Promise = require('bluebird')
const sql = require('mssql')
sql.Promise = require('bluebird')
const express = require('express')
const app = express()
// Database configuration
const sqlConfig = {
user: 'dometrics-etl',
password: 'thisisnotthepwyourelookingfor',
server: 'etl',
database: 'ETL_Core',
options: { encrypt: true }
}
app.listen(9999, function () {
console.log('API listening on port 9999!')
})
// Basic GET endpoint
// We are reading a text file as a string, then connecting to the SQL DB and
// performing a query. The data set is returned as an object and sent to client.
// Once the data has been sent (or error is caught), the connection is cloesd.
app.get('/allocations', (req, res) => {
fs.readFile('./asgpAllocations.txt', function (err, data) {
if (err) { throw err }
let query = data.toString()
sql.connect(sqlConfig)
.then(()=> { return makeRequest(query) })
.then((data) => { res.send(data) })
.finally(() => { sql.close() })
.catch(err => console.dir(err))
})
})
// Generic request - very simple implementation, could be expanded
function makeRequest(query) {
return new Promise( (resolve, reject) => {
new sql.Request().query(query)
.then(recordset => resolve(recordset))
.catch(err => console.dir(err))
})
}