Skip to content

Commit 37653b3

Browse files
author
Elin Angelow
committed
fix: kind a finnished
1 parent fdbf17d commit 37653b3

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

Diff for: examples/postgres/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
const methods = await require('../../lib/postgres')(
44
require('./config')
55
);
6-
if (methods && methods['exsschema/fnLastTx'])
7-
await methods['exsschema/fnLastTx']({pDevice: '34987a479f90'});
6+
if (methods && methods['exsschema/fnLastTx']){
7+
const res = await methods['exsschema/fnLastTx']({pDevice: '34987a479f90'});
8+
console.table(res.rows);
9+
}
810
} catch (e) {
911
console.error(e);
1012
}

Diff for: lib/postgres/index.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,35 @@
1-
const driver = require('postgres');
1+
const {Client} = require('pg');
22
const {SError, WError} = require('error');
33
const SqlSe = (() => (class Sql extends SError {}))();
44
const SqlWe = (() => (class Sql extends WError {}))();
55

66

7-
const predefinedSql = require('./sqls/index.js');
7+
const predefinedSql = require('./sqls/index.js')();
88

99
const conn = (() => {
1010
let connection;
1111
return async(config) => {
1212
if (connection === undefined) {
13-
connection = await driver(config);
13+
connection = new Client(config);
14+
await connection.connect();
1415
return connection;
1516
}
1617
return connection;
1718
};
1819
})();
1920

2021

21-
const Link = async(config) => {
22-
const sql = await conn(config);
23-
return {
24-
sql
25-
};
26-
};
27-
28-
2922
module.exports = async(config) => {
3023
const {link: {gluePrefix = '.', schemas} = {}} = config;
31-
const link = await Link(config.connect);
24+
const link = await conn(config.connect);
3225

3326
const predefinedQuery = async(key) => {
3427
if (!key) {
3528
throw SqlSe.create('noSuchSqlHelperFile');
3629
}
3730
try {
38-
const q = predefinedSql[key];
39-
const r = await q(link);
31+
const q = (await predefinedSql)[key];
32+
const r = await link.query(q);
4033
return r;
4134
} catch (e) {
4235
throw SqlWe.wrap(
@@ -54,13 +47,17 @@ module.exports = async(config) => {
5447
let cache;
5548
return async() => {
5649
if (!cache) {
57-
cache = await predefinedQuery('methods');
50+
cache = await predefinedQuery('types');
5851
}
5952
return cache;
6053
};
6154
})();
6255
// https://www.postgresql.org/docs/current/catalog-pg-proc.html
63-
const buildArgs = (argnames, argmodes) => {
56+
const buildArgs = (argnames, argmodesText) => {
57+
const argmodes = argmodesText
58+
.split('{').join('')
59+
.split('}').join('')
60+
.split(',');
6461
return argnames
6562
.reduce((args, arg, idx) => {
6663
const mode = (argmodes && argmodes[idx]) || 'i';
@@ -76,7 +73,8 @@ module.exports = async(config) => {
7673

7774
const build = async() => {
7875
const allTypes = await types();
79-
return (await predefinedQuery('methods'))
76+
const methods = (await predefinedQuery('methods'));
77+
return methods.rows
8078
.filter(({schema}) => schemas.indexOf(schema) > -1)
8179
.reduce((methods, {
8280
schema,
@@ -90,8 +88,10 @@ module.exports = async(config) => {
9088
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.
9189
}) => {
9290
const jsName = [schema, name].join(gluePrefix);
93-
const sqlName = [`${schema}`, `${name}`].join('.');
91+
const sqlName = [`"${schema}"`, `"${name}"`].join('.');
9492
const args = buildArgs(argnames, argmodes);
93+
const fillArgs = args.input
94+
.map((v, idx) => `$${idx + 1}`).join(',');
9595
return {
9696
...methods,
9797
[jsName]: async(arguments) => {
@@ -102,11 +102,9 @@ module.exports = async(config) => {
102102
{fn: jsName, argument: name}
103103
);
104104
}
105-
return `'${arguments[name]}'`;
105+
return arguments[name];
106106
});
107-
// const res = await link.sql`select * from ${link.sql(sqlName)}('abc')`
108-
const res = await link.sql`select * from ${link.sql(sqlName)}(${link.sql(arguments)})`
109-
return await res;
107+
return await link.query(`SELECT * FROM ${sqlName}(${fillArgs})`, dynArgs);
110108
}
111109
};
112110
}, {});

Diff for: lib/postgres/sqls/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const {join} = require('path');
2-
module.exports = {
3-
types: async(link) => link.sql.file(join(__dirname, 'types.sql')),
4-
methods: async(link) => link.sql.file(join(__dirname, 'methods.sql'))
5-
};
2+
const {readFile} = require('node:fs/promises');
3+
4+
module.exports = async() => ({
5+
types: (await readFile(join(__dirname, 'types.sql'))).toString('utf8'),
6+
methods: (await readFile(join(__dirname, 'methods.sql'))).toString('utf8')
7+
});

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dependencies": {
99
"error": "^10.4.0",
1010
"mssql": "10.0.1",
11-
"postgres": "3.4.3"
11+
"pg": "8.11.3"
1212
},
1313
"devDependencies": {
1414
"tap": "*",

0 commit comments

Comments
 (0)