-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
BaseEntity.ts
61 lines (47 loc) · 2.24 KB
/
BaseEntity.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Reference } from './Reference';
import type { AutoPath, Ref, EntityData, EntityDTO, Loaded, AddEager, LoadedReference, EntityKey } from '../typings';
import type { AssignOptions } from './EntityAssigner';
import { EntityAssigner } from './EntityAssigner';
import type { EntityLoaderOptions } from './EntityLoader';
import { helper } from './wrap';
export abstract class BaseEntity {
isInitialized(): boolean {
return helper(this).__initialized;
}
isTouched(): boolean {
return helper(this).__touched;
}
populated(populated = true): void {
helper(this).populated(populated);
}
async populate<This extends this, Hint extends string = never>(
populate: AutoPath<This, Hint>[] | boolean,
options: EntityLoaderOptions<This, Hint> = {},
): Promise<Loaded<This, Hint>> {
return helper(this as This).populate(populate, options);
}
toReference<Entity extends this = this>(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>> {
return Reference.create(this) as unknown as Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
}
toObject<Entity extends this = this, Ignored extends EntityKey<Entity> = never>(ignoreFields: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
toObject<Entity extends this = this>(...args: unknown[]): EntityDTO<Entity>;
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);
}
toPOJO<Entity extends this = this>(): EntityDTO<Entity> {
return helper(this as unknown as Entity).toPOJO();
}
assign<Entity extends this = this>(data: EntityData<Entity>, options?: AssignOptions): Entity {
return EntityAssigner.assign(this as Entity, data, options);
}
init<Entity extends this = this, Populate extends string = never>(populated = true): Promise<Loaded<Entity, Populate>> {
return helper(this as Entity).init<Populate>(populated);
}
getSchema(): string | undefined {
return helper(this).getSchema();
}
setSchema(schema?: string): void {
helper(this).setSchema(schema);
}
}
Object.defineProperty(BaseEntity.prototype, '__baseEntity', { value: true, writable: false, enumerable: false });