tachybase_todo/packages/plugins/@nocobase/plugin-workflow/src/server/utils.ts
Junyi f280dcfb00
fix(plugin-workflow): fix toJSON type check (#2772)
* fix(plugin-workflow): fix toJSON type check

* fix(plugin-workflow): fix type check logic order
2023-10-09 17:48:20 +08:00

18 lines
474 B
TypeScript

import { Model } from '@nocobase/database';
export function toJSON(data: Model | Model[]): object {
if (Array.isArray(data)) {
return data.map(toJSON);
}
if (!(data instanceof Model) || !data) {
return data;
}
const result = data.get();
Object.keys((<typeof Model>data.constructor).associations).forEach((key) => {
if (result[key] != null && typeof result[key] === 'object') {
result[key] = toJSON(result[key]);
}
});
return result;
}