1
0
Fork 0
mirror of synced 2024-07-11 17:26:01 +12:00
budibase/packages/server/src/integrations/oracle.ts

133 lines
3.6 KiB
TypeScript
Raw Normal View History

import {
Integration,
DatasourceFieldTypes,
QueryTypes,
SqlQuery,
} from "../definitions/datasource"
import { Table } from "../definitions/common"
import { getSqlQuery } from "./utils"
2021-11-09 11:08:47 +13:00
import oracledb, { ExecuteOptions, Result } from "oracledb"
import { Connection, ConnectionAttributes } from "oracledb"
import Sql from "./base/sql"
module OracleModule {
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
interface OracleConfig {
host: string
port: number
database: string
user: string
password: string
}
const SCHEMA: Integration = {
2021-11-09 11:08:47 +13:00
docs: "https://github.com/oracle/node-oracledb",
friendlyName: "Oracle",
2021-11-09 11:08:47 +13:00
description: "Oracle Database is an object-relational database management system developed by Oracle Corporation",
datasource: {
host: {
type: DatasourceFieldTypes.STRING,
default: "localhost",
required: true,
},
port: {
type: DatasourceFieldTypes.NUMBER,
required: true,
default: 1521,
},
database: {
type: DatasourceFieldTypes.STRING,
required: true,
},
user: {
type: DatasourceFieldTypes.STRING,
required: true,
},
password: {
type: DatasourceFieldTypes.PASSWORD,
required: true,
2021-11-10 06:33:29 +13:00
}
},
query: {
create: {
type: QueryTypes.SQL,
},
read: {
type: QueryTypes.SQL,
},
update: {
type: QueryTypes.SQL,
},
delete: {
type: QueryTypes.SQL,
},
},
}
2021-11-09 11:08:47 +13:00
class OracleIntegration extends Sql {
private readonly config: OracleConfig
public tables: Record<string, Table> = {}
public schemaErrors: Record<string, string> = {}
constructor(config: OracleConfig) {
super("oracle")
this.config = config
}
2021-11-09 11:08:47 +13:00
private query = async (query: SqlQuery): Promise<Result<any>> => {
let connection
try {
connection = await this.getConnection()
const options : ExecuteOptions = { autoCommit: true }
const result: Result<any> = await connection.execute(query.sql, [], options)
return result
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
private getConnection = async (): Promise<Connection> => {
//connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SID= ORCL)))"
const connectString = `${this.config.host}:${this.config.port || 1521}/${this.config.database}`
2021-11-09 11:08:47 +13:00
const attributes: ConnectionAttributes = {
user: this.config.user,
password: this.config.user,
2021-11-09 11:08:47 +13:00
connectString,
}
2021-11-09 11:08:47 +13:00
return oracledb.getConnection(attributes);
}
2021-11-09 11:08:47 +13:00
async create(query: SqlQuery | string) {
const response = await this.query(getSqlQuery(query))
return response.rows && response.rows.length ? response.rows : [{ created: true }]
}
2021-11-09 11:08:47 +13:00
async read(query: SqlQuery | string) {
const response = await this.query(getSqlQuery(query))
return response.rows
}
2021-11-09 11:08:47 +13:00
async update(query: SqlQuery | string) {
const response = await this.query(getSqlQuery(query))
return response.rows && response.rows.length ? response.rows : [{ updated: true }]
}
2021-11-09 11:08:47 +13:00
async delete(query: SqlQuery | string) {
const response = await this.query(getSqlQuery(query))
return response.rows && response.rows.length ? response.rows : [{ deleted: true }]
}
}
module.exports = {
schema: SCHEMA,
integration: OracleIntegration,
}
}