Node.js로 MSSQL Server 연결하기
https://www.npmjs.com/package/tedious
tedious
A TDS driver, for connecting to MS SQLServer databases.
www.npmjs.com
Node.js와 MSSQL을 연결하기에 앞서, 우선 MSSQL 서버 데이터베이스의 버전을 반드시 먼저 확인해야한다.
Node.js와 MSSQL을 연결하기 위해서는 tedious가 설치되어있어야 한다.
(2021-06-24 기준 tedious는 sql server 2017 버전까지만 지원한다.)
Knex.js - A SQL Query Builder for Javascript
The knex.schema is a getter function, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the knex.schema for every query. These methods return promises. createTable — knex.schema.createTable(tableName, cal
knexjs.org
그리고 knex를 이용해 MSSQL에 연결할것이다.
우선 위 모듈들을 npm으로 설치해준다.
npm install mssql
npm install tedious
npm install knex


다음으로 확인해보아야 할것은 Sql Server 구성 관리자에서 SQL Server Browser와 사용하려는 서버의 Tcp/Ip가 실행중/사용 상태인지 확인해야한다. (실행/사용중이 아니라면 실행/사용중으로 바꾼다.)

TCP 포트 번호는 따로 설정해줬을 경우 기억해둔다.

다음은 knex로 데이터베이스에 연결하기 위한 Config정보를 작성한다.
참고로 user와 password는 데이터베이스에 연결할 때 입력하는 아이디와 비밀번호다.

이제 knex를 이용해 데이터베이스의 테이블을 관리할 수 있다.
관련 메소드들은 상단에 링크된 페이지에서 확인 가능하다.
const knexConfiguration = { | |
client: 'mssql', | |
connection: { | |
user: 'Input your user name', | |
password: 'Input your password', | |
server: 'localhost', | |
database: 'Input your database name', | |
port: Input your port number, | |
} | |
} | |
const knex = require('knex')(knexConfiguration) | |
//Insert('test',{'hello':'bye','hi':'ayaya'}) | |
//Insert('test',{'hi':'ayaya'}) | |
//Update('test',{'hi':'this is update test'} ,{'hello':'this is update test'}) | |
//Delete('test','hello','bye') | |
Select('test') | |
function Insert(table,data) {//data는 {column:value} 형식 | |
//테이블을 가지고 있는 상태에서 받아온 data가 구조에 맞는지 확인 후 실행할것. | |
knex(table) | |
.insert(data) | |
.then(resp => { | |
console.log(resp) | |
}) | |
.catch(err => { | |
console.log(err) | |
}) | |
} | |
function Update(table,target, data) { | |
let targetKey= Object.keys(target)[0] | |
let dataKey = Object.keys(data)[0] | |
knex(table) | |
.where(target) | |
.update(data,[targetKey,dataKey],{includeTriggerModifications:true}) | |
.then(resp=>{ | |
console.log(resp) | |
}) | |
.catch(err =>{ | |
console.log(err) | |
}) | |
} | |
function Delete(table, targetColumn, targetData) { | |
knex(table).where(targetColumn, targetData).del() | |
.then(resp => { | |
console.log(resp) | |
}) | |
.catch(err => { | |
console.log(err) | |
}) | |
} | |
function Select(table) { | |
knex.select().table(table) | |
.then(resp => { | |
console.log(resp) | |
return resp | |
} | |
) | |
.catch(err => { | |
console.log(err) | |
}) | |
} | |
function GetTableColumns(table) { | |
return new knex('INFORMATION_SCHEMA.COLUMNS').where({ | |
'TABLE_NAME': table, | |
}).select('COLUMN_NAME') | |
} | |
module.exports = { | |
getTableColumns: function(table){ | |
return GetTableColumns(table) | |
}, | |
selectTable: function(table){ | |
Select(table) | |
}, | |
insertTable:function(table,data){ | |
Insert(table,data) | |
}, | |
updateTable:function(table,target,data){ | |
Update(table,target,data) | |
}, | |
deleteTable:function(table, targetColumn, targetData){ | |
Delete(table, targetColumn, targetData) | |
} | |
} |

위와 같이 SELECT로 정상적으로 테이블이 불러와지는것을 확인할 수 있다.