Skip to content

Commit

Permalink
fix: tx and config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Elin Angelow committed Dec 20, 2023
1 parent 37653b3 commit 7a20818
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
- every method that is called should consist on only 1 argument
of type object, in it should be passed every argument of sql function,procedure ... etc

- mssql: ok
- mssql
- main: ok
- transactions: pending
- mysql: pending
- postgres: wip
- postgres:
- main: ok
- transactions: ok
- sqlite: pending

[![Build Status](https://travis-ci.com/zetxx/expose-sql-methods.svg?branch=master)](https://travis-ci.com/zetxx/expose-sql-methods)
61 changes: 48 additions & 13 deletions lib/postgres/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {Client} = require('pg');
const {Pool} = require('pg');
const {SError, WError} = require('error');
const SqlSe = (() => (class Sql extends SError {}))();
const SqlWe = (() => (class Sql extends WError {}))();
Expand All @@ -10,8 +10,7 @@ const conn = (() => {
let connection;
return async(config) => {
if (connection === undefined) {
connection = new Client(config);
await connection.connect();
connection = new Pool(config);
return connection;
}
return connection;
Expand All @@ -20,8 +19,11 @@ const conn = (() => {


module.exports = async(config) => {
const {link: {gluePrefix = '.', schemas} = {}} = config;
const link = await conn(config.connect);
const {link: {gluePrefix = '.', schemas: sc} = {}} = config;
const schemas = sc instanceof Array ? sc : [sc];
const link = await conn(config.connection);
const txMap = new Map();
let txId = 0;

const predefinedQuery = async(key) => {
if (!key) {
Expand All @@ -30,7 +32,7 @@ module.exports = async(config) => {
try {
const q = (await predefinedSql)[key];
const r = await link.query(q);
return r;
return r.rows;
} catch (e) {
throw SqlWe.wrap(
'sqlHelper',
Expand All @@ -47,14 +49,14 @@ module.exports = async(config) => {
let cache;
return async() => {
if (!cache) {
cache = await predefinedQuery('types');
cache = (await predefinedQuery('types')).rows;
}
return cache;
};
})();
// https://www.postgresql.org/docs/current/catalog-pg-proc.html
const buildArgs = (argnames, argmodesText) => {
const argmodes = argmodesText
const argmodes = (!argmodesText && argnames.map(() => 'i')) || argmodesText
.split('{').join('')
.split('}').join('')
.split(',');
Expand All @@ -74,7 +76,7 @@ module.exports = async(config) => {
const build = async() => {
const allTypes = await types();
const methods = (await predefinedQuery('methods'));
return methods.rows
return methods
.filter(({schema}) => schemas.indexOf(schema) > -1)
.reduce((methods, {
schema,
Expand All @@ -94,20 +96,53 @@ module.exports = async(config) => {
.map((v, idx) => `$${idx + 1}`).join(',');
return {
...methods,
[jsName]: async(arguments) => {
[jsName]: async(arguments, txId) => {
const dynArgs = args.input.map(({name}) => {
if (!arguments[name]) {
if (arguments[name] === undefined) {
throw SqlSe.create(
'argumentNotFound',
{fn: jsName, argument: name}
);
}
return arguments[name];
});
return await link.query(`SELECT * FROM ${sqlName}(${fillArgs})`, dynArgs);
if (!txId) {
const res = await link.query(`SELECT * FROM ${sqlName}(${fillArgs})`, dynArgs);
return res;
}
if (!txMap.get(txId)) {
throw SqlSe.create(
'transactionIdNotFound',
{fn: jsName, argument: name, txId}
);
}
const res = await txMap.get(txId).query(`SELECT * FROM ${sqlName}(${fillArgs})`, dynArgs);
return res;
}
};
}, {});
};
return await build();
const methods = await build();

return {
methods,
async txBegin() {
const id = ++txId;
const client = await link.connect();
await client.query('BEGIN');
txMap.set(id, client);
return id;
},
/**
* Description
* @param {number} id - transaction id
* @param {'COMMIT'|'ROLLBACK'} action - do we commit or decline tx
* @returns {void}
*/
async txEnd(id, action) {
const client = txMap.get(id);
await client.query(action);
txMap.delete(id);
}
};
};

0 comments on commit 7a20818

Please sign in to comment.