/
wrap.ts
36 lines (30 loc) · 1.4 KB
/
wrap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { Dictionary, IWrappedEntity, IWrappedEntityInternal } from '../typings';
/**
* returns WrappedEntity instance associated with this entity. This includes all the internal properties like `__meta` or `__em`.
*/
export function wrap<T extends object>(entity: T, preferHelper: true): IWrappedEntityInternal<T>;
/**
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
*/
export function wrap<T extends object>(entity: T, preferHelper?: false): IWrappedEntity<T>;
/**
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
* use `preferHelper = true` to have access to the internal `__` properties like `__meta` or `__em`
*/
export function wrap<T extends object>(entity: T & Dictionary, preferHelper = false): IWrappedEntity<T> | IWrappedEntityInternal<T> {
if (!entity) {
return entity;
}
if (entity.__baseEntity && !preferHelper) {
return entity as unknown as IWrappedEntity<T>;
}
return entity.__helper ?? entity;
}
/**
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
* use `preferHelper = true` to have access to the internal `__` properties like `__meta` or `__em`
* @internal
*/
export function helper<T extends object>(entity: T): IWrappedEntityInternal<T> {
return (entity as Dictionary).__helper;
}