Skip to content

Commit

Permalink
feat(core): allow using destructing assignments in entity ctors
Browse files Browse the repository at this point in the history
Previously when entity had ctor params that were either unnamed (like destructing assignments)
or not matching some property name, `em.create()` would simple not provide any param values to
the ctor.

With this change, we now provide the full `data` object in such cases.

Closes #781
  • Loading branch information
B4nan committed Aug 27, 2020
1 parent 50bb457 commit 06a5490
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/entity/EntityFactory.ts
@@ -1,4 +1,4 @@
import { Utils } from '../utils';
import { ObjectBindingPattern, Utils } from '../utils';
import { Dictionary, EntityData, EntityMetadata, EntityName, EntityProperty, New, Populate, Primary } from '../typings';
import { UnitOfWork } from '../unit-of-work';
import { ReferenceType } from './enums';
Expand Down Expand Up @@ -138,6 +138,10 @@ export class EntityFactory {
return this.createReference(meta.properties[k].type, data[k]);
}

if (!(k in data) || k as unknown === ObjectBindingPattern) {
return data;
}

return data[k];
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/Utils.ts
Expand Up @@ -13,6 +13,8 @@ import { Collection, ReferenceType } from '../entity';
import { Platform } from '../platforms';
import { GroupOperator, QueryOperator } from '../enums';

export const ObjectBindingPattern = Symbol('ObjectBindingPattern');

export class Utils {

/**
Expand Down Expand Up @@ -241,6 +243,10 @@ export class Utils {
ret.push(...params.map((p: any) => {
switch (p.type) {
case 'BindingElement':
if (p.left.type === 'ObjectBindingPattern') {
return ObjectBindingPattern;
}

return p.left.name;
case 'BindingRestElement':
return '...' + p.argument.name;
Expand Down
12 changes: 11 additions & 1 deletion tests/issues/GH610.test.ts
@@ -1,4 +1,4 @@
import { Entity, PrimaryKey, ManyToOne, IdentifiedReference, Property, MikroORM, wrap, Logger } from '@mikro-orm/core';
import { Entity, PrimaryKey, ManyToOne, IdentifiedReference, Property, MikroORM, wrap, Logger, ObjectBindingPattern } from '@mikro-orm/core';
import { AbstractSqlDriver, SchemaGenerator } from '@mikro-orm/knex';

@Entity()
Expand All @@ -13,6 +13,10 @@ export class Test {
@ManyToOne(() => Test, { nullable: true, wrappedReference: true })
rootNode?: IdentifiedReference<Test>;

constructor({ name }: Partial<Test> = {}) {
this.name = name!;
}

}

describe('GH issue 610', () => {
Expand Down Expand Up @@ -47,4 +51,10 @@ describe('GH issue 610', () => {
expect(mock.mock.calls[3][0]).toMatch('commit');
});

test('GH issue 781', async () => {
expect(orm.em.getMetadata().get('Test').constructorParams[0]).toBe(ObjectBindingPattern);
const t1 = orm.em.create(Test, { name: 't1' });
expect(t1.name).toBe('t1');
});

});

0 comments on commit 06a5490

Please sign in to comment.