Skip to content

Commit

Permalink
fix: init
Browse files Browse the repository at this point in the history
  • Loading branch information
Elin Angelow committed Dec 18, 2023
1 parent 723aedb commit 8b522df
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
.nyc_output/**/*
coverage/**/*
tests/unit/runtime/ledger/**/*
examples/mssql.config.js
examples/**/config.js
11 changes: 10 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"name": "Mssql",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/examples/mssql/index.js"
},
{
"type": "pwa-node",
"request": "launch",
"name": "Postgres",
"skipFiles": [
"<node_internals>/**"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
server: 'localhost',
user: 'sa',
password: '***',
database: 'some-test-example-mssql',
database: 'some-test-example',
pool: {
max: 10,
min: 0,
Expand Down
12 changes: 12 additions & 0 deletions examples/postgresql/example.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
connect: {
server: 'localhost',
user: 'sa',
password: '***',
database: 'some-test-example'
},
link: {
gluePrefix: '.',
schema: ['a', 'b'] // schemas allowed
}
};
32 changes: 32 additions & 0 deletions examples/postgresql/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE TYPE LessonType AS TABLE (LessonId INT, LessonName VARCHAR(100))

CREATE TABLE Lesson (
Id INT PRIMARY KEY,
LName VARCHAR(50)
)
-------------------------
CREATE PROCEDURE Usp_InsertLesson
@ParLessonType LessonType READONLY,
@ParLessonType2 LessonType READONLY
AS
INSERT INTO Lesson
SELECT * FROM @ParLessonType;

CREATE PROCEDURE Usp_InsertLesson2
@ParLessonType LessonType READONLY,
@ParLessonType2 LessonType READONLY
AS
INSERT INTO Lesson
SELECT * FROM @ParLessonType
-------------------------
DECLARE @VarLessonType AS LessonType

INSERT INTO @VarLessonType VALUES ( 1, 'Math')
INSERT INTO @VarLessonType VALUES ( 2, 'Science')
INSERT INTO @VarLessonType VALUES ( 3, 'Geometry')


EXECUTE Usp_InsertLesson @VarLessonType
-------------------------
-------------------------
-------------------------
10 changes: 10 additions & 0 deletions examples/postgresql/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(async() => {
const methods = await require('../../lib/mssql/index.js')(
require('../mssql.config.js')
);
try {
await methods['dbo.Usp_InsertLesson']({ParLessonType: [{LessonId: 100, LessonName: 'example lesson'}]});
} catch (e) {
console.error(e);
}
})();
68 changes: 68 additions & 0 deletions lib/postgresql/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const driver = require('postgresql');
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 = null;
return async(config) => {
if (connection === null) {
connection = await driver.connect(config);
return connection;
}
return connection;
};
})();


const Link = async(config) => {
const {
server,
user,
password,
database,
pool,
options
} = config;
const cPool = await conn({
server,
user,
password,
pool,
database,
options
});
return {
query: async(q) => {
try {
return await cPool.request().query(q);
} catch (e) {
console.warn(q);
console.error(e);
throw e;
}
},
request: () => cPool.request()
};
};


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

const predefinedQuery = async(key) => {
if (!key) {
throw SqlSe.create('noSuchSqlHelperFile');
}
const q = (await predefinedSql[key]).toString('utf8');
return link.query(
q
);
}

return {};
};
5 changes: 5 additions & 0 deletions lib/postgresql/sqls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const read = require('util').promisify(require('fs').readFile);
module.exports = {
internalMethod: read([__dirname, 'internalMethod.sql'].join('/')),
tableTypes: read([__dirname, 'tableTypes.sql'].join('/'))
};
20 changes: 20 additions & 0 deletions lib/postgresql/sqls/internalMethod.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
SELECT
OBJECT_SCHEMA_NAME([SYSPROC].object_id) [schema],
[SYSPROC].name name,
[SYSPROC].object_id objectId,
[SYSPARAMS].name AS [column],
TYPE_NAME([SYSPARAMS].user_type_id) AS [type],
[SYSPARAMS].max_length AS length,
CASE
WHEN TYPE_NAME([SYSPARAMS].system_type_id) = 'uniqueidentifier'
THEN [SYSPARAMS].PRECISION
ELSE OdbcPrec([SYSPARAMS].system_type_id, [SYSPARAMS].max_length, [SYSPARAMS].PRECISION)
END AS [precision],
OdbcScale([SYSPARAMS].system_type_id, [SYSPARAMS].scale) AS [scale],
[SYSPARAMS].parameter_id AS [order],
[SYSPARAMS].user_type_id AS userTypeId,
[SYSPARAMS].is_output AS isOutput
FROM
sys.procedures [SYSPROC]
LEFT JOIN
sys.parameters [SYSPARAMS] ON [SYSPARAMS].object_id = [SYSPROC].object_id
27 changes: 27 additions & 0 deletions lib/postgresql/sqls/tableTypes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SELECT
SCHEMA_NAME(types.schema_id) + '.' + types.name name,
c.name [column],
st.name type,
CASE
WHEN st.name IN ('decimal', 'numeric') THEN CAST(c.[precision] AS VARCHAR)
WHEN st.name IN ('datetime2', 'time', 'datetimeoffset') THEN CAST(c.[scale] AS VARCHAR)
WHEN st.name IN ('varchar', 'varbinary', 'char', 'binary') AND c.max_length >= 0 THEN CAST(c.max_length AS VARCHAR)
WHEN st.name IN ('nvarchar', 'nchar') AND c.max_length >= 0 THEN CAST(c.max_length / 2 AS VARCHAR)
WHEN st.name IN ('varchar', 'varbinary', 'char', 'binary', 'nvarchar', 'nchar') AND c.max_length < 0 THEN 'max'
END [length],
CASE
WHEN st.name IN ('decimal', 'numeric') THEN c.scale
END scale,
OBJECT_DEFINITION(c.default_object_id) [default],
types.user_type_id AS userTypeId,
c.is_nullable AS isNullable
FROM
sys.table_types types
JOIN
sys.columns c ON types.type_table_object_id = c.object_id
JOIN
sys.systypes AS st ON st.xtype = c.system_type_id
WHERE
types.is_user_defined = 1 AND st.name <> 'sysname'
ORDER BY
1, c.column_id

0 comments on commit 8b522df

Please sign in to comment.