Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres (minor update) DeleteRow, UpdateRow, CreateRow update #222

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/appmixer/postgres/bundle.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "appmixer.postgres",
"version": "1.0.2",
"version": "1.1.0",
"changelog": {
"1.0.0": [
"Initial version"
],
"1.0.2": [
"Password input changed to type password.",
"Postgres lib upgraded to the newest version."
],
"1.1.0": [
"CreateRow: poolSize configuration removed."
]
}
}
54 changes: 33 additions & 21 deletions src/appmixer/postgres/db/CreateRow/CreateRow.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
'use strict';
const { Pool } = require('pg');

module.exports = {

async connect(context) {

if (this.pool) {
return this.pool.connect();
}
const { Pool } = require('pg');
const crypto = require('crypto');

this.pool = new Pool({
user: context.auth.dbUser,
host: context.auth.dbHost,
database: context.auth.database,
password: context.auth.dbPassword,
port: context.auth.dbPort,
poolSize: context.properties.poolSize || 1
});
const CONNECTIONS = {};

return this.pool.connect();
},
module.exports = {

async receive(context) {

Expand All @@ -30,7 +16,7 @@

let query = `INSERT INTO ${context.properties.table}(${columns.join(',')}) VALUES(${valuesMarkers.join(',')}) RETURNING *`;

let client = await this.connect(context);
let client = await connect(context);
try {
let res = await client.query(query, values);
await context.sendJson(res.rows[0], 'newRow');
Expand All @@ -41,8 +27,34 @@

async stop(context) {

if (this.pool) {
await this.pool.end();
const connectionId = connectionHash(context.auth);
if (CONNECTIONS[connectionId]) {
await CONNECTIONS[connectionId].end();
}
}
};

function connectionHash(auth) {

const authString = JSON.stringify(auth);
return crypto.createHash('md5').update(authString).digest('hex');
};

Check failure on line 41 in src/appmixer/postgres/db/CreateRow/CreateRow.js

View workflow job for this annotation

GitHub Actions / build

Expected blank line before this statement

async function connect(context) {

const connectionId = connectionHash(context.auth);
if (CONNECTIONS[connectionId]) {
return CONNECTIONS[connectionId].connect();
}

const pool = CONNECTIONS[connectionId] = new Pool({
user: context.auth.dbUser,
host: context.auth.dbHost,
database: context.auth.database,
password: context.auth.dbPassword,
port: context.auth.dbPort,
poolSize: context.config.CreateRowPoolSize || 1
});

return pool.connect();
}
12 changes: 1 addition & 11 deletions src/appmixer/postgres/db/CreateRow/component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appmixer.postgres.db.CreateRow",
"author": "David Durman <[email protected]>",
"author": "Appmixer <[email protected]>",
"description": "Create a new row in PostgreSQL database table.",
"private": false,
"auth": {
Expand All @@ -11,9 +11,6 @@
"properties": {
"table": {
"type": "string"
},
"poolSize": {
"type": "number"
}
},
"required": [
Expand All @@ -32,13 +29,6 @@
"transform": "./ListTables#toSelectArray"
}
}
},
"poolSize": {
"type": "number",
"label": "Connection pool size",
"index": 2,
"defaultValue": 1,
"tooltip": "Limit maximum number of connections for this component."
}
}
}
Expand Down
99 changes: 99 additions & 0 deletions src/appmixer/postgres/db/DeleteRow/DeleteRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

const { Pool } = require('pg');
const crypto = require('crypto');

const CONNECTIONS = {};

module.exports = {

async receive(context) {

const filter = context.messages.in.content.filter;
let where = '';
const whereConditionsAnd = [];

filter.AND.forEach(expressionAnd => {

const whereConditionsOr = [];

expressionAnd.OR.forEach(expressionOr => {

const { column, operator, value } = expressionOr;
let whereCondition = '';
if (operator === 'CONTAINS') {
whereCondition += `${column} LIKE '%${value}%'`;
} else if (operator === 'NOT CONTAINS') {
whereCondition += `${column} NOT LIKE '%${value}%'`;
} else if (operator === 'STARTS WITH') {
whereCondition += `${column} LIKE '${value}%'`;
} else if (operator === 'NOT STARTS WITH') {
whereCondition += `${column} NOT LIKE '%${value}'`;
} else if (operator === 'ENDS WITH') {
whereCondition += `${column} LIKE '%${value}'`;
} else if (operator === 'NOT ENDS WITH') {
whereCondition += `${column} NOT LIKE '%${value}'`;
} else if (operator === 'IN') {
whereCondition += `${column} IN (${value.trim().split(',').map(v => `'${v}'`).join(',')})`;
} else if (operator === 'NOT IN') {
whereCondition += `${column} NOT IN (${value.trim().split(',').map(v => `'${v}'`).join(',')})`;
} else if (operator === 'IS NULL') {
whereCondition += `${column} IS NULL`;
} else if (operator === 'IS NOT NULL') {
whereCondition += `${column} IS NOT NULL`;
} else {
whereCondition += `${column} ${operator} '${value}'`;
}
whereConditionsOr.push(whereCondition);
});

whereConditionsAnd.push('(' + whereConditionsOr.join(' OR ') + ')');
});

where = whereConditionsAnd.join(' AND ');

let query = `DELETE FROM ${context.properties.table} WHERE ${where}`;
await context.log({ step: 'query', query });

let client = await connect(context);
try {
let res = await client.query(query);
await context.sendJson({ rowCount: res.rowCount }, 'out');
} finally {
await client.release();
}
},

async stop(context) {

const connectionId = connectionHash(context.auth);
if (CONNECTIONS[connectionId]) {
await CONNECTIONS[connectionId].end();
}
}
};

function connectionHash(auth) {

const authString = JSON.stringify(auth);
return crypto.createHash('md5').update(authString).digest('hex');
};

Check failure on line 80 in src/appmixer/postgres/db/DeleteRow/DeleteRow.js

View workflow job for this annotation

GitHub Actions / build

Expected blank line before this statement

async function connect(context) {

const connectionId = connectionHash(context.auth);
if (CONNECTIONS[connectionId]) {
return CONNECTIONS[connectionId].connect();
}

const pool = CONNECTIONS[connectionId] = new Pool({
user: context.auth.dbUser,
host: context.auth.dbHost,
database: context.auth.database,
password: context.auth.dbPassword,
port: context.auth.dbPort,
poolSize: context.config.CreateRowPoolSize || 1
});

return pool.connect();
}
120 changes: 120 additions & 0 deletions src/appmixer/postgres/db/DeleteRow/component.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/appmixer/postgres/db/ListColumns/ListColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ module.exports = {
if (column['is_nullable'] === 'NO' && column['column_default'] === null) {
// For example serial type is nullable but has a column_default set
// to: 'nextval(\'shifts_id_seq\'::regclass)'.
inspector.schema.required.push(name);
// We do not return the field as required since the same ListColumns is also
// used in UpdateRow component which does not require any field to have a value.
// Instead, we add the information to the tooltip.
inspector.inputs[name].tooltip = 'This field is required since it is not nullable and does not have a default value.';
}
});
}
Expand Down
104 changes: 104 additions & 0 deletions src/appmixer/postgres/db/UpdateRow/UpdateRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'use strict';

