nos-sql
v3.0.2
Published
SQL module for IBI Data's Node Starter
Downloads
78
Readme
NOS-SQL
A SQL module for IBI Data's Node Starter
- All data is now returned with whitespace trimmed. No need to call deepTrim().
Usage
const NosSql = require('nos-sql');
const sql = new NosSql({
database: 'YOUR_DB',
user: 'YOUR_USER',
password: 'YOUR_PASSWORD',
server: 'YOUR_SERVER',
port: 1203, // must be a number
})
sql.storedProcedure('YOUR_PROC', {
param: 'YOUR_PARAM',
})
.then(results => console.log('DO STUFF'))
.catch(err => console.log('HANDLE ERRORS'));
Methods
storedProcedure(spName [, params, configOverride ])
Will always return a promise.
If configOverride object is present, that sql config will take precedence. If not supplied, the default config sql object will be used. It's OK to pass in the new db connection object as the 2nd or 3rd parameter, as long as it's the last one. Feel free to chain together different procedures using different database connections.
Promises
// Promises
sql.storedProcedure('proc_name', myParameters)
.then(data => {
// Do stuff with data
})
.catch(next);
// Async/await
try {
const data = await sql.storedProcedure('proc_name', myParameters);
// Do stuff with data
} catch (err) {
next(err);
}
query(query, [, params, configOverride])
- Simple query method. Same rules apply as storedProcedure method.
sql.query('SELECT * FROM table')
.then(results => console.log(results))
.catch(next);
sql.query('SELECT * FROM users WHERE userid = @userid', {
userid: 'guest',
})
.then(results => console.log(results))
.catch(next);
Extra Methods
close()
- Closes active sql connection. Note that this not necessary in web apps, but can be useful to gracefully exit a script.
sql.storedProcedure('some_proc')
.then(() => sql.close())
.catch(next);
// async/await
await sql.storedProcedure('some_proc');
await sql.close();
NosSql.deepTrim(obj)
- Deep trim an object
const trimmedObj = NosSql.deepTrim(obj);
Development
- Clone repository
- Run
npm i && npm prepare
The 'prepare' script ensures that husky is initialized and will run tests on each commit.