2023-07-18 11:36:17 +08:00
|
|
|
import { Model } from '@nocobase/database';
|
|
|
|
|
|
|
|
export function toJSON(data: Model | Model[]): object {
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
return data.map(toJSON);
|
|
|
|
}
|
2023-10-09 17:48:20 +08:00
|
|
|
if (!(data instanceof Model) || !data) {
|
|
|
|
return data;
|
|
|
|
}
|
2023-07-18 11:36:17 +08:00
|
|
|
const result = data.get();
|
|
|
|
Object.keys((<typeof Model>data.constructor).associations).forEach((key) => {
|
2023-10-09 17:48:20 +08:00
|
|
|
if (result[key] != null && typeof result[key] === 'object') {
|
2023-07-18 11:36:17 +08:00
|
|
|
result[key] = toJSON(result[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|