const { Pool } = require('pg');
const crypto = require('crypto');

const CONNECTIONS = {};

module.exports = {

async receive(context) {

const row = context.messages.row.content;
const filter = row.filter;
delete row['filter'];
const columns = Object.keys(row);
const values = Object.values(row);
const set = columns.map((col, index) => `${col} = ${'$' + (index + 1)}`).join(',');
let where = '';
const whereConditionsAnd = [];

filter.AND.forEach(expressionAnd => {

const whereConditionsOr = [];

expressionAnd.OR.forEach(expressionOr => {

const { column, operator, value } = expressionOr;
let whereCondition = '';
if (operator === 'CONTAINS') {
whereCondition += `${column} LIKE '%${value}%'`;
} else if (operator === 'NOT CONTAINS') {
whereCondition += `${column} NOT LIKE '%${value}%'`;
} else if (operator === 'STARTS WITH') {
whereCondition += `${column} LIKE '${value}%'`;
} else if (operator === 'NOT STARTS WITH') {
whereCondition += `${column} NOT LIKE '%${value}'`;
} else if (operator === 'ENDS WITH') {
whereCondition += `${column} LIKE '%${value}'`;
} else if (operator === 'NOT ENDS WITH') {
whereCondition += `${column} NOT LIKE '%${value}'`;
} else if (operator === 'IN') {
whereCondition += `${column} IN (${value.trim().split(',').map(v => `'${v}'`).join(',')})`;
} else if (operator === 'NOT IN') {
whereCondition += `${column} NOT IN (${value.trim().split(',').map(v => `'${v}'`).join(',')})`;
} else if (operator === 'IS NULL') {
whereCondition += `${column} IS NULL`;
} else if (operator === 'IS NOT NULL') {
whereCondition += `${column} IS NOT NULL`;
} else {
whereCondition += `${column} ${operator} '${value}'`;
}
whereConditionsOr.push(whereCondition);
});

whereConditionsAnd.push('(' + whereConditionsOr.join(' OR ') + ')');
});

where = whereConditionsAnd.join(' AND ');

let query = `UPDATE ${context.properties.table} SET ${set} WHERE ${where}`;
await context.log({ step: 'query', query, values });

let client = await connect(context);
try {
let res = await client.query(query, values);
await context.sendJson({ rowCount: res.rowCount }, 'out');
} finally {
await client.release();
}
},

async stop(context) {

const connectionId = connectionHash(context.auth);
if (CONNECTIONS[connectionId]) {
await CONNECTIONS[connectionId].end();
}
}
};

function connectionHash(auth) {

const authString = JSON.stringify(auth);
return crypto.createHash('md5').update(authString).digest('hex');
};

Check failure on line 85 in src/appmixer/postgres/db/UpdateRow/UpdateRow.js

View workflow job for this annotation

GitHub Actions / build

Expected blank line before this statement

async function connect(context) {

const connectionId = connectionHash(context.auth);
if (CONNECTIONS[connectionId]) {
return CONNECTIONS[connectionId].connect();
}

const pool = CONNECTIONS[connectionId] = new Pool({
user: context.auth.dbUser,
host: context.auth.dbHost,
database: context.auth.database,
password: context.auth.dbPassword,
port: context.auth.dbPort,
poolSize: context.config.CreateRowPoolSize || 1
});

return pool.connect();
}
Loading
Loading