MSSQL Connection Class for the NK Node Package
Install using NPM
npm i nk-mssql --save
MSSQL is the Industry leader of Relational Database with Error Protection
NKMSSQL.start(
<Database Name>, //String
<IP>, //String
<Port>, //Number
<User>, //String
<Password>, //String
<Timeout in milliseconds>, //Number
<Use Encrypted Connection>, //Bool
<Callback> //Function
);
The database connection object is saved in the NKMSSQL Object. Indexed by the database name, so there is a caveat to not use the same database name across distinct servers.
Example:
const NKMSSQL = require( 'nk-mssql' )
NKMSSQL.start( 'MyDatabase', '127.0.0.1', 1433, null, null, null, null, true, ( isError, errorMessage ) => {
//Super duper awesome code here!
console.log( isError, errorMessage )
})
WHAT?! Yes, you can connect to multiple servers in the same core, using them as objects for real-time compliances, for example:
const NKMSSQL = require( 'nk-mssql' )
NKMSSQL.start( 'MyDatabase', '127.0.0.1', 1433, null, null, null, ( isError1, errorMessage1 ) => NKMSSQL.start( 'RemoteDB1', 'remote.mydomain.com', 1433, null, null, null, ( isError2, errorMessage2 ) => NKMSSQL.start( 'RemoteDB2', 'remote2.mydomain.com', 1433, null, null, null, ( isError3, errorMessage3 ) => {
console.log( isError1, errorMessage1, isError2, errorMessage2, isError3, errorMessage3 )
})))
NKMSSQL.insert(
<Database Name>, //String
<Table Name>, //String
<ROW OR ROWS>, //A single Object to insert one, or an Array of Objects to insert many
<Callback> //Function
)
Example:
NKMSSQL.insert( 'MyDatabase', 'users',
{
username: 'jose',
pass: '123',
active: false,
added: ( new Date() ).getTime()
},
() => console.log( 'all done' ) )
NKMSSQL.delete(
<Database Name>, //String
<Table Name>, //String
<DATA TO REMOVE>, //Object
<Callback> //Function
)
Example:
NKMSSQL.delete( 'MyDatabase', 'users',
{
myuser: NKMSSQL.id( user._id )
},
() => console.log( 'all done' ) )
NKMSSQL.update(
<Database Name>, //String
<Table Name>, //String
<DATA TO UPDATE>, //Object
<NEW DATA>, //Object
<Callback> //Function
)
Example:
NKMSSQL.update( 'MyDatabase', 'users',
{
active: false
},
{
active: true
},
() => console.log( 'all done' )
)
NKMSSQL.query(
<Database Name>, //String
<Table Name>, //String
<QUERY>, //Object
<Callback> //Function, Recieves rows from query
)
Example:
NKMSSQL.query( 'MyDatabase', 'users',
{
active: true
},
rowsFromQuery => console.log( rowsFromQuery )
)
NKMSSQL.querySort(
<Database Name>, //String
<Table Name>, //String
<SORT BY>, //Object
<QUERY>, //Object
<Callback> //Function, Recieves rows from query
)
Example:
NKMSSQL.querySort( 'MyDatabase', 'users',
{ added: 1 },
{ active: true },
rowsFromQuery => console.log( rowsFromQuery ) )
NKMSSQL.queryLimitSort(
<Database Name>, //String
<Table Name>, //String
<LIMIT>, //Number
<SORT BY>, //Object
<QUERY>, //Object
<Callback> //Function, Recieves rows from query
)
Example
NKMSSQL.queryLimitSort( 'MyDatabase', 'users',
100,
{ added: 1 },
{ active: true },
rowsFromQuery => console.log( rowsFromQuery )
)
Note: singleQuery should only ever query ONE ROW.
NKMSSQL.singleQuery(
<Database Name>, //String
<Table Name>, //String
<QUERY>, //Object
<Calback> //Function, Recieves a single row from query.
);
Example:
NKMSSQL.singleQuery( 'MyDatabase', 'users',
{
myuser: NKMSSQL.id( user._id )
},
rowFromQuery => console.log( rowFromQuery )
)
This singleQuery is very useful in the authentication methods, e.g.
NKMSSQL.singleQuery( 'MyDatabase', 'users',
{ loginSessionKey: req.sessionKey },
rowFromQuery => res.json( rowFromQuery? true: false )
)
NKMSSQL.singleQuery(
<Database Name>, //String
<Table Name>, //String
<Table ID Field>, //String
<Name of Table to join to>, //String
<ID Field of Table to join to>, //String
<Join to Element>, //String
<Sort By>, //Object
<Query>, // Object
<Calback> //Function, Recieves rows from query
);
Example:
NKMSSQL.join( 'MyDatabase', 'users',
'_id',
'photos',
'user_id',
'photos',
{ added: 1 },
{ myuser: NKMSSQL.id( user._id ) },
rowsFromQuery => console.log( rowsFromQuery )
)
NKMSSQL.joinsLimit(
<Database Name>, //String
<Table Name>, //String
<JOINS>, //Array of Objects
<LIMIT>, //Number
<SORT BY>, //Object
<QUERY>, //Object
<Calback> //Function, Recieves rows from query
);
Example
const joins = [
{ from: 'photos', field: '_id', fromField: 'user_id', as: 'photos' },
{ from: 'history', field: '_id', fromField: 'user_id', as: 'transactions' }
];
NKMSSQL.joinsLimit( 'MyDatabase', 'users',
joins,
100,
{ added: 1 },
{ active: true },
rowsFromQuery => console.log( rowsFromQuery )
)
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.