ivanovna.orm/src/app/data.ts

51 lines
1.2 KiB
TypeScript

import rfdc from 'rfdc'
type ValuesObject = Record<string, any>
export abstract class Data {
[key: string]: any
static assign(vo: Data, values: ValuesObject): Data {
for (const key of Object.getOwnPropertyNames(values)) {
vo[key] = values[key]
}
return vo
}
set(key: string, value: any) {
this[key] = value
}
fromObject(object: ValuesObject): void {
for (const key of Object.getOwnPropertyNames(object)) {
if (key === '_id') continue
this[key] = object[key]
}
}
toObject(exclude: string[] = []): Record<string, any> {
const object = {} as ValuesObject
for (const key of Object.getOwnPropertyNames(this)) {
if (key.startsWith('__')) {
continue
}
if (exclude.includes(key)) continue
const type = typeof this[key]
if (type === 'object') {
object[key] = rfdc()(this[key])
} else if (['function', 'undefined'].includes(type)) {
// ...
} else {
object[key] = this[key]
}
}
return object
}
abstract uniqKey(): string
}