Skip to content

Commit

Permalink
fix(mikro): serialize entity properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Feb 25, 2022
1 parent b413af3 commit 4a18543
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"releaseName": "Release ${version}"
},
"hooks": {
"before:bump": "yarn test:all",
"after:bump": "yarn build:all"
"before:bump": "npm run test:all",
"after:bump": "npm run build:all"
}
}
26 changes: 22 additions & 4 deletions packages/mikro/src/lib/mikro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from '@automapper/classes';
import type { Dictionary, MapPluginInitializer } from '@automapper/core';
import { createInitialMapping, MappingClassId } from '@automapper/core';
import 'reflect-metadata';
import { instantiate, isEntity } from './utils';
import { AnyEntity } from '@mikro-orm/core';
import { instantiate, isCollection, isEntity } from './utils';

/**
*
Expand Down Expand Up @@ -131,9 +131,11 @@ export const mikro: MapPluginInitializer<Constructible> = (errorHandler) => {
sourceArr: TSource[]
) {
return sourceArr.map((item) => {
if (isEntity(item)) return item.toPOJO();
if (isEntity(item)) {
return mapEntity(item);
}
return item;
});
}) as TSource[];
},
dispose() {
metadataStorage.dispose();
Expand All @@ -142,3 +144,19 @@ export const mikro: MapPluginInitializer<Constructible> = (errorHandler) => {
},
};
};

function mapEntity(item: AnyEntity) {
const result = {} as Record<string | symbol, unknown>;
for (const key of Reflect.ownKeys(item)) {
const value = item[key as string];
if (isCollection(value)) {
result[key] = value.getSnapshot();
} else if (isEntity(value)) {
result[key] = mapEntity(value);
} else {
result[key] = value;
}
}

return result;
}

0 comments on commit 4a18543

Please sign in to comment.