Skip to content

Commit

Permalink
fix(mikro): run toJSON on reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Mar 5, 2022
1 parent bdc94a0 commit a5e2a67
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 35 deletions.
4 changes: 2 additions & 2 deletions packages/mikro/src/lib/mikro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
} from '@automapper/classes';
import type { Dictionary, MapPluginInitializer } from '@automapper/core';
import { createInitialMapping, MappingClassId } from '@automapper/core';
import { Utils } from '@mikro-orm/core';
import { MikroInitializerOptions } from './types';
import {
instantiate,
isEntity,
serializeEntity as defaultSerializeEntity,
} from './utils';

Expand Down Expand Up @@ -148,7 +148,7 @@ export const mikro: (
sourceArr: TSource[]
) {
return sourceArr.map((item) => {
if (isEntity(item)) {
if (Utils.isEntity(item)) {
return this.instantiate(source, item)[0];
}
return item;
Expand Down
7 changes: 3 additions & 4 deletions packages/mikro/src/lib/utils/get-entity.util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { isEntity } from './is-entity.util';
import { isReference } from './is-reference.util';
import { Reference, Utils } from '@mikro-orm/core';
import { serializeEntity } from './serialize-entity.util';

export function getEntity<TValue = any>(value: TValue): TValue {
if (isEntity(value)) {
if (Utils.isEntity(value)) {
return serializeEntity(value) as TValue;
}

if (isReference(value)) {
if (Reference.isReference(value)) {
return serializeEntity(value.getEntity()) as TValue;
}

Expand Down
3 changes: 0 additions & 3 deletions packages/mikro/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './instantiate.util';
export * from './is-collection.util';
export * from './is-entity.util';
export * from './is-reference.util';
export * from './get-entity.util';
export * from './serialize-entity.util';
6 changes: 3 additions & 3 deletions packages/mikro/src/lib/utils/instantiate.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
isPrimitiveConstructor,
setMutate,
} from '@automapper/core';
import {Utils} from '@mikro-orm/core';
import { MikroInitializerOptions } from '../types';
import { getEntity } from './get-entity.util';
import { isCollection } from './is-collection.util';

/**
* Recursively instantiate a model with its metadata
Expand Down Expand Up @@ -93,7 +93,7 @@ export function instantiate<TModel extends Dictionary<TModel>>(
setMutate(
instance as Record<string, unknown>,
key,
isCollection(valueAtKey) ? valueAtKey.getSnapshot() : valueAtKey
Utils.isCollection(valueAtKey) ? valueAtKey.getSnapshot() : valueAtKey
);
continue;
}
Expand All @@ -103,7 +103,7 @@ export function instantiate<TModel extends Dictionary<TModel>>(
setMutate(
instance as Record<string, unknown>,
key,
isCollection(valueAtKey) ? valueAtKey.getSnapshot() : valueAtKey
Utils.isCollection(valueAtKey) ? valueAtKey.getSnapshot() : valueAtKey
);
continue;
}
Expand Down
7 changes: 0 additions & 7 deletions packages/mikro/src/lib/utils/is-collection.util.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/mikro/src/lib/utils/is-entity.util.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/mikro/src/lib/utils/is-reference.util.ts

This file was deleted.

14 changes: 8 additions & 6 deletions packages/mikro/src/lib/utils/serialize-entity.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AnyEntity } from '@mikro-orm/core';
import { isCollection } from './is-collection.util';
import { isEntity } from './is-entity.util';
import { AnyEntity, Reference, Utils } from '@mikro-orm/core';

const excluded = [
'__gettersDefined',
Expand All @@ -19,10 +17,14 @@ export function serializeEntity(item: AnyEntity) {
}

const value = item[key as string];
if (isCollection(value)) {
result[key] = (value.isInitialized(true) ? value.getItems() : value.getSnapshot()) || [];
} else if (isEntity(value)) {
if (Utils.isCollection(value)) {
result[key] =
(value.isInitialized(true) ? value.getItems() : value.getSnapshot()) ||
[];
} else if (Utils.isEntity(value)) {
result[key] = serializeEntity(value);
} else if (Reference.isReference(value)) {
result[key] = value.toJSON();
} else {
result[key] = value;
}
Expand Down

0 comments on commit a5e2a67

Please sign in to comment.