ODBC Connector
소개
이 커넥터는 데이터베이스에 ODBC를 이용하여 연결하고, API와 IoT 형식으로 SCLAB 쪽에 데이터를 연결할 수 있게 해줍니다.
Features
- unixODBC를 이용하여 디비 연결
- SQL 쿼리를 사용하여 일정 간격으로 MQTT 메시지 발행하기
- SQL 쿼리로 REST API 엔드포인트 생성하기
- HTTP 인증을 위한 JWT
- 결과 데이터를 JSON 형식으로 처리하기
- SQL Injection 차단 (기본 on)
- Mybatis 매핑
.env 파일에 "QUERY_1=mqtt;query;topic;interval ms" 형식으로 쿼리를 추가하면, SQL을 사용하여 데이터 가져오고 SCLAB과 연결합니다.
두 가지 방법을 지원합니다: MQTT와 API, 각각 다른 변수 형식을 사용합니다:
MQTT 형식:
QUERY_#=mqtt;SQL Query;topic;interval MS
API 형식:
QUERY_#=api;SQL Query;Endpoint URL
설치방법
사전 준비사항
- install nodejs
- unixODBC binaries and development libraries for module compilation
- on Ubuntu/Debian
sudo apt-get install unixodbc unixodbc-dev
- on RedHat/CentOS
sudo yum install unixODBC unixODBC-devel
- on OSX
- using macports.org
sudo port unixODBC
- using brew
brew install unixODBC
- using macports.org
- on FreeBSD from ports
cd /usr/ports/databases/unixODBC; make install
- on IBM i
yum install unixODBC unixODBC-devel
(requires yum)
- on Ubuntu/Debian
- ODBC drivers for target database
- properly configured odbc.ini and odbcinst.ini.
- print config info
odbcinst -j
- test dsn
isql -v mydsn myusername mypassword
Sample /etc/odbcinst.ini
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/psqlodbcw.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbcw.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
[Tibero]
Description = ODBC for Tibero
Driver = /home/tibero/tibero7/client/lib/libtbodbc.so
Setup = /home/tibero/tibero7/client/lib/libtbodbc.so
Setup = 1
FileUsage = 1
Sample ~/.odbc.ini
[ODBC Data Sources]
MyDB = MySQL
MyTB = Tibero 7
[ODBC]
Trace = yes
TraceFile = /home/odbc.log
[MyDB]
Driver = MySQL
server = 127.0.0.1
port = 3306
user = myuser
database = mydb
password = password
[MyTB]
driver = Tibero
server = 127.0.0.1
port = 8629
SID = tibero
user = sys
password = tibero
Database = tibero
Node.js 지원 정보
- Node.js 12
- Node.js 14
- Node.js 16
- Node.js 18
nodejs 18 설치
curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install nodejs
소스 받기
$ git clone https://github.com/sclab-io/sclab-odbc-connector-node
create JWT key file for API
$ mkdir jwt
$ ssh-keygen -t rsa -b 4096 -m PEM -f ./jwt/jwtRS256.key
# empty passphrase - just press enter
$ openssl rsa -in ./jwt/jwtRS256.key -pubout -outform PEM -out ./jwt/jwtRS256.key.pub
create .env.production.local
$ vi .env.production.local
# ODBC Connection string
CONNECTION_STRING="DSN=MyDB"
MAX_POOL_SIZE=5
# SCLAB IoT (Remove this environment if you do not need to use MQTT)
# MQTT_TOPIC=yourtopic/
# MQTT_HOST=yourhost
# MQTT_CLIENT_ID=your-client-id/1
# MQTT_ID=your-id
# MQTT_PASSWORD=your-password
# QUERY_#=mqtt;query;topic;interval ms
# QUERY_#=api;query;endPoint
QUERY_1=api;SELECT ROUND( RAND() * 100 ) AS value, NOW() AS datetime;/api/1
QUERY_2=api;SELECT ${field} from ${table} where name="${name}";/api/2
# QUERY_3=mqtt;SELECT ROUND( RAND() * 100 ) AS value, NOW() AS datetime;test0;1000
# QUERY_4=mqtt;SELECT ROUND( RAND() * 1000 ) AS value, NOW() AS datetime;test1;5000
# QUERY_mybatis_1=mybatis;sample;test;/api/mybatistest
# PORT
PORT=3000
# MyBatis
MY_BATIS_FILE_FOLDER=./mybatis
# TOKEN
SECRET_KEY=secretKey
JWT_PRIVATE_KEY_PATH=./jwt/jwtRS256.key
JWT_PUBLIC_KEY_PATH=./jwt/jwtRS256.key.pub
# LOG
LOG_FORMAT=combined
LOG_DIR=../logs
# CORS
ORIGIN=your.domain.com
CREDENTIALS=true
# SQL INJECTION
SQL_INJECTION=1