From 37653b35251dae44e9d04eb10636626a23b1d423 Mon Sep 17 00:00:00 2001 From: Elin Angelow Date: Tue, 19 Dec 2023 16:46:11 +0200 Subject: [PATCH] fix: kind a finnished --- examples/postgres/index.js | 6 ++++-- lib/postgres/index.js | 42 ++++++++++++++++++-------------------- lib/postgres/sqls/index.js | 10 +++++---- package.json | 2 +- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/examples/postgres/index.js b/examples/postgres/index.js index c5138be..8c32bc4 100644 --- a/examples/postgres/index.js +++ b/examples/postgres/index.js @@ -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); } diff --git a/lib/postgres/index.js b/lib/postgres/index.js index 27a280f..d71f535 100644 --- a/lib/postgres/index.js +++ b/lib/postgres/index.js @@ -1,16 +1,17 @@ -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; @@ -18,25 +19,17 @@ const conn = (() => { })(); -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( @@ -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'; @@ -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, @@ -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) => { @@ -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); } }; }, {}); diff --git a/lib/postgres/sqls/index.js b/lib/postgres/sqls/index.js index e5ad95e..ef6f628 100644 --- a/lib/postgres/sqls/index.js +++ b/lib/postgres/sqls/index.js @@ -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') +}); diff --git a/package.json b/package.json index 1feb149..0e1c677 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "dependencies": { "error": "^10.4.0", "mssql": "10.0.1", - "postgres": "3.4.3" + "pg": "8.11.3" }, "devDependencies": { "tap": "*",