打包优化
This commit is contained in:
parent
5b13c09091
commit
fd0b083f47
121
lib/store.js
121
lib/store.js
|
|
@ -1,10 +1,7 @@
|
|||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const lodash_1 = require("lodash");
|
||||
const assert_1 = __importDefault(require("assert"));
|
||||
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
||||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||||
const Demand_1 = require("oak-domain/lib/types/Demand");
|
||||
const CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
|
||||
const OakError_1 = require("oak-domain/lib/OakError");
|
||||
|
|
@ -68,7 +65,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return null;
|
||||
}
|
||||
else {
|
||||
return (0, lodash_1.assign)({}, data, node.$next);
|
||||
return Object.assign({}, data, node.$next);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
|
@ -110,7 +107,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
};
|
||||
}
|
||||
default: {
|
||||
(0, assert_1.default)(false, `${attr}算子暂不支持`);
|
||||
(0, assert_1.assert)(false, `${attr}算子暂不支持`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -196,7 +193,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return row[expression['#attr']];
|
||||
}
|
||||
else {
|
||||
(0, assert_1.default)(expression.hasOwnProperty('#refId'));
|
||||
(0, assert_1.assert)(expression.hasOwnProperty('#refId'));
|
||||
const { ['#refId']: refId, ['#refAttr']: refAttr } = expression;
|
||||
if (nodeDict.hasOwnProperty(refId)) {
|
||||
return nodeDict[refId][refAttr];
|
||||
|
|
@ -308,7 +305,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
case '$exists': {
|
||||
const exists = filter[op];
|
||||
(0, assert_1.default)(typeof exists === 'boolean');
|
||||
(0, assert_1.assert)(typeof exists === 'boolean');
|
||||
fns.push(async (row) => {
|
||||
if (exists) {
|
||||
return [null].includes(row[attr]) || obscurePassLocal(row);
|
||||
|
|
@ -321,7 +318,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
case '$in': {
|
||||
const inData = filter[op];
|
||||
(0, assert_1.default)(typeof inData === 'object');
|
||||
(0, assert_1.assert)(typeof inData === 'object');
|
||||
if (inData instanceof Array) {
|
||||
fns.push(async (row) => inData.includes(row[attr]) || obscurePassLocal(row));
|
||||
}
|
||||
|
|
@ -343,7 +340,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
catch (err) {
|
||||
if (err instanceof OakError_1.OakError && err.$$code === RowStore_1.RowStore.$$CODES.expressionUnresolved[0]) {
|
||||
fns.push(async (row, nodeDict) => {
|
||||
(0, lodash_1.assign)(params, {
|
||||
Object.assign(params, {
|
||||
nodeDict,
|
||||
});
|
||||
const legalSets = (await this.selectAbjointRow(inData.entity, inData, context, params)).map((ele) => {
|
||||
|
|
@ -365,7 +362,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
case '$nin': {
|
||||
const inData = filter[op];
|
||||
(0, assert_1.default)(typeof inData === 'object');
|
||||
(0, assert_1.assert)(typeof inData === 'object');
|
||||
if (inData instanceof Array) {
|
||||
fns.push(async (row) => !inData.includes(row[attr]) || obscurePassLocal(row));
|
||||
}
|
||||
|
|
@ -383,7 +380,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
catch (err) {
|
||||
if (err instanceof OakError_1.OakError && err.$$code === RowStore_1.RowStore.$$CODES.expressionUnresolved[0]) {
|
||||
fns.push(async (row, nodeDict) => {
|
||||
(0, lodash_1.assign)(params, {
|
||||
Object.assign(params, {
|
||||
nodeDict,
|
||||
});
|
||||
const legalSets = (await this.selectAbjointRow(inData.entity, inData, context, params)).map((ele) => {
|
||||
|
|
@ -475,7 +472,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
});
|
||||
}
|
||||
else {
|
||||
(0, assert_1.default)(typeof relation === 'string');
|
||||
(0, assert_1.assert)(typeof relation === 'string');
|
||||
// 只能是基于普通属性的外键
|
||||
const fn = await this.translateFilter(relation, filter[attr], context, params);
|
||||
fns.push(async (node, nodeDict, exprResolveFns) => {
|
||||
|
|
@ -500,8 +497,8 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
return async (node, nodeDict, exprResolveFns) => {
|
||||
if (nodeId) {
|
||||
(0, assert_1.default)(!nodeDict.hasOwnProperty(nodeId), new OakError_1.OakError(RowStore_1.RowStore.$$LEVEL, RowStore_1.RowStore.$$CODES.nodeIdRepeated, `Filter中的nodeId「${nodeId}」出现了多次`));
|
||||
(0, lodash_1.assign)(nodeDict, {
|
||||
(0, assert_1.assert)(!nodeDict.hasOwnProperty(nodeId), new OakError_1.OakError(RowStore_1.RowStore.$$LEVEL, RowStore_1.RowStore.$$CODES.nodeIdRepeated, `Filter中的nodeId「${nodeId}」出现了多次`));
|
||||
Object.assign(nodeDict, {
|
||||
[nodeId]: this.constructRow(node, context),
|
||||
});
|
||||
}
|
||||
|
|
@ -521,7 +518,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
const compare = (row1, row2, entity2, sortAttr, direction) => {
|
||||
const row11 = row1;
|
||||
const row22 = row2;
|
||||
(0, assert_1.default)(Object.keys(sortAttr).length === 1);
|
||||
(0, assert_1.assert)(Object.keys(sortAttr).length === 1);
|
||||
const attr = Object.keys(sortAttr)[0];
|
||||
const relation = (0, relation_1.judgeRelation)(this.storageSchema, entity2, attr);
|
||||
if (relation === 1 || relation === 0) {
|
||||
|
|
@ -531,7 +528,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
else {
|
||||
// 改变策略,让所有需要获得的值在projection上取得
|
||||
(0, assert_1.default)(typeof sortAttr[attr] === 'string' && sortAttr[attr].startsWith('$expr'));
|
||||
(0, assert_1.assert)(typeof sortAttr[attr] === 'string' && sortAttr[attr].startsWith('$expr'));
|
||||
return r[sortAttr[attr]];
|
||||
}
|
||||
};
|
||||
|
|
@ -574,8 +571,8 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
else {
|
||||
if (relation === 2) {
|
||||
(0, assert_1.default)(row11['entity'] === row22['entity']);
|
||||
(0, assert_1.default)(row11.entity === attr);
|
||||
(0, assert_1.assert)(row11['entity'] === row22['entity']);
|
||||
(0, assert_1.assert)(row11.entity === attr);
|
||||
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);
|
||||
|
|
@ -583,7 +580,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return compare(row111, row222, row11['entity'], sortAttr[attr], direction);
|
||||
}
|
||||
else {
|
||||
(0, assert_1.default)(typeof relation === 'string');
|
||||
(0, assert_1.assert)(typeof relation === 'string');
|
||||
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);
|
||||
|
|
@ -611,14 +608,14 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
*/
|
||||
addToResultSelections(entity, rows, context) {
|
||||
const { opRecords } = context;
|
||||
let lastOperation = (0, lodash_1.last)(opRecords);
|
||||
let lastOperation = opRecords[opRecords.length - 1];
|
||||
if (lastOperation && lastOperation.a === 's') {
|
||||
const entityBranch = lastOperation.d[entity];
|
||||
if (entityBranch) {
|
||||
rows.forEach((row) => {
|
||||
const { id } = row;
|
||||
if (!entityBranch[id]) {
|
||||
(0, lodash_1.assign)(entityBranch, {
|
||||
Object.assign(entityBranch, {
|
||||
[id]: (0, lodash_1.cloneDeep)(row),
|
||||
});
|
||||
}
|
||||
|
|
@ -636,11 +633,11 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
const entityBranch = {};
|
||||
rows.forEach((row) => {
|
||||
const { id } = row;
|
||||
(0, lodash_1.assign)(entityBranch, {
|
||||
Object.assign(entityBranch, {
|
||||
[id]: (0, lodash_1.cloneDeep)(row),
|
||||
});
|
||||
});
|
||||
(0, lodash_1.assign)(lastOperation.d, {
|
||||
Object.assign(lastOperation.d, {
|
||||
[entity]: entityBranch,
|
||||
});
|
||||
}
|
||||
|
|
@ -653,7 +650,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
for (const n of entityNodes) {
|
||||
const nodeDict2 = {};
|
||||
if (nodeDict) {
|
||||
(0, lodash_1.assign)(nodeDict2, nodeDict);
|
||||
Object.assign(nodeDict2, nodeDict);
|
||||
}
|
||||
const exprResolveFns = [];
|
||||
if (!filterFn || await (await filterFn)(n, nodeDict2, exprResolveFns)) {
|
||||
|
|
@ -692,7 +689,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
/* if (row) {
|
||||
throw new OakError(RowStore.$$LEVEL, RowStore.$$CODES.primaryKeyConfilict);
|
||||
} */
|
||||
const data2 = (0, lodash_1.assign)(data, {
|
||||
const data2 = Object.assign(data, {
|
||||
$$createAt$$: data.$$createAt$$ || now,
|
||||
$$updateAt$$: data.$$updateAt$$ || now,
|
||||
});
|
||||
|
|
@ -717,7 +714,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return 1;
|
||||
}
|
||||
default: {
|
||||
const selection = (0, lodash_1.assign)({}, operation, {
|
||||
const selection = Object.assign({}, operation, {
|
||||
data: {
|
||||
id: 1,
|
||||
},
|
||||
|
|
@ -728,12 +725,12 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
ids.forEach((id) => {
|
||||
let alreadyDirtyNode = false;
|
||||
const node = (this.store[entity])[id];
|
||||
(0, assert_1.default)(node);
|
||||
(0, assert_1.assert)(node);
|
||||
if (!node.$txnId) {
|
||||
node.$txnId = context.getCurrentTxnId();
|
||||
}
|
||||
else {
|
||||
(0, assert_1.default)(node.$txnId === context.getCurrentTxnId());
|
||||
(0, assert_1.assert)(node.$txnId === context.getCurrentTxnId());
|
||||
alreadyDirtyNode = true;
|
||||
}
|
||||
if (action === 'remove') {
|
||||
|
|
@ -753,10 +750,10 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
else {
|
||||
const row = node && this.constructRow(node, context) || {};
|
||||
const data2 = (0, lodash_1.assign)(data, {
|
||||
const data2 = Object.assign(data, {
|
||||
$$updateAt$$: data.$$updateAt$$ || now,
|
||||
});
|
||||
const data3 = (0, lodash_1.assign)(row, data2);
|
||||
const data3 = Object.assign(row, data2);
|
||||
node.$next = data3;
|
||||
if (!alreadyDirtyNode) {
|
||||
// 如果已经更新过的结点就不能再加了,会形成循环
|
||||
|
|
@ -784,7 +781,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
const result = await this.formResult(entity, rows, operation as any, context, params);
|
||||
|
||||
const operationResult: OperationResult<ED> = {};
|
||||
assign(operationResult, {
|
||||
Object.assign(operationResult, {
|
||||
[entity]: {
|
||||
select: result.length,
|
||||
}
|
||||
|
|
@ -798,7 +795,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
}
|
||||
async operate(entity, operation, context, params) {
|
||||
(0, assert_1.default)(context.getCurrentTxnId());
|
||||
(0, assert_1.assert)(context.getCurrentTxnId());
|
||||
return await this.doOperation(entity, operation, context, params);
|
||||
}
|
||||
async formProjection(entity, row, data, result, nodeDict, context) {
|
||||
|
|
@ -810,20 +807,20 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
const ExprNodeTranslator = this.translateExpression(entity, data2[attr], context, {});
|
||||
const exprResult = await ExprNodeTranslator(row, nodeDict);
|
||||
if (typeof exprResult === 'function') {
|
||||
(0, lodash_1.assign)(laterExprDict, {
|
||||
Object.assign(laterExprDict, {
|
||||
[attr]: exprResult,
|
||||
});
|
||||
}
|
||||
else {
|
||||
(0, lodash_1.assign)(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: exprResult,
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (attr === '#id') {
|
||||
const nodeId = data[attr];
|
||||
(0, assert_1.default)(!nodeDict.hasOwnProperty(nodeId), new OakError_1.OakError(RowStore_1.RowStore.$$LEVEL, RowStore_1.RowStore.$$CODES.nodeIdRepeated, `Filter中的nodeId「${nodeId}」出现了多次`));
|
||||
(0, lodash_1.assign)(nodeDict, {
|
||||
(0, assert_1.assert)(!nodeDict.hasOwnProperty(nodeId), new OakError_1.OakError(RowStore_1.RowStore.$$LEVEL, RowStore_1.RowStore.$$CODES.nodeIdRepeated, `Filter中的nodeId「${nodeId}」出现了多次`));
|
||||
Object.assign(nodeDict, {
|
||||
[nodeId]: row,
|
||||
});
|
||||
}
|
||||
|
|
@ -832,7 +829,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
if (!attr.startsWith(Demand_1.EXPRESSION_PREFIX) && attr !== '#id') {
|
||||
const relation = (0, relation_1.judgeRelation)(this.storageSchema, entity, attr);
|
||||
if (relation === 1) {
|
||||
(0, lodash_1.assign)(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: row2[attr],
|
||||
});
|
||||
}
|
||||
|
|
@ -841,7 +838,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
const result2 = {};
|
||||
const { entity, entityId } = row2;
|
||||
await this.formProjection(attr, row2[attr], data2[attr], result2, nodeDict, context);
|
||||
(0, lodash_1.assign)(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: result2,
|
||||
entity,
|
||||
entityId,
|
||||
|
|
@ -852,16 +849,16 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
if (row2[attr]) {
|
||||
const result2 = {};
|
||||
await this.formProjection(relation, row2[attr], data2[attr], result2, nodeDict, context);
|
||||
(0, lodash_1.assign)(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: result2,
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
(0, assert_1.default)(relation instanceof Array);
|
||||
(0, assert_1.assert)(relation instanceof Array);
|
||||
if (row2[attr] instanceof Array) {
|
||||
const result2 = await this.formResult(relation[0], row2[attr], data2[attr], context, nodeDict);
|
||||
(0, lodash_1.assign)(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: result2,
|
||||
});
|
||||
}
|
||||
|
|
@ -871,8 +868,8 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
for (const attr in laterExprDict) {
|
||||
const exprResult = laterExprDict[attr](nodeDict);
|
||||
// projection是不应出现计算不出来的情况
|
||||
(0, assert_1.default)(typeof exprResult !== 'function', new OakError_1.OakError(RowStore_1.RowStore.$$LEVEL, RowStore_1.RowStore.$$CODES.expressionUnresolved, 'data中的expr无法计算,请检查命名与引用的一致性'));
|
||||
(0, lodash_1.assign)(result, {
|
||||
(0, assert_1.assert)(typeof exprResult !== 'function', new OakError_1.OakError(RowStore_1.RowStore.$$LEVEL, RowStore_1.RowStore.$$CODES.expressionUnresolved, 'data中的expr无法计算,请检查命名与引用的一致性'));
|
||||
Object.assign(result, {
|
||||
[attr]: exprResult,
|
||||
});
|
||||
}
|
||||
|
|
@ -887,7 +884,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return exprName;
|
||||
}
|
||||
}
|
||||
(0, assert_1.default)(false, '找不到可用的expr命名');
|
||||
(0, assert_1.assert)(false, '找不到可用的expr命名');
|
||||
};
|
||||
const sortToProjection = (entity2, proj, sort) => {
|
||||
Object.keys(sort).forEach((attr) => {
|
||||
|
|
@ -912,7 +909,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
sortToProjection(entity3, proj[attr], sort[attr]);
|
||||
}
|
||||
else if (rel === 1) {
|
||||
(0, lodash_1.assign)(proj, {
|
||||
Object.assign(proj, {
|
||||
[attr]: 1,
|
||||
});
|
||||
}
|
||||
|
|
@ -929,7 +926,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
const result = {};
|
||||
const nodeDict2 = {};
|
||||
if (nodeDict) {
|
||||
(0, lodash_1.assign)(nodeDict2, nodeDict);
|
||||
Object.assign(nodeDict2, nodeDict);
|
||||
}
|
||||
await this.formProjection(entity, row, data, result, nodeDict2, context);
|
||||
rows2.push(result);
|
||||
|
|
@ -948,7 +945,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
}
|
||||
async select(entity, selection, context, params) {
|
||||
(0, assert_1.default)(context.getCurrentTxnId());
|
||||
(0, assert_1.assert)(context.getCurrentTxnId());
|
||||
const result = await this.cascadeSelect(entity, selection, context, params);
|
||||
return {
|
||||
result,
|
||||
|
|
@ -956,7 +953,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
};
|
||||
}
|
||||
async count(entity, selection, context, params) {
|
||||
const { result } = await this.select(entity, (0, lodash_1.assign)({}, selection, {
|
||||
const { result } = await this.select(entity, Object.assign({}, selection, {
|
||||
data: {
|
||||
id: 1,
|
||||
}
|
||||
|
|
@ -965,7 +962,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
addToTxnNode(node, context, action) {
|
||||
const txnNode = this.activeTxnDict[context.getCurrentTxnId()];
|
||||
(0, assert_1.default)(txnNode);
|
||||
(0, assert_1.assert)(txnNode);
|
||||
if (!node.$nextNode) {
|
||||
// 如果nextNode有值,说明这个结点已经在链表中了
|
||||
if (txnNode.nodeHeader) {
|
||||
|
|
@ -983,8 +980,8 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
async begin() {
|
||||
const uuid = `${Math.random()}`;
|
||||
(0, assert_1.default)(!this.activeTxnDict.hasOwnProperty(uuid));
|
||||
(0, lodash_1.assign)(this.activeTxnDict, {
|
||||
(0, assert_1.assert)(!this.activeTxnDict.hasOwnProperty(uuid));
|
||||
Object.assign(this.activeTxnDict, {
|
||||
[uuid]: {
|
||||
create: 0,
|
||||
update: 0,
|
||||
|
|
@ -994,14 +991,14 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return uuid;
|
||||
}
|
||||
async commit(uuid) {
|
||||
(0, assert_1.default)(this.activeTxnDict.hasOwnProperty(uuid), uuid);
|
||||
(0, assert_1.assert)(this.activeTxnDict.hasOwnProperty(uuid), uuid);
|
||||
let node = this.activeTxnDict[uuid].nodeHeader;
|
||||
while (node) {
|
||||
const node2 = node.$nextNode;
|
||||
if (node.$txnId === uuid) {
|
||||
if (node.$next) {
|
||||
// create/update
|
||||
node.$current = (0, lodash_1.assign)(node.$current, node.$next);
|
||||
node.$current = Object.assign(node.$current || {}, node.$next);
|
||||
(0, lodash_1.unset)(node, '$txnId');
|
||||
(0, lodash_1.unset)(node, '$next');
|
||||
(0, lodash_1.unset)(node, '$path');
|
||||
|
|
@ -1009,14 +1006,14 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
else {
|
||||
// remove
|
||||
(0, assert_1.default)(node.$path);
|
||||
(0, assert_1.assert)(node.$path);
|
||||
(0, lodash_1.unset)(this.store, node.$path);
|
||||
(0, lodash_1.unset)(node, '$txnId');
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 同一行被同一事务更新多次
|
||||
(0, assert_1.default)(node.$txnId === undefined);
|
||||
(0, assert_1.assert)(node.$txnId === undefined);
|
||||
}
|
||||
node = node2;
|
||||
}
|
||||
|
|
@ -1029,7 +1026,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
(0, lodash_1.unset)(this.activeTxnDict, uuid);
|
||||
}
|
||||
async rollback(uuid) {
|
||||
(0, assert_1.default)(this.activeTxnDict.hasOwnProperty(uuid));
|
||||
(0, assert_1.assert)(this.activeTxnDict.hasOwnProperty(uuid));
|
||||
let node = this.activeTxnDict[uuid].nodeHeader;
|
||||
while (node) {
|
||||
const node2 = node.$nextNode;
|
||||
|
|
@ -1043,14 +1040,14 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
else {
|
||||
// create
|
||||
(0, assert_1.default)(node.$path);
|
||||
(0, assert_1.assert)(node.$path);
|
||||
(0, lodash_1.unset)(this.store, node.$path);
|
||||
(0, lodash_1.unset)(node, '$txnId');
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 该结点被同一事务反复处理
|
||||
(0, assert_1.default)(node.$txnId === undefined);
|
||||
(0, assert_1.assert)(node.$txnId === undefined);
|
||||
}
|
||||
node = node2;
|
||||
}
|
||||
|
|
@ -1058,7 +1055,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
// 将输入的OpRecord同步到数据中
|
||||
async sync(opRecords, context) {
|
||||
(0, assert_1.default)(context.getCurrentTxnId());
|
||||
(0, assert_1.assert)(context.getCurrentTxnId());
|
||||
for (const record of opRecords) {
|
||||
switch (record.a) {
|
||||
case 'c': {
|
||||
|
|
@ -1108,7 +1105,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
break;
|
||||
}
|
||||
default: {
|
||||
(0, assert_1.default)(false);
|
||||
(0, assert_1.assert)(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
"version": "1.0.0",
|
||||
"description": "oak框架中内存级store的实现",
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21",
|
||||
"luxon": "^2.3.0",
|
||||
"uuid": "^8.3.2",
|
||||
"oak-domain": "file:../oak-domain"
|
||||
|
|
|
|||
58
src/store.ts
58
src/store.ts
|
|
@ -1,5 +1,5 @@
|
|||
import { assign, cloneDeep, get, keys, last, set, unset } from 'lodash';
|
||||
import assert from 'assert';
|
||||
import { cloneDeep, get, set, unset } from 'oak-domain/lib/utils/lodash';
|
||||
import { assert } from 'oak-domain/lib/utils/assert';
|
||||
import { DeduceCreateSingleOperation, DeduceFilter, DeduceSelection, EntityShape, DeduceRemoveOperation, DeduceUpdateOperation, DeduceSorter, DeduceSorterAttr, OperationResult, OperateParams, OpRecord, DeduceCreateOperationData, DeduceUpdateOperationData, UpdateOpResult, RemoveOpResult, SelectOpResult, EntityDict, SelectRowShape, SelectionResult, DeduceSorterItem } from "oak-domain/lib/types/Entity";
|
||||
import { ExpressionKey, EXPRESSION_PREFIX, NodeId, RefAttr } from 'oak-domain/lib/types/Demand';
|
||||
import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
|
||||
|
|
@ -112,7 +112,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
return null;
|
||||
}
|
||||
else {
|
||||
return assign({}, data, node.$next);
|
||||
return Object.assign({}, data, node.$next);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
|
@ -441,7 +441,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
if (err instanceof OakError && err.$$code === RowStore.$$CODES.expressionUnresolved[0]) {
|
||||
fns.push(
|
||||
async (row, nodeDict) => {
|
||||
assign(params, {
|
||||
Object.assign(params, {
|
||||
nodeDict,
|
||||
});
|
||||
const legalSets = (await this.selectAbjointRow(inData.entity, inData, context, params)).map(
|
||||
|
|
@ -490,7 +490,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
if (err instanceof OakError && err.$$code === RowStore.$$CODES.expressionUnresolved[0]) {
|
||||
fns.push(
|
||||
async (row, nodeDict) => {
|
||||
assign(params, {
|
||||
Object.assign(params, {
|
||||
nodeDict,
|
||||
});
|
||||
const legalSets = (await this.selectAbjointRow(inData.entity, inData, context, params)).map(
|
||||
|
|
@ -627,7 +627,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
return async (node, nodeDict, exprResolveFns) => {
|
||||
if (nodeId) {
|
||||
assert(!nodeDict.hasOwnProperty(nodeId), new OakError(RowStore.$$LEVEL, RowStore.$$CODES.nodeIdRepeated, `Filter中的nodeId「${nodeId}」出现了多次`));
|
||||
assign(nodeDict, {
|
||||
Object.assign(nodeDict, {
|
||||
[nodeId]: this.constructRow(node, context),
|
||||
});
|
||||
}
|
||||
|
|
@ -752,7 +752,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
private addToResultSelections<T extends keyof ED>(entity: T, rows: Array<ED[T]['OpSchema']>, context: Cxt) {
|
||||
const { opRecords } = context;
|
||||
|
||||
let lastOperation = last(opRecords);
|
||||
let lastOperation = opRecords[opRecords.length - 1];
|
||||
if (lastOperation && lastOperation.a === 's') {
|
||||
const entityBranch = lastOperation.d[entity];
|
||||
if (entityBranch) {
|
||||
|
|
@ -760,7 +760,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
(row) => {
|
||||
const { id } = row;
|
||||
if (!entityBranch![id!]) {
|
||||
assign(entityBranch!, {
|
||||
Object.assign(entityBranch!, {
|
||||
[id!]: cloneDeep(row),
|
||||
});
|
||||
}
|
||||
|
|
@ -781,12 +781,12 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
rows.forEach(
|
||||
(row) => {
|
||||
const { id } = row;
|
||||
assign(entityBranch!, {
|
||||
Object.assign(entityBranch!, {
|
||||
[id!]: cloneDeep(row),
|
||||
});
|
||||
}
|
||||
);
|
||||
assign(lastOperation.d, {
|
||||
Object.assign(lastOperation.d, {
|
||||
[entity]: entityBranch,
|
||||
});
|
||||
}
|
||||
|
|
@ -807,7 +807,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
for (const n of entityNodes) {
|
||||
const nodeDict2: NodeDict = {};
|
||||
if (nodeDict) {
|
||||
assign(nodeDict2, nodeDict);
|
||||
Object.assign(nodeDict2, nodeDict);
|
||||
}
|
||||
const exprResolveFns: Array<ExprResolveFn> = [];
|
||||
if (!filterFn || await (await filterFn)(n, nodeDict2, exprResolveFns)) {
|
||||
|
|
@ -856,7 +856,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
/* if (row) {
|
||||
throw new OakError(RowStore.$$LEVEL, RowStore.$$CODES.primaryKeyConfilict);
|
||||
} */
|
||||
const data2 = assign(data as DeduceCreateOperationData<ED[T]["Schema"]>, {
|
||||
const data2 = Object.assign(data as DeduceCreateOperationData<ED[T]["Schema"]>, {
|
||||
$$createAt$$: data.$$createAt$$ || now,
|
||||
$$updateAt$$: data.$$updateAt$$ || now,
|
||||
});
|
||||
|
|
@ -881,7 +881,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
return 1;
|
||||
}
|
||||
default: {
|
||||
const selection = assign({}, operation, {
|
||||
const selection = Object.assign({}, operation, {
|
||||
data: {
|
||||
id: 1,
|
||||
},
|
||||
|
|
@ -919,10 +919,10 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
}
|
||||
else {
|
||||
const row = node && this.constructRow(node, context) || {};
|
||||
const data2 = assign(data as DeduceUpdateOperationData<ED[T]['Schema']>, {
|
||||
const data2 = Object.assign(data as DeduceUpdateOperationData<ED[T]['Schema']>, {
|
||||
$$updateAt$$: data.$$updateAt$$ || now,
|
||||
});
|
||||
const data3 = assign(row, data2);
|
||||
const data3 = Object.assign(row, data2);
|
||||
node.$next = data3;
|
||||
if (!alreadyDirtyNode) {
|
||||
// 如果已经更新过的结点就不能再加了,会形成循环
|
||||
|
|
@ -952,7 +952,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
const result = await this.formResult(entity, rows, operation as any, context, params);
|
||||
|
||||
const operationResult: OperationResult<ED> = {};
|
||||
assign(operationResult, {
|
||||
Object.assign(operationResult, {
|
||||
[entity]: {
|
||||
select: result.length,
|
||||
}
|
||||
|
|
@ -989,12 +989,12 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
const ExprNodeTranslator = this.translateExpression(entity, data2[attr], context, {});
|
||||
const exprResult = await ExprNodeTranslator(row, nodeDict);
|
||||
if (typeof exprResult === 'function') {
|
||||
assign(laterExprDict, {
|
||||
Object.assign(laterExprDict, {
|
||||
[attr]: exprResult,
|
||||
});
|
||||
}
|
||||
else {
|
||||
assign(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: exprResult,
|
||||
});
|
||||
}
|
||||
|
|
@ -1002,7 +1002,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
else if (attr === '#id') {
|
||||
const nodeId = data[attr] as NodeId;
|
||||
assert(!nodeDict.hasOwnProperty(nodeId), new OakError(RowStore.$$LEVEL, RowStore.$$CODES.nodeIdRepeated, `Filter中的nodeId「${nodeId}」出现了多次`));
|
||||
assign(nodeDict, {
|
||||
Object.assign(nodeDict, {
|
||||
[nodeId]: row,
|
||||
});
|
||||
}
|
||||
|
|
@ -1012,7 +1012,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
if (!attr.startsWith(EXPRESSION_PREFIX) && attr !== '#id') {
|
||||
const relation = judgeRelation(this.storageSchema, entity, attr);
|
||||
if (relation === 1) {
|
||||
assign(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: row2[attr],
|
||||
});
|
||||
}
|
||||
|
|
@ -1021,7 +1021,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
const result2 = {};
|
||||
const { entity, entityId } = row2;
|
||||
await this.formProjection(attr, row2[attr], data2[attr], result2, nodeDict, context);
|
||||
assign(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: result2,
|
||||
entity,
|
||||
entityId,
|
||||
|
|
@ -1032,7 +1032,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
if (row2[attr]) {
|
||||
const result2 = {};
|
||||
await this.formProjection(relation, row2[attr], data2[attr], result2, nodeDict, context);
|
||||
assign(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: result2,
|
||||
});
|
||||
}
|
||||
|
|
@ -1042,7 +1042,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
if (row2[attr] instanceof Array) {
|
||||
const result2 = await this.formResult(relation[0], row2[attr], data2[attr], context, nodeDict);
|
||||
|
||||
assign(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: result2,
|
||||
});
|
||||
}
|
||||
|
|
@ -1054,7 +1054,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
const exprResult = laterExprDict[attr as ExpressionKey]!(nodeDict);
|
||||
// projection是不应出现计算不出来的情况
|
||||
assert(typeof exprResult !== 'function', new OakError(RowStore.$$LEVEL, RowStore.$$CODES.expressionUnresolved, 'data中的expr无法计算,请检查命名与引用的一致性'));
|
||||
assign(result, {
|
||||
Object.assign(result, {
|
||||
[attr]: exprResult,
|
||||
});
|
||||
}
|
||||
|
|
@ -1103,7 +1103,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
sortToProjection(entity3, proj[attr], sort[attr]);
|
||||
}
|
||||
else if (rel === 1) {
|
||||
assign(proj, {
|
||||
Object.assign(proj, {
|
||||
[attr]: 1,
|
||||
})
|
||||
}
|
||||
|
|
@ -1124,7 +1124,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
const result = {};
|
||||
const nodeDict2: NodeDict = {};
|
||||
if (nodeDict) {
|
||||
assign(nodeDict2, nodeDict);
|
||||
Object.assign(nodeDict2, nodeDict);
|
||||
}
|
||||
await this.formProjection(entity, row, data, result, nodeDict2, context);
|
||||
rows2.push(result as SelectRowShape<ED[T]['Schema'], S['data']>);
|
||||
|
|
@ -1159,7 +1159,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
}
|
||||
|
||||
async count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter'>, context: Cxt, params?: Object): Promise<number> {
|
||||
const { result } = await this.select(entity, assign({}, selection, {
|
||||
const { result } = await this.select(entity, Object.assign({}, selection, {
|
||||
data: {
|
||||
id: 1,
|
||||
}
|
||||
|
|
@ -1191,7 +1191,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
async begin() {
|
||||
const uuid = `${Math.random()}`;
|
||||
assert(!this.activeTxnDict.hasOwnProperty(uuid));
|
||||
assign(this.activeTxnDict, {
|
||||
Object.assign(this.activeTxnDict, {
|
||||
[uuid]: {
|
||||
create: 0,
|
||||
update: 0,
|
||||
|
|
@ -1209,7 +1209,7 @@ export default class TreeStore<ED extends EntityDict, Cxt extends Context<ED>> e
|
|||
if (node.$txnId === uuid) {
|
||||
if (node.$next) {
|
||||
// create/update
|
||||
node.$current = assign(node.$current, node.$next);
|
||||
node.$current = Object.assign(node.$current || {}, node.$next) as EntityShape;
|
||||
unset(node, '$txnId');
|
||||
unset(node, '$next');
|
||||
unset(node, '$path');
|
||||
|
|
|
|||
Loading…
Reference in New Issue