Skip to content

Commit

Permalink
fix: types, methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Elin Angelow committed Dec 19, 2023
1 parent 9afdd60 commit 720bfb8
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/examples/mssql/index.js"
"program": "${workspaceFolder}/examples/postgres/index.js"
}
]
}
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions examples/postgres/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(async() => {
try {
const methods = await require('../../lib/postgres')(
require('./config')
);
if (methods['some-test-example/exsschema/fnLastTx"'])
await methods['some-test-example/exsschema/fnLastTx"']({ParLessonType: [{LessonId: 100, LessonName: 'example lesson'}]});
} catch (e) {
console.error(e);
}
})();
10 changes: 0 additions & 10 deletions examples/postgresql/index.js

This file was deleted.

80 changes: 80 additions & 0 deletions lib/postgres/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const driver = require('postgres');
const {SError, WError} = require('error');
const SqlSe = (() => (class Sql extends SError {}))();
const SqlWe = (() => (class Sql extends WError {}))();


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

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


const Link = async(config) => {
const sql = await conn(config);
return {
request: async(q) => {
try {
return await sql(['select 123']);
} catch (e) {
console.warn(q);
console.error(e);
throw e;
}
},
sql
};
};


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

const predefinedQuery = async(key) => {
if (!key) {
throw SqlSe.create('noSuchSqlHelperFile');
}
try {
const q = predefinedSql[key];
const r = await q(link);
return r;
} catch (e) {
throw SqlWe.wrap(
'sqlHelper',
e,
{
key,
query: e.query
}
);
}
}

const types = (() => {
let cache;
return async() => {
if (!cache) {
cache = await predefinedQuery('methods');
}
return cache;
};
})();

const build = async() => {
const allTypes = await types();
(await predefinedQuery('methods'))
.map((method) => {
console.log(method);
});
};
return await build();
};
5 changes: 5 additions & 0 deletions lib/postgres/sqls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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'))
};
10 changes: 10 additions & 0 deletions lib/postgres/sqls/methods.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
*
FROM
pg_catalog.pg_namespace n
JOIN
pg_catalog.pg_proc p ON
p.pronamespace = n.oid
WHERE
nspname not IN ('pg_catalog', 'information_schema')

11 changes: 11 additions & 0 deletions lib/postgres/sqls/types.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
select
t."oid",
n.nspname as "Schema",
pg_catalog.format_type(t.oid, NULL) AS "Name",
pg_catalog.obj_description(t.oid, 'pg_type') as "Description"
FROM pg_catalog.pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
AND pg_catalog.pg_type_is_visible(t.oid)
ORDER BY 1, 2;
53 changes: 0 additions & 53 deletions lib/postgresql/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions lib/postgresql/sqls/index.js

This file was deleted.

20 changes: 0 additions & 20 deletions lib/postgresql/sqls/internalMethod.sql

This file was deleted.

27 changes: 0 additions & 27 deletions lib/postgresql/sqls/tableTypes.sql

This file was deleted.

0 comments on commit 720bfb8

Please sign in to comment.