From e6f996c280b62c43c2ea6532a3a21d45534fa1c3 Mon Sep 17 00:00:00 2001 From: "Xc@centOs" Date: Thu, 14 Sep 2023 17:44:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7ts=E5=88=B05=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/types/type.d.ts | 6 +- lib/index.js | 4 +- lib/store.js | 2146 ++++++++++++++++++------------------------- lib/types/type.d.ts | 6 +- package.json | 4 +- tsconfig.es.json | 3 +- tsconfig.json | 5 +- 7 files changed, 929 insertions(+), 1245 deletions(-) diff --git a/es/types/type.d.ts b/es/types/type.d.ts index ac19783..bf2992a 100644 --- a/es/types/type.d.ts +++ b/es/types/type.d.ts @@ -1,6 +1,6 @@ import { NodeId } from "oak-domain/lib/types/Demand"; import { EntityShape } from "oak-domain/lib/types/Entity"; -export declare type RowNode = { +export type RowNode = { $txnId?: string; $next?: Partial ExprResolveFn | any; +export type ExprResolveFn = (nodeDict: NodeDict) => ExprResolveFn | any; diff --git a/lib/index.js b/lib/index.js index 48bf9ed..036aec4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TreeStore = void 0; -var tslib_1 = require("tslib"); -var store_1 = tslib_1.__importDefault(require("./store")); +const tslib_1 = require("tslib"); +const store_1 = tslib_1.__importDefault(require("./store")); exports.TreeStore = store_1.default; diff --git a/lib/store.js b/lib/store.js index 930d722..63e1e39 100644 --- a/lib/store.js +++ b/lib/store.js @@ -1,44 +1,27 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -var tslib_1 = require("tslib"); -var lodash_1 = require("oak-domain/lib/utils/lodash"); -var assert_1 = require("oak-domain/lib/utils/assert"); -var Entity_1 = require("oak-domain/lib/types/Entity"); -var Demand_1 = require("oak-domain/lib/types/Demand"); -var Exception_1 = require("oak-domain/lib/types/Exception"); -var Demand_2 = require("oak-domain/lib/types/Demand"); -var relation_1 = require("oak-domain/lib/store/relation"); -var Expression_1 = require("oak-domain/lib/types/Expression"); -var CascadeStore_1 = require("oak-domain/lib/store/CascadeStore"); -var filter_1 = require("oak-domain/lib/store/filter"); +const lodash_1 = require("oak-domain/lib/utils/lodash"); +const assert_1 = require("oak-domain/lib/utils/assert"); +const Entity_1 = require("oak-domain/lib/types/Entity"); +const Demand_1 = require("oak-domain/lib/types/Demand"); +const Exception_1 = require("oak-domain/lib/types/Exception"); +const Demand_2 = require("oak-domain/lib/types/Demand"); +const relation_1 = require("oak-domain/lib/store/relation"); +const Expression_1 = require("oak-domain/lib/types/Expression"); +const CascadeStore_1 = require("oak-domain/lib/store/CascadeStore"); +const filter_1 = require("oak-domain/lib/store/filter"); ; ; function obscurePass(value, option) { - return !!((option === null || option === void 0 ? void 0 : option.obscure) && value === undefined); + return !!(option?.obscure && value === undefined); +} +class OakExpressionUnresolvedException extends Exception_1.OakException { } -var OakExpressionUnresolvedException = /** @class */ (function (_super) { - tslib_1.__extends(OakExpressionUnresolvedException, _super); - function OakExpressionUnresolvedException() { - return _super !== null && _super.apply(this, arguments) || this; - } - return OakExpressionUnresolvedException; -}(Exception_1.OakException)); ; -var TreeStore = /** @class */ (function (_super) { - tslib_1.__extends(TreeStore, _super); - function TreeStore(storageSchema) { - var _this = _super.call(this, storageSchema) || this; - _this.commitCallbacks = []; - _this.store = {}; - _this.activeTxnDict = {}; - _this.stat = { - create: 0, - update: 0, - remove: 0, - commit: 0, - }; - return _this; - } +class TreeStore extends CascadeStore_1.CascadeStore { + store; + activeTxnDict; + stat; /* treeStore改成同步以后不会再出现 private async waitOnTxn(id: string, context: Cxt) { // 先检查自己的等待者中有没有id,以避免死锁 @@ -58,183 +41,149 @@ var TreeStore = /** @class */ (function (_super) { ); await p; } */ - TreeStore.prototype.supportMultipleCreate = function () { + supportMultipleCreate() { return false; - }; - TreeStore.prototype.supportManyToOneJoin = function () { + } + supportManyToOneJoin() { return false; - }; - TreeStore.prototype.resetInitialData = function (data, stat) { - var e_1, _a, _b; + } + resetInitialData(data, stat) { this.store = {}; - var now = Date.now(); - for (var entity in data) { - var attributes = this.getSchema()[entity].attributes; + const now = Date.now(); + for (const entity in data) { + const { attributes } = this.getSchema()[entity]; this.store[entity] = {}; - try { - for (var _c = (e_1 = void 0, tslib_1.__values(data[entity])), _d = _c.next(); !_d.done; _d = _c.next()) { - var row = _d.value; - for (var key in attributes) { - if (row[key] === undefined) { - Object.assign(row, (_b = {}, - _b[key] = null, - _b)); - } - } - /** - * 处理初始化数据 - */ - if (!row.$$createAt$$) { + for (const row of data[entity]) { + for (const key in attributes) { + if (row[key] === undefined) { Object.assign(row, { - $$createAt$$: now, + [key]: null, }); } - if (!row.$$deleteAt$$) { - Object.assign(row, { - $$deleteAt$$: null, - }); - } - if (!row.$$updateAt$$) { - Object.assign(row, { - $$updateAt$$: now, - }); - } - if (!row.$$seq$$) { - Object.assign(row, { - $$seq$$: "".concat(Math.ceil((Math.random() + 1000) * 100)), - }); - } - (0, assert_1.assert)(row.id && !row.id.includes('.')); - (0, lodash_1.set)(this.store, "".concat(entity, ".").concat(row.id, ".$current"), row); } - } - catch (e_1_1) { e_1 = { error: e_1_1 }; } - finally { - try { - if (_d && !_d.done && (_a = _c.return)) _a.call(_c); + /** + * 处理初始化数据 + */ + if (!row.$$createAt$$) { + Object.assign(row, { + $$createAt$$: now, + }); } - finally { if (e_1) throw e_1.error; } + if (!row.$$deleteAt$$) { + Object.assign(row, { + $$deleteAt$$: null, + }); + } + if (!row.$$updateAt$$) { + Object.assign(row, { + $$updateAt$$: now, + }); + } + if (!row.$$seq$$) { + Object.assign(row, { + $$seq$$: `${Math.ceil((Math.random() + 1000) * 100)}`, + }); + } + (0, assert_1.assert)(row.id && !row.id.includes('.')); + (0, lodash_1.set)(this.store, `${entity}.${row.id}.$current`, row); } } if (stat) { this.stat = stat; } - }; - TreeStore.prototype.getCurrentData = function (keys) { - var _a; - var result = {}; - for (var entity in this.store) { + } + getCurrentData(keys) { + const result = {}; + for (const entity in this.store) { if (keys && !keys.includes(entity)) { continue; } result[entity] = []; - for (var rowId in this.store[entity]) { - (_a = result[entity]) === null || _a === void 0 ? void 0 : _a.push(this.store[entity][rowId]['$current']); + for (const rowId in this.store[entity]) { + result[entity]?.push(this.store[entity][rowId]['$current']); } } return result; - }; - TreeStore.prototype.constructRow = function (node, context, option) { - var _a, _b, _c; - var data = (0, lodash_1.cloneDeep)(node.$current); + } + constructor(storageSchema) { + super(storageSchema); + this.store = {}; + this.activeTxnDict = {}; + this.stat = { + create: 0, + update: 0, + remove: 0, + commit: 0, + }; + } + constructRow(node, context, option) { + let data = (0, lodash_1.cloneDeep)(node.$current); if (context.getCurrentTxnId() && node.$txnId === context.getCurrentTxnId()) { if (!node.$next) { // 如果要求返回delete数据,返回带$$deleteAt$$的行 - if (option === null || option === void 0 ? void 0 : option.includedDeleted) { - return Object.assign({}, data, (_a = {}, - _a[Entity_1.DeleteAtAttribute] = 1, - _a)); + if (option?.includedDeleted) { + return Object.assign({}, data, { + [Entity_1.DeleteAtAttribute]: 1, + }); } return null; } else if (!node.$current) { // 本事务创建的,$$createAt$$和$$updateAt$$置为1 - return Object.assign({}, data, node.$next, (_b = {}, - _b[Entity_1.CreateAtAttribute] = 1, - _b[Entity_1.UpdateAtAttribute] = 1, - _b)); + return Object.assign({}, data, node.$next, { + [Entity_1.CreateAtAttribute]: 1, + [Entity_1.UpdateAtAttribute]: 1, + }); } else { // 本事务更新的,$$updateAt$$置为1 - return Object.assign({}, data, node.$next, (_c = {}, - _c[Entity_1.UpdateAtAttribute] = 1, - _c)); + return Object.assign({}, data, node.$next, { + [Entity_1.UpdateAtAttribute]: 1, + }); } } return data; - }; - TreeStore.prototype.testFilterFns = function (node, nodeDict, exprResolveFns, fns) { - var e_2, _a, e_3, _b, e_4, _c; - var self = fns.self, otm = fns.otm, mto = fns.mto; - try { - // 三种filterFn是and的关系,有一个失败就返回false,优先判断顺序:self -> mto -> otm - for (var self_1 = tslib_1.__values(self), self_1_1 = self_1.next(); !self_1_1.done; self_1_1 = self_1.next()) { - var f = self_1_1.value; - if (!f(node, nodeDict, exprResolveFns)) { - return false; - } + } + testFilterFns(node, nodeDict, exprResolveFns, fns) { + const { self, otm, mto } = fns; + // 三种filterFn是and的关系,有一个失败就返回false,优先判断顺序:self -> mto -> otm + for (const f of self) { + if (!f(node, nodeDict, exprResolveFns)) { + return false; } } - catch (e_2_1) { e_2 = { error: e_2_1 }; } - finally { - try { - if (self_1_1 && !self_1_1.done && (_a = self_1.return)) _a.call(self_1); - } - finally { if (e_2) throw e_2.error; } - } - try { - for (var mto_1 = tslib_1.__values(mto), mto_1_1 = mto_1.next(); !mto_1_1.done; mto_1_1 = mto_1.next()) { - var f = mto_1_1.value; - if (!f(node, nodeDict, exprResolveFns)) { - return false; - } + for (const f of mto) { + if (!f(node, nodeDict, exprResolveFns)) { + return false; } } - catch (e_3_1) { e_3 = { error: e_3_1 }; } - finally { - try { - if (mto_1_1 && !mto_1_1.done && (_b = mto_1.return)) _b.call(mto_1); + for (const f of otm) { + if (!f(node, nodeDict, exprResolveFns)) { + return false; } - finally { if (e_3) throw e_3.error; } - } - try { - for (var otm_1 = tslib_1.__values(otm), otm_1_1 = otm_1.next(); !otm_1_1.done; otm_1_1 = otm_1.next()) { - var f = otm_1_1.value; - if (!f(node, nodeDict, exprResolveFns)) { - return false; - } - } - } - catch (e_4_1) { e_4 = { error: e_4_1 }; } - finally { - try { - if (otm_1_1 && !otm_1_1.done && (_c = otm_1.return)) _c.call(otm_1); - } - finally { if (e_4) throw e_4.error; } } return true; - }; - TreeStore.prototype.translateLogicFilter = function (entity, projection, filter, attr, context, option) { - var _this = this; - var self = []; - var otm = []; - var mto = []; + } + translateLogicFilter(entity, projection, filter, attr, context, option) { + const self = []; + const otm = []; + const mto = []; switch (attr) { case '$and': { - var filters = filter[attr]; - var fns = filters.map(function (ele) { return _this.translateFilterInner(entity, projection, ele, context, option); }); - self.push.apply(self, tslib_1.__spreadArray([], tslib_1.__read((fns.map(function (ele) { return ele.self; }).flat())), false)); - otm.push.apply(otm, tslib_1.__spreadArray([], tslib_1.__read((fns.map(function (ele) { return ele.otm; }).flat())), false)); - mto.push.apply(mto, tslib_1.__spreadArray([], tslib_1.__read((fns.map(function (ele) { return ele.mto; }).flat())), false)); + const filters = filter[attr]; + const fns = filters.map((ele) => this.translateFilterInner(entity, projection, ele, context, option)); + self.push(...(fns.map(ele => ele.self).flat())); + otm.push(...(fns.map(ele => ele.otm).flat())); + mto.push(...(fns.map(ele => ele.mto).flat())); break; } case '$or': { - var filters = filter[attr]; - var fns_1 = filters.map(function (ele) { return _this.translateFilterInner(entity, projection, ele, context, option); }); + const filters = filter[attr]; + const fns = filters.map((ele) => this.translateFilterInner(entity, projection, ele, context, option)); /** * 对于or的情况,按最坏的一种判定来计算,同时对所有的判定也可以排序,先计算代价最轻的 */ - fns_1.sort(function (ele1, ele2) { + fns.sort((ele1, ele2) => { if (ele2.mto.length > 0) { return -1; } @@ -249,26 +198,15 @@ var TreeStore = /** @class */ (function (_super) { } return 0; }); - var fn = function (node, nodeDict, exprResolveFns) { - var e_5, _a; - try { - for (var fns_2 = tslib_1.__values(fns_1), fns_2_1 = fns_2.next(); !fns_2_1.done; fns_2_1 = fns_2.next()) { - var fn_1 = fns_2_1.value; - if (_this.testFilterFns(node, nodeDict, exprResolveFns, fn_1)) { - return true; - } + const fn = (node, nodeDict, exprResolveFns) => { + for (const fn of fns) { + if (this.testFilterFns(node, nodeDict, exprResolveFns, fn)) { + return true; } } - catch (e_5_1) { e_5 = { error: e_5_1 }; } - finally { - try { - if (fns_2_1 && !fns_2_1.done && (_a = fns_2.return)) _a.call(fns_2); - } - finally { if (e_5) throw e_5.error; } - } return false; }; - var last = fns_1[fns_1.length - 1]; + const last = fns[fns.length - 1]; if (last.mto.length > 0) { mto.push(fn); } @@ -281,18 +219,18 @@ var TreeStore = /** @class */ (function (_super) { break; } case '$not': { - var filter2 = filter[attr]; - var filterFn_1 = this.translateFilterInner(entity, projection, filter2, context, option); - var fn = function (node, nodeDict, exprResolveFns) { - if (_this.testFilterFns(node, nodeDict, exprResolveFns, filterFn_1)) { + const filter2 = filter[attr]; + const filterFn = this.translateFilterInner(entity, projection, filter2, context, option); + const fn = (node, nodeDict, exprResolveFns) => { + if (this.testFilterFns(node, nodeDict, exprResolveFns, filterFn)) { return false; } return true; }; - if (filterFn_1.otm.length > 0) { + if (filterFn.otm.length > 0) { otm.push(fn); } - else if (filterFn_1.mto.length > 0) { + else if (filterFn.mto.length > 0) { mto.push(fn); } else { @@ -301,15 +239,15 @@ var TreeStore = /** @class */ (function (_super) { break; } default: { - (0, assert_1.assert)(false, "".concat(attr, "\u7B97\u5B50\u6682\u4E0D\u652F\u6301")); + (0, assert_1.assert)(false, `${attr}算子暂不支持`); } } return { - self: self, - otm: otm, - mto: mto, + self, + otm, + mto, }; - }; + } /** * 对表达式中某个结点的翻译,有三种情况: * 1、结点是一个表达式,此时递归翻译其子结点 @@ -322,18 +260,17 @@ var TreeStore = /** @class */ (function (_super) { * @param context * @returns */ - TreeStore.prototype.translateExpressionNode = function (entity, expression, context, option) { - var _this = this; + translateExpressionNode(entity, expression, context, option) { if ((0, Expression_1.isExpression)(expression)) { - var op_1 = Object.keys(expression)[0]; - var option2_1 = expression[op_1]; - if ((0, Expression_1.opMultipleParams)(op_1)) { - var paramsTranslated_1 = option2_1.map(function (ele) { return _this.translateExpressionNode(entity, ele, context, option2_1); }); - return function (row, nodeDict) { - var later = false; - var results = paramsTranslated_1.map(function (ele) { + const op = Object.keys(expression)[0]; + const option2 = expression[op]; + if ((0, Expression_1.opMultipleParams)(op)) { + const paramsTranslated = option2.map(ele => this.translateExpressionNode(entity, ele, context, option2)); + return (row, nodeDict) => { + let later = false; + let results = paramsTranslated.map((ele) => { if (typeof ele === 'function') { - var r = ele(row, nodeDict); + const r = ele(row, nodeDict); if (typeof r === 'function') { later = true; } @@ -342,70 +279,70 @@ var TreeStore = /** @class */ (function (_super) { return ele; }); if (!later) { - return (0, Expression_1.execOp)(op_1, results, option2_1.obscure); + return (0, Expression_1.execOp)(op, results, option2.obscure); } - var laterCheckFn = function (nodeDict2) { - results = results.map(function (ele) { + const laterCheckFn = (nodeDict2) => { + results = results.map((ele) => { if (typeof ele === 'function') { - var r = ele(nodeDict2); + const r = ele(nodeDict2); return r; } return ele; }); - if (results.find(function (ele) { return typeof ele === 'function'; })) { + if (results.find(ele => typeof ele === 'function')) { return laterCheckFn; } - return (0, Expression_1.execOp)(op_1, results, option && option.obscure); + return (0, Expression_1.execOp)(op, results, option && option.obscure); }; return laterCheckFn; }; } else { - var paramsTranslated_2 = this.translateExpressionNode(entity, option2_1, context, option2_1); - if (typeof paramsTranslated_2 === 'function') { - return function (row, nodeDict) { - var result = paramsTranslated_2(row, nodeDict); + const paramsTranslated = this.translateExpressionNode(entity, option2, context, option2); + if (typeof paramsTranslated === 'function') { + return (row, nodeDict) => { + let result = paramsTranslated(row, nodeDict); if (typeof result === 'function') { - var laterCheckFn_1 = function (nodeDict2) { + const laterCheckFn = (nodeDict2) => { result = result(nodeDict2); if (typeof result === 'function') { - return laterCheckFn_1; + return laterCheckFn; } return result; }; - return laterCheckFn_1; + return laterCheckFn; } - return (0, Expression_1.execOp)(op_1, result, option2_1.obscure); + return (0, Expression_1.execOp)(op, result, option2.obscure); }; } else { - return function () { - return (0, Expression_1.execOp)(op_1, paramsTranslated_2, option2_1.obscure); + return () => { + return (0, Expression_1.execOp)(op, paramsTranslated, option2.obscure); }; } } } else if ((0, Demand_2.isRefAttrNode)(expression)) { // 是RefAttr结点 - return function (row, nodeDict) { + return (row, nodeDict) => { if (expression.hasOwnProperty('#attr')) { // 说明是本结点的属性; return row[expression['#attr']]; } else { (0, assert_1.assert)(expression.hasOwnProperty('#refId')); - var _a = expression, refId_1 = _a["#refId"], refAttr_1 = _a["#refAttr"]; - if (nodeDict.hasOwnProperty(refId_1)) { - return nodeDict[refId_1][refAttr_1]; + const { ['#refId']: refId, ['#refAttr']: refAttr } = expression; + if (nodeDict.hasOwnProperty(refId)) { + return nodeDict[refId][refAttr]; } // 引用的结点还没有取到,此时需要在未来的某个时刻再检查 - var laterCheckFn_2 = function (nodeDict2) { - if (nodeDict2.hasOwnProperty(refId_1)) { - return nodeDict2[refId_1][refAttr_1]; + const laterCheckFn = (nodeDict2) => { + if (nodeDict2.hasOwnProperty(refId)) { + return nodeDict2[refId][refAttr]; } - return laterCheckFn_2; + return laterCheckFn; }; - return laterCheckFn_2; + return laterCheckFn; } }; } @@ -413,112 +350,100 @@ var TreeStore = /** @class */ (function (_super) { // 是常量结点 return expression; } - }; - TreeStore.prototype.translateExpression = function (entity, expression, context, option) { - var expr = this.translateExpressionNode(entity, expression, context, option); - return function (row, nodeDict) { + } + translateExpression(entity, expression, context, option) { + const expr = this.translateExpressionNode(entity, expression, context, option); + return (row, nodeDict) => { if (typeof expr !== 'function') { return expr; } - var result = expr(row, nodeDict); + const result = expr(row, nodeDict); return result; }; - }; - TreeStore.prototype.translateFulltext = function (entity, filter, context, option) { - var _this = this; + } + translateFulltext(entity, filter, context, option) { // 全文索引查找 - var _a = this.getSchema(), _b = entity, indexes = _a[_b].indexes; - var fulltextIndex = indexes.find(function (ele) { return ele.config && ele.config.type === 'fulltext'; }); - var attributes = fulltextIndex.attributes; - var $search = filter.$search; - return function (node) { - var e_6, _a; - var row = _this.constructRow(node, context, option); - try { - for (var attributes_1 = tslib_1.__values(attributes), attributes_1_1 = attributes_1.next(); !attributes_1_1.done; attributes_1_1 = attributes_1.next()) { - var attr = attributes_1_1.value; - var name_1 = attr.name; - if (row && row[name_1] && (typeof row[name_1] === 'string' && row[name_1].includes($search) || obscurePass(row[name_1], option))) { - return true; - } + const { [entity]: { indexes } } = this.getSchema(); + const fulltextIndex = indexes.find(ele => ele.config && ele.config.type === 'fulltext'); + const { attributes } = fulltextIndex; + const { $search } = filter; + return (node) => { + const row = this.constructRow(node, context, option); + for (const attr of attributes) { + const { name } = attr; + if (row && row[name] && (typeof row[name] === 'string' && row[name].includes($search) || obscurePass(row[name], option))) { + return true; } } - catch (e_6_1) { e_6 = { error: e_6_1 }; } - finally { - try { - if (attributes_1_1 && !attributes_1_1.done && (_a = attributes_1.return)) _a.call(attributes_1); - } - finally { if (e_6) throw e_6.error; } - } return false; }; - }; - TreeStore.prototype.translatePredicate = function (path, predicate, value, option) { + } + translatePredicate(path, predicate, value, option) { switch (predicate) { case '$gt': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data > value || obscurePass(data, option); }; } case '$lt': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data < value || obscurePass(data, option); }; } case '$gte': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data >= value || obscurePass(data, option); }; } case '$lte': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data <= value || obscurePass(data, option); }; } case '$eq': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data === value || obscurePass(data, option); }; } case '$ne': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data !== value || obscurePass(data, option); }; } case '$between': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['number', 'string'].includes(typeof data) && data >= value[0] && data <= value[1] || obscurePass(data, option); }; } case '$startsWith': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['string'].includes(typeof data) && data.startsWith(value) || obscurePass(data, option); }; } case '$endsWith': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['string'].includes(typeof data) && data.endsWith(value) || obscurePass(data, option); }; } case '$includes': { - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return ['string'].includes(typeof data) && data.includes(value) || obscurePass(data, option); }; } case '$exists': { (0, assert_1.assert)(typeof value === 'boolean'); - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); if (value) { return ![null, undefined].includes(data) || obscurePass(data, option); } @@ -529,55 +454,54 @@ var TreeStore = /** @class */ (function (_super) { } case '$in': { (0, assert_1.assert)(value instanceof Array); - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return value.includes(data) || obscurePass(data, option); }; } case '$nin': { (0, assert_1.assert)(value instanceof Array); - return function (row) { - var data = (0, lodash_1.get)(row, path); + return (row) => { + const data = (0, lodash_1.get)(row, path); return !value.includes(data) || obscurePass(data, option); }; } case '$contains': { // json中的多值查询 - var array_1 = value instanceof Array ? value : [value]; - return function (row) { - var data = (0, lodash_1.get)(row, path); - return (0, lodash_1.difference)(array_1, data).length === 0 || obscurePass(data, option); + const array = value instanceof Array ? value : [value]; + return (row) => { + const data = (0, lodash_1.get)(row, path); + return (0, lodash_1.difference)(array, data).length === 0 || obscurePass(data, option); }; } case '$overlaps': { // json中的多值查询 - var array_2 = value instanceof Array ? value : [value]; - return function (row) { - var data = (0, lodash_1.get)(row, path); - return (0, lodash_1.intersection)(array_2, data).length > 0 || obscurePass(data, option); + const array = value instanceof Array ? value : [value]; + return (row) => { + const data = (0, lodash_1.get)(row, path); + return (0, lodash_1.intersection)(array, data).length > 0 || obscurePass(data, option); }; } default: { - throw new Error("predicate ".concat(predicate, " is not recoganized")); + throw new Error(`predicate ${predicate} is not recoganized`); } } - }; - TreeStore.prototype.translateObjectPredicate = function (filter) { - var _this = this; - var fns = []; - var translatePredicateInner = function (p, path) { - var predicate = Object.keys(p)[0]; + } + translateObjectPredicate(filter) { + const fns = []; + const translatePredicateInner = (p, path) => { + const predicate = Object.keys(p)[0]; if (predicate.startsWith('$')) { (0, assert_1.assert)(Object.keys(p).length === 1); - fns.push(_this.translatePredicate(path, predicate, p[predicate])); + fns.push(this.translatePredicate(path, predicate, p[predicate])); } else { if (p instanceof Array) { - p.forEach(function (ele, idx) { - var path2 = "".concat(path, "[").concat(idx, "]"); + p.forEach((ele, idx) => { + const path2 = `${path}[${idx}]`; if (typeof ele !== 'object') { if (![null, undefined].includes(ele)) { - fns.push(_this.translatePredicate(path2, '$eq', ele)); + fns.push(this.translatePredicate(path2, '$eq', ele)); } } else { @@ -586,10 +510,10 @@ var TreeStore = /** @class */ (function (_super) { }); } else { - for (var attr in p) { - var path2 = path ? "".concat(path, ".").concat(attr) : attr; + for (const attr in p) { + const path2 = path ? `${path}.${attr}` : attr; if (typeof p[attr] !== 'object') { - fns.push(_this.translatePredicate(path2, '$eq', filter[attr])); + fns.push(this.translatePredicate(path2, '$eq', filter[attr])); } else { translatePredicateInner(p[attr], path2); @@ -599,78 +523,65 @@ var TreeStore = /** @class */ (function (_super) { } }; translatePredicateInner(filter, ''); - return function (value) { - var e_7, _a; - try { - for (var fns_3 = tslib_1.__values(fns), fns_3_1 = fns_3.next(); !fns_3_1.done; fns_3_1 = fns_3.next()) { - var fn = fns_3_1.value; - if (!fn(value)) { - return false; - } + return (value) => { + for (const fn of fns) { + if (!fn(value)) { + return false; } } - catch (e_7_1) { e_7 = { error: e_7_1 }; } - finally { - try { - if (fns_3_1 && !fns_3_1.done && (_a = fns_3.return)) _a.call(fns_3); - } - finally { if (e_7) throw e_7.error; } - } return true; }; - }; - TreeStore.prototype.translateAttribute = function (entity, filter, attr, context, option) { - var _this = this; - var _a; + } + translateAttribute(entity, filter, attr, context, option) { // 如果是模糊查询且该属性为undefined,说明没取到,返回true function obscurePassLocal(row) { return obscurePass(row[attr], option); } if (typeof filter !== 'object') { - return function (node) { - var row = _this.constructRow(node, context, option); + return (node) => { + const row = this.constructRow(node, context, option); return row ? row[attr] === filter || obscurePassLocal(row) : false; }; } else { - var predicate = Object.keys(filter)[0]; + const predicate = Object.keys(filter)[0]; if (predicate.startsWith('$')) { if (['$in', '$nin'].includes(predicate) && !(filter[predicate] instanceof Array)) { throw new Error('子查询已经改用一对多的外键连接方式'); - var inData_1 = filter[predicate]; + const inData = filter[predicate]; if (predicate === '$in') { // 如果是obscure,则返回的集合中有没有都不能否决“可能有”,所以可以直接返回true - if (option === null || option === void 0 ? void 0 : option.obscure) { - return function () { return true; }; + if (option?.obscure) { + return () => true; } else { // 这里只有当子查询中的filter不包含引用外部的子查询时才可以提前计算,否则必须等到执行时再计算 // 子查询查询的行不用返回,和数据库的行为保持一致 try { - var legalSets_1 = (this.selectAbjointRow(inData_1.entity, inData_1, context, { dontCollect: true })).map(function (ele) { - var data = inData_1.data; - var key = Object.keys(data)[0]; + const legalSets = (this.selectAbjointRow(inData.entity, inData, context, { dontCollect: true })).map((ele) => { + const { data } = inData; + const key = Object.keys(data)[0]; return ele[key]; }); - return function (node) { - var row = _this.constructRow(node, context, option); + return (node) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - return legalSets_1.includes(row[attr]); + return legalSets.includes(row[attr]); }; } catch (err) { if (err instanceof OakExpressionUnresolvedException) { - return function (node, nodeDict) { - var row = _this.constructRow(node, context, option); + return (node, nodeDict) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - var option2 = Object.assign({}, option, { nodeDict: nodeDict }); - var legalSets = _this.selectAbjointRow(inData_1.entity, inData_1, context, option2).map(function (ele) { - var data = inData_1.data; - var key = Object.keys(data)[0]; + const option2 = Object.assign({}, option, { nodeDict }); + const legalSets = this.selectAbjointRow(inData.entity, inData, context, option2).map((ele) => { + const { data } = inData; + const key = Object.keys(data)[0]; return ele[key]; }); return legalSets.includes(row[attr]); @@ -687,30 +598,30 @@ var TreeStore = /** @class */ (function (_super) { // 这里只有当子查询中的filter不包含引用外部的子查询时才可以提前计算,否则必须等到执行时再计算 // 子查询查询的行不用返回,和数据库的行为保持一致 try { - var legalSets_2 = this.selectAbjointRow(inData_1.entity, inData_1, context, { dontCollect: true }).map(function (ele) { - var data = inData_1.data; - var key = Object.keys(data)[0]; + const legalSets = this.selectAbjointRow(inData.entity, inData, context, { dontCollect: true }).map((ele) => { + const { data } = inData; + const key = Object.keys(data)[0]; return ele[key]; }); - return function (node) { - var row = _this.constructRow(node, context, option); + return (node) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - return !legalSets_2.includes(row[attr]) || obscurePassLocal(row); + return !legalSets.includes(row[attr]) || obscurePassLocal(row); }; } catch (err) { if (err instanceof OakExpressionUnresolvedException) { - return function (node, nodeDict) { - var row = _this.constructRow(node, context, option); + return (node, nodeDict) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - var option2 = Object.assign({}, option, { nodeDict: nodeDict }); - var legalSets = _this.selectAbjointRow(inData_1.entity, inData_1, context, option2).map(function (ele) { - var data = inData_1.data; - var key = Object.keys(data)[0]; + const option2 = Object.assign({}, option, { nodeDict }); + const legalSets = this.selectAbjointRow(inData.entity, inData, context, option2).map((ele) => { + const { data } = inData; + const key = Object.keys(data)[0]; return ele[key]; }); return !legalSets.includes(row[attr]) || obscurePassLocal(row); @@ -723,56 +634,54 @@ var TreeStore = /** @class */ (function (_super) { } } else { - var fn_2 = this.translatePredicate(attr, predicate, filter[predicate], option); - return function (node) { - var row = _this.constructRow(node, context, option); + const fn = this.translatePredicate(attr, predicate, filter[predicate], option); + return (node) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - return fn_2(row); + return fn(row); }; } } else { // 对象的内部查询 - (0, assert_1.assert)(((_a = this.getSchema()[entity].attributes[attr]) === null || _a === void 0 ? void 0 : _a.type) === 'object'); - var fn_3 = this.translateObjectPredicate(filter); - return function (node) { - var row = _this.constructRow(node, context, option); + (0, assert_1.assert)(this.getSchema()[entity].attributes[attr]?.type === 'object'); + const fn = this.translateObjectPredicate(filter); + return (node) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - return fn_3(row[attr]) || obscurePassLocal(row); + return fn(row[attr]) || obscurePassLocal(row); }; } } - }; - TreeStore.prototype.translateFilterInner = function (entity, projection, filter, context, option) { - var _this = this; - var self = []; - var otm = []; - var mto = []; - var nodeId; - var _loop_1 = function (attr) { - var _a, _b, _c; + } + translateFilterInner(entity, projection, filter, context, option) { + const self = []; + const otm = []; + const mto = []; + let nodeId; + for (const attr in filter) { if (attr === '#id') { nodeId = filter['#id']; } else if (['$and', '$or', '$xor', '$not'].includes(attr)) { - var filterFns = this_1.translateLogicFilter(entity, projection, filter, attr, context, option); - self.push.apply(self, tslib_1.__spreadArray([], tslib_1.__read((filterFns.self)), false)); - otm.push.apply(otm, tslib_1.__spreadArray([], tslib_1.__read((filterFns.otm)), false)); - mto.push.apply(mto, tslib_1.__spreadArray([], tslib_1.__read((filterFns.mto)), false)); + const filterFns = this.translateLogicFilter(entity, projection, filter, attr, context, option); + self.push(...(filterFns.self)); + otm.push(...(filterFns.otm)); + mto.push(...(filterFns.mto)); } else if (attr.toLowerCase().startsWith(Demand_1.EXPRESSION_PREFIX)) { - var fn_4 = this_1.translateExpression(entity, filter[attr], context, option); + const fn = this.translateExpression(entity, filter[attr], context, option); // expression上先假设大都是只查询自身和外层的属性,不一定对。by Xc 20230824 - self.push(function (node, nodeDict, exprResolveFns) { - var row = _this.constructRow(node, context, option); + self.push((node, nodeDict, exprResolveFns) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - var result = fn_4(row, nodeDict); + const result = fn(row, nodeDict); if (typeof result === 'function') { exprResolveFns.push(result); } @@ -780,20 +689,20 @@ var TreeStore = /** @class */ (function (_super) { }); } else if (attr.toLowerCase() === '$text') { - self.push(this_1.translateFulltext(entity, filter[attr], context, option)); + self.push(this.translateFulltext(entity, filter[attr], context, option)); } else { // 属性级过滤 - var relation_2 = (0, relation_1.judgeRelation)(this_1.getSchema(), entity, attr); - if (relation_2 === 1) { + const relation = (0, relation_1.judgeRelation)(this.getSchema(), entity, attr); + if (relation === 1) { // 行本身的属性 - self.push(this_1.translateAttribute(entity, filter[attr], attr, context, option)); + self.push(this.translateAttribute(entity, filter[attr], attr, context, option)); } - else if (relation_2 === 2) { + else if (relation === 2) { // 基于entity/entityId的指针 - var filterFn_2 = this_1.translateFilter(attr, projection[attr] || {}, filter[attr], context, option); - mto.push(function (node, nodeDict, exprResolveFns) { - var row = _this.constructRow(node, context, option); + const filterFn = this.translateFilter(attr, projection[attr] || {}, filter[attr], context, option); + mto.push((node, nodeDict, exprResolveFns) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } @@ -802,14 +711,14 @@ var TreeStore = /** @class */ (function (_super) { } if (row.entityId === undefined || row.entity === undefined) { (0, assert_1.assert)(typeof projection[attr] === 'object'); - if (option === null || option === void 0 ? void 0 : option.ignoreAttrMiss) { + if (option?.ignoreAttrMiss) { if (process.env.NODE_ENV === 'development') { - console.warn("\u5BF9\u8C61".concat(entity, "\u4E0A\u7684entity/entityId\u4E0D\u80FD\u786E\u5B9A\u503C\uFF0C\u53EF\u80FD\u4F1A\u5F71\u54CD\u5224\u5B9A\u7ED3\u679C")); + console.warn(`对象${entity}上的entity/entityId不能确定值,可能会影响判定结果`); } return false; // 若不能确定,认定为条件不满足 } throw new Exception_1.OakRowUnexistedException([{ - entity: entity, + entity, selection: { data: projection, filter: { @@ -824,48 +733,48 @@ var TreeStore = /** @class */ (function (_super) { if (row.entityId === null) { return false; } - var node2 = (0, lodash_1.get)(_this.store, "".concat(attr, ".").concat(row.entityId)); + const node2 = (0, lodash_1.get)(this.store, `${attr}.${row.entityId}`); if (!node2) { - if (option === null || option === void 0 ? void 0 : option.obscure) { + if (option?.obscure) { return true; } return false; } - return filterFn_2(node2, nodeDict, exprResolveFns); + return filterFn(node2, nodeDict, exprResolveFns); }); } - else if (typeof relation_2 === 'string') { + else if (typeof relation === 'string') { // 只能是基于普通属性的外键 - var filterFn_3 = this_1.translateFilter(relation_2, projection[attr] || {}, filter[attr], context, option); - mto.push(function (node, nodeDict, exprResolveFns) { - var row = _this.constructRow(node, context, option); + const filterFn = this.translateFilter(relation, projection[attr] || {}, filter[attr], context, option); + mto.push((node, nodeDict, exprResolveFns) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - if (obscurePass(row["".concat(attr, "Id")], option)) { + if (obscurePass(row[`${attr}Id`], option)) { return true; } - if (row["".concat(attr, "Id")]) { - var node2 = (0, lodash_1.get)(_this.store, "".concat(relation_2, ".").concat(row["".concat(attr, "Id")])); + if (row[`${attr}Id`]) { + const node2 = (0, lodash_1.get)(this.store, `${relation}.${row[`${attr}Id`]}`); if (!node2) { - if (option === null || option === void 0 ? void 0 : option.obscure) { + if (option?.obscure) { return true; } return false; } - return filterFn_3(node2, nodeDict, exprResolveFns); + return filterFn(node2, nodeDict, exprResolveFns); } - if (row["".concat(attr, "Id")] === undefined) { + if (row[`${attr}Id`] === undefined) { // 说明一对多的外键没有取出来,需要抛出RowUnexists异常 (0, assert_1.assert)(typeof projection[attr] === 'object'); - if (option === null || option === void 0 ? void 0 : option.ignoreAttrMiss) { + if (option?.ignoreAttrMiss) { if (process.env.NODE_ENV === 'development') { - console.warn("\u5BF9\u8C61".concat(entity, "\u4E0A\u7684").concat(attr, "Id\u4E0D\u80FD\u786E\u5B9A\u503C\uFF0C\u53EF\u80FD\u4F1A\u5F71\u54CD\u5224\u5B9A\u7ED3\u679C")); + console.warn(`对象${entity}上的${attr}Id不能确定值,可能会影响判定结果`); } return false; // 若不能确定,认定为条件不满足 } throw new Exception_1.OakRowUnexistedException([{ - entity: entity, + entity, selection: { data: projection, filter: { @@ -874,27 +783,26 @@ var TreeStore = /** @class */ (function (_super) { }, }]); } - (0, assert_1.assert)(row["".concat(attr, "Id")] === null); + (0, assert_1.assert)(row[`${attr}Id`] === null); return false; }); } - else if (relation_2 instanceof Array) { + else if (relation instanceof Array) { // 一对多的子查询 - var _d = tslib_1.__read(relation_2, 2), otmEntity_1 = _d[0], otmForeignKey_1 = _d[1]; - var predicate_1 = filter[attr][Demand_1.SUB_QUERY_PREDICATE_KEYWORD] || 'in'; - if (option === null || option === void 0 ? void 0 : option.obscure) { + const [otmEntity, otmForeignKey] = relation; + const predicate = filter[attr][Demand_1.SUB_QUERY_PREDICATE_KEYWORD] || 'in'; + if (option?.obscure) { // 如果是obscure,则返回的集合中有没有都不能否决“可能有”或“并不全部是”,所以可以直接返回true - if (['in', 'not all'].includes(predicate_1)) { - self.push(function () { return true; }); - return "continue"; + if (['in', 'not all'].includes(predicate)) { + self.push(() => true); + continue; } } - var fk_1 = otmForeignKey_1 || 'entityId'; - var otmProjection_1 = (_a = { - id: 1 - }, - _a[fk_1] = 1, - _a); + const fk = otmForeignKey || 'entityId'; + const otmProjection = { + id: 1, + [fk]: 1, + }; /** * in代表外键连接后至少有一行数据 * not in代表外键连接后一行也不能有 @@ -903,10 +811,9 @@ var TreeStore = /** @class */ (function (_super) { * * 此时还没有确定父行,只有查询中明确带有id的查询可以先执行,否则不执行,暂先这个逻辑 by Xc 20230725 */ - var makeAfterLogic = function () { - otm.push(function (node, nodeDict) { - var _a, _b; - var row = _this.constructRow(node, context, option); + const makeAfterLogic = () => { + otm.push((node, nodeDict) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } @@ -916,31 +823,31 @@ var TreeStore = /** @class */ (function (_super) { * all代表反外键连接条件的一行也不能有(符合的是否至少要有一行?直觉上没这个限制) * not all 代表反外键连接条件的至少有一行 */ - var otmFilter = !otmForeignKey_1 ? Object.assign({ - entity: entity, + const otmFilter = !otmForeignKey ? Object.assign({ + entity, }, filter[attr]) : (0, lodash_1.cloneDeep)(filter[attr]); - if (['not in', 'in'].includes(predicate_1)) { - Object.assign(otmFilter, (_a = {}, - _a[fk_1] = row.id, - _a)); + if (['not in', 'in'].includes(predicate)) { + Object.assign(otmFilter, { + [fk]: row.id, + }); } else { - Object.assign(otmFilter, (_b = {}, - _b[fk_1] = { + Object.assign(otmFilter, { + [fk]: { $ne: row.id, - }, - _b)); + } + }); } - var option2 = Object.assign({}, option, { nodeDict: nodeDict, dontCollect: true }); - var subQuerySet = (_this.selectAbjointRow(otmEntity_1, { - data: otmProjection_1, + const option2 = Object.assign({}, option, { nodeDict, dontCollect: true }); + const subQuerySet = (this.selectAbjointRow(otmEntity, { + data: otmProjection, filter: otmFilter, indexFrom: 0, count: 1, - }, context, option2)).map(function (ele) { - return (ele)[fk_1]; + }, context, option2)).map((ele) => { + return (ele)[fk]; }); - switch (predicate_1) { + switch (predicate) { case 'in': case 'not all': { return subQuerySet.length > 0; @@ -950,52 +857,52 @@ var TreeStore = /** @class */ (function (_super) { return subQuerySet.length === 0; } default: { - throw new Error("illegal sqp: ".concat(predicate_1)); + throw new Error(`illegal sqp: ${predicate}`); } } }); }; if (filter.id && typeof filter.id === 'string') { - var otmFilter = !otmForeignKey_1 ? Object.assign({ - entity: entity, + const otmFilter = !otmForeignKey ? Object.assign({ + entity, }, filter[attr]) : (0, lodash_1.cloneDeep)(filter[attr]); - if (['not in', 'in'].includes(predicate_1)) { - Object.assign(otmFilter, (_b = {}, - _b[fk_1] = filter.id, - _b)); + if (['not in', 'in'].includes(predicate)) { + Object.assign(otmFilter, { + [fk]: filter.id, + }); } else { - Object.assign(otmFilter, (_c = {}, - _c[fk_1] = { + Object.assign(otmFilter, { + [fk]: { $ne: filter.id, - }, - _c)); + } + }); } try { - var subQuerySet_1 = (this_1.selectAbjointRow(otmEntity_1, { - data: otmProjection_1, + const subQuerySet = (this.selectAbjointRow(otmEntity, { + data: otmProjection, filter: otmFilter, indexFrom: 0, count: 1, - }, context, { dontCollect: true })).map(function (ele) { - return (ele)[fk_1]; + }, context, { dontCollect: true })).map((ele) => { + return (ele)[fk]; }); - self.push(function (node) { - var row = _this.constructRow(node, context, option); + self.push((node) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - switch (predicate_1) { + switch (predicate) { case 'in': case 'not all': { - return subQuerySet_1.length > 0; + return subQuerySet.length > 0; } case 'not in': case 'all': { - return subQuerySet_1.length === 0; + return subQuerySet.length === 0; } default: { - throw new Error("illegal sqp: ".concat(predicate_1)); + throw new Error(`illegal sqp: ${predicate}`); } } }); @@ -1009,38 +916,37 @@ var TreeStore = /** @class */ (function (_super) { } } } - else if (!(option === null || option === void 0 ? void 0 : option.disableSubQueryHashjoin)) { + else if (!option?.disableSubQueryHashjoin) { /** * 尝试用hashjoin将内表数组取出,因为memory中的表都不会太大,且用不了索引(未来优化了可能可以用id直接取值),因而用hash应当会更快 */ - var option2 = Object.assign({}, option, { dontCollect: true }); + const option2 = Object.assign({}, option, { dontCollect: true }); try { - var subQueryRows = this_1.selectAbjointRow(otmEntity_1, { - data: otmProjection_1, + const subQueryRows = this.selectAbjointRow(otmEntity, { + data: otmProjection, filter: filter[attr], }, context, option2); - var buckets_1 = (0, lodash_1.groupBy)(subQueryRows, fk_1); - otm.push(function (node, nodeDict) { - var _a, _b; - var row = _this.constructRow(node, context, option); + const buckets = (0, lodash_1.groupBy)(subQueryRows, fk); + otm.push((node, nodeDict) => { + const row = this.constructRow(node, context, option); if (!row) { return false; } - switch (predicate_1) { + switch (predicate) { case 'in': { - return (((_a = buckets_1[row.id]) === null || _a === void 0 ? void 0 : _a.length) > 0); + return (buckets[row.id]?.length > 0); } case 'not in': { - return (!buckets_1[row.id] || buckets_1[row.id].length === 0); + return (!buckets[row.id] || buckets[row.id].length === 0); } case 'all': { - return (((_b = buckets_1[row.id]) === null || _b === void 0 ? void 0 : _b.length) > 0 && Object.keys(buckets_1).length === 1); + return (buckets[row.id]?.length > 0 && Object.keys(buckets).length === 1); } case 'not all': { - return Object.keys(buckets_1).length > 1 || !buckets_1.hasOwnProperty(row.id); + return Object.keys(buckets).length > 1 || !buckets.hasOwnProperty(row.id); } default: { - (0, assert_1.assert)(false, "unrecoganized sqp operator: ".concat(predicate_1)); + (0, assert_1.assert)(false, `unrecoganized sqp operator: ${predicate}`); } } }); @@ -1060,46 +966,39 @@ var TreeStore = /** @class */ (function (_super) { } else { // metadata - (0, assert_1.assert)(relation_2 === 0); + (0, assert_1.assert)(relation === 0); } } - }; - var this_1 = this; - for (var attr in filter) { - _loop_1(attr); } return { - self: self, - otm: otm, - mto: mto, - nodeId: nodeId, + self, + otm, + mto, + nodeId, }; - }; - TreeStore.prototype.translateFilter = function (entity, projection, filter, context, option) { - var _this = this; - var filterFns = this.translateFilterInner(entity, projection, filter, context, option); - var nodeId = filterFns.nodeId; - return function (node, nodeDict, exprResolveFns) { - var _a; + } + translateFilter(entity, projection, filter, context, option) { + const filterFns = this.translateFilterInner(entity, projection, filter, context, option); + const { nodeId } = filterFns; + return (node, nodeDict, exprResolveFns) => { if (nodeId) { - (0, assert_1.assert)(!nodeDict.hasOwnProperty(nodeId), "Filter\u4E2D\u7684nodeId\u300C".concat(nodeId, "\u300D\u51FA\u73B0\u4E86\u591A\u6B21")); - Object.assign(nodeDict, (_a = {}, - _a[nodeId] = _this.constructRow(node, context, option), - _a)); + (0, assert_1.assert)(!nodeDict.hasOwnProperty(nodeId), `Filter中的nodeId「${nodeId}」出现了多次`); + Object.assign(nodeDict, { + [nodeId]: this.constructRow(node, context, option), + }); } - return _this.testFilterFns(node, nodeDict, exprResolveFns, filterFns); + return this.testFilterFns(node, nodeDict, exprResolveFns, filterFns); }; - }; - TreeStore.prototype.translateSorter = function (entity, sorter, context, option) { - var _this = this; - var compare = function (row1, row2, entity2, sortAttr, direction) { - var row11 = row1; - var row22 = row2; + } + translateSorter(entity, sorter, context, option) { + const compare = (row1, row2, entity2, sortAttr, direction) => { + const row11 = row1; + const row22 = row2; (0, assert_1.assert)(Object.keys(sortAttr).length === 1); - var attr = Object.keys(sortAttr)[0]; - var relation = (0, relation_1.judgeRelation)(_this.getSchema(), entity2, attr); + const attr = Object.keys(sortAttr)[0]; + const relation = (0, relation_1.judgeRelation)(this.getSchema(), entity2, attr); if (relation === 1 || relation === 0) { - var getAttrOrExprValue = function (r) { + const getAttrOrExprValue = (r) => { if (sortAttr[attr] === 1) { return r[attr]; } @@ -1109,8 +1008,8 @@ var TreeStore = /** @class */ (function (_super) { return r[sortAttr[attr]]; } }; - var v1 = row1 && getAttrOrExprValue(row11); - var v2 = row2 && getAttrOrExprValue(row22); + const v1 = row1 && getAttrOrExprValue(row11); + const v2 = row2 && getAttrOrExprValue(row22); if ([null, undefined].includes(v1) || [null, undefined].includes(v2)) { if ([null, undefined].includes(v1) && [null, undefined].includes(v2)) { return 0; @@ -1150,131 +1049,97 @@ var TreeStore = /** @class */ (function (_super) { if (relation === 2) { (0, assert_1.assert)(row11['entity'] === row22['entity']); (0, assert_1.assert)(row11.entity === attr); - var node1 = _this.store[row11.entity] && _this.store[row11.entity][row11.entityId]; - var node2 = _this.store[row22.entity] && _this.store[row22.entity][row22.entityId]; - var row111 = node1 && _this.constructRow(node1, context, option); - var row222 = node2 && _this.constructRow(node2, context, option); + const node1 = this.store[row11.entity] && this.store[row11.entity][row11.entityId]; + const node2 = this.store[row22.entity] && this.store[row22.entity][row22.entityId]; + const row111 = node1 && this.constructRow(node1, context, option); + const row222 = node2 && this.constructRow(node2, context, option); return compare(row111, row222, row11['entity'], sortAttr[attr], direction); } else { (0, assert_1.assert)(typeof relation === 'string'); - var node1 = _this.store[relation] && _this.store[relation][row11["".concat(attr, "Id")]]; - var node2 = _this.store[relation] && _this.store[relation][row22["".concat(attr, "Id")]]; - var row111 = node1 && _this.constructRow(node1, context, option); - var row222 = node2 && _this.constructRow(node2, context, option); + const node1 = this.store[relation] && this.store[relation][row11[`${attr}Id`]]; + const node2 = this.store[relation] && this.store[relation][row22[`${attr}Id`]]; + const row111 = node1 && this.constructRow(node1, context, option); + const row222 = node2 && this.constructRow(node2, context, option); return compare(row111, row222, relation, sortAttr[attr], direction); } } }; - return function (row1, row2) { - var e_8, _a; - try { - for (var sorter_1 = tslib_1.__values(sorter), sorter_1_1 = sorter_1.next(); !sorter_1_1.done; sorter_1_1 = sorter_1.next()) { - var sorterElement = sorter_1_1.value; - var $attr = sorterElement.$attr, $direction = sorterElement.$direction; - var result = compare(row1, row2, entity, $attr, $direction); - if (result !== 0) { - return result; - } + return (row1, row2) => { + for (const sorterElement of sorter) { + const { $attr, $direction } = sorterElement; + const result = compare(row1, row2, entity, $attr, $direction); + if (result !== 0) { + return result; } } - catch (e_8_1) { e_8 = { error: e_8_1 }; } - finally { - try { - if (sorter_1_1 && !sorter_1_1.done && (_a = sorter_1.return)) _a.call(sorter_1); - } - finally { if (e_8) throw e_8.error; } - } return 0; }; - }; + } /** * 目标行,如果有id过滤条件可直接取 * @param entity * @param selection * @returns */ - TreeStore.prototype.getEntityNodes = function (entity, selection, context) { - var filter = selection.filter; - var ids = (0, filter_1.getRelevantIds)(filter); + getEntityNodes(entity, selection, context) { + const { filter } = selection; + const ids = (0, filter_1.getRelevantIds)(filter); if (this.store[entity]) { if (ids.length > 0) { - var entityNodes = (0, lodash_1.pick)(this.store[entity], ids); + const entityNodes = (0, lodash_1.pick)(this.store[entity], ids); return Object.values(entityNodes); } return Object.values(this.store[entity]); } return []; - }; - TreeStore.prototype.selectAbjointRow = function (entity, selection, context, option) { - var e_9, _a, e_10, _b; - var _this = this; - var data = selection.data, filter = selection.filter; - var nodeDict = option === null || option === void 0 ? void 0 : option.nodeDict; - var filterFn = filter && this.translateFilter(entity, data, filter, context, option); - var entityNodes = this.getEntityNodes(entity, selection, context); - var nodes = []; - try { - for (var entityNodes_1 = tslib_1.__values(entityNodes), entityNodes_1_1 = entityNodes_1.next(); !entityNodes_1_1.done; entityNodes_1_1 = entityNodes_1.next()) { - var n = entityNodes_1_1.value; - if (n.$txnId && n.$txnId !== context.getCurrentTxnId() && n.$current === null) { - continue; - } - (0, assert_1.assert)(!n.$txnId || n.$txnId === context.getCurrentTxnId()); - var exprResolveFns = []; - var nodeDict2 = {}; - if (nodeDict) { - Object.assign(nodeDict2, nodeDict); - } - // 如果没有filterFn,要保证行不为null(本事务remove的case) - if (filterFn ? filterFn(n, nodeDict2, exprResolveFns) : this.constructRow(n, context, option)) { - // 如果有延时处理的expression,在这里加以判断,此时所有在filter中的node应该都已经加以遍历了 - var exprResult = true; - if (exprResolveFns.length > 0) { - try { - for (var exprResolveFns_1 = (e_10 = void 0, tslib_1.__values(exprResolveFns)), exprResolveFns_1_1 = exprResolveFns_1.next(); !exprResolveFns_1_1.done; exprResolveFns_1_1 = exprResolveFns_1.next()) { - var fn = exprResolveFns_1_1.value; - var result = fn(nodeDict2); - if (typeof result === 'function') { - throw new OakExpressionUnresolvedException(); - } - if (!!!result) { - exprResult = false; - break; - } - } + } + selectAbjointRow(entity, selection, context, option) { + const { data, filter } = selection; + const nodeDict = option?.nodeDict; + const filterFn = filter && this.translateFilter(entity, data, filter, context, option); + const entityNodes = this.getEntityNodes(entity, selection, context); + const nodes = []; + for (const n of entityNodes) { + if (n.$txnId && n.$txnId !== context.getCurrentTxnId() && n.$current === null) { + continue; + } + (0, assert_1.assert)(!n.$txnId || n.$txnId === context.getCurrentTxnId()); + const exprResolveFns = []; + const nodeDict2 = {}; + if (nodeDict) { + Object.assign(nodeDict2, nodeDict); + } + // 如果没有filterFn,要保证行不为null(本事务remove的case) + if (filterFn ? filterFn(n, nodeDict2, exprResolveFns) : this.constructRow(n, context, option)) { + // 如果有延时处理的expression,在这里加以判断,此时所有在filter中的node应该都已经加以遍历了 + let exprResult = true; + if (exprResolveFns.length > 0) { + for (const fn of exprResolveFns) { + const result = fn(nodeDict2); + if (typeof result === 'function') { + throw new OakExpressionUnresolvedException(); } - catch (e_10_1) { e_10 = { error: e_10_1 }; } - finally { - try { - if (exprResolveFns_1_1 && !exprResolveFns_1_1.done && (_b = exprResolveFns_1.return)) _b.call(exprResolveFns_1); - } - finally { if (e_10) throw e_10.error; } + if (!!!result) { + exprResult = false; + break; } } - if (exprResult) { - nodes.push(n); - } + } + if (exprResult) { + nodes.push(n); } } } - catch (e_9_1) { e_9 = { error: e_9_1 }; } - finally { - try { - if (entityNodes_1_1 && !entityNodes_1_1.done && (_a = entityNodes_1.return)) _a.call(entityNodes_1); - } - finally { if (e_9) throw e_9.error; } - } - var rows = nodes.map(function (node) { return _this.constructRow(node, context, option); }); - var rows2 = this.formResult(entity, rows, selection, context, option); + const rows = nodes.map((node) => this.constructRow(node, context, option)); + const rows2 = this.formResult(entity, rows, selection, context, option); return rows2; - }; - TreeStore.prototype.updateAbjointRow = function (entity, operation, context, option) { - var e_11, _a; - var data = operation.data, action = operation.action, operId = operation.id; + } + updateAbjointRow(entity, operation, context, option) { + const { data, action, id: operId } = operation; switch (action) { case 'create': { - var id = data.id; + const { id } = data; (0, assert_1.assert)(id); // const node = this.store[entity] && (this.store[entity]!)[id as string]; // const row = node && this.constructRow(node, context) || {}; @@ -1282,30 +1147,30 @@ var TreeStore = /** @class */ (function (_super) { throw new OakError(RowStore.$$LEVEL, RowStore.$$CODES.primaryKeyConfilict); } */ if (this.store[entity] && (this.store[entity])[id]) { - var node = this.store[entity] && (this.store[entity])[id]; + const node = this.store[entity] && (this.store[entity])[id]; throw new Exception_1.OakCongruentRowExists(entity, this.constructRow(node, context, option)); } if (!data.$$seq$$) { // tree-store随意生成即可 Object.assign(data, { - $$seq$$: "".concat(Math.ceil((Math.random() + 1000) * 100)), + $$seq$$: `${Math.ceil((Math.random() + 1000) * 100)}`, }); } - var node2 = { + const node2 = { $txnId: context.getCurrentTxnId(), $current: null, $next: data, - $path: "".concat(entity, ".").concat(id), + $path: `${entity}.${id}`, }; if (!this.store[entity]) { this.store[entity] = {}; } - (0, lodash_1.set)(this.store, "".concat(entity, ".").concat(id), node2); + (0, lodash_1.set)(this.store, `${entity}.${id}`, node2); this.addToTxnNode(node2, context, 'create'); return 1; } default: { - var selection = { + const selection = { data: { id: 1, }, @@ -1313,75 +1178,53 @@ var TreeStore = /** @class */ (function (_super) { indexFrom: operation.indexFrom, count: operation.count, }; - var rows = this.selectAbjointRow(entity, selection, context, { dontCollect: true }); - var ids = rows.map(function (ele) { return ele.id; }); - try { - for (var ids_1 = tslib_1.__values(ids), ids_1_1 = ids_1.next(); !ids_1_1.done; ids_1_1 = ids_1.next()) { - var id = ids_1_1.value; - var alreadyDirtyNode = false; - var node = (this.store[entity])[id]; - (0, assert_1.assert)(node && (!node.$txnId || node.$txnId == context.getCurrentTxnId())); - if (!node.$txnId) { - node.$txnId = context.getCurrentTxnId(); - } - else { - (0, assert_1.assert)(node.$txnId === context.getCurrentTxnId()); - alreadyDirtyNode = true; - } - node.$path = "".concat(entity, ".").concat(id); - if (action === 'remove') { - node.$next = null; - if (!alreadyDirtyNode) { - // 如果已经更新过的结点就不能再加了,会形成循环 - this.addToTxnNode(node, context, 'remove'); - } - } - else { - node.$next = Object.assign(node.$next || {}, data); - if (!alreadyDirtyNode) { - // 如果已经更新过的结点就不能再加了,会形成循环 - this.addToTxnNode(node, context, 'update'); - } + const rows = this.selectAbjointRow(entity, selection, context, { dontCollect: true }); + const ids = rows.map(ele => ele.id); + for (const id of ids) { + let alreadyDirtyNode = false; + const node = (this.store[entity])[id]; + (0, assert_1.assert)(node && (!node.$txnId || node.$txnId == context.getCurrentTxnId())); + if (!node.$txnId) { + node.$txnId = context.getCurrentTxnId(); + } + else { + (0, assert_1.assert)(node.$txnId === context.getCurrentTxnId()); + alreadyDirtyNode = true; + } + node.$path = `${entity}.${id}`; + if (action === 'remove') { + node.$next = null; + if (!alreadyDirtyNode) { + // 如果已经更新过的结点就不能再加了,会形成循环 + this.addToTxnNode(node, context, 'remove'); } } - } - catch (e_11_1) { e_11 = { error: e_11_1 }; } - finally { - try { - if (ids_1_1 && !ids_1_1.done && (_a = ids_1.return)) _a.call(ids_1); + else { + node.$next = Object.assign(node.$next || {}, data); + if (!alreadyDirtyNode) { + // 如果已经更新过的结点就不能再加了,会形成循环 + this.addToTxnNode(node, context, 'update'); + } } - finally { if (e_11) throw e_11.error; } } return rows.length; } } - }; - TreeStore.prototype.selectAbjointRowAsync = function (entity, selection, context, option) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - return tslib_1.__generator(this, function (_a) { - return [2 /*return*/, this.selectAbjointRow(entity, selection, context, option)]; - }); - }); - }; - TreeStore.prototype.updateAbjointRowAsync = function (entity, operation, context, option) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - return tslib_1.__generator(this, function (_a) { - return [2 /*return*/, this.updateAbjointRow(entity, operation, context, option)]; - }); - }); - }; - TreeStore.prototype.operateSync = function (entity, operation, context, option) { + } + async selectAbjointRowAsync(entity, selection, context, option) { + return this.selectAbjointRow(entity, selection, context, option); + } + async updateAbjointRowAsync(entity, operation, context, option) { + return this.updateAbjointRow(entity, operation, context, option); + } + operateSync(entity, operation, context, option) { (0, assert_1.assert)(context.getCurrentTxnId()); - return _super.prototype.operateSync.call(this, entity, operation, context, option); - }; - TreeStore.prototype.operateAsync = function (entity, operation, context, option) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - return tslib_1.__generator(this, function (_a) { - (0, assert_1.assert)(context.getCurrentTxnId()); - return [2 /*return*/, _super.prototype.operateAsync.call(this, entity, operation, context, option)]; - }); - }); - }; + return super.operateSync(entity, operation, context, option); + } + async operateAsync(entity, operation, context, option) { + (0, assert_1.assert)(context.getCurrentTxnId()); + return super.operateAsync(entity, operation, context, option); + } /** * 计算最终结果集当中的函数,这个函数可能测试不够充分 * @param entity @@ -1390,216 +1233,192 @@ var TreeStore = /** @class */ (function (_super) { * @param nodeDict * @param context */ - TreeStore.prototype.formExprInResult = function (entity, projection, data, nodeDict, context) { - var _a, _b, _c, _d; - var _this = this; - var laterExprDict = {}; - for (var attr in projection) { + formExprInResult(entity, projection, data, nodeDict, context) { + const laterExprDict = {}; + for (const attr in projection) { if (attr.startsWith(Demand_1.EXPRESSION_PREFIX)) { - var ExprNodeTranslator = this.translateExpression(entity, projection[attr], context, {}); - var exprResult = ExprNodeTranslator(data, nodeDict); + const ExprNodeTranslator = this.translateExpression(entity, projection[attr], context, {}); + const exprResult = ExprNodeTranslator(data, nodeDict); if (typeof exprResult === 'function') { - Object.assign(laterExprDict, (_a = {}, - _a[attr] = exprResult, - _a)); + Object.assign(laterExprDict, { + [attr]: exprResult, + }); } else { - Object.assign(data, (_b = {}, - _b[attr] = exprResult, - _b)); + Object.assign(data, { + [attr]: exprResult, + }); } } else if (attr === '#id') { - var nodeId = projection[attr]; - (0, assert_1.assert)(!nodeDict.hasOwnProperty(nodeId), "Filter\u4E2D\u7684nodeId\u300C".concat(nodeId, "\u300D\u51FA\u73B0\u4E86\u591A\u6B21")); - Object.assign(nodeDict, (_c = {}, - _c[nodeId] = data, - _c)); + const nodeId = projection[attr]; + (0, assert_1.assert)(!nodeDict.hasOwnProperty(nodeId), `Filter中的nodeId「${nodeId}」出现了多次`); + Object.assign(nodeDict, { + [nodeId]: data, + }); } } - var _loop_2 = function (attr) { - var rel = this_2.judgeRelation(entity, attr); + for (const attr in projection) { + const rel = this.judgeRelation(entity, attr); if (rel === 1) { } else if (rel === 2) { if (data[attr]) { - this_2.formExprInResult(attr, projection[attr], data[attr], nodeDict, context); + this.formExprInResult(attr, projection[attr], data[attr], nodeDict, context); } } else if (typeof rel === 'string') { if (data[attr]) { - var result2 = {}; - this_2.formExprInResult(rel, projection[attr], data[attr], nodeDict, context); + const result2 = {}; + this.formExprInResult(rel, projection[attr], data[attr], nodeDict, context); } } else if (rel instanceof Array) { if (!attr.endsWith('$$aggr')) { if (data[attr] && data[attr] instanceof Array) { - data[attr].map(function (ele) { return _this.formExprInResult(rel[0], projection[attr].data, ele, nodeDict, context); }); + data[attr].map((ele) => this.formExprInResult(rel[0], projection[attr].data, ele, nodeDict, context)); } } } - }; - var this_2 = this; - for (var attr in projection) { - _loop_2(attr); } - for (var attr in laterExprDict) { - var exprResult = laterExprDict[attr](nodeDict); + for (const attr in laterExprDict) { + const exprResult = laterExprDict[attr](nodeDict); // projection是不应出现计算不出来的情况 (0, assert_1.assert)(typeof exprResult !== 'function', 'data中的expr无法计算,请检查命名与引用的一致性'); - Object.assign(data, (_d = {}, - _d[attr] = exprResult, - _d)); + Object.assign(data, { + [attr]: exprResult, + }); } - }; - TreeStore.prototype.formResult = function (entity, rows, selection, context, option) { - var e_12, _a, _b, _c, _d; - var _this = this; - var data = selection.data, sorter = selection.sorter, indexFrom = selection.indexFrom, count = selection.count; - var findAvailableExprName = function (current) { - var counter = 1; + } + formResult(entity, rows, selection, context, option) { + const { data, sorter, indexFrom, count } = selection; + const findAvailableExprName = (current) => { + let counter = 1; while (counter < 20) { - var exprName = "$expr".concat(counter++); + const exprName = `$expr${counter++}`; if (!current.includes(exprName)) { return exprName; } } (0, assert_1.assert)(false, '找不到可用的expr命名'); }; - var sortToProjection = function (entity2, proj, sort) { - Object.keys(sort).forEach(function (attr) { - var _a, _b, _c, _d; + const sortToProjection = (entity2, proj, sort) => { + Object.keys(sort).forEach((attr) => { // 要把sorter中的expr运算提到这里做掉,否则异步运算无法排序 if (attr.startsWith('$expr') && typeof sort[attr] === 'object') { - var attrName = findAvailableExprName(Object.keys(proj)); - Object.assign(proj, (_a = {}, - _a[attrName] = sort[attr], - _a)); - Object.assign(sort, (_b = {}, - _b[attr] = attrName, - _b)); + const attrName = findAvailableExprName(Object.keys(proj)); + Object.assign(proj, { + [attrName]: sort[attr], + }); + Object.assign(sort, { + [attr]: attrName, + }); } - var rel = (0, relation_1.judgeRelation)(_this.getSchema(), entity2, attr); + const rel = (0, relation_1.judgeRelation)(this.getSchema(), entity2, attr); if (rel === 2 || typeof rel === 'string') { if (!proj[attr]) { - Object.assign(proj, (_c = {}, - _c[attr] = {}, - _c)); + Object.assign(proj, { + [attr]: {}, + }); } - var entity3 = typeof rel === 'string' ? rel : attr; + const entity3 = typeof rel === 'string' ? rel : attr; sortToProjection(entity3, proj[attr], sort[attr]); } else if (rel === 1) { - Object.assign(proj, (_d = {}, - _d[attr] = 1, - _d)); + Object.assign(proj, { + [attr]: 1, + }); } }); }; if (sorter) { - sorter.forEach(function (ele) { + sorter.forEach((ele) => { sortToProjection(entity, data, ele.$attr); }); } // 先计算projection,formResult只处理abjoint的行,不需要考虑expression和一对多多对一关系 - var rows2 = []; - var incompletedRowIds = []; - var projection = selection.data; - try { - for (var rows_1 = tslib_1.__values(rows), rows_1_1 = rows_1.next(); !rows_1_1.done; rows_1_1 = rows_1.next()) { - var row = rows_1_1.value; - var result = {}; - var _loop_3 = function (attr) { - var _e, _f; - var rel = this_3.judgeRelation(entity, attr); - if (rel === 1) { - if (row[attr] === undefined) { - incompletedRowIds.push(row.id); - // break; - } - else if (typeof projection[attr] === 'number') { - Object.assign(result, (_e = {}, - _e[attr] = row[attr], - _e)); - } - else { - // object数据的深层次select - Object.assign(result, (_f = {}, - _f[attr] = {}, - _f)); - var assignIner_1 = function (dest, proj, source) { - if (proj instanceof Array) { - (0, assert_1.assert)(dest instanceof Array); - (0, assert_1.assert)(source instanceof Array); - proj.forEach(function (attr, idx) { - if (typeof attr === 'number') { - dest[idx] = source[idx]; - } - else if (typeof attr === 'object') { - dest[idx] = {}; - assignIner_1(dest[idx], attr, source[idx]); - } - }); - } - else { - for (var attr_1 in proj) { - if (typeof proj[attr_1] === 'number') { - dest[attr_1] = source[attr_1]; - } - else if (typeof proj[attr_1] === 'object') { - dest[attr_1] = proj[attr_1] instanceof Array ? [] : {}; - assignIner_1(dest[attr_1], proj[attr_1], source[attr_1]); - } + const rows2 = []; + const incompletedRowIds = []; + const { data: projection } = selection; + for (const row of rows) { + const result = {}; + for (const attr in projection) { + const rel = this.judgeRelation(entity, attr); + if (rel === 1) { + if (row[attr] === undefined) { + incompletedRowIds.push(row.id); + // break; + } + else if (typeof projection[attr] === 'number') { + Object.assign(result, { + [attr]: row[attr], + }); + } + else { + // object数据的深层次select + Object.assign(result, { + [attr]: {}, + }); + const assignIner = (dest, proj, source) => { + if (proj instanceof Array) { + (0, assert_1.assert)(dest instanceof Array); + (0, assert_1.assert)(source instanceof Array); + proj.forEach((attr, idx) => { + if (typeof attr === 'number') { + dest[idx] = source[idx]; + } + else if (typeof attr === 'object') { + dest[idx] = {}; + assignIner(dest[idx], attr, source[idx]); + } + }); + } + else { + for (const attr in proj) { + if (typeof proj[attr] === 'number') { + dest[attr] = source[attr]; + } + else if (typeof proj[attr] === 'object') { + dest[attr] = proj[attr] instanceof Array ? [] : {}; + assignIner(dest[attr], proj[attr], source[attr]); } } - }; - assignIner_1(result[attr], projection[attr], row[attr]); - } + } + }; + assignIner(result[attr], projection[attr], row[attr]); } - }; - var this_3 = this; - for (var attr in projection) { - _loop_3(attr); } - // 这三个属性在前台cache中可能表达特殊语义的,需要返回 - if (row[Entity_1.DeleteAtAttribute]) { - Object.assign(result, (_b = {}, - _b[Entity_1.DeleteAtAttribute] = row[Entity_1.DeleteAtAttribute], - _b)); - } - if (row[Entity_1.UpdateAtAttribute]) { - Object.assign(result, (_c = {}, - _c[Entity_1.UpdateAtAttribute] = row[Entity_1.UpdateAtAttribute], - _c)); - } - if (row[Entity_1.CreateAtAttribute]) { - Object.assign(result, (_d = {}, - _d[Entity_1.CreateAtAttribute] = row[Entity_1.CreateAtAttribute], - _d)); - } - rows2.push(result); } - } - catch (e_12_1) { e_12 = { error: e_12_1 }; } - finally { - try { - if (rows_1_1 && !rows_1_1.done && (_a = rows_1.return)) _a.call(rows_1); + // 这三个属性在前台cache中可能表达特殊语义的,需要返回 + if (row[Entity_1.DeleteAtAttribute]) { + Object.assign(result, { + [Entity_1.DeleteAtAttribute]: row[Entity_1.DeleteAtAttribute], + }); } - finally { if (e_12) throw e_12.error; } + if (row[Entity_1.UpdateAtAttribute]) { + Object.assign(result, { + [Entity_1.UpdateAtAttribute]: row[Entity_1.UpdateAtAttribute], + }); + } + if (row[Entity_1.CreateAtAttribute]) { + Object.assign(result, { + [Entity_1.CreateAtAttribute]: row[Entity_1.CreateAtAttribute], + }); + } + rows2.push(result); } if (incompletedRowIds.length > 0) { // 如果有缺失属性的行,则报OakRowUnexistedException错误 // fixed: 这里不报了。按约定框架应当保证取到要访问的属性 // fixed: 和外键缺失一样,还是报,上层在知道框架会保证取到的情况下用allowMiss忽略此错误 - if (option === null || option === void 0 ? void 0 : option.ignoreAttrMiss) { + if (option?.ignoreAttrMiss) { if (process.env.NODE_ENV === 'development') { - console.warn("\u5BF9\u8C61".concat(entity, "\u4E0A\u6709\u5C5E\u6027\u7F3A\u5931\uFF0C\u53EF\u80FD\u4F1A\u5F71\u54CD\u4E0A\u5C42\u4F7F\u7528\u7ED3\u679C\uFF0C\u8BF7\u786E\u5B9A")); + console.warn(`对象${entity}上有属性缺失,可能会影响上层使用结果,请确定`); } } else { throw new Exception_1.OakRowUnexistedException([{ - entity: entity, + entity, selection: { data: projection, filter: { @@ -1613,7 +1432,7 @@ var TreeStore = /** @class */ (function (_super) { } // 再计算sorter if (sorter) { - var sorterFn = this.translateSorter(entity, sorter, context, option); + const sorterFn = this.translateSorter(entity, sorter, context, option); rows2.sort(sorterFn); } // 最后用indexFrom和count来截断 @@ -1623,298 +1442,220 @@ var TreeStore = /** @class */ (function (_super) { else { return rows2; } - }; + } /** * 本函数把结果中的相应属性映射成一个字符串,用于GroupBy * @param entity * @param row * @param projection */ - TreeStore.prototype.mappingProjectionOnRow = function (entity, row, projection) { - var _this = this; - var key = ''; - var result = {}; - var values = []; - var mappingIter = function (entity2, row2, p2, result2) { - var e_13, _a; - var keys = Object.keys(p2).sort(function (ele1, ele2) { return ele1 < ele2 ? -1 : 1; }); - try { - for (var keys_1 = tslib_1.__values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) { - var k = keys_1_1.value; - var rel = _this.judgeRelation(entity2, k); - if (rel === 2) { - result2[k] = {}; - if (row2[k]) { - mappingIter(k, row2[k], p2[k], result2[k]); - } - } - else if (typeof rel === 'string') { - result2[k] = {}; - if (row2[k]) { - mappingIter(rel, row2[k], p2[k], result2[k]); - } - } - else { - (0, assert_1.assert)([0, 1].includes(rel)); - result2[k] = row2[k]; - (0, assert_1.assert)(['string', 'number', 'boolean'].includes(typeof row2[k])); - key += "".concat(row2[k]); - values.push(row2[k]); + mappingProjectionOnRow(entity, row, projection) { + let key = ''; + let result = {}; + const values = []; + const mappingIter = (entity2, row2, p2, result2) => { + const keys = Object.keys(p2).sort((ele1, ele2) => ele1 < ele2 ? -1 : 1); + for (const k of keys) { + const rel = this.judgeRelation(entity2, k); + if (rel === 2) { + result2[k] = {}; + if (row2[k]) { + mappingIter(k, row2[k], p2[k], result2[k]); } } - } - catch (e_13_1) { e_13 = { error: e_13_1 }; } - finally { - try { - if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1); + else if (typeof rel === 'string') { + result2[k] = {}; + if (row2[k]) { + mappingIter(rel, row2[k], p2[k], result2[k]); + } + } + else { + (0, assert_1.assert)([0, 1].includes(rel)); + result2[k] = row2[k]; + (0, assert_1.assert)(['string', 'number', 'boolean'].includes(typeof row2[k])); + key += `${row2[k]}`; + values.push(row2[k]); } - finally { if (e_13) throw e_13.error; } } }; mappingIter(entity, row, projection, result); return { - result: result, - key: key, - values: values, + result, + key, + values, }; - }; - TreeStore.prototype.calcAggregation = function (entity, rows, aggregationData) { - var e_14, _a, e_15, _b, e_16, _c; - var ops = Object.keys(aggregationData).filter(function (ele) { return ele !== '#aggr'; }); - var result = {}; - try { - for (var rows_2 = tslib_1.__values(rows), rows_2_1 = rows_2.next(); !rows_2_1.done; rows_2_1 = rows_2.next()) { - var row = rows_2_1.value; - try { - for (var ops_1 = (e_15 = void 0, tslib_1.__values(ops)), ops_1_1 = ops_1.next(); !ops_1_1.done; ops_1_1 = ops_1.next()) { - var op = ops_1_1.value; - var values = this.mappingProjectionOnRow(entity, row, aggregationData[op]).values; - (0, assert_1.assert)(values.length === 1, "\u805A\u5408\u8FD0\u7B97\u4E2D\uFF0C".concat(op, "\u7684\u76EE\u6807\u5C5E\u6027\u591A\u4E8E1\u4E2A")); - if (op.startsWith('#max')) { - if (![undefined, null].includes(values[0]) && (!result.hasOwnProperty(op) || result[op] < values[0])) { - result[op] = values[0]; - } - } - else if (op.startsWith('#min')) { - if (![undefined, null].includes(values[0]) && (!result.hasOwnProperty(op) || result[op] > values[0])) { - result[op] = values[0]; - } - } - else if (op.startsWith('#sum')) { - if (![undefined, null].includes(values[0])) { - (0, assert_1.assert)(typeof values[0] === 'number', '只有number类型的属性才可以计算sum'); - if (!result.hasOwnProperty(op)) { - result[op] = values[0]; - } - else { - result[op] += values[0]; - } - } - } - else if (op.startsWith('#count')) { - if (![undefined, null].includes(values[0])) { - if (!result.hasOwnProperty(op)) { - result[op] = 1; - } - else { - result[op] += 1; - } - } + } + calcAggregation(entity, rows, aggregationData) { + const ops = Object.keys(aggregationData).filter(ele => ele !== '#aggr'); + const result = {}; + for (const row of rows) { + for (const op of ops) { + const { values } = this.mappingProjectionOnRow(entity, row, aggregationData[op]); + (0, assert_1.assert)(values.length === 1, `聚合运算中,${op}的目标属性多于1个`); + if (op.startsWith('#max')) { + if (![undefined, null].includes(values[0]) && (!result.hasOwnProperty(op) || result[op] < values[0])) { + result[op] = values[0]; + } + } + else if (op.startsWith('#min')) { + if (![undefined, null].includes(values[0]) && (!result.hasOwnProperty(op) || result[op] > values[0])) { + result[op] = values[0]; + } + } + else if (op.startsWith('#sum')) { + if (![undefined, null].includes(values[0])) { + (0, assert_1.assert)(typeof values[0] === 'number', '只有number类型的属性才可以计算sum'); + if (!result.hasOwnProperty(op)) { + result[op] = values[0]; } else { - (0, assert_1.assert)(op.startsWith('#avg')); - if (![undefined, null].includes(values[0])) { - (0, assert_1.assert)(typeof values[0] === 'number', '只有number类型的属性才可以计算avg'); - if (!result.hasOwnProperty(op)) { - result[op] = { - total: values[0], - count: 1, - }; - } - else { - result[op].total += values[0]; - result[op].count += 1; - } - } + result[op] += values[0]; } } } - catch (e_15_1) { e_15 = { error: e_15_1 }; } - finally { - try { - if (ops_1_1 && !ops_1_1.done && (_b = ops_1.return)) _b.call(ops_1); + else if (op.startsWith('#count')) { + if (![undefined, null].includes(values[0])) { + if (!result.hasOwnProperty(op)) { + result[op] = 1; + } + else { + result[op] += 1; + } + } + } + else { + (0, assert_1.assert)(op.startsWith('#avg')); + if (![undefined, null].includes(values[0])) { + (0, assert_1.assert)(typeof values[0] === 'number', '只有number类型的属性才可以计算avg'); + if (!result.hasOwnProperty(op)) { + result[op] = { + total: values[0], + count: 1, + }; + } + else { + result[op].total += values[0]; + result[op].count += 1; + } } - finally { if (e_15) throw e_15.error; } } } } - catch (e_14_1) { e_14 = { error: e_14_1 }; } - finally { - try { - if (rows_2_1 && !rows_2_1.done && (_a = rows_2.return)) _a.call(rows_2); - } - finally { if (e_14) throw e_14.error; } - } - try { - for (var ops_2 = tslib_1.__values(ops), ops_2_1 = ops_2.next(); !ops_2_1.done; ops_2_1 = ops_2.next()) { - var op = ops_2_1.value; - if (!result[op]) { - if (op.startsWith('#count')) { - result[op] = 0; - } - else { - result[op] = null; - } + for (const op of ops) { + if (!result[op]) { + if (op.startsWith('#count')) { + result[op] = 0; } - else if (op.startsWith('#avg')) { - result[op] = result[op].total / result[op].count; + else { + result[op] = null; } } - } - catch (e_16_1) { e_16 = { error: e_16_1 }; } - finally { - try { - if (ops_2_1 && !ops_2_1.done && (_c = ops_2.return)) _c.call(ops_2); + else if (op.startsWith('#avg')) { + result[op] = result[op].total / result[op].count; } - finally { if (e_16) throw e_16.error; } } return result; - }; - TreeStore.prototype.formAggregation = function (entity, rows, aggregationData) { - var _this = this; - var aggrExpr = aggregationData["#aggr"]; + } + formAggregation(entity, rows, aggregationData) { + const { "#aggr": aggrExpr } = aggregationData; if (aggrExpr) { - var groups_1 = (0, lodash_1.groupBy)(rows, function (row) { - var key = _this.mappingProjectionOnRow(entity, row, aggrExpr).key; + const groups = (0, lodash_1.groupBy)(rows, (row) => { + const { key } = this.mappingProjectionOnRow(entity, row, aggrExpr); return key; }); - var result = Object.keys(groups_1).map(function (ele) { - var aggr = _this.calcAggregation(entity, groups_1[ele], aggregationData); - var r = _this.mappingProjectionOnRow(entity, groups_1[ele][0], aggrExpr).result; + const result = Object.keys(groups).map((ele) => { + const aggr = this.calcAggregation(entity, groups[ele], aggregationData); + const { result: r } = this.mappingProjectionOnRow(entity, groups[ele][0], aggrExpr); aggr['#data'] = r; return aggr; }); return result; } - var aggr = this.calcAggregation(entity, rows, aggregationData); + const aggr = this.calcAggregation(entity, rows, aggregationData); return [aggr]; - }; - TreeStore.prototype.selectSync = function (entity, selection, context, option) { - var _this = this; + } + selectSync(entity, selection, context, option) { (0, assert_1.assert)(context.getCurrentTxnId()); - var result = _super.prototype.selectSync.call(this, entity, selection, context, option); + const result = super.selectSync(entity, selection, context, option); // 在这里再计算所有的表达式 - result.forEach(function (ele) { return _this.formExprInResult(entity, selection.data, ele, {}, context); }); + result.forEach((ele) => this.formExprInResult(entity, selection.data, ele, {}, context)); return result; - }; - TreeStore.prototype.selectAsync = function (entity, selection, context, option) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - var result; - var _this = this; - return tslib_1.__generator(this, function (_a) { - switch (_a.label) { - case 0: - (0, assert_1.assert)(context.getCurrentTxnId()); - return [4 /*yield*/, _super.prototype.selectAsync.call(this, entity, selection, context, option)]; - case 1: - result = _a.sent(); - // 在这里再计算所有的表达式 - result.forEach(function (ele) { return _this.formExprInResult(entity, selection.data, ele, {}, context); }); - return [2 /*return*/, result]; - } - }); - }); - }; - TreeStore.prototype.aggregateSync = function (entity, aggregation, context, option) { - var _this = this; + } + async selectAsync(entity, selection, context, option) { (0, assert_1.assert)(context.getCurrentTxnId()); - var data = aggregation.data, filter = aggregation.filter, sorter = aggregation.sorter, indexFrom = aggregation.indexFrom, count = aggregation.count; - var p = {}; - for (var k in data) { + const result = await super.selectAsync(entity, selection, context, option); + // 在这里再计算所有的表达式 + result.forEach((ele) => this.formExprInResult(entity, selection.data, ele, {}, context)); + return result; + } + aggregateSync(entity, aggregation, context, option) { + (0, assert_1.assert)(context.getCurrentTxnId()); + const { data, filter, sorter, indexFrom, count } = aggregation; + const p = {}; + for (const k in data) { Object.assign(p, (0, lodash_1.cloneDeep)(data[k])); } - var selection = { + const selection = { data: p, - filter: filter, - sorter: sorter, - indexFrom: indexFrom, - count: count, + filter, + sorter, + indexFrom, + count, }; - var result = this.cascadeSelect(entity, selection, context, Object.assign({}, option, { + const result = this.cascadeSelect(entity, selection, context, Object.assign({}, option, { dontCollect: true, })); // 在这里再计算所有的表达式 - result.forEach(function (ele) { return _this.formExprInResult(entity, selection.data, ele, {}, context); }); + result.forEach((ele) => this.formExprInResult(entity, selection.data, ele, {}, context)); // 最后计算Aggregation return this.formAggregation(entity, result, aggregation.data); - }; - TreeStore.prototype.aggregateAsync = function (entity, aggregation, context, option) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - var data, filter, sorter, indexFrom, count, p, k, selection, result; - var _this = this; - return tslib_1.__generator(this, function (_a) { - switch (_a.label) { - case 0: - (0, assert_1.assert)(context.getCurrentTxnId()); - data = aggregation.data, filter = aggregation.filter, sorter = aggregation.sorter, indexFrom = aggregation.indexFrom, count = aggregation.count; - p = {}; - for (k in data) { - Object.assign(p, (0, lodash_1.cloneDeep)(data[k])); - } - selection = { - data: p, - filter: filter, - sorter: sorter, - indexFrom: indexFrom, - count: count, - }; - return [4 /*yield*/, this.cascadeSelectAsync(entity, selection, context, Object.assign({}, option, { - dontCollect: true, - }))]; - case 1: - result = _a.sent(); - // 在这里再计算所有的表达式 - result.forEach(function (ele) { return _this.formExprInResult(entity, selection.data, ele, {}, context); }); - // 最后计算Aggregation - return [2 /*return*/, this.formAggregation(entity, result, aggregation.data)]; - } - }); - }); - }; - TreeStore.prototype.countSync = function (entity, selection, context, option) { - var selection2 = Object.assign({}, selection, { + } + async aggregateAsync(entity, aggregation, context, option) { + (0, assert_1.assert)(context.getCurrentTxnId()); + const { data, filter, sorter, indexFrom, count } = aggregation; + const p = {}; + for (const k in data) { + Object.assign(p, (0, lodash_1.cloneDeep)(data[k])); + } + const selection = { + data: p, + filter, + sorter, + indexFrom, + count, + }; + const result = await this.cascadeSelectAsync(entity, selection, context, Object.assign({}, option, { + dontCollect: true, + })); + // 在这里再计算所有的表达式 + result.forEach((ele) => this.formExprInResult(entity, selection.data, ele, {}, context)); + // 最后计算Aggregation + return this.formAggregation(entity, result, aggregation.data); + } + countSync(entity, selection, context, option) { + const selection2 = Object.assign({}, selection, { data: { id: 1, }, }); - var result = this.selectSync(entity, selection2, context, Object.assign({}, option, { + const result = this.selectSync(entity, selection2, context, Object.assign({}, option, { dontCollect: true, })); return typeof selection.count === 'number' ? Math.min(result.length, selection.count) : result.length; - }; - TreeStore.prototype.countAsync = function (entity, selection, context, option) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - var selection2, result; - return tslib_1.__generator(this, function (_a) { - switch (_a.label) { - case 0: - selection2 = Object.assign({}, selection, { - data: { - id: 1, - }, - }); - return [4 /*yield*/, this.selectAsync(entity, selection2, context, Object.assign({}, option, { - dontCollect: true, - }))]; - case 1: - result = _a.sent(); - return [2 /*return*/, typeof selection.count === 'number' ? Math.min(result.length, selection.count) : result.length]; - } - }); + } + async countAsync(entity, selection, context, option) { + const selection2 = Object.assign({}, selection, { + data: { + id: 1, + }, }); - }; - TreeStore.prototype.addToTxnNode = function (node, context, action) { - var txnNode = this.activeTxnDict[context.getCurrentTxnId()]; + const result = await this.selectAsync(entity, selection2, context, Object.assign({}, option, { + dontCollect: true, + })); + return typeof selection.count === 'number' ? Math.min(result.length, selection.count) : result.length; + } + addToTxnNode(node, context, action) { + const txnNode = this.activeTxnDict[context.getCurrentTxnId()]; (0, assert_1.assert)(txnNode); if (!node.$nextNode) { // 如果nextNode有值,说明这个结点已经在链表中了 @@ -1927,60 +1668,56 @@ var TreeStore = /** @class */ (function (_super) { } } txnNode[action]++; - }; - TreeStore.prototype.getStat = function () { + } + getStat() { return this.stat; - }; - TreeStore.prototype.beginSync = function () { - var _a; - var uuid = "".concat(Math.random()); + } + beginSync() { + const uuid = `${Math.random()}`; (0, assert_1.assert)(!this.activeTxnDict.hasOwnProperty(uuid)); - Object.assign(this.activeTxnDict, (_a = {}, - _a[uuid] = { + Object.assign(this.activeTxnDict, { + [uuid]: { create: 0, update: 0, remove: 0, waitList: [], }, - _a)); + }); return uuid; - }; - TreeStore.prototype.onCommit = function (callback) { - var _this = this; + } + commitCallbacks = []; + onCommit(callback) { this.commitCallbacks.push(callback); - return function () { return (0, lodash_1.pull)(_this.commitCallbacks, callback); }; - }; - TreeStore.prototype.addToOperationResult = function (result, entity, action) { - var _a, _b, _c; + return () => (0, lodash_1.pull)(this.commitCallbacks, callback); + } + addToOperationResult(result, entity, action) { if (result[entity]) { if (result[entity][action]) { result[entity][action]++; } else { - Object.assign(result[entity], (_a = {}, - _a[action] = 1, - _a)); + Object.assign(result[entity], { + [action]: 1, + }); } } else { - Object.assign(result, (_b = {}, - _b[entity] = (_c = {}, - _c[action] = 1, - _c), - _b)); + Object.assign(result, { + [entity]: { + [action]: 1, + }, + }); } - }; - TreeStore.prototype.commitSync = function (uuid) { - var e_17, _a; - var _b; + } + commitSync(uuid) { (0, assert_1.assert)(this.activeTxnDict.hasOwnProperty(uuid), uuid); - var node = this.activeTxnDict[uuid].nodeHeader; - var result = {}; + let node = this.activeTxnDict[uuid].nodeHeader; + const result = {}; while (node) { - var node2 = node.$nextNode; + const node2 = node.$nextNode; if (node.$txnId === uuid) { (0, assert_1.assert)(node.$path); - var entity = (_b = node.$path) === null || _b === void 0 ? void 0 : _b.split('.')[0]; + const entity = node.$path?.split('.')[0]; if (node.$next) { // create/update node.$current = Object.assign(node.$current || {}, node.$next); @@ -2014,29 +1751,18 @@ var TreeStore = /** @class */ (function (_super) { this.stat.remove += this.activeTxnDict[uuid].remove; this.stat.commit++; } - try { - // 唤起等待者 - for (var _c = tslib_1.__values(this.activeTxnDict[uuid].waitList), _d = _c.next(); !_d.done; _d = _c.next()) { - var waiter = _d.value; - waiter.fn(); - } - } - catch (e_17_1) { e_17 = { error: e_17_1 }; } - finally { - try { - if (_d && !_d.done && (_a = _c.return)) _a.call(_c); - } - finally { if (e_17) throw e_17.error; } + // 唤起等待者 + for (const waiter of this.activeTxnDict[uuid].waitList) { + waiter.fn(); } (0, lodash_1.unset)(this.activeTxnDict, uuid); - this.commitCallbacks.forEach(function (callback) { return callback(result); }); - }; - TreeStore.prototype.rollbackSync = function (uuid) { - var e_18, _a; + this.commitCallbacks.forEach(callback => callback(result)); + } + rollbackSync(uuid) { (0, assert_1.assert)(this.activeTxnDict.hasOwnProperty(uuid)); - var node = this.activeTxnDict[uuid].nodeHeader; + let node = this.activeTxnDict[uuid].nodeHeader; while (node) { - var node2 = node.$nextNode; + const node2 = node.$nextNode; if (node.$txnId === uuid) { if (node.$current) { // update/remove @@ -2058,98 +1784,41 @@ var TreeStore = /** @class */ (function (_super) { } node = node2; } - try { - // 唤起等待者 - for (var _b = tslib_1.__values(this.activeTxnDict[uuid].waitList), _c = _b.next(); !_c.done; _c = _b.next()) { - var waiter = _c.value; - waiter.fn(); - } - } - catch (e_18_1) { e_18 = { error: e_18_1 }; } - finally { - try { - if (_c && !_c.done && (_a = _b.return)) _a.call(_b); - } - finally { if (e_18) throw e_18.error; } + // 唤起等待者 + for (const waiter of this.activeTxnDict[uuid].waitList) { + waiter.fn(); } (0, lodash_1.unset)(this.activeTxnDict, uuid); - }; - TreeStore.prototype.beginAsync = function () { - return tslib_1.__awaiter(this, void 0, void 0, function () { - return tslib_1.__generator(this, function (_a) { - return [2 /*return*/, this.beginSync()]; - }); - }); - }; - TreeStore.prototype.commitAsync = function (uuid) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - return tslib_1.__generator(this, function (_a) { - return [2 /*return*/, this.commitSync(uuid)]; - }); - }); - }; - TreeStore.prototype.rollbackAsync = function (uuid) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - return tslib_1.__generator(this, function (_a) { - return [2 /*return*/, this.rollbackSync(uuid)]; - }); - }); - }; + } + async beginAsync() { + return this.beginSync(); + } + async commitAsync(uuid) { + return this.commitSync(uuid); + } + async rollbackAsync(uuid) { + return this.rollbackSync(uuid); + } // 将输入的OpRecord同步到数据中 - TreeStore.prototype.sync = function (opRecords, context, option) { - var e_19, _a, e_20, _b; - var option2 = Object.assign({}, option, { + sync(opRecords, context, option) { + const option2 = Object.assign({}, option, { dontCollect: true, dontCreateOper: true, }); - var result = {}; - try { - for (var opRecords_1 = tslib_1.__values(opRecords), opRecords_1_1 = opRecords_1.next(); !opRecords_1_1.done; opRecords_1_1 = opRecords_1.next()) { - var record = opRecords_1_1.value; - switch (record.a) { - case 'c': { - var e = record.e, d = record.d; - if (d instanceof Array) { - try { - for (var d_1 = (e_20 = void 0, tslib_1.__values(d)), d_1_1 = d_1.next(); !d_1_1.done; d_1_1 = d_1.next()) { - var dd = d_1_1.value; - if (this.store[e] && this.store[e][dd.id]) { - this.updateAbjointRow(e, { - id: 'dummy', - action: 'update', - data: dd, - filter: { - id: dd.id, - }, - }, context, option2); - this.addToOperationResult(result, e, 'update'); - } - else { - this.updateAbjointRow(e, { - id: 'dummy', - action: 'create', - data: dd, - }, context, option2); - this.addToOperationResult(result, e, 'create'); - } - } - } - catch (e_20_1) { e_20 = { error: e_20_1 }; } - finally { - try { - if (d_1_1 && !d_1_1.done && (_b = d_1.return)) _b.call(d_1); - } - finally { if (e_20) throw e_20.error; } - } - } - else { - if (this.store[e] && this.store[e][d.id]) { + const result = {}; + for (const record of opRecords) { + switch (record.a) { + case 'c': { + const { e, d } = record; + if (d instanceof Array) { + for (const dd of d) { + if (this.store[e] && this.store[e][dd.id]) { this.updateAbjointRow(e, { id: 'dummy', action: 'update', - data: d, + data: dd, filter: { - id: d.id, + id: dd.id, }, }, context, option2); this.addToOperationResult(result, e, 'update'); @@ -2158,77 +1827,90 @@ var TreeStore = /** @class */ (function (_super) { this.updateAbjointRow(e, { id: 'dummy', action: 'create', - data: d, + data: dd, }, context, option2); this.addToOperationResult(result, e, 'create'); } } - break; } - case 'u': { - var _c = record, e = _c.e, d = _c.d, f = _c.f; - this.updateAbjointRow(e, { - id: 'dummy', - action: 'update', - data: d, - filter: f, - }, context, option2); - this.addToOperationResult(result, e, 'update'); - break; + else { + if (this.store[e] && this.store[e][d.id]) { + this.updateAbjointRow(e, { + id: 'dummy', + action: 'update', + data: d, + filter: { + id: d.id, + }, + }, context, option2); + this.addToOperationResult(result, e, 'update'); + } + else { + this.updateAbjointRow(e, { + id: 'dummy', + action: 'create', + data: d, + }, context, option2); + this.addToOperationResult(result, e, 'create'); + } } - case 'r': { - var _d = record, e = _d.e, f = _d.f; - this.updateAbjointRow(e, { - id: 'dummy', - action: 'remove', - data: {}, - filter: f, - }, context, option2); - this.addToOperationResult(result, e, 'remove'); - break; - } - case 's': { - var d = record.d; - for (var entity in d) { - for (var id in d[entity]) { - if (this.store[entity] && this.store[entity][id]) { - this.updateAbjointRow(entity, { - id: 'dummy', - action: 'update', - data: d[entity][id], - filter: { - id: id, - }, - }, context, option2); - this.addToOperationResult(result, entity, 'update'); - } - else { - this.updateAbjointRow(entity, { - id: 'dummy', - action: 'create', - data: d[entity][id], - }, context, option2); - this.addToOperationResult(result, entity, 'create'); - } + break; + } + case 'u': { + const { e, d, f } = record; + this.updateAbjointRow(e, { + id: 'dummy', + action: 'update', + data: d, + filter: f, + }, context, option2); + this.addToOperationResult(result, e, 'update'); + break; + } + case 'r': { + const { e, f } = record; + this.updateAbjointRow(e, { + id: 'dummy', + action: 'remove', + data: {}, + filter: f, + }, context, option2); + this.addToOperationResult(result, e, 'remove'); + break; + } + case 's': { + const { d } = record; + for (const entity in d) { + for (const id in d[entity]) { + if (this.store[entity] && this.store[entity][id]) { + this.updateAbjointRow(entity, { + id: 'dummy', + action: 'update', + data: d[entity][id], + filter: { + id, + }, + }, context, option2); + this.addToOperationResult(result, entity, 'update'); + } + else { + this.updateAbjointRow(entity, { + id: 'dummy', + action: 'create', + data: d[entity][id], + }, context, option2); + this.addToOperationResult(result, entity, 'create'); } } - break; - } - default: { - (0, assert_1.assert)(false); } + break; + } + default: { + (0, assert_1.assert)(false); } } } - catch (e_19_1) { e_19 = { error: e_19_1 }; } - finally { - try { - if (opRecords_1_1 && !opRecords_1_1.done && (_a = opRecords_1.return)) _a.call(opRecords_1); - } - finally { if (e_19) throw e_19.error; } - } - this.commitCallbacks.forEach(function (callback) { return callback(result); }); - }; - return TreeStore; -}(CascadeStore_1.CascadeStore)); + this.commitCallbacks.forEach(callback => callback(result)); + } +} exports.default = TreeStore; diff --git a/lib/types/type.d.ts b/lib/types/type.d.ts index ac19783..bf2992a 100644 --- a/lib/types/type.d.ts +++ b/lib/types/type.d.ts @@ -1,6 +1,6 @@ import { NodeId } from "oak-domain/lib/types/Demand"; import { EntityShape } from "oak-domain/lib/types/Entity"; -export declare type RowNode = { +export type RowNode = { $txnId?: string; $next?: Partial ExprResolveFn | any; +export type ExprResolveFn = (nodeDict: NodeDict) => ExprResolveFn | any; diff --git a/package.json b/package.json index a54c99d..4cae7a2 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@types/fs-extra": "^9.0.13", "@types/lodash": "^4.14.168", "@types/mocha": "^8.2.0", - "@types/node": "^14.18.16", + "@types/node": "^20.6.0", "@types/react": "^17.0.2", "@types/uuid": "^8.3.0", "assert": "^2.0.0", @@ -40,6 +40,6 @@ "mocha": "^8.2.1", "ts-node": "~10.9.1", "tslib": "^2.4.0", - "typescript": "~4.7.4" + "typescript": "^5.2.2" } } diff --git a/tsconfig.es.json b/tsconfig.es.json index 2e0eeac..95d809d 100644 --- a/tsconfig.es.json +++ b/tsconfig.es.json @@ -28,8 +28,9 @@ "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ + // "strict": true, /* Enable all strict type-checking options. */ "importHelpers": true, + "skipLibCheck": true, // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ diff --git a/tsconfig.json b/tsconfig.json index 395e6f8..6fbdee0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "jsx": "preserve", /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [ "dom", @@ -28,7 +28,7 @@ "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ + // "strict": true, /* Enable all strict type-checking options. */ "importHelpers": true, // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ @@ -51,6 +51,7 @@ // "types": [], /* Type declaration files to be included in compilation. */ "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + "skipLibCheck": true, // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ /* Source Map Options */