From 06a54908db89b9f438d8ce9308045a6c383b8120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Thu, 27 Aug 2020 09:14:53 +0200 Subject: [PATCH] feat(core): allow using destructing assignments in entity ctors 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 --- packages/core/src/entity/EntityFactory.ts | 6 +++++- packages/core/src/utils/Utils.ts | 6 ++++++ tests/issues/GH610.test.ts | 12 +++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/core/src/entity/EntityFactory.ts b/packages/core/src/entity/EntityFactory.ts index 43321c1c2423..effb7ffa5b91 100644 --- a/packages/core/src/entity/EntityFactory.ts +++ b/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'; @@ -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]; }); } diff --git a/packages/core/src/utils/Utils.ts b/packages/core/src/utils/Utils.ts index 8c1d34f2a02a..bcbcb58738f5 100644 --- a/packages/core/src/utils/Utils.ts +++ b/packages/core/src/utils/Utils.ts @@ -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 { /** @@ -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; diff --git a/tests/issues/GH610.test.ts b/tests/issues/GH610.test.ts index 7ee2b7ad24c0..d3ff8be8f83c 100644 --- a/tests/issues/GH610.test.ts +++ b/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() @@ -13,6 +13,10 @@ export class Test { @ManyToOne(() => Test, { nullable: true, wrappedReference: true }) rootNode?: IdentifiedReference; + constructor({ name }: Partial = {}) { + this.name = name!; + } + } describe('GH issue 610', () => { @@ -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'); + }); + });