summaryrefslogtreecommitdiff
path: root/lib/oracle-knex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/oracle-knex')
-rw-r--r--lib/oracle-knex/README.md54
-rw-r--r--lib/oracle-knex/db.ts68
2 files changed, 122 insertions, 0 deletions
diff --git a/lib/oracle-knex/README.md b/lib/oracle-knex/README.md
new file mode 100644
index 00000000..fdff6e2f
--- /dev/null
+++ b/lib/oracle-knex/README.md
@@ -0,0 +1,54 @@
+# oracledb / knex.js 사용 방법
+
+1. oracledb로 SQL 직접 사용하기
+
+```ts
+ // Oracle DB 연결 설정
+ const connection = await oracledb.getConnection({
+ user: process.env.ORACLE_USER,
+ password: process.env.ORACLE_PASSWORD,
+ connectString: process.env.ORACLE_CONNECTION_STRING
+
+ });
+
+ // 쿼리 수행
+ const result = await connection.execute('SELECT 1 FROM DUAL');
+
+ // 연결 종료
+ await connection.close();
+```
+
+2. knex.js에서 SQL 직접 사용하기
+
+```ts
+export const oracleKnex = knex({
+ client: 'oracledb',
+ connection: {
+ user: process.env.ORACLE_USER,
+ password: process.env.ORACLE_PASSWORD,
+ connectString: process.env.ORACLE_CONNECTION_STRING,
+ },
+ pool: { min: 0, max: 5 }
+});
+
+sql = 'SELECT 1 FROM DUAL;'
+const result = await oracleKnex.raw(sql);
+```
+
+3. knex.js에서 쿼리빌더 사용하기
+```ts
+export const oracleKnex = knex({
+ client: 'oracledb',
+ connection: {
+ user: process.env.ORACLE_USER,
+ password: process.env.ORACLE_PASSWORD,
+ connectString: process.env.ORACLE_CONNECTION_STRING,
+ },
+ pool: { min: 0, max: 5 }
+});
+
+const result = await knex
+.qb
+.select('column1', 'column2', 'column3')
+.from('table_name')
+``` \ No newline at end of file
diff --git a/lib/oracle-knex/db.ts b/lib/oracle-knex/db.ts
new file mode 100644
index 00000000..fe61f0a2
--- /dev/null
+++ b/lib/oracle-knex/db.ts
@@ -0,0 +1,68 @@
+import knex from 'knex';
+// import oracledb from 'oracledb';
+const oracledb = require('oracledb');
+
+// Knex Oracle 연결 생성
+export const oracleKnex = knex({
+ client: 'oracledb',
+ connection: {
+ user: process.env.ORACLE_USER,
+ password: process.env.ORACLE_PASSWORD,
+ connectString: process.env.ORACLE_CONNECTION_STRING,
+ },
+ pool: { min: 0, max: 5 }
+});
+
+// OracleDB 직접 연결 생성 함수
+export async function getOracleConnection() {
+ try {
+ const connection = await oracledb.getConnection({
+ user: process.env.ORACLE_USER,
+ password: process.env.ORACLE_PASSWORD,
+ connectString: process.env.ORACLE_CONNECTION_STRING
+ });
+
+ return connection;
+ } catch (error) {
+ console.error('Oracle DB 연결 오류:', error);
+ throw error;
+ }
+}
+
+// 연결을 테스트하는 함수
+export async function testOracleConnection() {
+ try {
+ const connection = await getOracleConnection();
+ const result = await connection.execute('SELECT 1 FROM DUAL');
+ await connection.close();
+ return {
+ success: true,
+ message: 'Oracle DB 연결 성공',
+ data: result.rows
+ };
+ } catch (error: any) {
+ return {
+ success: false,
+ message: 'Oracle DB 연결 실패',
+ error: error instanceof Error ? error.message : '알 수 없는 오류'
+ };
+ }
+}
+
+// Knex를 사용하여 Oracle 연결 테스트
+export async function testKnexOracleConnection() {
+ try {
+ const result = await oracleKnex.raw('SELECT 1 FROM DUAL');
+ return {
+ success: true,
+ message: 'Knex Oracle DB 연결 성공',
+ data: result
+ };
+ } catch (error: any) {
+ return {
+ success: false,
+ message: 'Knex Oracle DB 연결 실패',
+ error: error instanceof Error ? error.message : '알 수 없는 오류'
+ };
+ }
+} \ No newline at end of file