pao-aop-server
Version:
基于pao-aop的服务端框架
475 lines (473 loc) • 20.4 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "reflect-metadata", "typeorm", "../../base", "pao-aop"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
var typeorm_1 = require("typeorm");
var base_1 = require("../../base");
var pao_aop_1 = require("pao-aop");
/**
* 默认数据库名称
*/
var defaultDatabaseName = 'default';
/**
* 数据过滤器
* @author huyl
*/
var BaseRelationDataFilter = /** @class */ (function (_super) {
__extends(BaseRelationDataFilter, _super);
function BaseRelationDataFilter() {
var _this = _super !== null && _super.apply(this, arguments) || this;
/**
* 过滤参数
*/
_this.parameters = undefined;
return _this;
}
/**
* 获取过滤语句
* @param paramValues 参数
*/
BaseRelationDataFilter.prototype.getFilterSql = function (paramValues) {
var where = undefined;
this.parameters = [];
// 当前过滤器对象
var that = this;
if (that.childFilters) {
var composite = false;
// 遍历子过滤器列表,根据传入参数返回过滤语句,并拼接
// 拼接完后,如果是组合型的过滤语句则加()返回
for (var _i = 0, _a = that.childFilters; _i < _a.length; _i++) {
var filter = _a[_i];
var filterSql = filter.getFilterSql(paramValues);
var param = filter.getParameters(paramValues);
if (filterSql) {
if (where) {
where += " " + that.getLogicSign() + " " + filterSql;
composite = true;
}
else {
where = filterSql;
this.parameters = [];
}
this.parameters = this.parameters.concat(param);
}
}
if (composite) {
where = " (" + where + ") ";
}
}
return where;
};
/**
* 获取过滤参数
* @param paramValues 参数
*/
BaseRelationDataFilter.prototype.getParameters = function (paramValues) {
return this.parameters;
};
/**
* AND逻辑运算
* @param filters 表达式
* @remark 传入的表示式可以是sqlFilter或者baseDataFilter,
* 如果是baseDataFilter,则baseDataFilter生成表达式会用()闭包
* 例如(sqlfilter,baseDataFilter)生成结果(sqlFilterSql and ( baseDataFilterSql ) )
*/
BaseRelationDataFilter.prototype.and = function () {
var filters = [];
for (var _i = 0; _i < arguments.length; _i++) {
filters[_i] = arguments[_i];
}
if (!this.childFilters) {
this.childFilters = [];
}
var andFilters = new AndRelationDataFilter();
andFilters.childFilters = [];
for (var _a = 0, filters_1 = filters; _a < filters_1.length; _a++) {
var f = filters_1[_a];
andFilters.childFilters.push(f);
}
this.childFilters.push(andFilters);
return this;
};
/**
* OR逻辑运算
* @param filters 表达式
* @remark 和and方法一样,只是最后是使用OR的逻辑运算符
*/
BaseRelationDataFilter.prototype.or = function () {
var filters = [];
for (var _i = 0; _i < arguments.length; _i++) {
filters[_i] = arguments[_i];
}
if (!this.childFilters) {
this.childFilters = [];
}
var orFilters = new OrRelationDataFilter();
orFilters.childFilters = [];
for (var _a = 0, filters_2 = filters; _a < filters_2.length; _a++) {
var f = filters_2[_a];
orFilters.childFilters.push(f);
}
this.childFilters.push(orFilters);
return this;
};
/**
* 添加筛选类型的过滤条件
* @param filters 筛选类型的过滤条件
* @remark 筛选类型:group by/having/limit等
*/
BaseRelationDataFilter.prototype.add = function () {
var filters = [];
for (var _i = 0; _i < arguments.length; _i++) {
filters[_i] = arguments[_i];
}
if (!this.childFilters) {
this.childFilters = [];
}
for (var _a = 0, filters_3 = filters; _a < filters_3.length; _a++) {
var f = filters_3[_a];
this.childFilters.push(f);
}
return this;
};
BaseRelationDataFilter = __decorate([
pao_aop_1.addon('BaseRelationDataFilter', '数据过滤器', '数据过滤器')
], BaseRelationDataFilter);
return BaseRelationDataFilter;
}(pao_aop_1.BaseAddon));
exports.BaseRelationDataFilter = BaseRelationDataFilter;
/**
* AND逻辑运算过滤器
* @author huyl
*/
var AndRelationDataFilter = /** @class */ (function (_super) {
__extends(AndRelationDataFilter, _super);
function AndRelationDataFilter() {
return _super !== null && _super.apply(this, arguments) || this;
}
AndRelationDataFilter.prototype.getLogicSign = function () {
return 'AND';
};
AndRelationDataFilter = __decorate([
pao_aop_1.addon('AndRelationDataFilter', 'AND逻辑运算过滤器', 'AND逻辑运算过滤器')
], AndRelationDataFilter);
return AndRelationDataFilter;
}(BaseRelationDataFilter));
exports.AndRelationDataFilter = AndRelationDataFilter;
/**
* OR逻辑运算过滤器
* @author huyl
*/
var OrRelationDataFilter = /** @class */ (function (_super) {
__extends(OrRelationDataFilter, _super);
function OrRelationDataFilter() {
return _super !== null && _super.apply(this, arguments) || this;
}
OrRelationDataFilter.prototype.getLogicSign = function () {
return 'OR';
};
OrRelationDataFilter = __decorate([
pao_aop_1.addon('OrRelationDataFilter', 'OR逻辑运算过滤器', 'OR逻辑运算过滤器')
], OrRelationDataFilter);
return OrRelationDataFilter;
}(BaseRelationDataFilter));
exports.OrRelationDataFilter = OrRelationDataFilter;
/**
* SQL语句过滤器
* @author huyl
*/
var SqlRelationDataFilter = /** @class */ (function (_super) {
__extends(SqlRelationDataFilter, _super);
/**
* SQL语句过滤器
* @param fieldKey 字段关键字
* @param sql SQL过滤语句
* @param caption 标题
*/
function SqlRelationDataFilter(fieldKey, sql, caption) {
var _this = _super.call(this) || this;
_this.fieldKey = fieldKey;
_this.sql = sql;
_this.caption = caption;
return _this;
}
/**
* 获取过滤语句
* @param paramValues 参数
*/
SqlRelationDataFilter.prototype.getFilterSql = function (paramValues) {
try {
if (paramValues && paramValues[this.fieldKey]) {
return this.sql;
}
}
catch (e) {
pao_aop_1.log('SQL数据过滤器', '获取过滤语句异常');
}
return undefined;
};
/**
* 获取参数
* @param paramValues 参数列表
*/
SqlRelationDataFilter.prototype.getParameters = function (paramValues) {
try {
if (paramValues && paramValues[this.fieldKey]) {
var params = [];
params.push(paramValues[this.fieldKey]);
return params;
}
}
catch (e) {
pao_aop_1.log('SQL数据过滤器', '获取参数异常');
}
return undefined;
};
SqlRelationDataFilter = __decorate([
pao_aop_1.addon('SqlRelationDataFilter', 'SQL语句过滤器', 'SQL语句过滤器'),
__metadata("design:paramtypes", [String, String, String])
], SqlRelationDataFilter);
return SqlRelationDataFilter;
}(pao_aop_1.BaseAddon));
exports.SqlRelationDataFilter = SqlRelationDataFilter;
/**
* 数据命令
* @author huyl
*/
var RelationDataCommand = /** @class */ (function (_super) {
__extends(RelationDataCommand, _super);
/**
* 数据命令
* @param id 唯一标识
* @param name 名称
* @param primaryKeyFields 主键
* @param sql SQL语句
*/
function RelationDataCommand(id, name, primaryKeyFields, sql) {
var _this = _super.call(this, id) || this;
_this.id = id;
_this.name = name;
_this.primaryKeyFields = primaryKeyFields;
_this.sql = sql;
/** 是否为多表查询(默认为多表查询) */
_this.isMultiTableQuery = true;
/** TRUE条件过滤字符串 */
_this.trueFilterString = '1=1';
/** 参数 */
_this.parameters = undefined;
return _this;
}
/**
* 获取命令字符串
* @param paramValues 查询参数
*/
RelationDataCommand.prototype.getCommandText = function (paramValues) {
if (!this.dataFilter) {
return this.sql;
}
var filterStrings = [];
this.parameters = [];
for (var _i = 0, _a = this.dataFilter; _i < _a.length; _i++) {
var filter = _a[_i];
var filterString = filter.getFilterSql(paramValues);
var param = filter.getParameters(paramValues);
if (!filterString || filterString === '') {
filterString = this.trueFilterString;
}
if (param) {
this.parameters = this.parameters.concat(param);
}
filterStrings.push(filterString);
}
// 带不定参数方法需要用apply传递数组
return this.sql.format.apply(this.sql, filterStrings);
};
/**
* 获取过滤参数
*/
RelationDataCommand.prototype.getParameters = function () {
return this.parameters;
};
RelationDataCommand = __decorate([
pao_aop_1.addon('RelationDataCommand', '数据命令', '数据命令'),
__metadata("design:paramtypes", [String, String, Array, String])
], RelationDataCommand);
return RelationDataCommand;
}(pao_aop_1.BaseAddon));
exports.RelationDataCommand = RelationDataCommand;
/**
* 数据连接对象
* @author huyl
*/
var DatabaseConnection = /** @class */ (function (_super) {
__extends(DatabaseConnection, _super);
function DatabaseConnection() {
var _this = _super !== null && _super.apply(this, arguments) || this;
/** 当前TypeORM的连接对象 */
_this._currentConnection = undefined;
return _this;
}
/** 连接数据库 */
DatabaseConnection.prototype.connect = function () {
return __awaiter(this, void 0, void 0, function () {
var databaseName;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.databaseManager) {
this.databaseManager = typeorm_1.getConnectionManager();
}
if (this.dbConnection) {
databaseName = this.dbConnection.name ? this.dbConnection.name : defaultDatabaseName;
if (this.databaseManager.has(databaseName)) {
this._currentConnection = this.databaseManager.get(databaseName);
}
else {
this._currentConnection = this.databaseManager.create(this.dbConnection);
}
}
if (!this._currentConnection.isConnected) return [3 /*break*/, 2];
return [4 /*yield*/, this._currentConnection.close()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [4 /*yield*/, this._currentConnection.connect()];
case 3:
// 返回连接的Promise对象
return [2 /*return*/, _a.sent()];
}
});
});
};
DatabaseConnection = __decorate([
pao_aop_1.addon('DatabaseConnection', '数据库连接对象', '数据库连接对象')
], DatabaseConnection);
return DatabaseConnection;
}(pao_aop_1.BaseAddon));
exports.DatabaseConnection = DatabaseConnection;
/**
* 关系型数据服务
* @author huyl
*/
var RelationDataService = /** @class */ (function (_super) {
__extends(RelationDataService, _super);
function RelationDataService() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 获取命令信息
* @param commandID 命令ID
*/
RelationDataService.prototype.getCommandByID = function (commandID) {
if (this.commandList) {
for (var _i = 0, _a = this.commandList; _i < _a.length; _i++) {
var cmd = _a[_i];
if (cmd.id === commandID) {
return cmd;
}
}
}
return undefined;
};
/**
* 查询
* @param params 查询参数
*/
RelationDataService.prototype.query = function (command, params) {
return __awaiter(this, void 0, void 0, function () {
var conn, cmd, executeSql, param, connection, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
conn = new DatabaseConnection();
conn.dbConnection = this.connection;
cmd = this.getCommandByID(command);
if (!cmd) {
throw new Error("\u627E\u4E0D\u5230[" + command + "]\u7684\u547D\u4EE4");
}
executeSql = cmd.getCommandText(params);
param = cmd.getParameters();
return [4 /*yield*/, conn.connect()];
case 1:
connection = _a.sent();
return [4 /*yield*/, connection.query(executeSql, param)];
case 2:
result = _a.sent();
return [2 /*return*/, pao_aop_1.jsonConvertDataTable(result, cmd.tableName, cmd.primaryKeyFields)];
}
});
});
};
RelationDataService = __decorate([
pao_aop_1.addon('RelationDataService', '关系型数据服务', '操作关系型数据库,实现数据操作的服务')
], RelationDataService);
return RelationDataService;
}(base_1.BaseService));
exports.RelationDataService = RelationDataService;
});
//# sourceMappingURL=index.js.map