Skip to content

Commit

Permalink
refactor: add missing serialize method to the ORM BaseEntity
Browse files Browse the repository at this point in the history
Closes #4301
  • Loading branch information
B4nan committed May 26, 2023
1 parent b40600d commit 3343b38
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/entity/BaseEntity.ts
Expand Up @@ -3,6 +3,8 @@ import type { AutoPath, Ref, EntityData, EntityDTO, Loaded, AddEager, LoadedRefe
import type { AssignOptions } from './EntityAssigner';
import { EntityAssigner } from './EntityAssigner';
import type { EntityLoaderOptions } from './EntityLoader';
import type { SerializeOptions } from '../serialization/EntitySerializer';
import { EntitySerializer } from '../serialization/EntitySerializer';
import { helper } from './wrap';

export abstract class BaseEntity {
Expand Down Expand Up @@ -34,11 +36,15 @@ export abstract class BaseEntity {
toObject<Entity extends this = this>(ignoreFields: never[]): EntityDTO<Entity>;
toObject<Entity extends this = this, Ignored extends EntityKey<Entity> = never>(ignoreFields: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
toObject<Entity extends this = this, Ignored extends EntityKey<Entity> = never>(ignoreFields?: Ignored[]): Omit<EntityDTO<Entity>, Ignored> {
return helper(this as unknown as Entity).toObject(ignoreFields!);
return helper(this as Entity).toObject(ignoreFields!);
}

toPOJO<Entity extends this = this>(): EntityDTO<Entity> {
return helper(this as unknown as Entity).toPOJO();
return helper(this as Entity).toPOJO();
}

serialize<Entity extends this = this, Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Entity, Hint, Exclude>): EntityDTO<Loaded<Entity, Hint>> {
return EntitySerializer.serialize(this as Entity, options);
}

assign<Entity extends this = this>(data: EntityData<Entity>, options?: AssignOptions): Entity {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/entity/WrappedEntity.ts
Expand Up @@ -149,7 +149,7 @@ export class WrappedEntity<Entity extends object> {

if (this.__meta.compositePK) {
return this.__meta.primaryKeys.reduce((ret, pk) => {
const child = this.entity[pk] as AnyEntity<Entity> | Primary<unknown>;
const child = this.entity[pk] as AnyEntity<Entity>;

if (Utils.isEntity(child, true)) {
const childPk = helper(child).getPrimaryKeys(convertCustomTypes);
Expand Down
5 changes: 2 additions & 3 deletions tests/entities-sql/FooBar2.ts
@@ -1,10 +1,9 @@
import { ManyToMany, Collection, Entity, Formula, JsonType, OneToOne, PrimaryKey, Property, OptionalProps } from '@mikro-orm/core';
import { BaseEntity22 } from './BaseEntity22';
import { BaseEntity, ManyToMany, Collection, Entity, Formula, JsonType, OneToOne, PrimaryKey, Property, OptionalProps } from '@mikro-orm/core';
import { FooBaz2 } from './FooBaz2';
import { Test2 } from './Test2';

@Entity()
export class FooBar2 extends BaseEntity22 {
export class FooBar2 extends BaseEntity {

[OptionalProps]?: 'version';

Expand Down
15 changes: 13 additions & 2 deletions tests/features/serialization/explicit-serialization.test.ts
@@ -1,7 +1,7 @@
import { wrap, serialize } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';
import { initORMPostgreSql } from '../../bootstrap';
import { Author2, Book2, Publisher2, PublisherType } from '../../entities-sql';
import { Author2, Book2, FooBar2, Publisher2, PublisherType } from '../../entities-sql';

let orm: MikroORM;

Expand Down Expand Up @@ -32,11 +32,22 @@ async function createEntities() {
return { god, author, publisher, book1, book2, book3 };
}

test('explicit serialization with ORM BaseEntity', async () => {
const fb = orm.em.create(FooBar2, { name: 'fb' });
await orm.em.flush();

const o1 = wrap(fb).serialize();
expect(o1).toMatchObject({
id: fb.id,
name: fb.name,
});
});

test('explicit serialization', async () => {
const { god, author, publisher, book1, book2, book3 } = await createEntities();
const jon = await orm.em.findOneOrFail(Author2, author, { populate: true })!;

const [o1] = serialize(jon);
const o1 = wrap(jon).serialize();
expect(o1).toMatchObject({
id: jon.id,
createdAt: jon.createdAt,
Expand Down

0 comments on commit 3343b38

Please sign in to comment.