Created
February 1, 2016 17:51
-
-
Save colinhicks/4a998cd5d1bfe58919aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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