2022-02-20 21:23:39 +08:00
|
|
|
import { JOB_STATUS } from "../constants";
|
|
|
|
import FlowNodeModel from "../models/FlowNode";
|
|
|
|
|
|
|
|
export default {
|
2022-06-24 23:28:49 +08:00
|
|
|
async run(node: FlowNodeModel, input, processor) {
|
2022-02-20 21:23:39 +08:00
|
|
|
const {
|
|
|
|
collection,
|
2023-03-10 16:36:58 +08:00
|
|
|
params: { appends = [], ...params } = {}
|
2022-06-24 23:28:49 +08:00
|
|
|
} = node.config;
|
2022-02-20 21:23:39 +08:00
|
|
|
|
2023-03-10 16:36:58 +08:00
|
|
|
const { repository, model } = (<typeof FlowNodeModel>node.constructor).database.getCollection(collection);
|
2022-06-20 23:29:21 +08:00
|
|
|
const options = processor.getParsedValue(params);
|
2023-03-10 16:36:58 +08:00
|
|
|
const result = await repository.create({
|
2022-04-14 00:05:13 +08:00
|
|
|
...options,
|
2022-11-15 13:35:10 +08:00
|
|
|
context: {
|
|
|
|
executionId: processor.execution.id
|
|
|
|
},
|
2022-06-20 23:29:21 +08:00
|
|
|
transaction: processor.transaction
|
2022-02-20 21:23:39 +08:00
|
|
|
});
|
|
|
|
|
2023-03-24 23:54:07 +08:00
|
|
|
if (result && appends.length) {
|
2023-03-10 16:36:58 +08:00
|
|
|
const includeFields = appends.filter(field => !result.get(field) || !result[field]);
|
|
|
|
const included = await model.findByPk(result[model.primaryKeyAttribute], {
|
|
|
|
attributes: [model.primaryKeyAttribute],
|
|
|
|
include: includeFields,
|
|
|
|
transaction: processor.transaction
|
|
|
|
});
|
|
|
|
includeFields.forEach(field => {
|
2023-03-24 23:54:07 +08:00
|
|
|
const value = included!.get(field);
|
|
|
|
result.set(field, Array.isArray(value) ? value.map(item => item.toJSON()) : value.toJSON(), { raw: true });
|
2023-03-10 16:36:58 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-20 21:23:39 +08:00
|
|
|
return {
|
2022-05-13 13:26:49 +08:00
|
|
|
// NOTE: get() for non-proxied instance (#380)
|
2023-03-24 23:54:07 +08:00
|
|
|
result: result?.toJSON(),
|
2022-02-20 21:23:39 +08:00
|
|
|
status: JOB_STATUS.RESOLVED
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|