Skip to content

Commit

Permalink
fix: reform
Browse files Browse the repository at this point in the history
  • Loading branch information
zetxx committed Jul 29, 2022
1 parent 8766ec0 commit a46d523
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 70 deletions.
27 changes: 5 additions & 22 deletions examples/mssql/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
const sql = require('mssql');
const lib = require('../../lib/mssql');

const init = (async() => {
const {
server,
user,
password,
pool,
database,
options
} = require('../mssql.config.js');
const cPool = await sql.connect({
server,
user,
password,
pool,
database,
options
});
return cPool.request();
});
(async() => {
await require('../../lib/mssql')(
require('../mssql.config.js')
);
})();
142 changes: 94 additions & 48 deletions lib/mssql/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const driver = require('mssql');
const {SError, WError} = require('error');
const SqlSe = (() => (class Sql extends SError {}))();
const SqlWe = (() => (class Sql extends WError {}))();
Expand Down Expand Up @@ -31,23 +32,13 @@ const stringToType = (() => {
};
})();

module.exports = (args) => {
(args) => {
return class mssql {
constructor() {
this.tableTypes = {};
this.methods = this.methods || {};
}

async extractTTs() {
const qr = await this.predefinedQuery(
'tableTypes'
);

this.tableTypes = this.createTTs(
this.groupTTs(qr.recordset)
);
}

async storedProcedures() {
const schemas = []
.concat(this.config.link.SP.schemas);
Expand Down Expand Up @@ -118,15 +109,6 @@ module.exports = (args) => {
await this.storedProcedures();
}

async predefinedQuery(key) {
if (!key) {
throw SqlSe.create('noSuchSqlHelperFile');
}
return this.link.query(
(await sql[key]).toString('utf8')
);
}

groupTTs(list) {
return list.reduce((a, {
name,
Expand Down Expand Up @@ -289,34 +271,98 @@ module.exports = (args) => {
};
}

async transformResponse({recordsets, recordset, ...data}) {
if (recordsets.length === 1) {
return [...recordset];
} else if (
Array.isArray(recordsets) &&
recordsets[0] &&
recordsets[0].columns &&
recordsets[0].columns.resultSet
) {
return recordsets.reduce((res, curr, idx, arr) => {
if (curr.columns.resultSet) {
return {
...res,
[curr[0].resultSet]: []
};
} else {
const resultSet = (arr[idx - 1][0] &&
arr[idx - 1][0].resultSet) || 'orphan';
return {
...res,
[resultSet]:
res[resultSet].concat([...curr])
};
}
}, {orphan: []});
} else {
return recordsets.map((rs) => [...rs]);
}
// async transformResponse({recordsets, recordset, ...data}) {
// if (recordsets.length === 1) {
// return [...recordset];
// } else if (
// Array.isArray(recordsets) &&
// recordsets[0] &&
// recordsets[0].columns &&
// recordsets[0].columns.resultSet
// ) {
// return recordsets.reduce((res, curr, idx, arr) => {
// if (curr.columns.resultSet) {
// return {
// ...res,
// [curr[0].resultSet]: []
// };
// } else {
// const resultSet = (arr[idx - 1][0] &&
// arr[idx - 1][0].resultSet) || 'orphan';
// return {
// ...res,
// [resultSet]:
// res[resultSet].concat([...curr])
// };
// }
// }, {orphan: []});
// } else {
// return recordsets.map((rs) => [...rs]);
// }
// }
};
};

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


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

module.exports = async(config) => {
let tableTypes = {};
const dQuery = await query(config);

async predefinedQuery(key) {
if (!key) {
throw SqlSe.create('noSuchSqlHelperFile');
}
return dQuery.query(
(await sql[key]).toString('utf8')
);
}

const extractTTs = async() => {
const qr = await predefinedQuery(
'tableTypes'
);

tableTypes = createTTs(
groupTTs(qr.recordset)
);
};
};

0 comments on commit a46d523

Please sign in to comment.