Skip to content

Commit

Permalink
fix(core): allow hiding PKs in toObject()
Browse files Browse the repository at this point in the history
Closes #644
  • Loading branch information
B4nan committed Aug 9, 2020
1 parent c1025b9 commit 0a920dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/entity/EntityTransformer.ts
Expand Up @@ -14,7 +14,7 @@ export class EntityTransformer {
const ret = {} as EntityData<T>;

meta.primaryKeys
.filter(pk => !Utils.isDefined(entity[pk], true) || !meta.properties[pk].hidden)
.filter(pk => !Utils.isDefined(entity[pk], true) || !(meta.properties[pk].hidden || ignoreFields.includes(pk)))
.map(pk => [pk, Utils.getPrimaryKeyValue<T>(entity, [pk])] as [string, string])
.forEach(([pk, value]) => ret[platform.getSerializedPrimaryKeyField(pk) as keyof T] = platform.normalizePrimaryKey(value));

Expand Down
6 changes: 6 additions & 0 deletions tests/EntityHelper.mysql.test.ts
Expand Up @@ -94,6 +94,12 @@ describe('EntityHelperMySql', () => {
expect(jon.identities).toEqual(['3', '4']);
});

test(`toObject allows to hide PK (GH issue 644)`, async () => {
const bar = FooBar2.create('fb');
await orm.em.persistAndFlush(bar);
expect(wrap(bar).toObject(['id'])).not.toMatchObject({ id: bar.id, name: 'fb' });
});

test(`toObject handles recursion in 1:1`, async () => {
const bar = FooBar2.create('fb');
bar.baz = new FooBaz2('fz');
Expand Down

0 comments on commit 0a920dd

Please sign in to comment.