Files

47 lines
1010 B
TypeScript

import { assign, cloneDeep } from 'lodash';
import { privateSetKey, privateSet } from './modelFriends';
export type ModelAttributes = Record<string, any> & { id: string };
export abstract class Model<T extends ModelAttributes> {
private _attributes: T;
constructor(attributes: T) {
this._attributes = attributes;
}
get id(): string {
return this._attributes.id;
}
public get<K extends keyof T>(key: K): T[K] {
return this._attributes[key];
}
[privateSetKey]<K extends keyof T>(key: K, value: T[K] | undefined) {
const toSet: Partial<T> = {};
toSet[key] = value;
this[privateSet](toSet);
return this;
}
[privateSet](attrs: Partial<T>) {
this._attributes = assign(this._attributes, attrs);
return this;
}
get attributes(): Readonly<T> {
return this._attributes;
}
public setId(newId: string) {
if (!this.id) {
this._attributes.id = newId;
}
}
public cloneAttributes() {
return cloneDeep(this._attributes);
}
}