1
0
Fork 0
mirror of synced 2024-07-02 04:50:44 +12:00
budibase/packages/server/src/integrations/postgres.js

87 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-11-27 03:43:56 +13:00
const { Client } = require("pg")
const SCHEMA = {
2021-01-16 06:29:46 +13:00
docs: "https://node-postgres.com",
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: "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-12 04:34:43 +13:00
query: {
2021-01-14 03:11:53 +13:00
create: {
type: "sql",
2021-01-14 03:11:53 +13:00
},
read: {
type: "sql",
2021-01-14 03:11:53 +13:00
},
update: {
type: "sql",
2021-01-14 03:11:53 +13:00
},
delete: {
type: "sql",
},
},
}
2020-11-27 03:43:56 +13:00
class PostgresIntegration {
2021-01-14 03:11:53 +13:00
constructor(config) {
2020-11-27 03:43:56 +13:00
this.config = config
this.client = new Client(config)
this.connect()
}
async connect() {
return this.client.connect()
}
2021-01-14 03:11:53 +13:00
async create({ sql }) {
const response = await this.client.query(sql)
2021-01-27 05:02:44 +13:00
return response.rows.length ? response.rows : [{ created: true }]
}
2021-01-12 06:18:22 +13:00
2021-01-14 03:11:53 +13:00
async read({ sql }) {
const response = await this.client.query(sql)
2021-01-12 06:18:22 +13:00
return response.rows
}
2021-01-14 03:11:53 +13:00
async update({ sql }) {
const response = await this.client.query(sql)
2021-01-27 05:02:44 +13:00
return response.rows.length ? response.rows : [{ updated: true }]
}
2021-01-12 06:18:22 +13:00
2021-01-14 03:11:53 +13:00
async delete({ sql }) {
const response = await this.client.query(sql)
2021-01-27 05:02:44 +13:00
return response.rows.length ? response.rows : [{ deleted: true }]
}
2020-11-27 03:43:56 +13:00
}
module.exports = {
2021-01-12 04:34:43 +13:00
schema: SCHEMA,
2020-11-27 03:43:56 +13:00
integration: PostgresIntegration,
}