budibase/packages/server/src/integrations/oracle.ts

151 lines
4.0 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-08 23:08:47 +01: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
// ssl?: boolean
// ca?: string
// rejectUnauthorized?: boolean
}
const SCHEMA: Integration = {
2021-11-08 23:08:47 +01:00
docs: "https://github.com/oracle/node-oracledb",
friendlyName: "Oracle",
2021-11-08 23:08:47 +01: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,
},
// ssl: {
// type: DatasourceFieldTypes.BOOLEAN,
// default: false,
// required: false,
// },
// rejectUnauthorized: {
// type: DatasourceFieldTypes.BOOLEAN,
// default: false,
// required: false,
// },
// ca: {
// type: DatasourceFieldTypes.LONGFORM,
// default: false,
// required: false,
// },
},
query: {
create: {
type: QueryTypes.SQL,
},
read: {
type: QueryTypes.SQL,
},
update: {
type: QueryTypes.SQL,
},
delete: {
type: QueryTypes.SQL,
},
},
}
2021-11-08 23:08:47 +01: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-08 23:08:47 +01: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-08 23:08:47 +01:00
const attributes: ConnectionAttributes = {
user: this.config.user,
password: this.config.user,
2021-11-08 23:08:47 +01:00
connectString,
}
2021-11-08 23:08:47 +01:00
return oracledb.getConnection(attributes);
}
2021-11-08 23:08:47 +01: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-08 23:08:47 +01:00
async read(query: SqlQuery | string) {
const response = await this.query(getSqlQuery(query))
return response.rows
}
2021-11-08 23:08:47 +01: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-08 23:08:47 +01: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,
}
}