mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-17 10:04:08 -08:00
41669c0e0f
* add multi return * add independently and transaction to query * pgInsert normal and transaction * independently for pgInsert * normal, transaction and independently for pgUpdate * cleanup * implement it in other nodes * multiple fixes * add optional returning support * clean up Postgres functions * fix other postgres based dbs * Added option to run queries as a transaction to Postgres This commit allows users to configure Postgres, CrateDB, TimescaleDB and QuestDB to run queries independently or as transactions as well as the previous mode which is to execute multiple queries at once. Previous behavior remains untouched so we only added new options. * Standardize behavior across nodes that use postgres protocol Also fixed unit tests. * Added breaking change notice * Added more information to breaking changes * ⚡ Styling fixes Co-authored-by: lublak <[email protected]> Co-authored-by: Jan Oberhauser <[email protected]>
163 lines
4.3 KiB
JavaScript
163 lines
4.3 KiB
JavaScript
const PostgresFun = require('../../../nodes/Postgres/Postgres.node.functions')
|
|
const pgPromise = require('pg-promise');
|
|
|
|
describe('pgUpdate', () => {
|
|
it('runs query to update db', async () => {
|
|
const updateItem = {id: 1234, name: 'test'};
|
|
const nodeParams = {
|
|
table: 'mytable',
|
|
schema: 'myschema',
|
|
updateKey: 'id',
|
|
columns: 'id,name',
|
|
additionalFields: {},
|
|
returnFields: '*',
|
|
};
|
|
const getNodeParam = (key) => nodeParams[key];
|
|
const pgp = pgPromise();
|
|
const any = jest.fn();
|
|
const db = {any};
|
|
|
|
const items = [
|
|
{
|
|
json: updateItem
|
|
}
|
|
];
|
|
|
|
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items)
|
|
|
|
expect(db.any).toHaveBeenCalledWith(`update \"myschema\".\"mytable\" as t set \"id\"=v.\"id\",\"name\"=v.\"name\" from (values(1234,'test')) as v(\"id\",\"name\") WHERE v.\"id\" = t.\"id\" RETURNING *`);
|
|
});
|
|
|
|
it('runs query to update db if updateKey is not in columns', async () => {
|
|
const updateItem = {id: 1234, name: 'test'};
|
|
const nodeParams = {
|
|
table: 'mytable',
|
|
schema: 'myschema',
|
|
updateKey: 'id',
|
|
columns: 'name',
|
|
additionalFields: {},
|
|
returnFields: '*',
|
|
};
|
|
const getNodeParam = (key) => nodeParams[key];
|
|
const pgp = pgPromise();
|
|
const any = jest.fn();
|
|
const db = {any};
|
|
|
|
const items = [
|
|
{
|
|
json: updateItem
|
|
}
|
|
];
|
|
|
|
const results = await PostgresFun.pgUpdate(getNodeParam, pgp, db, items)
|
|
|
|
expect(db.any).toHaveBeenCalledWith(`update \"myschema\".\"mytable\" as t set \"id\"=v.\"id\",\"name\"=v.\"name\" from (values(1234,'test')) as v(\"id\",\"name\") WHERE v.\"id\" = t.\"id\" RETURNING *`);
|
|
});
|
|
|
|
it('runs query to update db with cast as updateKey', async () => {
|
|
const updateItem = {id: '1234', name: 'test'};
|
|
const nodeParams = {
|
|
table: 'mytable',
|
|
schema: 'myschema',
|
|
updateKey: 'id:uuid',
|
|
columns: 'name',
|
|
additionalFields: {},
|
|
returnFields: '*',
|
|
};
|
|
const getNodeParam = (key) => nodeParams[key];
|
|
const pgp = pgPromise();
|
|
const any = jest.fn();
|
|
const db = {any};
|
|
|
|
const items = [
|
|
{
|
|
json: updateItem
|
|
}
|
|
];
|
|
|
|
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items)
|
|
|
|
expect(db.any).toHaveBeenCalledWith(`update \"myschema\".\"mytable\" as t set \"id\"=v.\"id\",\"name\"=v.\"name\" from (values('1234'::uuid,'test')) as v(\"id\",\"name\") WHERE v.\"id\" = t.\"id\" RETURNING *`);
|
|
});
|
|
|
|
it('runs query to update db with cast in target columns', async () => {
|
|
const updateItem = {id: '1234', name: 'test'};
|
|
const nodeParams = {
|
|
table: 'mytable',
|
|
schema: 'myschema',
|
|
updateKey: 'id',
|
|
columns: 'id:uuid,name',
|
|
additionalFields: {},
|
|
returnFields: '*',
|
|
};
|
|
const getNodeParam = (key) => nodeParams[key];
|
|
const pgp = pgPromise();
|
|
const any = jest.fn();
|
|
const db = {any};
|
|
|
|
const items = [
|
|
{
|
|
json: updateItem
|
|
}
|
|
];
|
|
|
|
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items)
|
|
|
|
expect(db.any).toHaveBeenCalledWith(`update \"myschema\".\"mytable\" as t set \"id\"=v.\"id\",\"name\"=v.\"name\" from (values('1234'::uuid,'test')) as v(\"id\",\"name\") WHERE v.\"id\" = t.\"id\" RETURNING *`);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
describe('pgInsert', () => {
|
|
it('runs query to insert', async () => {
|
|
const insertItem = {id: 1234, name: 'test', age: 34};
|
|
const nodeParams = {
|
|
table: 'mytable',
|
|
schema: 'myschema',
|
|
columns: 'id,name,age',
|
|
returnFields: '*',
|
|
additionalFields: {},
|
|
};
|
|
const getNodeParam = (key) => nodeParams[key];
|
|
const pgp = pgPromise();
|
|
const any = jest.fn();
|
|
const db = {any};
|
|
|
|
const items = [
|
|
{
|
|
json: insertItem,
|
|
},
|
|
];
|
|
|
|
await PostgresFun.pgInsert(getNodeParam, pgp, db, items);
|
|
|
|
expect(db.any).toHaveBeenCalledWith(`insert into \"myschema\".\"mytable\"(\"id\",\"name\",\"age\") values(1234,'test',34) RETURNING *`);
|
|
});
|
|
|
|
it('runs query to insert with type casting', async () => {
|
|
const insertItem = {id: 1234, name: 'test', age: 34};
|
|
const nodeParams = {
|
|
table: 'mytable',
|
|
schema: 'myschema',
|
|
columns: 'id:int,name:text,age',
|
|
returnFields: '*',
|
|
additionalFields: {},
|
|
};
|
|
const getNodeParam = (key) => nodeParams[key];
|
|
const pgp = pgPromise();
|
|
const any = jest.fn();
|
|
const db = {any};
|
|
|
|
const items = [
|
|
{
|
|
json: insertItem,
|
|
},
|
|
];
|
|
|
|
await PostgresFun.pgInsert(getNodeParam, pgp, db, items);
|
|
|
|
expect(db.any).toHaveBeenCalledWith(`insert into \"myschema\".\"mytable\"(\"id\",\"name\",\"age\") values(1234::int,'test'::text,34) RETURNING *`);
|
|
});
|
|
});
|