进一步规范了定义

This commit is contained in:
Xu Chang 2022-04-14 18:30:31 +08:00
parent 42d1942211
commit 8cef650810
6 changed files with 73 additions and 62 deletions

View File

@ -1,5 +1,5 @@
import { Context } from '../types/Context';
import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, EntityDef, OperateParams, SelectionResult } from "../types/Entity";
import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, EntityDef, OperateParams } from "../types/Entity";
import { RowStore } from '../types/RowStore';
import { StorageSchema } from '../types/Storage';
/**这个用来处理级联的select和update对不同能力的 */
@ -7,9 +7,9 @@ export declare abstract class CascadeStore<ED extends {
[E: string]: EntityDef;
}> extends RowStore<ED> {
constructor(storageSchema: StorageSchema<ED>);
protected abstract selectAbjointRow<T extends keyof ED>(entity: T, selection: Omit<ED[T]['Selection'], 'indexFrom' | 'count' | 'data' | 'sorter'>, context: Context<ED>, params?: Object): Promise<SelectionResult<ED, T>['result']>;
protected abstract selectAbjointRow<T extends keyof ED>(entity: T, selection: Omit<ED[T]['Selection'], 'indexFrom' | 'count' | 'data' | 'sorter'>, context: Context<ED>, params?: Object): Promise<Array<ED[T]['OpSchema']>>;
protected abstract updateAbjointRow<T extends keyof ED>(entity: T, operation: DeduceCreateSingleOperation<ED[T]['Schema']> | DeduceUpdateOperation<ED[T]['Schema']> | DeduceRemoveOperation<ED[T]['Schema']>, context: Context<ED>, params?: OperateParams): Promise<void>;
protected cascadeSelect<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Context<ED>, params?: Object): Promise<SelectionResult<ED, T>['result']>;
protected cascadeSelect<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Context<ED>, params?: Object): Promise<Array<ED[T]['Schema']>>;
/**
*
* A --> B

51
lib/types/Entity.d.ts vendored
View File

@ -18,8 +18,8 @@ declare type SelectOption = {
export declare type OperateParams = {
notCollect?: boolean;
};
export declare type FormUpdateData<SH extends EntityShape> = Partial<Omit<SH, InstinctiveAttributes>>;
export declare type FormCreateData<SH extends EntityShape> = Omit<SH, InstinctiveAttributes> & {
export declare type FormUpdateData<SH extends GeneralEntityShape> = Partial<Omit<SH, InstinctiveAttributes>>;
export declare type FormCreateData<SH extends GeneralEntityShape> = Omit<SH, InstinctiveAttributes> & {
id: string;
};
export declare type Operation<A extends GenericAction | string, DATA extends Object, FILTER extends Object | undefined = undefined, SORTER extends Object | undefined = undefined> = {
@ -34,11 +34,13 @@ export interface EntityShape {
$$createAt$$: number | Date;
$$updateAt$$: number | Date;
$$removeAt$$?: number | Date | null;
}
interface GeneralEntityShape extends EntityShape {
[K: string]: any;
}
export interface EntityDef {
Schema: EntityShape;
OpSchema: EntityShape;
Schema: GeneralEntityShape;
OpSchema: GeneralEntityShape;
Action: string;
ParticularAction?: string;
Selection: Omit<DeduceSelection<this['Schema']>, 'action'>;
@ -47,36 +49,36 @@ export interface EntityDef {
export interface EntityDict {
[E: string]: EntityDef;
}
declare type DeduceProjection<SH extends EntityShape> = Partial<{
declare type DeduceProjection<SH extends GeneralEntityShape> = Partial<{
'#id': NodeId;
} & {
[K in keyof SH]: 1 | any;
} & ExprOp<keyof SH>>;
declare type AttrFilter<SH extends EntityShape> = {
declare type AttrFilter<SH extends GeneralEntityShape> = {
[K in keyof SH]: any;
};
export declare type DeduceFilter<SH extends EntityShape> = MakeFilter<AttrFilter<SH> & ExprOp<keyof SH>>;
export declare type DeduceSorterAttr<SH extends EntityShape> = OneOf<{
export declare type DeduceFilter<SH extends GeneralEntityShape> = MakeFilter<AttrFilter<SH> & ExprOp<keyof SH>>;
export declare type DeduceSorterAttr<SH extends GeneralEntityShape> = OneOf<{
[K: string]: 1 | object | undefined;
} & ExprOp<keyof SH>>;
export declare type DeduceSorter<SH extends EntityShape> = Array<{
export declare type DeduceSorter<SH extends GeneralEntityShape> = Array<{
$attr: DeduceSorterAttr<SH>;
$direction?: "asc" | "desc";
}>;
export declare type DeduceSelection<SH extends EntityShape> = Selection<DeduceProjection<SH>, DeduceFilter<SH>, DeduceSorter<SH>>;
export declare type DeduceCreateOperationData<SH extends EntityShape> = FormCreateData<SH>;
export declare type DeduceCreateSingleOperation<SH extends EntityShape> = Operation<'create', DeduceCreateOperationData<SH>>;
export declare type DeduceCreateMultipleOperation<SH extends EntityShape> = Operation<'create', Array<DeduceCreateOperationData<SH>>>;
export declare type DeduceCreateOperation<SH extends EntityShape> = DeduceCreateSingleOperation<SH> | DeduceCreateMultipleOperation<SH>;
export declare type DeduceUpdateOperationData<SH extends EntityShape> = FormUpdateData<SH>;
export declare type DeduceUpdateOperation<SH extends EntityShape> = Operation<'update' | string, DeduceUpdateOperationData<SH>, DeduceFilter<SH>>;
export declare type DeduceRemoveOperationData<SH extends EntityShape> = {
export declare type DeduceSelection<SH extends GeneralEntityShape> = Selection<DeduceProjection<SH>, DeduceFilter<SH>, DeduceSorter<SH>>;
export declare type DeduceCreateOperationData<SH extends GeneralEntityShape> = FormCreateData<SH>;
export declare type DeduceCreateSingleOperation<SH extends GeneralEntityShape> = Operation<'create', DeduceCreateOperationData<SH>>;
export declare type DeduceCreateMultipleOperation<SH extends GeneralEntityShape> = Operation<'create', Array<DeduceCreateOperationData<SH>>>;
export declare type DeduceCreateOperation<SH extends GeneralEntityShape> = DeduceCreateSingleOperation<SH> | DeduceCreateMultipleOperation<SH>;
export declare type DeduceUpdateOperationData<SH extends GeneralEntityShape> = FormUpdateData<SH>;
export declare type DeduceUpdateOperation<SH extends GeneralEntityShape> = Operation<'update' | string, DeduceUpdateOperationData<SH>, DeduceFilter<SH>>;
export declare type DeduceRemoveOperationData<SH extends GeneralEntityShape> = {
[A in keyof SH]?: any;
} & {
[A: string]: any;
};
export declare type DeduceRemoveOperation<SH extends EntityShape> = Operation<'remove', DeduceRemoveOperationData<SH>, DeduceFilter<SH>>;
export declare type DeduceOperation<SH extends EntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>;
export declare type DeduceRemoveOperation<SH extends GeneralEntityShape> = Operation<'remove', DeduceRemoveOperationData<SH>, DeduceFilter<SH>>;
export declare type DeduceOperation<SH extends GeneralEntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>;
export declare type CreateOpResult<ED extends EntityDict, T extends keyof ED> = {
a: 'c';
e: T;
@ -105,9 +107,10 @@ export declare type OpRecord<ED extends EntityDict> = CreateOpResult<ED, keyof E
export interface OperationResult {
ids?: string[];
}
export interface SelectionResult<ED extends EntityDict, T extends keyof ED> {
result: Array<Partial<ED[T]['Schema'] & {
[A in ExpressionKey]?: any;
}>>;
}
export declare type SelectRowShape<E extends GeneralEntityShape, P extends DeduceProjection<GeneralEntityShape>> = {
[K in keyof P]: K extends ExpressionKey ? any : K extends keyof E ? P[K] extends 1 ? E[K] : E[K] extends Array<any> ? Array<SelectRowShape<E[K][0], P[K]['data']>> : SelectRowShape<E[K], P[K]> : any;
};
export declare type SelectionResult2<E extends GeneralEntityShape, P extends DeduceProjection<GeneralEntityShape>> = {
result: Array<SelectRowShape<E, P>>;
};
export {};

View File

@ -1,15 +1,13 @@
import { EntityDef, OperationResult, SelectionResult, OperateParams } from './Entity';
import { OperationResult, OperateParams, EntityDict, SelectionResult2 } from './Entity';
import { Context } from './Context';
import { StorageSchema } from './Storage';
import { OakErrorDefDict } from '../OakError';
export declare abstract class RowStore<ED extends {
[E: string]: EntityDef;
}> {
export declare abstract class RowStore<ED extends EntityDict> {
static $$LEVEL: string;
static $$CODES: OakErrorDefDict;
protected storageSchema: StorageSchema<ED>;
abstract operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Context<ED>, params?: OperateParams): Promise<OperationResult>;
abstract select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Context<ED>, params?: Object): Promise<SelectionResult<ED, T>>;
abstract select<T extends keyof ED, S extends ED[T]['Selection']>(entity: T, selection: S, context: Context<ED>, params?: Object): Promise<SelectionResult2<ED[T]['Schema'], S['data']>>;
abstract count<T extends keyof ED>(entity: T, selection: Omit<ED[T]['Selection'], 'data' | 'sorter' | 'action'>, context: Context<ED>, params?: Object): Promise<number>;
constructor(storageSchema: StorageSchema<ED>);
}

View File

@ -1,7 +1,8 @@
import assert from "assert";
import { assign } from "lodash";
import { Context } from '../types/Context';
import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceFilter, DeduceRemoveOperation, DeduceSelection, DeduceUpdateOperation, EntityDef, EntityShape, OperateParams, SelectionResult } from "../types/Entity";
import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceFilter, DeduceRemoveOperation, DeduceSelection,
DeduceUpdateOperation, EntityDef, EntityShape, OperateParams, SelectionResult2 } from "../types/Entity";
import { RowStore } from '../types/RowStore';
import { StorageSchema } from '../types/Storage';
import { addFilterSegment } from "./filter";
@ -18,7 +19,7 @@ export abstract class CascadeStore<ED extends {
entity: T,
selection: Omit<ED[T]['Selection'], 'indexFrom' | 'count' | 'data' | 'sorter'>,
context: Context<ED>,
params?: Object): Promise<SelectionResult<ED, T>['result']>;
params?: Object): Promise<Array<ED[T]['OpSchema']>>;
protected abstract updateAbjointRow<T extends keyof ED>(
entity: T,
@ -29,7 +30,7 @@ export abstract class CascadeStore<ED extends {
protected async cascadeSelect<T extends keyof ED>(
entity: T,
selection: ED[T]['Selection'],
context: Context<ED>, params?: Object): Promise<SelectionResult<ED, T>['result']> {
context: Context<ED>, params?: Object): Promise<Array<ED[T]['Schema']>> {
const { data } = selection;
const projection: ED[T]['Selection']['data'] = {};

View File

@ -24,9 +24,9 @@ export type OperateParams = {
notCollect?: boolean;
};
export type FormUpdateData<SH extends EntityShape> = Partial<Omit<SH, InstinctiveAttributes>>;
export type FormUpdateData<SH extends GeneralEntityShape> = Partial<Omit<SH, InstinctiveAttributes>>;
export type FormCreateData<SH extends EntityShape> = Omit<SH, InstinctiveAttributes> & { id: string };
export type FormCreateData<SH extends GeneralEntityShape> = Omit<SH, InstinctiveAttributes> & { id: string };
export type Operation<A extends GenericAction | string,
DATA extends Object,
@ -47,13 +47,16 @@ export interface EntityShape {
$$createAt$$: number | Date;
$$updateAt$$: number | Date;
$$removeAt$$?: number | Date | null;
}
interface GeneralEntityShape extends EntityShape {
[K: string]: any;
}
export interface EntityDef {
// Name: E;
Schema: EntityShape;
OpSchema: EntityShape;
Schema: GeneralEntityShape;
OpSchema: GeneralEntityShape;
Action: string;
ParticularAction?: string;
Selection: Omit<DeduceSelection<this['Schema']>, 'action'>;
@ -64,50 +67,50 @@ export interface EntityDict {
[E: string]: EntityDef;
};
type DeduceProjection<SH extends EntityShape> = Partial<{
type DeduceProjection<SH extends GeneralEntityShape> = Partial<{
'#id': NodeId;
} & {
[K in keyof SH]: 1 | any;
} & ExprOp<keyof SH>>;
type AttrFilter<SH extends EntityShape> = {
type AttrFilter<SH extends GeneralEntityShape> = {
[K in keyof SH]: any;
}
export type DeduceFilter<SH extends EntityShape> = MakeFilter<AttrFilter<SH> & ExprOp<keyof SH>>;
export type DeduceFilter<SH extends GeneralEntityShape> = MakeFilter<AttrFilter<SH> & ExprOp<keyof SH>>;
export type DeduceSorterAttr<SH extends EntityShape> = OneOf<{
export type DeduceSorterAttr<SH extends GeneralEntityShape> = OneOf<{
[K: string]: 1 | object | undefined;
} & ExprOp<keyof SH>>;
export type DeduceSorter<SH extends EntityShape> = Array<{
export type DeduceSorter<SH extends GeneralEntityShape> = Array<{
$attr: DeduceSorterAttr<SH>;
$direction?: "asc" | "desc";
}>;
export type DeduceSelection<SH extends EntityShape> = Selection<DeduceProjection<SH>, DeduceFilter<SH>, DeduceSorter<SH>>;
export type DeduceSelection<SH extends GeneralEntityShape> = Selection<DeduceProjection<SH>, DeduceFilter<SH>, DeduceSorter<SH>>;
export type DeduceCreateOperationData<SH extends EntityShape> = FormCreateData<SH>;
export type DeduceCreateOperationData<SH extends GeneralEntityShape> = FormCreateData<SH>;
export type DeduceCreateSingleOperation<SH extends EntityShape> = Operation<'create', DeduceCreateOperationData<SH>>;
export type DeduceCreateSingleOperation<SH extends GeneralEntityShape> = Operation<'create', DeduceCreateOperationData<SH>>;
export type DeduceCreateMultipleOperation<SH extends EntityShape> = Operation<'create', Array<DeduceCreateOperationData<SH>>>;
export type DeduceCreateMultipleOperation<SH extends GeneralEntityShape> = Operation<'create', Array<DeduceCreateOperationData<SH>>>;
export type DeduceCreateOperation<SH extends EntityShape> = DeduceCreateSingleOperation<SH> | DeduceCreateMultipleOperation<SH>;
export type DeduceCreateOperation<SH extends GeneralEntityShape> = DeduceCreateSingleOperation<SH> | DeduceCreateMultipleOperation<SH>;
export type DeduceUpdateOperationData<SH extends EntityShape> = FormUpdateData<SH>;
export type DeduceUpdateOperationData<SH extends GeneralEntityShape> = FormUpdateData<SH>;
export type DeduceUpdateOperation<SH extends EntityShape> = Operation<
export type DeduceUpdateOperation<SH extends GeneralEntityShape> = Operation<
'update' | string,
DeduceUpdateOperationData<SH>, DeduceFilter<SH>>;
export type DeduceRemoveOperationData<SH extends EntityShape> = {
export type DeduceRemoveOperationData<SH extends GeneralEntityShape> = {
[A in keyof SH]?: any;
} & { [A: string]: any };
export type DeduceRemoveOperation<SH extends EntityShape> = Operation<'remove', DeduceRemoveOperationData<SH>, DeduceFilter<SH>>;
export type DeduceRemoveOperation<SH extends GeneralEntityShape> = Operation<'remove', DeduceRemoveOperationData<SH>, DeduceFilter<SH>>;
export type DeduceOperation<SH extends EntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>;
export type DeduceOperation<SH extends GeneralEntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>;
export type CreateOpResult<ED extends EntityDict, T extends keyof ED> = {
a: 'c';
@ -144,8 +147,16 @@ export interface OperationResult {
ids?: string[];
};
export interface SelectionResult<ED extends EntityDict, T extends keyof ED> {
/* export interface SelectionResult<ED extends EntityDict, T extends keyof ED> {
result: Array<Partial<ED[T]['Schema'] & {
[A in ExpressionKey]?: any;
}>>;
} */
export type SelectRowShape<E extends GeneralEntityShape, P extends DeduceProjection<GeneralEntityShape>> = {
[K in keyof P]: K extends ExpressionKey ? any : K extends keyof E ? P[K] extends 1 ? E[K]: E[K] extends Array<any> ? Array<SelectRowShape<E[K][0], P[K]['data']>> : SelectRowShape<E[K], P[K]> : any;
}
export type SelectionResult2<E extends GeneralEntityShape, P extends DeduceProjection<GeneralEntityShape>> = {
result: Array<SelectRowShape<E, P>>;
}

View File

@ -1,11 +1,9 @@
import { EntityDef, OperationResult, SelectionResult, EntityShape, OperateParams } from './Entity';
import { OperationResult, OperateParams, EntityDict, SelectionResult2 } from './Entity';
import { Context } from './Context';
import { StorageSchema } from './Storage';
import { OakErrorDefDict } from '../OakError';
export abstract class RowStore<ED extends {
[E: string]: EntityDef;
}> {
export abstract class RowStore<ED extends EntityDict> {
static $$LEVEL = 'store';
static $$CODES: OakErrorDefDict = {
primaryKeyConfilict: [1, '主键重复'],
@ -21,12 +19,12 @@ export abstract class RowStore<ED extends {
params?: OperateParams
): Promise<OperationResult>;
abstract select<T extends keyof ED> (
abstract select<T extends keyof ED, S extends ED[T]['Selection']> (
entity: T,
selection: ED[T]['Selection'],
selection: S,
context: Context<ED>,
params?: Object
): Promise<SelectionResult<ED, T>>;
): Promise<SelectionResult2<ED[T]['Schema'], S['data']>>;
abstract count<T extends keyof ED> (
entity: T,