From 59b13b9d2ec52675e43e7574d05038d81199d9c9 Mon Sep 17 00:00:00 2001 From: Xc Date: Fri, 18 Mar 2022 08:19:07 +0800 Subject: [PATCH] actionResult --- lib/schema/CascadeStore.d.ts | 6 +++--- lib/schema/CascadeStore.js | 14 +++++++------- lib/schema/relation.js | 19 +++++++++++-------- lib/types/Entity.d.ts | 7 +++++-- lib/types/RowStore.d.ts | 4 ++-- lib/types/Storage.d.ts | 1 + lib/types/Storage.js | 2 ++ src/schema/CascadeStore.ts | 20 ++++++++++---------- src/schema/relation.ts | 20 +++++++++++--------- src/types/Entity.ts | 8 ++++++-- src/types/RowStore.ts | 4 ++-- src/types/Storage.ts | 1 + 12 files changed, 61 insertions(+), 45 deletions(-) diff --git a/lib/schema/CascadeStore.d.ts b/lib/schema/CascadeStore.d.ts index 3b3b486..a2bfe81 100644 --- a/lib/schema/CascadeStore.d.ts +++ b/lib/schema/CascadeStore.d.ts @@ -1,5 +1,5 @@ import { Context } from '../types/Context'; -import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, EntityDef, SelectionResult } from "../types/Entity"; +import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, EntityDef, OperateParams, SelectionResult } from "../types/Entity"; import { RowStore } from '../types/RowStore'; import { StorageSchema } from '../types/Storage'; /**这个用来处理级联的select和update,对不同能力的 */ @@ -8,7 +8,7 @@ export declare abstract class CascadeStore extends RowStore { constructor(storageSchema: StorageSchema); protected abstract selectAbjointRow(entity: T, selection: Omit, context: Context, params?: Object): Promise['result']>; - protected abstract updateAbjointRow(entity: T, operation: DeduceCreateSingleOperation | DeduceUpdateOperation | DeduceRemoveOperation, context: Context, params?: Object): Promise; + protected abstract updateAbjointRow(entity: T, operation: DeduceCreateSingleOperation | DeduceUpdateOperation | DeduceRemoveOperation, context: Context, params?: OperateParams): Promise; protected cascadeSelect(entity: T, selection: ED[T]['Selection'], context: Context, params?: Object): Promise['result']>; /** * 级联更新 @@ -33,5 +33,5 @@ export declare abstract class CascadeStore(entity: T, operation: DeduceCreateOperation | DeduceUpdateOperation | DeduceRemoveOperation, context: Context, params?: Object): Promise; + protected cascadeUpdate(entity: T, operation: DeduceCreateOperation | DeduceUpdateOperation | DeduceRemoveOperation, context: Context, params?: OperateParams): Promise; } diff --git a/lib/schema/CascadeStore.js b/lib/schema/CascadeStore.js index f90f769..1fdf1ff 100644 --- a/lib/schema/CascadeStore.js +++ b/lib/schema/CascadeStore.js @@ -97,7 +97,7 @@ class CascadeStore extends RowStore_1.RowStore { const filter2 = data[attr]; const rows2 = await this.cascadeSelect(entity2, (0, lodash_1.assign)({}, filter2, { filter: (0, filter_1.addFilterSegment)({ - [`${foreignKey}Id`]: row.id, + [foreignKey]: row.id, }, filter2.filter), }), context, params); (0, lodash_1.assign)(row, { @@ -308,12 +308,12 @@ class CascadeStore extends RowStore_1.RowStore { const { id } = data2; if (dataOtm instanceof Array) { dataOtm.forEach(ele => (0, lodash_1.assign)(ele, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, })); } else { (0, lodash_1.assign)(dataOtm, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, }); } } @@ -323,19 +323,19 @@ class CascadeStore extends RowStore_1.RowStore { (0, assert_1.default)(typeof id === 'string'); if (dataOtm instanceof Array) { dataOtm.forEach(ele => (0, lodash_1.assign)(ele, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, })); } else { (0, lodash_1.assign)(dataOtm, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, }); } } else { (0, lodash_1.assign)(operationOtm, { filter: (0, filter_1.addFilterSegment)({ - [`${foreignKey}Id`]: { + [foreignKey]: { $in: { entity, data: { @@ -348,7 +348,7 @@ class CascadeStore extends RowStore_1.RowStore { }); if (action === 'remove' && actionOtm === 'update') { (0, lodash_1.assign)(dataOtm, { - [`${foreignKey}Id`]: null, + [foreignKey]: null, }); } } diff --git a/lib/schema/relation.js b/lib/schema/relation.js index 6d0e004..4100e62 100644 --- a/lib/schema/relation.js +++ b/lib/schema/relation.js @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.judgeRelation = void 0; const assert_1 = __importDefault(require("assert")); const Demand_1 = require("../types/Demand"); +const Storage_1 = require("../types/Storage"); /** * 判断对象和属性之间的关系 * @param schema @@ -22,9 +23,6 @@ function judgeRelation(schema, entity, attr) { } if (attributes.hasOwnProperty(attr)) { // 原生属性 - if (attributes[attr].type === 'ref') { - return attributes[attr].ref; // 直接外键关联 - } return 1; } if (attr.includes('$')) { @@ -36,11 +34,11 @@ function judgeRelation(schema, entity, attr) { // 基于反指对象的反向关联 return [entity2]; } - else if (attributes2.hasOwnProperty(foreignKey) - && attributes2[foreignKey].type === 'ref' - && attributes2[foreignKey].ref === entity) { + else if (attributes2.hasOwnProperty(`${foreignKey}Id`) + && attributes2[`${foreignKey}Id`].type === 'ref' + && attributes2[`${foreignKey}Id`].ref === entity) { // 基于外键的反向关联 - return [entity2, foreignKey]; + return [entity2, `${foreignKey}Id`]; } else { // 这种情况应该不会跑到 @@ -53,8 +51,13 @@ function judgeRelation(schema, entity, attr) { // 反向指针的外键 return 2; } + else if ((attributes.hasOwnProperty(`${attr}Id`))) { + const { type, ref } = attributes[`${attr}Id`]; + (0, assert_1.default)(type === 'ref'); + return ref; + } else { - // 派生属性 + (0, assert_1.default)(Storage_1.initinctiveAttributes.includes(attr), `${attr}属性找不到`); return 1; } } diff --git a/lib/types/Entity.d.ts b/lib/types/Entity.d.ts index 4e685fa..644eec5 100644 --- a/lib/types/Entity.d.ts +++ b/lib/types/Entity.d.ts @@ -10,6 +10,9 @@ declare type SelectOption = { forUpdate?: true; usingIndex?: 'todo'; }; +export declare type OperateParams = { + notCollect?: boolean; +}; export declare type FormUpdateData = { [A in keyof SH]?: any; } & { @@ -84,12 +87,12 @@ declare type UpdateOpResult = { a: 'u'; e: T; d: ED[T]['OpSchema']; - f?: DeduceFilter; + f?: DeduceFilter; }; declare type RemoveOpResult = { a: 'r'; e: T; - f?: DeduceFilter; + f?: DeduceFilter; }; declare type SelectOpResult = { a: 's'; diff --git a/lib/types/RowStore.d.ts b/lib/types/RowStore.d.ts index a8abf12..b98a3f5 100644 --- a/lib/types/RowStore.d.ts +++ b/lib/types/RowStore.d.ts @@ -1,4 +1,4 @@ -import { EntityDef, OperationResult, SelectionResult } from './Entity'; +import { EntityDef, OperationResult, SelectionResult, OperateParams } from './Entity'; import { Context } from './Context'; import { StorageSchema } from './Storage'; import { OakErrorDefDict } from '../OakError'; @@ -8,7 +8,7 @@ export declare abstract class RowStore; - abstract operate(entity: T, operation: ED[T]['Operation'], context: Context, params?: Object): Promise>; + abstract operate(entity: T, operation: ED[T]['Operation'], context: Context, params?: OperateParams): Promise>; abstract select(entity: T, selection: ED[T]['Selection'], context: Context, params?: Object): Promise>; abstract count(entity: T, selection: Omit, context: Context, params?: Object): Promise; constructor(storageSchema: StorageSchema); diff --git a/lib/types/Storage.d.ts b/lib/types/Storage.d.ts index 2253f55..b35c1cb 100644 --- a/lib/types/Storage.d.ts +++ b/lib/types/Storage.d.ts @@ -4,6 +4,7 @@ import { TriggerDataAttribute, TriggerTimestampAttribute } from './Trigger'; export declare type Ref = 'ref'; declare type PrimaryKeyAttribute = 'id'; declare type InstinctiveAttributes = PrimaryKeyAttribute | '$$createAt$$' | '$$updateAt$$' | '$$removeAt$$' | TriggerDataAttribute | TriggerTimestampAttribute; +export declare const initinctiveAttributes: string[]; export interface Column { name: keyof SH; size?: number; diff --git a/lib/types/Storage.js b/lib/types/Storage.js index c8ad2e5..a4b7afe 100644 --- a/lib/types/Storage.js +++ b/lib/types/Storage.js @@ -1,2 +1,4 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +exports.initinctiveAttributes = void 0; +exports.initinctiveAttributes = ['id', '$$createAt$$', '$$updateAt$$', '$$removeAt$$', '$$triggerData$$', '$$triggerTimestamp$$']; diff --git a/src/schema/CascadeStore.ts b/src/schema/CascadeStore.ts index ab7b6f5..c31faca 100644 --- a/src/schema/CascadeStore.ts +++ b/src/schema/CascadeStore.ts @@ -1,7 +1,7 @@ import assert from "assert"; import { assign } from "lodash"; import { Context } from '../types/Context'; -import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceFilter, DeduceRemoveOperation, DeduceSelection, DeduceUpdateOperation, EntityDef, EntityShape, SelectionResult } from "../types/Entity"; +import { DeduceCreateOperation, DeduceCreateSingleOperation, DeduceFilter, DeduceRemoveOperation, DeduceSelection, DeduceUpdateOperation, EntityDef, EntityShape, OperateParams, SelectionResult } from "../types/Entity"; import { RowStore } from '../types/RowStore'; import { StorageSchema } from '../types/Storage'; import { addFilterSegment } from "./filter"; @@ -24,7 +24,7 @@ export abstract class CascadeStore | DeduceUpdateOperation | DeduceRemoveOperation, context: Context, - params?: Object): Promise; + params?: OperateParams): Promise; protected async cascadeSelect( entity: T, @@ -115,7 +115,7 @@ export abstract class CascadeStore | DeduceUpdateOperation | DeduceRemoveOperation, context: Context, - params?: Object): Promise { + params?: OperateParams): Promise { const { action, data, filter } = operation; const opData = {}; @@ -341,13 +341,13 @@ export abstract class CascadeStore assign(ele, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, }) ); } else { assign(dataOtm, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, }); } } @@ -358,20 +358,20 @@ export abstract class CascadeStore assign(ele, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, }) ); } else { assign(dataOtm, { - [`${foreignKey}Id`]: id, + [foreignKey]: id, }); } } else { assign(operationOtm, { filter: addFilterSegment({ - [`${foreignKey}Id`]: { + [foreignKey]: { $in: { entity, data: { @@ -384,7 +384,7 @@ export abstract class CascadeStore = { [A in keyof SH]?: any; } & { id?: undefined }; @@ -112,13 +116,13 @@ type UpdateOpResult = { a: 'u', e: T; d: ED[T]['OpSchema']; - f?: DeduceFilter; + f?: DeduceFilter; }; type RemoveOpResult = { a: 'r', e: T; - f?: DeduceFilter; + f?: DeduceFilter; }; type SelectOpResult = { diff --git a/src/types/RowStore.ts b/src/types/RowStore.ts index 18f4ed5..4ad8a41 100644 --- a/src/types/RowStore.ts +++ b/src/types/RowStore.ts @@ -1,4 +1,4 @@ -import { EntityDef, OperationResult, SelectionResult, EntityShape } from './Entity'; +import { EntityDef, OperationResult, SelectionResult, EntityShape, OperateParams } from './Entity'; import { Context } from './Context'; import { StorageSchema } from './Storage'; import { OakErrorDefDict } from '../OakError'; @@ -18,7 +18,7 @@ export abstract class RowStore, - params?: Object + params?: OperateParams ): Promise>; abstract select ( diff --git a/src/types/Storage.ts b/src/types/Storage.ts index 1f75411..3f4c74d 100644 --- a/src/types/Storage.ts +++ b/src/types/Storage.ts @@ -5,6 +5,7 @@ export type Ref = 'ref'; type PrimaryKeyAttribute = 'id'; type InstinctiveAttributes = PrimaryKeyAttribute | '$$createAt$$' | '$$updateAt$$' | '$$removeAt$$' | TriggerDataAttribute | TriggerTimestampAttribute; +export const initinctiveAttributes = ['id', '$$createAt$$', '$$updateAt$$', '$$removeAt$$', '$$triggerData$$', '$$triggerTimestamp$$']; export interface Column { name: keyof SH,