cache对rowUnexist增强了一个判断

This commit is contained in:
Xu Chang 2023-01-12 20:04:52 +08:00
parent a1a6098170
commit f7b4198bb2
3 changed files with 43 additions and 7 deletions

View File

@ -11,7 +11,7 @@ export declare class Cache<ED extends EntityDict & BaseEntityDict, Cxt extends A
private syncEventsCallbacks;
private contextBuilder?;
constructor(aspectWrapper: AspectWrapper<ED, Cxt, AD>, contextBuilder: () => FrontCxt, store: CacheStore<ED, FrontCxt>);
exec<K extends keyof AD>(name: K, params: Parameters<AD[K]>[0], callback?: (result: Awaited<ReturnType<AD[K]>>) => void): Promise<any>;
exec<K extends keyof AD>(name: K, params: Parameters<AD[K]>[0], callback?: (result: Awaited<ReturnType<AD[K]>>, opRecords: OpRecord<ED>[]) => void): Promise<any>;
refresh<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP, getCount?: true, callback?: (result: Awaited<ReturnType<AD['select']>>) => void): Promise<{
data: Partial<ED[T]['Schema']>[];
count?: number | undefined;

View File

@ -5,6 +5,7 @@ var tslib_1 = require("tslib");
var Feature_1 = require("../types/Feature");
var lodash_1 = require("oak-domain/lib/utils/lodash");
var Exception_1 = require("oak-domain/lib/types/Exception");
var assert_1 = tslib_1.__importDefault(require("assert"));
var Cache = /** @class */ (function (_super) {
tslib_1.__extends(Cache, _super);
function Cache(aspectWrapper, contextBuilder, store) {
@ -36,7 +37,7 @@ var Cache = /** @class */ (function (_super) {
case 0: return [4 /*yield*/, this.aspectWrapper.exec(name, params)];
case 1:
_a = _b.sent(), result = _a.result, opRecords = _a.opRecords;
callback && callback(result);
callback && callback(result, opRecords);
this.sync(opRecords);
return [2 /*return*/, result];
}
@ -209,6 +210,7 @@ var Cache = /** @class */ (function (_super) {
}
};
Cache.prototype.getInner = function (entity, selection, context, allowMiss, includedDeleted) {
var _this = this;
try {
var result = this.cacheStore.select(entity, selection, context, {
dontCollect: true,
@ -220,7 +222,31 @@ var Cache = /** @class */ (function (_super) {
if (err instanceof Exception_1.OakRowUnexistedException) {
if (!allowMiss) {
var missedRows = err.getRows();
this.exec('fetchRows', missedRows);
this.exec('fetchRows', missedRows, function (result, opRecords) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var opRecords_1, opRecords_1_1, record, d, e;
var e_3, _a;
return tslib_1.__generator(this, function (_b) {
try {
// missedRows理论上一定要取到不能为空集。否则就是程序员有遗漏
for (opRecords_1 = tslib_1.__values(opRecords), opRecords_1_1 = opRecords_1.next(); !opRecords_1_1.done; opRecords_1_1 = opRecords_1.next()) {
record = opRecords_1_1.value;
d = record.d;
(0, assert_1.default)(Object.keys(d).length > 0, '在通过fetchRow取不一致数据时返回了空数据请拿该程序员祭天。');
for (e in d) {
(0, assert_1.default)(Object.keys(d[e]).length > 0, "\u5728\u901A\u8FC7fetchRow\u53D6\u4E0D\u4E00\u81F4\u6570\u636E\u65F6\u8FD4\u56DE\u4E86\u7A7A\u6570\u636E\uFF0C\u8BF7\u62FF\u8BE5\u7A0B\u5E8F\u5458\u796D\u5929\u3002entity\u662F".concat(e));
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (opRecords_1_1 && !opRecords_1_1.done && (_a = opRecords_1.return)) _a.call(opRecords_1);
}
finally { if (e_3) throw e_3.error; }
}
return [2 /*return*/];
});
}); });
}
return [];
}

View File

@ -1,4 +1,4 @@
import { EntityDict, OperateOption, SelectOption, OpRecord, AspectWrapper, CheckerType, Aspect } from 'oak-domain/lib/types';
import { EntityDict, OperateOption, SelectOption, OpRecord, AspectWrapper, CheckerType, Aspect, SelectOpResult } from 'oak-domain/lib/types';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { CommonAspectDict } from 'oak-common-aspect';
import { Feature } from '../types/Feature';
@ -7,6 +7,7 @@ import { CacheStore } from '../cacheStore/CacheStore';
import { OakRowUnexistedException } from 'oak-domain/lib/types/Exception';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
import assert from 'assert';
export class Cache<
ED extends EntityDict & BaseEntityDict,
@ -51,10 +52,10 @@ export class Cache<
async exec<K extends keyof AD>(
name: K,
params: Parameters<AD[K]>[0],
callback?: (result: Awaited<ReturnType<AD[K]>>) => void
callback?: (result: Awaited<ReturnType<AD[K]>>, opRecords: OpRecord<ED>[]) => void
) {
const { result, opRecords } = await this.aspectWrapper.exec(name, params);
callback && callback(result);
callback && callback(result, opRecords);
this.sync(opRecords);
return result;
}
@ -233,7 +234,16 @@ export class Cache<
if (err instanceof OakRowUnexistedException) {
if (!allowMiss) {
const missedRows = err.getRows();
this.exec('fetchRows', missedRows);
this.exec('fetchRows', missedRows, async (result, opRecords) => {
// missedRows理论上一定要取到不能为空集。否则就是程序员有遗漏
for (const record of opRecords) {
const { d } = record as SelectOpResult<ED>;
assert(Object.keys(d).length > 0, '在通过fetchRow取不一致数据时返回了空数据请拿该程序员祭天。');
for (const e in d) {
assert(Object.keys(d![e]!).length > 0, `在通过fetchRow取不一致数据时返回了空数据请拿该程序员祭天。entity是${e}`);
}
}
})
}
return [];
} else {