Skip to content

Commit

Permalink
fix: kind a finnished
Browse files Browse the repository at this point in the history
  • Loading branch information
Elin Angelow committed Dec 19, 2023
1 parent fdbf17d commit 37653b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
6 changes: 4 additions & 2 deletions examples/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const methods = await require('../../lib/postgres')(
require('./config')
);
if (methods && methods['exsschema/fnLastTx'])
await methods['exsschema/fnLastTx']({pDevice: '34987a479f90'});
if (methods && methods['exsschema/fnLastTx']){
const res = await methods['exsschema/fnLastTx']({pDevice: '34987a479f90'});
console.table(res.rows);
}
} catch (e) {
console.error(e);
}
Expand Down
42 changes: 20 additions & 22 deletions lib/postgres/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
const driver = require('postgres');
const {Client} = require('pg');
const {SError, WError} = require('error');
const SqlSe = (() => (class Sql extends SError {}))();
const SqlWe = (() => (class Sql extends WError {}))();


const predefinedSql = require('./sqls/index.js');
const predefinedSql = require('./sqls/index.js')();

const conn = (() => {
let connection;
return async(config) => {
if (connection === undefined) {
connection = await driver(config);
connection = new Client(config);
await connection.connect();
return connection;
}
return connection;
};
})();


const Link = async(config) => {
const sql = await conn(config);
return {
sql
};
};


module.exports = async(config) => {
const {link: {gluePrefix = '.', schemas} = {}} = config;
const link = await Link(config.connect);
const link = await conn(config.connect);

const predefinedQuery = async(key) => {
if (!key) {
throw SqlSe.create('noSuchSqlHelperFile');
}
try {
const q = predefinedSql[key];
const r = await q(link);
const q = (await predefinedSql)[key];
const r = await link.query(q);
return r;
} catch (e) {
throw SqlWe.wrap(
Expand All @@ -54,13 +47,17 @@ module.exports = async(config) => {
let cache;
return async() => {
if (!cache) {
cache = await predefinedQuery('methods');
cache = await predefinedQuery('types');
}
return cache;
};
})();
// https://www.postgresql.org/docs/current/catalog-pg-proc.html
const buildArgs = (argnames, argmodes) => {
const buildArgs = (argnames, argmodesText) => {
const argmodes = argmodesText
.split('{').join('')
.split('}').join('')
.split(',');
return argnames
.reduce((args, arg, idx) => {
const mode = (argmodes && argmodes[idx]) || 'i';
Expand All @@ -76,7 +73,8 @@ module.exports = async(config) => {

const build = async() => {
const allTypes = await types();
return (await predefinedQuery('methods'))
const methods = (await predefinedQuery('methods'));
return methods.rows
.filter(({schema}) => schemas.indexOf(schema) > -1)
.reduce((methods, {
schema,
Expand All @@ -90,8 +88,10 @@ module.exports = async(config) => {
argmodes // An array of the modes of the function arguments, encoded as i for IN arguments, o for OUT arguments, b for INOUT arguments, v for VARIADIC arguments, t for TABLE arguments. If all the arguments are IN arguments, this field will be null. Note that subscripts correspond to positions of proallargtypes not proargtypes.
}) => {
const jsName = [schema, name].join(gluePrefix);
const sqlName = [`${schema}`, `${name}`].join('.');
const sqlName = [`"${schema}"`, `"${name}"`].join('.');
const args = buildArgs(argnames, argmodes);
const fillArgs = args.input
.map((v, idx) => `$${idx + 1}`).join(',');
return {
...methods,
[jsName]: async(arguments) => {
Expand All @@ -102,11 +102,9 @@ module.exports = async(config) => {
{fn: jsName, argument: name}
);
}
return `'${arguments[name]}'`;
return arguments[name];
});
// const res = await link.sql`select * from ${link.sql(sqlName)}('abc')`
const res = await link.sql`select * from ${link.sql(sqlName)}(${link.sql(arguments)})`
return await res;
return await link.query(`SELECT * FROM ${sqlName}(${fillArgs})`, dynArgs);
}
};
}, {});
Expand Down
10 changes: 6 additions & 4 deletions lib/postgres/sqls/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const {join} = require('path');
module.exports = {
types: async(link) => link.sql.file(join(__dirname, 'types.sql')),
methods: async(link) => link.sql.file(join(__dirname, 'methods.sql'))
};
const {readFile} = require('node:fs/promises');

module.exports = async() => ({
types: (await readFile(join(__dirname, 'types.sql'))).toString('utf8'),
methods: (await readFile(join(__dirname, 'methods.sql'))).toString('utf8')
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"error": "^10.4.0",
"mssql": "10.0.1",
"postgres": "3.4.3"
"pg": "8.11.3"
},
"devDependencies": {
"tap": "*",
Expand Down

0 comments on commit 37653b3

Please sign in to comment.