2020-11-26 15:43:56 +01:00
|
|
|
const { Client } = require("pg")
|
|
|
|
|
2021-01-08 13:06:37 +01:00
|
|
|
const SCHEMA = {
|
|
|
|
datasource: {
|
|
|
|
host: {
|
|
|
|
type: "string",
|
|
|
|
default: "localhost",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
port: {
|
|
|
|
type: "number",
|
|
|
|
required: true,
|
|
|
|
default: 5432,
|
|
|
|
},
|
|
|
|
database: {
|
|
|
|
type: "string",
|
|
|
|
default: "postgres",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
username: {
|
|
|
|
type: "string",
|
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type: "password",
|
|
|
|
default: "root",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
2021-01-11 16:34:43 +01:00
|
|
|
query: {
|
2021-01-11 22:01:21 +01:00
|
|
|
SQL: {
|
2021-01-08 13:06:37 +01:00
|
|
|
type: "sql",
|
|
|
|
},
|
2021-01-11 22:01:21 +01:00
|
|
|
"Simple Query": {
|
|
|
|
type: "fields",
|
2021-01-08 13:06:37 +01:00
|
|
|
fields: {
|
2021-01-11 22:01:21 +01:00
|
|
|
table: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
column: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
condition: {
|
|
|
|
type: "options",
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "Equals",
|
|
|
|
value: "=",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Not Equals",
|
|
|
|
value: "!=",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Greater Than",
|
|
|
|
value: ">",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Less Than",
|
|
|
|
value: "<",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
type: "string",
|
|
|
|
},
|
2021-01-08 13:06:37 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-26 15:43:56 +01:00
|
|
|
class PostgresIntegration {
|
2020-12-18 19:19:43 +01:00
|
|
|
constructor(config, query) {
|
2020-11-26 15:43:56 +01:00
|
|
|
this.config = config
|
2020-12-18 19:19:43 +01:00
|
|
|
this.queryString = this.buildQuery(query)
|
2020-11-26 15:43:56 +01:00
|
|
|
this.client = new Client(config)
|
|
|
|
this.connect()
|
|
|
|
}
|
|
|
|
|
2020-12-18 19:19:43 +01:00
|
|
|
buildQuery(query) {
|
|
|
|
// TODO: account for different types
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2020-11-26 15:43:56 +01:00
|
|
|
async connect() {
|
|
|
|
return this.client.connect()
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:18:22 +01:00
|
|
|
// async create() {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
async read() {
|
|
|
|
const response = await this.client.query(this.queryString)
|
|
|
|
return response.rows
|
|
|
|
}
|
|
|
|
|
|
|
|
// async update() {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// async delete() {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
2020-11-26 15:43:56 +01:00
|
|
|
async query() {
|
2020-12-18 19:19:43 +01:00
|
|
|
const response = await this.client.query(this.queryString)
|
2020-11-26 15:43:56 +01:00
|
|
|
return response.rows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2021-01-11 16:34:43 +01:00
|
|
|
schema: SCHEMA,
|
2020-11-26 15:43:56 +01:00
|
|
|
integration: PostgresIntegration,
|
|
|
|
}
|