summary refs log tree commit diff
path: root/util/src/models/BaseClass.ts
blob: 78cd329ce578d285356a17e019da739f71525ba8 (plain) (blame)
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
import "reflect-metadata";
import { BaseEntity, Column } from "typeorm";

export class BaseClass extends BaseEntity {
	@Column()
	id?: string;

	constructor(props?: any) {
		super();
		BaseClass.assign(props, this, "body.");
	}

	private static assign(props: any, object: any, path?: string): any {
		const expectedType = Reflect.getMetadata("design:type", object, props);
		console.log(expectedType, object, props, path, typeof object);

		if (typeof object !== typeof props) throw new Error(`Property at ${path} must be`);
		if (typeof object === "object")
			return Object.keys(object).map((key) => BaseClass.assign(props[key], object[key], `${path}.${key}`));
	}
}

// @ts-ignore
global.BaseClass = BaseClass;

var test = new BaseClass({});

setTimeout(() => {}, 10000 * 1000);