access-db
Version:
[统一数据库的连接]Unified access to all kinds of databases
80 lines (79 loc) • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const process_1 = require("process");
const constants_1 = require("../constants/constants");
const error_1 = require("../constants/error");
const utils_1 = require("./utils");
const timeHash_1 = require("../utils/timeHash");
function setTrans(params, query, dbType) {
//mysql 类平台
let result;
if (dbType === constants_1.PLATFORM_NAME.MYSQL) {
let fieldsArr = [], valuesArr = [], valuesQue = [];
for (let pa in params) {
if (!(0, utils_1.isArray)(params[pa])) {
//不是数组
fieldsArr.push(pa);
valuesQue.push('?');
//valuesArr.push(isJson(params[pa]) ? JSON.stringify(params[pa]) : params[pa])
valuesArr.push((0, utils_1.changeSqlParam)(params[pa]));
//continue
}
else {
throw new Error(error_1.UPDATE_ERROR);
}
// if(SET_METHORD.indexOf(params[pa][0]) > -1 ){
// switch(params[pa][0]){
// case 'geo':
// let temp2: any = params[pa], tempGeo = ''
// temp2.shift()
// if(temp2.length > 1){
// let tempPs = temp2.map(item => {
// return `Point(${item.toString()})`
// })
// tempGeo = `Polygon(LineString(${tempPs.toString()}))`
// }else{
// tempGeo=`Point(${temp2[0].toString()})`
// }
// fieldsArr.push(pa)
// valuesQue.push(tempGeo)
// break
// default:
// throw new Error(UPDATE_ERROR)
// }
// }else{
// //直接 set
// fieldsArr.push(pa)
// valuesQue.push('?')
// valuesArr.push(isJson(params[pa]) ? JSON.stringify(params[pa]) : params[pa])
// }
}
result = { fieldsArr, valuesQue, valuesArr };
}
if (dbType === constants_1.PLATFORM_NAME.REDIS) {
let table = query, keyData = [], tempParams = Object.assign({}, params);
if (!tempParams.id) {
tempParams = Object.assign({ id: (0, timeHash_1.strHash)() }, tempParams);
}
else {
const tempid = tempParams.id;
delete tempParams.id;
tempParams = Object.assign({ id: tempid }, tempParams);
}
for (let p in tempParams) {
keyData.push({
method: 'SETNX',
key: table + ':id:key:' + tempParams.id,
value: `${new Date().getTime()}${process_1.hrtime.bigint()}` //保存更新时间
});
keyData.push({
method: 'HSETNX',
key: table + ':' + tempParams.id,
value: [p, `${tempParams[p]}`]
});
}
result = keyData;
}
return result;
}
exports.default = setTrans;