160 lines
7.8 KiB
TypeScript
160 lines
7.8 KiB
TypeScript
import { EntityDict, OperateOption, SelectOption, OpRecord, CheckerType, Aspect, StorageSchema, Checker, Connector, AggregationResult, SelectionRewriter, OperationRewriter } from 'oak-domain/lib/types';
|
||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||
import { AspectDict as CommonAspectDict } from 'oak-common-aspect';
|
||
import { Feature } from '../types/Feature';
|
||
import { CacheStore } from '../cacheStore/CacheStore';
|
||
import { OakUserException } from 'oak-domain/lib/types/Exception';
|
||
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
|
||
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
||
import { LocalStorage } from './localStorage';
|
||
import { CommonConfiguration } from 'oak-domain/lib/types/Configuration';
|
||
interface CacheSelectOption extends SelectOption {
|
||
ignoreKeepFreshRule?: true;
|
||
}
|
||
type RefreshOption = {
|
||
dontPublish?: true;
|
||
useLocalCache?: {
|
||
keys: string[];
|
||
gap?: number;
|
||
onlyReturnFresh?: boolean;
|
||
};
|
||
ignoreContext?: true;
|
||
};
|
||
type WholeAspectDict<ED extends EntityDict & BaseEntityDict, AD extends Record<string, Aspect<ED, AsyncContext<ED>>>> = CommonAspectDict<ED> & AD;
|
||
export declare class Cache<ED extends EntityDict & BaseEntityDict> extends Feature {
|
||
private cacheStore;
|
||
private syncEventsCallbacks;
|
||
private contextBuilder;
|
||
private executing;
|
||
private savedEntities;
|
||
private keepFreshPeriod;
|
||
private localStorage;
|
||
private refreshRecords;
|
||
private context?;
|
||
private initPromise;
|
||
private attrUpdateMatrix?;
|
||
private connector;
|
||
private baseRelationAuth;
|
||
private entityActionAuthDict;
|
||
registerSelectionRewriter(rewriter: SelectionRewriter<ED, AsyncContext<ED> | SyncContext<ED>, SelectOption>): void;
|
||
registerOperationRewriter(rewriter: OperationRewriter<ED, AsyncContext<ED> | SyncContext<ED>, OperateOption>): void;
|
||
constructor(storageSchema: StorageSchema<ED>, connector: Connector<ED, SyncContext<ED>>, frontendContextBuilder: (store: CacheStore<ED>) => SyncContext<ED>, checkers: Array<Checker<ED, keyof ED, SyncContext<ED>>>, localStorage: LocalStorage, common: CommonConfiguration<ED>);
|
||
/**
|
||
* 处理cache中需要缓存的数据
|
||
*/
|
||
private initSavedLogic;
|
||
onInitialized(): Promise<void>;
|
||
getSchema(): StorageSchema<ED>;
|
||
exec<AD extends Record<string, Aspect<ED, AsyncContext<ED>>>, K extends keyof WholeAspectDict<ED, AD>>(name: K, params: Parameters<WholeAspectDict<ED, AD>[K]>[0], callback?: (result: Awaited<ReturnType<WholeAspectDict<ED, AD>[K]>>, opRecords?: OpRecord<ED>[]) => void, dontPublish?: true, ignoreContext?: true): Promise<{
|
||
result: Awaited<ReturnType<WholeAspectDict<ED, AD>[K]>>;
|
||
message: string | null | undefined;
|
||
}>;
|
||
private saveRefreshRecord;
|
||
private addRefreshRecord;
|
||
/**
|
||
* 向服务器刷新数据
|
||
* @param entity 要刷新的实体
|
||
* @param selection 查询映射
|
||
* @param option 选项
|
||
* @param callback 回调函数
|
||
* @param refreshOption 刷新选项
|
||
* @returns 返回查询结果
|
||
* @description 支持增量更新,可以使用useLocalCache来将一些metadata级的数据本地缓存,减少更新次数。
|
||
* 使用增量更新这里要注意,传入的keys如果有一个key是首次更新,会导致所有的keys全部更新。使用模块自己保证这种情况不要出现
|
||
*/
|
||
refresh<T extends keyof ED, OP extends CacheSelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP, callback?: (result: Awaited<ReturnType<CommonAspectDict<ED>['select']>>) => void, refreshOption?: RefreshOption): Promise<{
|
||
data: Partial<ED[T]["Schema"]>[];
|
||
total?: undefined;
|
||
} | {
|
||
data: Partial<ED[T]["Schema"]>[];
|
||
total: number | undefined;
|
||
}>;
|
||
/**
|
||
* 聚合查询
|
||
* @param entity 实体
|
||
* @param aggregation 聚合条件
|
||
* @param option 选项
|
||
* @returns 返回查询结果
|
||
*/
|
||
aggregate<T extends keyof ED, OP extends SelectOption>(entity: T, aggregation: ED[T]['Aggregation'], option?: OP): Promise<AggregationResult<ED[T]["Schema"]>>;
|
||
/**
|
||
* 操作
|
||
* @param entity 实体
|
||
* @param operation 操作名
|
||
* @param option 选项
|
||
* @param callback 回调函数
|
||
* @returns 返回操作结果
|
||
*/
|
||
operate<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'] | ED[T]['Operation'][], option?: OP, callback?: (result: Awaited<ReturnType<CommonAspectDict<ED>['operate']>>) => void): Promise<{
|
||
result: import("oak-domain/lib/types").OperationResult<ED>[] | Awaited<import("oak-domain/lib/types").OperationResult<ED>>;
|
||
message: string | null | undefined;
|
||
}>;
|
||
count<T extends keyof ED, OP extends SelectOption>(entity: T, selection: Pick<ED[T]['Selection'], 'filter'>, option?: OP, callback?: (result: Awaited<ReturnType<CommonAspectDict<ED>['count']>>) => void): Promise<number>;
|
||
private syncInner;
|
||
sync(records: OpRecord<ED>[]): void;
|
||
/**
|
||
* 前端缓存做operation只可能是测试权限,必然回滚
|
||
* @param entity
|
||
* @param operation
|
||
* @returns
|
||
*/
|
||
tryRedoOperations<T extends keyof ED>(operations: ({
|
||
operation: ED[T]['Operation'];
|
||
entity: T;
|
||
})[]): true | Error;
|
||
/**
|
||
* 根据初始化定义的attrUpdateMatrix,检查当前entity是否支持用action去更新Attrs属性
|
||
* 返回通过合法性检查的Attrs
|
||
* @param entity
|
||
* @param action
|
||
* @param attrs
|
||
* @returns
|
||
*/
|
||
getLegalUpdateAttrs<T extends keyof ED>(entity: T, action: ED[T]['Action'], attrs: (keyof ED[T]['Update']['data'])[], id: string): (keyof ED[T]["Update"]["data"])[];
|
||
checkOperation<T extends keyof ED>(entity: T, operation: {
|
||
action: ED[T]['Action'];
|
||
data?: ED[T]['Operation']['data'];
|
||
filter?: ED[T]['Filter'];
|
||
}, checkerTypes?: CheckerType[], cacheInsensative?: true): boolean | { [A in ED[T]["Action"]]: boolean | OakUserException<ED>; }[ED[T]["Action"]];
|
||
redoOperation(opers: Array<{
|
||
entity: keyof ED;
|
||
operation: ED[keyof ED]['Operation'];
|
||
}>): void;
|
||
fetchRows(missedRows: Array<{
|
||
entity: keyof ED;
|
||
selection: ED[keyof ED]['Selection'];
|
||
}>): void;
|
||
private getInner;
|
||
/**
|
||
* 把select的结果merge到sr中,因为select有可能存在aggr数据,在这里必须要使用合并后的结果
|
||
* sr的数据结构不好规范化描述,参见common-aspect中的select接口
|
||
* @param entity
|
||
* @param rows
|
||
* @param sr
|
||
*/
|
||
mergeSelectResult<T extends keyof ED>(entity: T, rows: Partial<ED[T]['Schema']>[], sr: Record<string, any>): void;
|
||
get<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], sr?: Record<string, any>): Partial<ED[T]["Schema"]>[];
|
||
judgeRelation(entity: keyof ED, attr: string): string | 0 | 1 | string[] | 2 | -1;
|
||
bindOnSync(callback: (opRecords: OpRecord<ED>[]) => void): void;
|
||
unbindOnSync(callback: (opRecords: OpRecord<ED>[]) => void): void;
|
||
getCachedData(): { [T in keyof ED]?: ED[T]["OpSchema"][] | undefined; };
|
||
getFullData(): Promise<{ [T in keyof ED]?: ED[T]["OpSchema"][] | undefined; }>;
|
||
makeBridgeUrl(url: string, headers?: Record<string, string>): string;
|
||
begin(allowInTxn?: boolean): (() => void) | undefined;
|
||
checkFilterContains<T extends keyof ED>(entity: T, contained: NonNullable<ED[T]['Filter']>, filter: NonNullable<ED[T]['Filter']>, dataCompare?: true): boolean | Promise<boolean>;
|
||
private entityGraph?;
|
||
private buildEntityGraph;
|
||
getEntityGraph(): {
|
||
data: {
|
||
name: string;
|
||
}[];
|
||
links: {
|
||
source: string;
|
||
target: string;
|
||
value: number;
|
||
}[];
|
||
};
|
||
getRelationIdByName(entity: keyof ED, name: string, entityId?: string): Promise<ED["relation"]["Schema"]["id"] | undefined>;
|
||
}
|
||
export {};
|