Skip to content

Instantly share code, notes, and snippets.

@colinhicks
Created February 1, 2016 17:51
Show Gist options
  • Save colinhicks/4a998cd5d1bfe58919aa to your computer and use it in GitHub Desktop.
Save colinhicks/4a998cd5d1bfe58919aa to your computer and use it in GitHub Desktop.
import { Connection } from 'tedious';
class TediousClient {
constructor(config) {
this.config = config;
this._cx = null;
}
connect() {
if (!this._cx) {
console.log('Connecting to', this.config.server, '...');
this._cx = new Promise((resolve, reject) => {
const db = new Connection(this.config);
db.on('connect', (err) => err
? reject(err)
: resolve(db));
});
}
return this._cx;
}
}
async function verifyConnection(client) {
try {
const db = await client.connect();
console.log('Connected!');
} catch (err) {
console.warn(err);
}
}
const client = new TediousClient({
userName: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
server: process.env.DATABASE_URL,
database: process.env.DATABASE_NAME
});
verifyConnection(client);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment