一些定义

This commit is contained in:
Xu Chang 2022-03-17 19:37:21 +08:00
parent 169ea75924
commit 135d8ac69f
8 changed files with 103 additions and 48 deletions

View File

@ -1568,7 +1568,17 @@ function outputEntityDict(outputDir, printer) {
const entityLc = (0, utils_1.firstLetterLowerCase)(entity); const entityLc = (0, utils_1.firstLetterLowerCase)(entity);
propertySignatures.push(factory.createPropertySignature(undefined, factory.createIdentifier(entityLc), undefined, factory.createTypeReferenceNode(entity))); propertySignatures.push(factory.createPropertySignature(undefined, factory.createIdentifier(entityLc), undefined, factory.createTypeReferenceNode(entity)));
} }
statements.push(factory.createTypeAliasDeclaration(undefined, [factory.createModifier(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier("EntityDict"), undefined, factory.createTypeLiteralNode(propertySignatures))); if ( /* process.env.TARGET_IN_OAK_DOMAIN */false) {
statements.push(factory.createImportDeclaration(undefined, undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, undefined, factory.createIdentifier("EntityDef"))])), factory.createStringLiteral("../types/Entity"), undefined), factory.createTypeAliasDeclaration(undefined, [factory.createModifier(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier("EntityDict"), undefined, factory.createIntersectionTypeNode([
factory.createTypeLiteralNode(propertySignatures),
factory.createTypeLiteralNode([
factory.createIndexSignature(undefined, undefined, [factory.createParameterDeclaration(undefined, undefined, undefined, factory.createIdentifier("E"), undefined, factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined)], factory.createTypeReferenceNode(factory.createIdentifier("EntityDef"), undefined))
])
])));
}
else {
statements.push(factory.createTypeAliasDeclaration(undefined, [factory.createModifier(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier("EntityDict"), undefined, factory.createTypeLiteralNode(propertySignatures)));
}
const resultFile = ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS); const resultFile = ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS);
const result = printer.printNode(ts.EmitHint.Unspecified, factory.createSourceFile(statements, factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None), resultFile); const result = printer.printNode(ts.EmitHint.Unspecified, factory.createSourceFile(statements, factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None), resultFile);
const fileName = path_1.default.join(outputDir, 'EntityDict.ts'); const fileName = path_1.default.join(outputDir, 'EntityDict.ts');

View File

@ -1,5 +1,7 @@
import { RunningContext } from "./Context"; import { RunningContext } from "./Context";
import { EntityDef } from "./Entity"; import { EntityDef } from "./Entity";
export interface Aspect<ED extends Record<string, EntityDef>> { export interface Aspect<ED extends {
[E: string]: EntityDef;
}> {
(params: any, context: RunningContext<ED>): any; (params: any, context: RunningContext<ED>): any;
} }

View File

@ -1,19 +1,15 @@
import { EntityDef } from './Entity'; import { EntityDict } from './Entity';
import { RowStore } from './RowStore'; import { RowStore } from './RowStore';
import { Schema as Application } from '../entities/Application'; import { Schema as Application } from '../base-domain/Application/Schema';
import { Schema as Token } from '../entities/Token'; import { Schema as Token } from '../base-domain/Token/Schema';
export interface Context<ED extends { export interface Context<ED extends EntityDict> {
[E: string]: EntityDef;
}> {
rowStore: RowStore<ED>; rowStore: RowStore<ED>;
on(event: 'commit' | 'rollback', callback: (context: Context<ED>) => Promise<void>): void; on(event: 'commit' | 'rollback', callback: (context: Context<ED>) => Promise<void>): void;
begin(options?: object): Promise<void>; begin(options?: object): Promise<void>;
commit(): Promise<void>; commit(): Promise<void>;
rollback(): Promise<void>; rollback(): Promise<void>;
} }
export interface RunningContext<ED extends { export interface RunningContext<ED extends EntityDict> extends Context<ED> {
[E: string]: EntityDef; getApplication: () => Application;
}> extends Context<ED> { getToken: () => Token | undefined;
application: Application;
token?: Token;
} }

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

@ -42,6 +42,9 @@ export interface EntityDef {
Selection: Omit<DeduceSelection<this['Schema']>, 'action'>; Selection: Omit<DeduceSelection<this['Schema']>, 'action'>;
Operation: DeduceOperation<this['Schema']>; Operation: DeduceOperation<this['Schema']>;
} }
export interface EntityDict {
[E: string]: EntityDef;
}
declare type DeduceProjection<SH extends EntityShape> = Partial<{ declare type DeduceProjection<SH extends EntityShape> = Partial<{
'#id': NodeId; '#id': NodeId;
} & { } & {
@ -72,9 +75,7 @@ export declare type DeduceRemoveOperationData<SH extends EntityShape> = {
}; };
export declare type DeduceRemoveOperation<SH extends EntityShape> = Operation<'remove', DeduceRemoveOperationData<SH>, DeduceFilter<SH>>; 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 DeduceOperation<SH extends EntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>;
export interface OperationResult<ED extends { export interface OperationResult<ED extends EntityDict> {
[K: string]: EntityDef;
}> {
operations?: { operations?: {
[T in keyof ED]?: Array<ED[keyof ED]['Operation']>; [T in keyof ED]?: Array<ED[keyof ED]['Operation']>;
}; };
@ -85,9 +86,7 @@ export interface OperationResult<ED extends {
message: string; message: string;
}>; }>;
} }
export declare type SelectionResult<ED extends { export declare type SelectionResult<ED extends EntityDict, T extends keyof ED> = {
[K: string]: EntityDef;
}, T extends keyof ED> = {
result: Array<Partial<ED[T]['Schema'] & { result: Array<Partial<ED[T]['Schema'] & {
[A in ExpressionKey]?: any; [A in ExpressionKey]?: any;
}>>; }>>;

View File

@ -2,3 +2,4 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
; ;
; ;
;

View File

@ -3311,17 +3311,68 @@ function outputEntityDict(outputDir: string, printer: ts.Printer) {
); );
} }
statements.push( if (/* process.env.TARGET_IN_OAK_DOMAIN */false) {
factory.createTypeAliasDeclaration( statements.push(
undefined, factory.createImportDeclaration(
[factory.createModifier(ts.SyntaxKind.ExportKeyword)], undefined,
factory.createIdentifier("EntityDict"), undefined,
undefined, factory.createImportClause(
factory.createTypeLiteralNode( false,
propertySignatures undefined,
factory.createNamedImports([factory.createImportSpecifier(
false,
undefined,
factory.createIdentifier("EntityDef")
)])
),
factory.createStringLiteral("../types/Entity"),
undefined
),
factory.createTypeAliasDeclaration(
undefined,
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
factory.createIdentifier("EntityDict"),
undefined,
factory.createIntersectionTypeNode([
factory.createTypeLiteralNode(
propertySignatures
),
factory.createTypeLiteralNode([
factory.createIndexSignature(
undefined,
undefined,
[factory.createParameterDeclaration(
undefined,
undefined,
undefined,
factory.createIdentifier("E"),
undefined,
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
undefined
)],
factory.createTypeReferenceNode(
factory.createIdentifier("EntityDef"),
undefined
)
)
])
])
) )
) );
); }
else {
statements.push(
factory.createTypeAliasDeclaration(
undefined,
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
factory.createIdentifier("EntityDict"),
undefined,
factory.createTypeLiteralNode(
propertySignatures
)
)
);
}
const resultFile = ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS); const resultFile = ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS);
const result = printer.printNode( const result = printer.printNode(
@ -4152,7 +4203,7 @@ export function analyzeEntities(inputDir: string) {
); );
} }
export function buildSchema(outputDir: string): void { export function buildSchema(outputDir: string): void {
addReverseRelationship(); addReverseRelationship();
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
resetOutputDir(outputDir); resetOutputDir(outputDir);
@ -4161,7 +4212,7 @@ export function buildSchema(outputDir: string): void {
outputAction(outputDir, printer); outputAction(outputDir, printer);
outputEntityDict(outputDir, printer); outputEntityDict(outputDir, printer);
outputStorage(outputDir, printer); outputStorage(outputDir, printer);
if (!process.env.TARGET_IN_OAK_DOMAIN) { if (!process.env.TARGET_IN_OAK_DOMAIN) {
outputPackageJson(outputDir); outputPackageJson(outputDir);
} }

View File

@ -1,12 +1,10 @@
import { EntityDef, EntityShape } from './Entity'; import { EntityDef, EntityDict } from './Entity';
import { RowStore } from './RowStore'; import { RowStore } from './RowStore';
import { Schema as Application } from '../entities/Application'; import { Schema as Application } from '../base-domain/Application/Schema';
import { Schema as Token } from '../entities/Token'; import { Schema as Token } from '../base-domain/Token/Schema';
export interface Context<ED extends { export interface Context<ED extends EntityDict>{
[E: string]: EntityDef;
}>{
rowStore: RowStore<ED>; rowStore: RowStore<ED>;
on(event: 'commit' | 'rollback', callback: (context: Context<ED>) => Promise<void>): void; on(event: 'commit' | 'rollback', callback: (context: Context<ED>) => Promise<void>): void;
begin(options?: object): Promise<void>; begin(options?: object): Promise<void>;
@ -14,9 +12,7 @@ export interface Context<ED extends {
rollback():Promise<void>; rollback():Promise<void>;
}; };
export interface RunningContext<ED extends { export interface RunningContext<ED extends EntityDict> extends Context<ED> {
[E: string]: EntityDef; getApplication: () => Application;
}> extends Context<ED> { getToken: () => Token | undefined;
application: Application;
token?: Token;
}; };

View File

@ -53,6 +53,10 @@ export interface EntityDef {
Operation: DeduceOperation<this['Schema']>; Operation: DeduceOperation<this['Schema']>;
}; };
export interface EntityDict {
[E: string]: EntityDef;
};
type DeduceProjection<SH extends EntityShape> = Partial<{ type DeduceProjection<SH extends EntityShape> = Partial<{
'#id': NodeId; '#id': NodeId;
} & { } & {
@ -98,9 +102,7 @@ export type DeduceRemoveOperation<SH extends EntityShape> = Operation<'remove',
export type DeduceOperation<SH extends EntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>; export type DeduceOperation<SH extends EntityShape> = DeduceCreateOperation<SH> | DeduceUpdateOperation<SH> | DeduceRemoveOperation<SH> | DeduceSelection<SH>;
export interface OperationResult<ED extends { export interface OperationResult<ED extends EntityDict> {
[K: string]: EntityDef;
}> {
operations?: { // cud返回的结果select返回create operations?: { // cud返回的结果select返回create
[T in keyof ED]?: Array<ED[keyof ED]['Operation']>; [T in keyof ED]?: Array<ED[keyof ED]['Operation']>;
}; // create/update/remove返回的动作结果 }; // create/update/remove返回的动作结果
@ -112,9 +114,7 @@ export interface OperationResult<ED extends {
}>; }>;
}; };
export type SelectionResult<ED extends { export type SelectionResult<ED extends EntityDict, T extends keyof ED> = {
[K: string]: EntityDef;
}, T extends keyof ED> = {
result: Array<Partial<ED[T]['Schema'] & { result: Array<Partial<ED[T]['Schema'] & {
[A in ExpressionKey]?: any; [A in ExpressionKey]?: any;
}>>; }>>;