-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
兼容会导致多个.trigger.js监听同一个对象和事件时导致触发器重复执行,且本身.trigger.js不支持微服务模式,故.trigg…
- Loading branch information
Showing
2 changed files
with
96 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
/* | ||
* @Author: [email protected] | ||
* @Date: 2022-06-12 19:08:48 | ||
* @LastEditors: baozhoutao@steedos.com | ||
* @LastEditTime: 2023-05-30 09:52:23 | ||
* @LastEditors: 孙浩林 sunhaolin@steedos.com | ||
* @LastEditTime: 2024-04-25 11:22:11 | ||
* @Description: 加载*.trigger.js文件注册为新版action trigger | ||
*/ | ||
import * as _ from "underscore"; | ||
import * as path from "path"; | ||
import { JSONStringify, getMD5 } from "@steedos/metadata-core"; | ||
// import { JSONStringify, getMD5 } from "@steedos/metadata-core"; | ||
import { loadObjectTriggers } from "@steedos/metadata-registrar"; | ||
|
||
|
||
|
||
const TRIGGERKEYS = ['beforeFind', 'beforeInsert', 'beforeUpdate', 'beforeDelete', 'afterFind', 'afterInsert', 'afterUpdate', 'afterDelete', 'afterFindOne', 'afterCount'] | ||
// const TRIGGERKEYS = ['beforeFind', 'beforeInsert', 'beforeUpdate', 'beforeDelete', 'afterFind', 'afterInsert', 'afterUpdate', 'afterDelete', 'afterFindOne', 'afterCount'] | ||
|
||
function getObject(objectName: string) { | ||
try { | ||
const objectql = require('@steedos/objectql'); | ||
return objectql.getObject(objectName); | ||
} catch (error) { | ||
return null | ||
} | ||
} | ||
// function getObject(objectName: string) { | ||
// try { | ||
// const objectql = require('@steedos/objectql'); | ||
// return objectql.getObject(objectName); | ||
// } catch (error) { | ||
// return null | ||
// } | ||
// } | ||
|
||
export async function load(broker: any, packagePath: string, packageServiceName: string) { | ||
let actions = {}; | ||
let serviceName = `~triggers-${packageServiceName}`; | ||
// let actions = {}; | ||
// let serviceName = `~triggers-${packageServiceName}`; | ||
let filePath = path.join(packagePath, "**"); | ||
let objTriggers = loadObjectTriggers(filePath, packageServiceName); | ||
if (_.isEmpty(objTriggers)) { | ||
|
@@ -58,96 +58,96 @@ export async function load(broker: any, packagePath: string, packageServiceName: | |
} | ||
} | ||
*/ | ||
const actionTriggerName = getMD5(JSONStringify(trigger)); | ||
actions[actionTriggerName] = generateActionTrigger(trigger) | ||
// const actionTriggerName = getMD5(JSONStringify(trigger)); | ||
// actions[actionTriggerName] = generateActionTrigger(trigger) | ||
|
||
broker.emit('trigger.loaded', { | ||
objectName: trigger['listenTo'] | ||
}) | ||
} | ||
|
||
let serviceConfig = { | ||
name: serviceName, | ||
actions: actions | ||
}; | ||
let service = broker.createService(serviceConfig); | ||
if (!broker.started) { | ||
await broker._restartService(service) | ||
} | ||
// let serviceConfig = { | ||
// name: serviceName, | ||
// actions: actions | ||
// }; | ||
// let service = broker.createService(serviceConfig); | ||
// if (!broker.started) { | ||
// await broker._restartService(service) | ||
// } | ||
|
||
} | ||
|
||
// 生成action trigger | ||
function generateActionTrigger(trigger) { | ||
const when = []; | ||
for (const key in trigger) { | ||
if (Object.hasOwnProperty.call(trigger, key)) { | ||
if (_.contains(TRIGGERKEYS, key)) { | ||
when.push(key); | ||
} | ||
} | ||
} | ||
const actionTrigger = { | ||
trigger: { | ||
listenTo: trigger.listenTo, | ||
when: when | ||
}, | ||
async handler(ctx) { | ||
// 调用trigger.js的处理函数 | ||
const { | ||
isInsert, isUpdate, isDelete, isFind, isBefore, isAfter, isFindOne, isCount, | ||
id, doc, previousDoc, | ||
// size, | ||
userId, spaceId, objectName, query, data }: any = ctx.params; | ||
// function generateActionTrigger(trigger) { | ||
// const when = []; | ||
// for (const key in trigger) { | ||
// if (Object.hasOwnProperty.call(trigger, key)) { | ||
// if (_.contains(TRIGGERKEYS, key)) { | ||
// when.push(key); | ||
// } | ||
// } | ||
// } | ||
// const actionTrigger = { | ||
// trigger: { | ||
// listenTo: trigger.listenTo, | ||
// when: when | ||
// }, | ||
// async handler(ctx) { | ||
// // 调用trigger.js的处理函数 | ||
// const { | ||
// isInsert, isUpdate, isDelete, isFind, isBefore, isAfter, isFindOne, isCount, | ||
// id, doc, previousDoc, | ||
// // size, | ||
// userId, spaceId, objectName, query, data }: any = ctx.params; | ||
|
||
const context: any = { | ||
id, | ||
userId, | ||
spaceId, | ||
doc, | ||
previousDoc, | ||
query, | ||
data, | ||
objectName | ||
} | ||
// const context: any = { | ||
// id, | ||
// userId, | ||
// spaceId, | ||
// doc, | ||
// previousDoc, | ||
// query, | ||
// data, | ||
// objectName | ||
// } | ||
|
||
let when = '' | ||
if (isBefore) { | ||
if (isFind) { | ||
when = 'beforeFind' | ||
} else if (isInsert) { | ||
when = 'beforeInsert' | ||
} else if (isUpdate) { | ||
when = 'beforeUpdate' | ||
} else if (isDelete) { | ||
when = 'beforeDelete' | ||
} | ||
} | ||
else if (isAfter) { | ||
if (isFind) { | ||
when = 'afterFind' | ||
} else if (isInsert) { | ||
when = 'afterInsert' | ||
} else if (isUpdate) { | ||
when = 'afterUpdate' | ||
} else if (isDelete) { | ||
when = 'afterDelete' | ||
} else if (isFindOne) { | ||
when = 'afterFindOne' | ||
} else if (isCount) { | ||
when = 'afterCount' | ||
} | ||
} | ||
// let when = '' | ||
// if (isBefore) { | ||
// if (isFind) { | ||
// when = 'beforeFind' | ||
// } else if (isInsert) { | ||
// when = 'beforeInsert' | ||
// } else if (isUpdate) { | ||
// when = 'beforeUpdate' | ||
// } else if (isDelete) { | ||
// when = 'beforeDelete' | ||
// } | ||
// } | ||
// else if (isAfter) { | ||
// if (isFind) { | ||
// when = 'afterFind' | ||
// } else if (isInsert) { | ||
// when = 'afterInsert' | ||
// } else if (isUpdate) { | ||
// when = 'afterUpdate' | ||
// } else if (isDelete) { | ||
// when = 'afterDelete' | ||
// } else if (isFindOne) { | ||
// when = 'afterFindOne' | ||
// } else if (isCount) { | ||
// when = 'afterCount' | ||
// } | ||
// } | ||
|
||
if (when) { | ||
const object = getObject(objectName); | ||
if(!object){ | ||
return ; | ||
} | ||
await object.runTriggers(when, context); | ||
return context; | ||
} | ||
} | ||
}; | ||
return actionTrigger; | ||
} | ||
// if (when) { | ||
// const object = getObject(objectName); | ||
// if(!object){ | ||
// return ; | ||
// } | ||
// await object.runTriggers(when, context); | ||
// return context; | ||
// } | ||
// } | ||
// }; | ||
// return actionTrigger; | ||
// } |