budibase/packages/server/src/integrations/postgres.js

85 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-11-26 15:43:56 +01:00
const { Client } = require("pg")
const SCHEMA = {
2021-01-15 18:29:46 +01:00
docs: "https://node-postgres.com",
friendlyName: "PostgreSQL",
datasource: {
host: {
type: "string",
default: "localhost",
required: true,
},
port: {
type: "number",
required: true,
default: 5432,
},
database: {
type: "string",
default: "postgres",
required: true,
},
user: {
type: "string",
default: "root",
required: true,
},
password: {
type: "password",
default: "root",
required: true,
},
},
2021-01-11 16:34:43 +01:00
query: {
2021-01-13 15:11:53 +01:00
create: {
type: "sql",
2021-01-13 15:11:53 +01:00
},
read: {
type: "sql",
2021-01-13 15:11:53 +01:00
},
update: {
type: "sql",
2021-01-13 15:11:53 +01:00
},
delete: {
type: "sql",
},
},
}
2020-11-26 15:43:56 +01:00
class PostgresIntegration {
2021-01-13 15:11:53 +01:00
constructor(config) {
2020-11-26 15:43:56 +01:00
this.config = config
this.client = new Client(config)
this.connect()
}
async connect() {
return this.client.connect()
}
2021-01-13 15:11:53 +01:00
async create({ sql }) {
const response = await this.client.query(sql)
2021-01-26 17:02:44 +01:00
return response.rows.length ? response.rows : [{ created: true }]
}
2021-01-11 18:18:22 +01:00
2021-01-13 15:11:53 +01:00
async read({ sql }) {
const response = await this.client.query(sql)
2021-01-11 18:18:22 +01:00
return response.rows
}
2021-01-13 15:11:53 +01:00
async update({ sql }) {
const response = await this.client.query(sql)
2021-01-26 17:02:44 +01:00
return response.rows.length ? response.rows : [{ updated: true }]
}
2021-01-11 18:18:22 +01:00
2021-01-13 15:11:53 +01:00
async delete({ sql }) {
const response = await this.client.query(sql)
2021-01-26 17:02:44 +01:00
return response.rows.length ? response.rows : [{ deleted: true }]
}
2020-11-26 15:43:56 +01:00
}
module.exports = {
2021-01-11 16:34:43 +01:00
schema: SCHEMA,
2020-11-26 15:43:56 +01:00
integration: PostgresIntegration,
}