Skip to content

Commit

Permalink
Allow not existing properties in BaseEntity.assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Adamek committed Jun 8, 2018
1 parent 55ab40f commit 30b6b43
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/BaseEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class BaseEntity {
const props = meta.properties;

Object.keys(data).forEach(prop => {
if (props[prop].reference === ReferenceType.MANY_TO_ONE && data[prop]) {
if (props[prop] && props[prop].reference === ReferenceType.MANY_TO_ONE && data[prop]) {
if (data[prop] instanceof BaseEntity) {
return this[prop] = data[prop];
}
Expand All @@ -66,7 +66,7 @@ export class BaseEntity {
}
}

const isCollection = [ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(props[prop].reference);
const isCollection = props[prop] && [ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(props[prop].reference);

if (isCollection && Utils.isArray(data[prop])) {
const items = data[prop].map((item: any) => {
Expand Down
3 changes: 2 additions & 1 deletion tests/BaseEntity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ describe('BaseEntity', () => {
await orm.em.persist(book);
expect(book.title).toBe('Book');
expect(book.author).toBe(jon);
book.assign({ title: 'Better Book 1', author: god });
book.assign({ title: 'Better Book 1', author: god, notExisting: true });
expect(book.author).toBe(god);
expect(book.notExisting).toBe(true);
await orm.em.persist(god);
book.assign({ title: 'Better Book 2', author: god.id });
expect(book.author).toBe(god);
Expand Down

0 comments on commit 30b6b43

Please sign in to comment.