2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
QueryTypes,
|
|
|
|
QueryJson,
|
2021-06-25 14:46:02 +02:00
|
|
|
SqlQuery,
|
2021-06-27 00:09:46 +02:00
|
|
|
} from "../definitions/datasource"
|
|
|
|
import { Table } from "../definitions/common"
|
2021-11-08 19:12:40 +01:00
|
|
|
import {
|
|
|
|
getSqlQuery,
|
|
|
|
buildExternalTableId,
|
|
|
|
convertSqlType,
|
|
|
|
finaliseExternalTables,
|
|
|
|
SqlClients,
|
|
|
|
} from "./utils"
|
2021-10-26 21:03:54 +02:00
|
|
|
import { DatasourcePlus } from "./base/datasourcePlus"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
module PostgresModule {
|
2022-06-07 09:31:00 +02:00
|
|
|
const { Client, types } = require("pg")
|
2021-06-24 19:16:48 +02:00
|
|
|
const Sql = require("./base/sql")
|
2021-09-24 19:10:30 +02:00
|
|
|
const { escapeDangerousCharacters } = require("../utilities")
|
|
|
|
|
2022-06-07 09:31:00 +02:00
|
|
|
// Return "date" and "timestamp" types as plain strings.
|
|
|
|
// This lets us reference the original stored timezone.
|
2022-06-07 09:41:45 +02:00
|
|
|
// types is undefined when running in a test env for some reason.
|
|
|
|
if (types) {
|
|
|
|
types.setTypeParser(1114, (val: any) => val) // timestamp
|
|
|
|
types.setTypeParser(1082, (val: any) => val) // date
|
|
|
|
types.setTypeParser(1184, (val: any) => val) // timestampz
|
|
|
|
}
|
2022-06-07 09:31:00 +02:00
|
|
|
|
2021-09-24 19:10:30 +02:00
|
|
|
const JSON_REGEX = /'{.*}'::json/s
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
interface PostgresConfig {
|
2021-06-24 19:17:26 +02:00
|
|
|
host: string
|
|
|
|
port: number
|
|
|
|
database: string
|
|
|
|
user: string
|
|
|
|
password: string
|
2021-10-27 17:21:19 +02:00
|
|
|
schema: string
|
2021-06-24 19:17:26 +02:00
|
|
|
ssl?: boolean
|
2021-10-12 17:13:54 +02:00
|
|
|
ca?: string
|
|
|
|
rejectUnauthorized?: boolean
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://node-postgres.com",
|
|
|
|
plus: true,
|
|
|
|
friendlyName: "PostgreSQL",
|
|
|
|
description:
|
|
|
|
"PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance.",
|
|
|
|
datasource: {
|
|
|
|
host: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "localhost",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
port: {
|
|
|
|
type: DatasourceFieldTypes.NUMBER,
|
|
|
|
required: true,
|
|
|
|
default: 5432,
|
|
|
|
},
|
|
|
|
database: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "postgres",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type: DatasourceFieldTypes.PASSWORD,
|
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
2021-10-27 17:21:19 +02:00
|
|
|
schema: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
default: "public",
|
|
|
|
required: true,
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
ssl: {
|
|
|
|
type: DatasourceFieldTypes.BOOLEAN,
|
|
|
|
default: false,
|
|
|
|
required: false,
|
|
|
|
},
|
2021-10-12 17:13:54 +02:00
|
|
|
rejectUnauthorized: {
|
|
|
|
type: DatasourceFieldTypes.BOOLEAN,
|
|
|
|
default: false,
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
ca: {
|
|
|
|
type: DatasourceFieldTypes.LONGFORM,
|
|
|
|
default: false,
|
|
|
|
required: false,
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryTypes.SQL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:03:54 +02:00
|
|
|
class PostgresIntegration extends Sql implements DatasourcePlus {
|
2021-06-24 19:16:48 +02:00
|
|
|
private readonly client: any
|
|
|
|
private readonly config: PostgresConfig
|
2022-03-02 23:45:10 +01:00
|
|
|
private index: number = 1
|
2021-10-26 21:03:54 +02:00
|
|
|
public tables: Record<string, Table> = {}
|
|
|
|
public schemaErrors: Record<string, string> = {}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2021-11-12 20:39:12 +01:00
|
|
|
COLUMNS_SQL!: string
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
PRIMARY_KEYS_SQL = `
|
|
|
|
select tc.table_schema, tc.table_name, kc.column_name as primary_key
|
|
|
|
from information_schema.table_constraints tc
|
|
|
|
join
|
|
|
|
information_schema.key_column_usage kc on kc.table_name = tc.table_name
|
|
|
|
and kc.table_schema = tc.table_schema
|
|
|
|
and kc.constraint_name = tc.constraint_name
|
|
|
|
where tc.constraint_type = 'PRIMARY KEY';
|
|
|
|
`
|
|
|
|
|
|
|
|
constructor(config: PostgresConfig) {
|
2021-11-08 19:12:40 +01:00
|
|
|
super(SqlClients.POSTGRES)
|
2021-06-24 19:16:48 +02:00
|
|
|
this.config = config
|
|
|
|
|
|
|
|
let newConfig = {
|
|
|
|
...this.config,
|
2021-10-12 17:13:54 +02:00
|
|
|
ssl: this.config.ssl
|
|
|
|
? {
|
|
|
|
rejectUnauthorized: this.config.rejectUnauthorized,
|
|
|
|
ca: this.config.ca,
|
|
|
|
}
|
|
|
|
: undefined,
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-04-29 00:24:52 +02:00
|
|
|
this.client = new Client(newConfig)
|
2022-05-20 18:41:27 +02:00
|
|
|
this.open = false
|
2021-10-27 17:21:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 23:45:10 +01:00
|
|
|
getBindingIdentifier(): string {
|
|
|
|
return `$${this.index++}`
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:19:26 +01:00
|
|
|
getStringConcat(parts: string[]): string {
|
|
|
|
return parts.join(" || ")
|
|
|
|
}
|
|
|
|
|
2022-05-20 18:41:27 +02:00
|
|
|
async openConnection() {
|
|
|
|
await this.client.connect()
|
|
|
|
if (!this.config.schema) {
|
|
|
|
this.config.schema = "public"
|
|
|
|
}
|
|
|
|
this.client.query(`SET search_path TO ${this.config.schema}`)
|
|
|
|
this.COLUMNS_SQL = `select * from information_schema.columns where table_schema = '${this.config.schema}'`
|
|
|
|
this.open = true
|
|
|
|
}
|
|
|
|
|
|
|
|
closeConnection() {
|
|
|
|
const pg = this
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
this.client.end((err: any) => {
|
|
|
|
pg.open = false
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:13:40 +02:00
|
|
|
async internalQuery(query: SqlQuery, close: boolean = true) {
|
2022-05-20 18:41:27 +02:00
|
|
|
if (!this.open) {
|
|
|
|
await this.openConnection()
|
|
|
|
}
|
2022-03-02 23:45:10 +01:00
|
|
|
const client = this.client
|
|
|
|
this.index = 1
|
|
|
|
// need to handle a specific issue with json data types in postgres,
|
|
|
|
// new lines inside the JSON data will break it
|
|
|
|
if (query && query.sql) {
|
|
|
|
const matches = query.sql.match(JSON_REGEX)
|
|
|
|
if (matches && matches.length > 0) {
|
|
|
|
for (let match of matches) {
|
|
|
|
const escaped = escapeDangerousCharacters(match)
|
|
|
|
query.sql = query.sql.replace(match, escaped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await client.query(query.sql, query.bindings || [])
|
|
|
|
} catch (err) {
|
2022-05-20 18:41:27 +02:00
|
|
|
await this.closeConnection()
|
2022-03-02 23:45:10 +01:00
|
|
|
// @ts-ignore
|
|
|
|
throw new Error(err)
|
2022-04-29 00:24:52 +02:00
|
|
|
} finally {
|
2022-05-20 18:41:27 +02:00
|
|
|
if (close) {
|
|
|
|
await this.closeConnection()
|
|
|
|
}
|
2021-10-27 17:21:19 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the tables from the postgres table and assigns them to the datasource.
|
|
|
|
* @param {*} datasourceId - datasourceId to fetch
|
2021-07-03 11:26:37 +02:00
|
|
|
* @param entities - the tables that are to be built
|
2021-06-24 19:16:48 +02:00
|
|
|
*/
|
2021-07-02 15:33:05 +02:00
|
|
|
async buildSchema(datasourceId: string, entities: Record<string, Table>) {
|
2021-06-24 19:17:26 +02:00
|
|
|
let tableKeys: { [key: string]: string[] } = {}
|
2022-05-20 18:43:47 +02:00
|
|
|
await this.openConnection()
|
2021-06-24 19:16:48 +02:00
|
|
|
try {
|
2021-06-24 19:17:26 +02:00
|
|
|
const primaryKeysResponse = await this.client.query(
|
|
|
|
this.PRIMARY_KEYS_SQL
|
|
|
|
)
|
2021-06-24 19:16:48 +02:00
|
|
|
for (let table of primaryKeysResponse.rows) {
|
|
|
|
const tableName = table.table_name
|
|
|
|
if (!tableKeys[tableName]) {
|
|
|
|
tableKeys[tableName] = []
|
|
|
|
}
|
2021-07-06 16:45:14 +02:00
|
|
|
const key = table.column_name || table.primary_key
|
|
|
|
// only add the unique keys
|
|
|
|
if (key && tableKeys[tableName].indexOf(key) === -1) {
|
|
|
|
tableKeys[tableName].push(key)
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
tableKeys = {}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:13:40 +02:00
|
|
|
try {
|
|
|
|
const columnsResponse = await this.client.query(this.COLUMNS_SQL)
|
|
|
|
|
|
|
|
const tables: { [key: string]: Table } = {}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-05-10 12:13:40 +02:00
|
|
|
for (let column of columnsResponse.rows) {
|
|
|
|
const tableName: string = column.table_name
|
|
|
|
const columnName: string = column.column_name
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-05-10 12:13:40 +02:00
|
|
|
// table key doesn't exist yet
|
|
|
|
if (!tables[tableName] || !tables[tableName].schema) {
|
|
|
|
tables[tableName] = {
|
|
|
|
_id: buildExternalTableId(datasourceId, tableName),
|
|
|
|
primary: tableKeys[tableName] || [],
|
|
|
|
name: tableName,
|
|
|
|
schema: {},
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2022-05-10 12:13:40 +02:00
|
|
|
const identity = !!(
|
|
|
|
column.identity_generation ||
|
|
|
|
column.identity_start ||
|
|
|
|
column.identity_increment
|
|
|
|
)
|
|
|
|
const hasDefault =
|
|
|
|
typeof column.column_default === "string" &&
|
|
|
|
column.column_default.startsWith("nextval")
|
|
|
|
const isGenerated =
|
|
|
|
column.is_generated && column.is_generated !== "NEVER"
|
|
|
|
const isAuto: boolean = hasDefault || identity || isGenerated
|
|
|
|
tables[tableName].schema[columnName] = {
|
|
|
|
autocolumn: isAuto,
|
|
|
|
name: columnName,
|
|
|
|
...convertSqlType(column.data_type),
|
2022-06-20 19:08:30 +02:00
|
|
|
externalType: column.data_type,
|
2022-05-10 12:13:40 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2021-09-22 18:46:54 +02:00
|
|
|
|
2022-05-10 12:13:40 +02:00
|
|
|
const final = finaliseExternalTables(tables, entities)
|
|
|
|
this.tables = final.tables
|
|
|
|
this.schemaErrors = final.errors
|
|
|
|
} catch (err) {
|
|
|
|
// @ts-ignore
|
|
|
|
throw new Error(err)
|
|
|
|
} finally {
|
2022-05-20 18:41:27 +02:00
|
|
|
await this.closeConnection()
|
2022-05-10 12:13:40 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
async create(query: SqlQuery | string) {
|
2022-03-02 23:45:10 +01:00
|
|
|
const response = await this.internalQuery(getSqlQuery(query))
|
2021-06-24 19:17:26 +02:00
|
|
|
return response.rows.length ? response.rows : [{ created: true }]
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
async read(query: SqlQuery | string) {
|
2022-03-02 23:45:10 +01:00
|
|
|
const response = await this.internalQuery(getSqlQuery(query))
|
2021-06-24 19:16:48 +02:00
|
|
|
return response.rows
|
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
async update(query: SqlQuery | string) {
|
2022-03-02 23:45:10 +01:00
|
|
|
const response = await this.internalQuery(getSqlQuery(query))
|
2021-06-24 19:17:26 +02:00
|
|
|
return response.rows.length ? response.rows : [{ updated: true }]
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
async delete(query: SqlQuery | string) {
|
2022-03-02 23:45:10 +01:00
|
|
|
const response = await this.internalQuery(getSqlQuery(query))
|
2021-06-24 19:17:26 +02:00
|
|
|
return response.rows.length ? response.rows : [{ deleted: true }]
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async query(json: QueryJson) {
|
|
|
|
const operation = this._operation(json).toLowerCase()
|
|
|
|
const input = this._query(json)
|
2021-10-28 20:39:42 +02:00
|
|
|
if (Array.isArray(input)) {
|
|
|
|
const responses = []
|
|
|
|
for (let query of input) {
|
2022-05-10 12:13:40 +02:00
|
|
|
responses.push(await this.internalQuery(query, false))
|
2021-10-28 20:39:42 +02:00
|
|
|
}
|
2022-05-20 18:41:27 +02:00
|
|
|
await this.closeConnection()
|
2021-10-28 20:39:42 +02:00
|
|
|
return responses
|
|
|
|
} else {
|
2022-03-02 23:45:10 +01:00
|
|
|
const response = await this.internalQuery(input)
|
2021-10-28 20:39:42 +02:00
|
|
|
return response.rows.length ? response.rows : [{ [operation]: true }]
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: PostgresIntegration,
|
|
|
|
}
|
|
|
|
}
|