Skip to content

Commit

Permalink
fix: interceptor stringifyEntities logic
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Jan 4, 2022
1 parent 6c529c2 commit f6b40e4
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions source/orm/orm.interceptor/orm.entity.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,37 @@ export class OrmEntityManager implements NestInterceptor {
* @param data
*/
private stringifyEntities(data: any): any {
let stringifiedData;
if (!data) return;

// Array of entities
if (Array.isArray(data)) {
data = data.map((d) => d?.toJSON ? d.toJSON() : d);
stringifiedData = data.map((d) => d?.toJSON ? d.toJSON() : d);

for (const d of data) {
const dataItem = d as { id: string | number };
this.eliminateRecursion(dataItem.id, d);
for (const dataItem of stringifiedData) {
this.eliminateRecursion(dataItem['id'] as string | number, dataItem);
}
}

// Paginated entity
if (data.records && Array.isArray(data.records)) {
data.records = data.records.map((d) => d?.toJSON ? d.toJSON() : d);

for (const d of data.records) {
const dataItem = d as { id: string | number };
this.eliminateRecursion(dataItem.id, d);
else if (data.records && Array.isArray(data.records)) {
stringifiedData = {
...data,
records: data.records.map((d) => d?.toJSON ? d.toJSON() : d),
};

for (const dataItem of stringifiedData.records) {
this.eliminateRecursion(dataItem['id'] as string | number, dataItem);
}
}

// Single entity
if (data.toJSON) {
const dataItem: { id: string | number } = data.toJSON();
this.eliminateRecursion(dataItem.id, data);
else if (data.toJSON) {
stringifiedData = data.toJSON();
this.eliminateRecursion(stringifiedData['id'] as string | number, stringifiedData);
}

return data;
return stringifiedData;
}

/**
Expand All @@ -106,7 +108,10 @@ export class OrmEntityManager implements NestInterceptor {
if (!data || !parentId || typeof data !== 'object') return;

if (Array.isArray(data)) {
for (const d of data) this.eliminateRecursion(parentId, d);
for (const dataItem of data) {
this.eliminateRecursion(parentId, dataItem);
}

return;
}

Expand Down

0 comments on commit f6b40e4

Please sign in to comment.