Skip to content

Instantly share code, notes, and snippets.

@ihgrant
Created May 23, 2016 21:17
Show Gist options
  • Save ihgrant/8ed2ef9bee1332451b40cffdd364eb96 to your computer and use it in GitHub Desktop.
Save ihgrant/8ed2ef9bee1332451b40cffdd364eb96 to your computer and use it in GitHub Desktop.
using mssql node library with promise support
const sql = require('mssql');
const Promise = require('bluebird');
Promise.promisifyAll([
sql.Connection,
]);
const createConnection = connectionOptions => {
const connection = new sql.Connection(connectionOptions);
return connection.connectAsync().then(() => {
return connection;
}).disposer(() => {
return connection.closeAsync();
});
};
module.exports = createConnection;
const sql = require('mssql');
const Promise = require('bluebird');
const createConnection = require('./create-connection');
/**
* Run the specified stored procedure with the supplied parameters, if any.
* @param connectionOptions {Object} - the detail needed to connect to the database.
* @param procedure {string} - the name of the stored procedure to run.
* @param params {Object} - the parameters, if any, to run the stored procedure with.
*/
function execute(connectionOptions, procedure, params = {}) {
return Promise.using(createConnection(connectionOptions), (connection) => {
let request = new sql.Request(connection);
Object.keys(params).forEach(key => {
request.input(key, params[key]);
});
return request.execute(procedure);
});
}
module.exports = execute;
const express = require('express');
const app = express();
const http = require('http').Server(app);
const Promise = require('bluebird');
const api = express.Router();
const PORT = process.env.PORT || 3000;
const CONNECTION_INFO = {
server: '',
port: 1433,
database: '',
user: '',
password: '',
pool: {
idleTimeoutMillis: 1000
}
};
const query = require('./query');
const execute = require('./execute');
// a stub for the function to get connection info (from redis, eventually...)
const getInfo = companyId => Promise.resolve(CONNECTION_INFO);
api.get('/', (req, res) => {
getInfo('100128')
// .then(info => query(info, 'SELECT 1 AS result'))
.then(info => execute(info, 'sp_GetSBIInfo', {}))
.then(res.json)
.catch(err => res.status(500).json(err));
});
/* boring server setup ------------------------------------------- */
app
.use('*/api/', api)
.use((req, res) => {
res.send('hello. Request path: ' + req.url);
});
http.listen(PORT, () => {
var d = new Date();
console.log(`server started at ${d.toString()}`);
console.log(`listening on *:${PORT}`);
});
const sql = require('mssql');
const Promise = require('bluebird');
const createConnection = require('./create-connection');
/**
* run the specified query with supplied paramseters (if any)
* @param connectionOptions {Object} - the detail needed to connect to the database.
* @param query {string} - the sql query to run.
* @param params {Object} - a dictionary of keys and values to run the query with.
*/
function query(connectionOptions, query, params = {}) {
return Promise.using(createConnection(connectionOptions), (connection) => {
let request = new sql.Request(connection);
Object.keys(params).forEach(key => {
request.input(key, params[key]);
});
return request.query(query)
});
}
module.exports = query;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment