We can use Knex to connect to MSSQL, jsut install Tedious library.
Version
Knex 0.95.11
Tedious 14.0.0
MSSQL 2017
Add Library
$ yarn add knex tedious
knex
: SQL builder to generate SQLtedious
: MSSQL library
Knex
import Knex from 'knex'
let knex = Knex ({
client: 'mssql',
connection: {
host: '10.211.55.7',
port: 1433,
user: 'sa',
password: '111111',
database: 'DBLab'
}
})
let result = await knex ('articles').select ('*')
console.log (result)
Line 3
let knex = Knex ({
client: 'mssql',
connection: {
host: '10.211.55.7',
port: 1433,
user: 'sa',
password: '111111',
database: 'DBLab'
}
})
Use Knex to connect to database :
client: 'mssql'
: connect to MSSQLhost
:setup serverport
:setup portuser
:setup idpassword
:setup passworddatabase
:setup database
Line 14
let result = await knex ('articles').select ('*')
console.log (result)
Use Knex to generate SQL query and return result :
knex
: table for selectselect
:fields for select
Becuase Knex will return Promise, we have to use top-level await to deal with Promise.
Conclusion
- Just add
Knex
andtedious
library, changeclient
tomssql
, then you can connect to MSSQL by Knex