正确的处理了deleteAt

This commit is contained in:
Xu Chang 2023-01-04 17:59:13 +08:00
parent 4ab09a9bde
commit d6d2344ccb
2 changed files with 75 additions and 57 deletions

View File

@ -130,15 +130,18 @@ var TreeStore = /** @class */ (function (_super) {
}
return result;
};
TreeStore.prototype.constructRow = function (node, context) {
TreeStore.prototype.constructRow = function (node, context, option) {
var _a;
var data = (0, lodash_1.cloneDeep)(node.$current);
if (context.getCurrentTxnId() && node.$txnId === context.getCurrentTxnId()) {
if (!node.$next) {
// 如果是删除,返回带$$deleteAt$$的行
return Object.assign({}, data, (_a = {},
_a[Entity_1.DeleteAtAttribute] = 1,
_a));
// 如果要求返回delete数据返回带$$deleteAt$$的行
if (option === null || option === void 0 ? void 0 : option.includedDeleted) {
return Object.assign({}, data, (_a = {},
_a[Entity_1.DeleteAtAttribute] = 1,
_a));
}
return null;
}
else {
return Object.assign({}, data, node.$next);
@ -333,7 +336,7 @@ var TreeStore = /** @class */ (function (_super) {
var $search = filter.$search;
return function (node) {
var e_4, _a;
var row = _this.constructRow(node, context);
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;
@ -362,14 +365,14 @@ var TreeStore = /** @class */ (function (_super) {
}
if (typeof filter !== 'object') {
return function (node) {
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
return row ? row[attr] === filter || obscurePassLocal(row) : false;
};
}
else if (((_a = this.getSchema()[entity].attributes[attr]) === null || _a === void 0 ? void 0 : _a.type) === 'object') {
// 如果查询的目标就是object则转化成object的比较
return function (node) {
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
return row ? JSON.stringify(row[attr]) === JSON.stringify(filter) || obscurePassLocal(row) : false;
};
}
@ -527,7 +530,7 @@ var TreeStore = /** @class */ (function (_super) {
}
return function (node, nodeDict, exprResolveFns) {
var e_5, _a;
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
if (!row) {
return false;
}
@ -563,7 +566,7 @@ var TreeStore = /** @class */ (function (_super) {
else if (attr.toLowerCase().startsWith(Demand_1.EXPRESSION_PREFIX)) {
var fn_2 = this_2.translateExpression(entity, filter[attr], context, option);
fns.push(function (node, nodeDict, exprResolveFns) {
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
if (!row) {
return false;
}
@ -588,7 +591,7 @@ var TreeStore = /** @class */ (function (_super) {
// 基于entity/entityId的指针
var fn_3 = this_2.translateFilter(attr, filter[attr], context, option);
fns.push(function (node, nodeDict, exprResolveFns) {
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
if (obscurePass(row, 'entity', option) || obscurePass(row, 'entityId', option)) {
return true;
}
@ -610,7 +613,7 @@ var TreeStore = /** @class */ (function (_super) {
// 只能是基于普通属性的外键
var fn_4 = this_2.translateFilter(relation_2, filter[attr], context, option);
fns.push(function (node, nodeDict, exprResolveFns) {
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
if (obscurePass(row, "".concat(attr, "Id"), option)) {
return true;
}
@ -638,10 +641,10 @@ var TreeStore = /** @class */ (function (_super) {
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),
_a[nodeId] = _this.constructRow(node, context, option),
_a));
}
var row = _this.constructRow(node, context);
var row = _this.constructRow(node, context, option);
if (!row) {
return false;
}
@ -663,7 +666,7 @@ var TreeStore = /** @class */ (function (_super) {
return true;
};
};
TreeStore.prototype.translateSorter = function (entity, sorter, context) {
TreeStore.prototype.translateSorter = function (entity, sorter, context, option) {
var _this = this;
var compare = function (row1, row2, entity2, sortAttr, direction) {
var row11 = row1;
@ -725,16 +728,16 @@ var TreeStore = /** @class */ (function (_super) {
(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);
var row222 = node2 && _this.constructRow(node2, context);
var row111 = node1 && _this.constructRow(node1, context, option);
var 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);
var row222 = node2 && _this.constructRow(node2, context);
var row111 = node1 && _this.constructRow(node1, context, option);
var row222 = node2 && _this.constructRow(node2, context, option);
return compare(row111, row222, relation, sortAttr[attr], direction);
}
}
@ -820,8 +823,8 @@ var TreeStore = /** @class */ (function (_super) {
}
finally { if (e_8) throw e_8.error; }
}
var rows = nodes.map(function (node) { return _this.constructRow(node, context); });
var rows2 = this.formResult(entity, rows, selection, context);
var rows = nodes.map(function (node) { return _this.constructRow(node, context, option); });
var rows2 = this.formResult(entity, rows, selection, context, option);
return rows2;
};
TreeStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
@ -838,7 +841,7 @@ var TreeStore = /** @class */ (function (_super) {
} */
if (this.store[entity] && (this.store[entity])[id]) {
var node = this.store[entity] && (this.store[entity])[id];
throw new Exception_1.OakCongruentRowExists(entity, this.constructRow(node, context));
throw new Exception_1.OakCongruentRowExists(entity, this.constructRow(node, context, option));
}
if (!data.$$seq$$) {
// tree-store随意生成即可
@ -1006,7 +1009,7 @@ var TreeStore = /** @class */ (function (_super) {
_d));
}
};
TreeStore.prototype.formResult = function (entity, rows, selection, context) {
TreeStore.prototype.formResult = function (entity, rows, selection, context, option) {
var e_11, _a, _b, _c;
var _this = this;
var data = selection.data, sorter = selection.sorter, indexFrom = selection.indexFrom, count = selection.count;
@ -1108,7 +1111,7 @@ var TreeStore = /** @class */ (function (_super) {
}
// 再计算sorter
if (sorter) {
var sorterFn = this.translateSorter(entity, sorter, context);
var sorterFn = this.translateSorter(entity, sorter, context, option);
rows2.sort(sorterFn);
}
// 最后用indexFrom和count来截断
@ -1254,7 +1257,12 @@ var TreeStore = /** @class */ (function (_super) {
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]) {
result[op] = null;
if (op.startsWith('$count')) {
result[op] = 0;
}
else {
result[op] = null;
}
}
else if (op.startsWith('$avg')) {
result[op] = result[op].total / result[op].count;

View File

@ -177,14 +177,17 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
};
}
private constructRow<Cxt extends Context>(node: RowNode, context: Cxt) {
private constructRow<Cxt extends Context, OP extends TreeStoreSelectOption>(node: RowNode, context: Cxt, option?: OP) {
let data = cloneDeep(node.$current);
if (context.getCurrentTxnId() && node.$txnId === context.getCurrentTxnId()) {
if (!node.$next) {
// 如果是删除,返回带$$deleteAt$$的行
return Object.assign({}, data, {
[DeleteAtAttribute]: 1,
});
if (!node.$next ) {
// 如果要求返回delete数据返回带$$deleteAt$$的行
if (option?.includedDeleted) {
return Object.assign({}, data, {
[DeleteAtAttribute]: 1,
});
}
return null;
}
else {
return Object.assign({}, data, node.$next);
@ -398,7 +401,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
const { $search } = filter;
return (node) => {
const row = this.constructRow(node, context) as any;
const row = this.constructRow(node, context, option) as any;
for (const attr of attributes) {
const { name } = attr;
if (row && row[name] && (typeof row[name] === 'string' && row[name].includes($search) || obscurePass(row, name as string, option))) {
@ -423,14 +426,14 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
}
if (typeof filter !== 'object') {
return (node) => {
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
return row ? (row as any)[attr] === filter || obscurePassLocal(row) : false;
};
}
else if (this.getSchema()[entity].attributes[attr]?.type === 'object') {
// 如果查询的目标就是object则转化成object的比较
return (node) => {
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
return row ? JSON.stringify((row as any)[attr]) === JSON.stringify(filter) || obscurePassLocal(row) : false;
};
}
@ -598,7 +601,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
}
}
return (node, nodeDict, exprResolveFns) => {
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
if (!row) {
return false;
}
@ -632,7 +635,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
const fn = this.translateExpression(entity, (filter as any)[attr], context, option);
fns.push(
(node, nodeDict, exprResolveFns) => {
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
if (!row) {
return false;
}
@ -660,7 +663,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
const fn = this.translateFilter(attr, (filter as any)[attr], context, option);
fns.push(
(node, nodeDict, exprResolveFns) => {
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
if (obscurePass(row, 'entity', option) || obscurePass(row, 'entityId', option)) {
return true;
}
@ -684,7 +687,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
const fn = this.translateFilter(relation, (filter as any)[attr], context, option);
fns.push(
(node, nodeDict, exprResolveFns) => {
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
if (obscurePass(row, `${attr}Id`, option)) {
return true;
}
@ -709,10 +712,10 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
if (nodeId) {
assert(!nodeDict.hasOwnProperty(nodeId), `Filter中的nodeId「${nodeId}」出现了多次`);
Object.assign(nodeDict, {
[nodeId]: this.constructRow(node, context),
[nodeId]: this.constructRow(node, context, option),
});
}
const row = this.constructRow(node, context);
const row = this.constructRow(node, context, option);
if (!row) {
return false;
}
@ -725,10 +728,11 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
};
}
private translateSorter<T extends keyof ED, Cxt extends Context>(
private translateSorter<T extends keyof ED, OP extends TreeStoreSelectOption, Cxt extends Context>(
entity: T,
sorter: DeduceSorter<ED[T]['Schema']>,
context: Cxt):
context: Cxt,
option?: OP):
(row1: object | null | undefined, row2: object | null | undefined) => number {
const compare = <T2 extends keyof ED>(
row1: object | null | undefined,
@ -795,8 +799,8 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
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);
const row222 = node2 && this.constructRow(node2, context);
const row111 = node1 && this.constructRow(node1, context, option);
const row222 = node2 && this.constructRow(node2, context, option);
return compare(row111, row222, row11['entity'], (sortAttr as any)[attr], direction);
}
@ -804,8 +808,8 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
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);
const row222 = node2 && this.constructRow(node2, context);
const row111 = node1 && this.constructRow(node1, context, option);
const row222 = node2 && this.constructRow(node2, context, option);
return compare(row111, row222, relation, (sortAttr as any)[attr], direction);
}
@ -866,10 +870,10 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
}
}
const rows = nodes.map(
(node) => this.constructRow(node, context) as EntityShape
(node) => this.constructRow(node, context, option) as EntityShape
);
const rows2 = this.formResult(entity, rows, selection, context);
const rows2 = this.formResult(entity, rows, selection, context, option);
return rows2;
}
@ -891,7 +895,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
} */
if (this.store[entity] && (this.store[entity]!)[id]) {
const node = this.store[entity] && (this.store[entity]!)[id as string];
throw new OakCongruentRowExists(entity as string, this.constructRow(node, context)!);
throw new OakCongruentRowExists(entity as string, this.constructRow(node, context, option)!);
}
if (!data.$$seq$$) {
// tree-store随意生成即可
@ -1060,11 +1064,12 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
}
}
private formResult<T extends keyof ED, Cxt extends Context>(
private formResult<T extends keyof ED, OP extends TreeStoreSelectOption, Cxt extends Context>(
entity: T,
rows: Array<Partial<ED[T]['Schema']>>,
selection: ED[T]['Selection'],
context: Cxt) {
context: Cxt,
option?: OP) {
const { data, sorter, indexFrom, count } = selection;
const findAvailableExprName = (current: string[]) => {
@ -1160,7 +1165,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
// 再计算sorter
if (sorter) {
const sorterFn = this.translateSorter(entity, sorter, context);
const sorterFn = this.translateSorter(entity, sorter, context, option);
rows2.sort(sorterFn);
}
@ -1203,7 +1208,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
}
else if (typeof rel === 'string') {
(result2 as any)[k] = {};
if(row2[k]) {
if (row2[k]) {
mappingIter(rel, row2[k]!, p2[k], result2[k]!);
}
}
@ -1232,7 +1237,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
) {
const ops = Object.keys(aggregationData).filter(
ele => ele !== '$aggr'
) as AggregationOp [];
) as AggregationOp[];
const result = {} as Record<string, any>;
for (const row of rows) {
for (const op of ops) {
@ -1290,10 +1295,15 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
}
for (const op of ops) {
if (!result[op]) {
result[op] = null;
if (op.startsWith('$count')) {
result[op] = 0;
}
else {
result[op] = null;
}
}
else if (op.startsWith('$avg')) {
result[op] = result[op].total/result[op].count;
result[op] = result[op].total / result[op].count;
}
}
return result as AggregationResult<ED[T]['Schema']>[number];
@ -1321,7 +1331,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
return result;
}
const aggr = this.calcAggregation(entity, rows, aggregationData);
return [ aggr ];
return [aggr];
}
protected selectSync<T extends keyof ED, OP extends TreeStoreSelectOption, Cxt extends SyncContext<ED>>(