Skip to content

Commit 720bfb8

Browse files
author
Elin Angelow
committed
fix: types, methods
1 parent 9afdd60 commit 720bfb8

File tree

13 files changed

+118
-116
lines changed

13 files changed

+118
-116
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"skipFiles": [
2121
"<node_internals>/**"
2222
],
23-
"program": "${workspaceFolder}/examples/mssql/index.js"
23+
"program": "${workspaceFolder}/examples/postgres/index.js"
2424
}
2525
]
2626
}
File renamed without changes.
File renamed without changes.

examples/postgres/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(async() => {
2+
try {
3+
const methods = await require('../../lib/postgres')(
4+
require('./config')
5+
);
6+
if (methods['some-test-example/exsschema/fnLastTx"'])
7+
await methods['some-test-example/exsschema/fnLastTx"']({ParLessonType: [{LessonId: 100, LessonName: 'example lesson'}]});
8+
} catch (e) {
9+
console.error(e);
10+
}
11+
})();

examples/postgresql/index.js

-10
This file was deleted.

lib/postgres/index.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const driver = require('postgres');
2+
const {SError, WError} = require('error');
3+
const SqlSe = (() => (class Sql extends SError {}))();
4+
const SqlWe = (() => (class Sql extends WError {}))();
5+
6+
7+
const predefinedSql = require('./sqls/index.js');
8+
9+
const conn = (() => {
10+
let connection;
11+
return async(config) => {
12+
if (connection === undefined) {
13+
connection = await driver(config);
14+
return connection;
15+
}
16+
return connection;
17+
};
18+
})();
19+
20+
21+
const Link = async(config) => {
22+
const sql = await conn(config);
23+
return {
24+
request: async(q) => {
25+
try {
26+
return await sql(['select 123']);
27+
} catch (e) {
28+
console.warn(q);
29+
console.error(e);
30+
throw e;
31+
}
32+
},
33+
sql
34+
};
35+
};
36+
37+
38+
module.exports = async(config) => {
39+
const {gluePrefix = '.'} = config;
40+
const link = await Link(config.connect);
41+
42+
const predefinedQuery = async(key) => {
43+
if (!key) {
44+
throw SqlSe.create('noSuchSqlHelperFile');
45+
}
46+
try {
47+
const q = predefinedSql[key];
48+
const r = await q(link);
49+
return r;
50+
} catch (e) {
51+
throw SqlWe.wrap(
52+
'sqlHelper',
53+
e,
54+
{
55+
key,
56+
query: e.query
57+
}
58+
);
59+
}
60+
}
61+
62+
const types = (() => {
63+
let cache;
64+
return async() => {
65+
if (!cache) {
66+
cache = await predefinedQuery('methods');
67+
}
68+
return cache;
69+
};
70+
})();
71+
72+
const build = async() => {
73+
const allTypes = await types();
74+
(await predefinedQuery('methods'))
75+
.map((method) => {
76+
console.log(method);
77+
});
78+
};
79+
return await build();
80+
};

lib/postgres/sqls/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
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+
};

lib/postgres/sqls/methods.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SELECT
2+
*
3+
FROM
4+
pg_catalog.pg_namespace n
5+
JOIN
6+
pg_catalog.pg_proc p ON
7+
p.pronamespace = n.oid
8+
WHERE
9+
nspname not IN ('pg_catalog', 'information_schema')
10+

lib/postgres/sqls/types.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
select
2+
t."oid",
3+
n.nspname as "Schema",
4+
pg_catalog.format_type(t.oid, NULL) AS "Name",
5+
pg_catalog.obj_description(t.oid, 'pg_type') as "Description"
6+
FROM pg_catalog.pg_type t
7+
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
8+
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
9+
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
10+
AND pg_catalog.pg_type_is_visible(t.oid)
11+
ORDER BY 1, 2;

lib/postgresql/index.js

-53
This file was deleted.

lib/postgresql/sqls/index.js

-5
This file was deleted.

lib/postgresql/sqls/internalMethod.sql

-20
This file was deleted.

lib/postgresql/sqls/tableTypes.sql

-27
This file was deleted.

0 commit comments

Comments
 (0)