diff --git a/lib/base-app-domain/Oper/Storage.js b/lib/base-app-domain/Oper/Storage.js index 1932a85..b1ca871 100644 --- a/lib/base-app-domain/Oper/Storage.js +++ b/lib/base-app-domain/Oper/Storage.js @@ -38,5 +38,19 @@ exports.desc = { } }, actionType: "appendOnly", - actions: action_1.appendOnlyActions + actions: action_1.appendOnlyActions, + indexes: [ + { + name: 'index_bornAt_operatorId', + attributes: [ + { + name: 'bornAt', + direction: 'DESC', + }, + { + name: "operatorId", + }, + ] + } + ] }; diff --git a/lib/entities/Oper.js b/lib/entities/Oper.js index afbc0dc..dd9ea4a 100644 --- a/lib/entities/Oper.js +++ b/lib/entities/Oper.js @@ -18,5 +18,19 @@ const entityDesc = { }, configuration: { actionType: 'appendOnly', - } + }, + indexes: [ + { + name: 'index_bornAt_operatorId', + attributes: [ + { + name: 'bornAt', + direction: 'DESC', + }, + { + name: 'operator', + }, + ] + } + ] }; diff --git a/lib/store/CascadeStore.js b/lib/store/CascadeStore.js index f06d3a1..a156bc2 100644 --- a/lib/store/CascadeStore.js +++ b/lib/store/CascadeStore.js @@ -1017,7 +1017,7 @@ class CascadeStore extends RowStore_1.RowStore { * @param option */ async doUpdateSingleRowAsync(entity, operation, context, option) { - const { data, action, id: operId, filter } = operation; + const { data, action, id: operId, filter, bornAt } = operation; const now = Date.now(); switch (action) { case 'create': { @@ -1161,6 +1161,7 @@ class CascadeStore extends RowStore_1.RowStore { data, operatorId, targetEntity: entity, + bornAt, operEntity$oper: data instanceof Array ? { id: 'dummy', action: 'create', @@ -1313,6 +1314,7 @@ class CascadeStore extends RowStore_1.RowStore { action, data, targetEntity: entity, + bornAt, operEntity$oper: { id: 'dummy', action: 'create', @@ -1350,7 +1352,7 @@ class CascadeStore extends RowStore_1.RowStore { if (updateAttrCount > 0) { // 优化一下,如果不更新任何属性,则不实际执行 Object.assign(data, { - $$updateAt$$: now, + [Entity_1.UpdateAtAttribute]: now, }); if (!option.dontCollect) { context.saveOpRecord(entity, { diff --git a/lib/triggers/index.d.ts b/lib/triggers/index.d.ts index 044fcd1..9b9ea0a 100644 --- a/lib/triggers/index.d.ts +++ b/lib/triggers/index.d.ts @@ -1,5 +1,5 @@ import { EntityDict as BaseEntityDict } from '../base-app-domain'; -import { StorageSchema, EntityDict } from '../types'; +import { StorageSchema, EntityDict, Trigger } from '../types'; import { AsyncContext } from '../store/AsyncRowStore'; import { SyncContext } from '../store/SyncRowStore'; -export declare function createDynamicTriggers | SyncContext>(schema: StorageSchema): import("../types").Trigger[]; +export declare function createDynamicTriggers | SyncContext>(schema: StorageSchema): Trigger[]; diff --git a/lib/triggers/index.js b/lib/triggers/index.js index bcfbd87..b9470ea 100644 --- a/lib/triggers/index.js +++ b/lib/triggers/index.js @@ -1,8 +1,28 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createDynamicTriggers = void 0; +const tslib_1 = require("tslib"); +const assert_1 = tslib_1.__importDefault(require("assert")); const modi_1 = require("../store/modi"); +function createOperTriggers() { + return [ + { + name: 'assign initial bornAt for local oper', + entity: 'oper', + action: 'create', + when: 'before', + fn({ operation }) { + const { data } = operation; + (0, assert_1.default)(!(data instanceof Array) && data.$$createAt$$); + if (!data.bornAt) { + data.bornAt = data.$$createAt$$; + } + return 1; + } + } + ]; +} function createDynamicTriggers(schema) { - return (0, modi_1.createModiRelatedTriggers)(schema); + return (0, modi_1.createModiRelatedTriggers)(schema).concat(createOperTriggers()); } exports.createDynamicTriggers = createDynamicTriggers; diff --git a/src/entities/Oper.ts b/src/entities/Oper.ts index b12ff1d..76c017d 100644 --- a/src/entities/Oper.ts +++ b/src/entities/Oper.ts @@ -30,5 +30,19 @@ const entityDesc: EntityDesc = { }, configuration: { actionType: 'appendOnly', - } + }, + indexes: [ + { + name: 'index_bornAt_operatorId', + attributes: [ + { + name: 'bornAt', + direction: 'DESC', + }, + { + name: 'operator', + }, + ] + } + ] }; diff --git a/src/store/CascadeStore.ts b/src/store/CascadeStore.ts index d3a6d7a..c7a074a 100644 --- a/src/store/CascadeStore.ts +++ b/src/store/CascadeStore.ts @@ -1277,7 +1277,7 @@ export abstract class CascadeStore exten context: Cxt, option: OP ): Promise> { - const { data, action, id: operId, filter } = operation; + const { data, action, id: operId, filter, bornAt } = operation; const now = Date.now(); switch (action) { @@ -1429,6 +1429,7 @@ export abstract class CascadeStore exten data, operatorId, targetEntity: entity as string, + bornAt, operEntity$oper: data instanceof Array ? { id: 'dummy', action: 'create', @@ -1590,6 +1591,7 @@ export abstract class CascadeStore exten action, data, targetEntity: entity as string, + bornAt, operEntity$oper: { id: 'dummy', action: 'create', @@ -1631,7 +1633,7 @@ export abstract class CascadeStore exten if (updateAttrCount > 0) { // 优化一下,如果不更新任何属性,则不实际执行 Object.assign(data, { - $$updateAt$$: now, + [UpdateAtAttribute]: now, }); if (!option.dontCollect) { context.saveOpRecord(entity, { diff --git a/src/triggers/index.ts b/src/triggers/index.ts index ccf5c4e..d17c004 100644 --- a/src/triggers/index.ts +++ b/src/triggers/index.ts @@ -1,9 +1,29 @@ +import assert from 'assert'; import { EntityDict as BaseEntityDict } from '../base-app-domain'; -import { StorageSchema, EntityDict } from '../types'; +import { StorageSchema, EntityDict, Trigger, CreateTrigger } from '../types'; import { createModiRelatedTriggers } from '../store/modi'; import { AsyncContext } from '../store/AsyncRowStore'; import { SyncContext } from '../store/SyncRowStore'; -export function createDynamicTriggers | SyncContext>(schema: StorageSchema) { - return createModiRelatedTriggers(schema); +function createOperTriggers | SyncContext>() { + return [ + { + name: 'assign initial bornAt for local oper', + entity: 'oper', + action: 'create', + when: 'before', + fn({ operation }) { + const { data } = operation; + assert(!(data instanceof Array) && data.$$createAt$$); + if (!data.bornAt) { + data.bornAt = data.$$createAt$$; + } + return 1; + } + } as CreateTrigger + ] as Trigger[]; +} + +export function createDynamicTriggers | SyncContext>(schema: StorageSchema) { + return createModiRelatedTriggers(schema).concat(createOperTriggers()); }