better persistent model, fixes

This commit is contained in:
Timo Hocker 2020-05-05 11:37:48 +02:00
parent ac3296387e
commit 9895e1d386
3 changed files with 41 additions and 35 deletions

View File

@ -5,35 +5,37 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import { Assignable } from './interfaces/Assignable';
import { Persistent } from './Persistent';
export abstract class ControlModel extends Assignable {
public constructor (obj: Record<string, string|number|boolean>) {
export abstract class ControlModel extends Persistent {
public constructor (obj?: Record<string, string|number|boolean>) {
super ();
this.data = obj as Record<string, string|number|boolean>;
}
public apply_object (obj: Record<string, unknown>): void {
for (const key of Object.keys (obj)) {
for (const prop of Object.keys (this.properties)) {
if ([
'string',
'number',
'boolean'
].indexOf (typeof obj[key]) > -1)
this.data[key] = obj[key];
].indexOf (this.properties[prop]) < 0) {
throw new Error (
'property types have to be either string, number or boolean'
);
}
}
if (typeof obj !== 'undefined')
this.assign_object (obj);
}
public get_data (): Record<string, string|number|boolean> {
return super.get_data () as Record<string, string|number|boolean>;
public to_object (): Record<string, string|number|boolean> {
return super.to_object () as Record<string, string|number|boolean>;
}
public get (key: string): string|number|boolean {
return this.data[key] as string|number|boolean;
return super.get (key) as string|number|boolean;
}
public set (key: string, value: string|number|boolean): void {
this.data[key] = value;
super.set (key, value);
}
public update (): void {

View File

@ -5,39 +5,41 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import { Assignable } from './interfaces/Assignable';
import { Persistent } from './Persistent';
export abstract class DatabaseModel extends Assignable {
export abstract class DatabaseModel extends Persistent {
public get id (): number {
return this.data.id as number;
return this.get ('id') as number;
}
public apply_object (obj: Record<string, unknown>): void {
for (const key of Object.keys (obj)) {
public constructor (id?: number) {
super ();
this.properties.id = 'number';
for (const prop of Object.keys (this.properties)) {
if ([
'string',
'number',
'boolean'
].indexOf (typeof obj[key]) > -1)
this.data[key] = obj[key];
].indexOf (this.properties[prop]) < 0) {
throw new Error (
'property types have to be either string, number or boolean'
);
}
}
}
public constructor (id: number) {
super ();
this.data.id = id;
if (typeof id !== 'undefined')
this.set ('id', id);
}
public get (key: string): string|number|boolean {
return this.data[key] as string|number|boolean;
return super.get (key) as string|number|boolean;
}
public set (key: string, value: string|number|boolean): void {
this.data[key] = value;
super.set (key, value);
}
public get_data (): Record<string, string|number|boolean> {
return super.get_data () as Record<string, string|number|boolean>;
public to_object (): Record<string, string|number|boolean> {
return super.to_object () as Record<string, string|number|boolean>;
}
public abstract async read(): Promise<boolean>;

View File

@ -2,12 +2,14 @@ import { Assignable, Serializable } from './interfaces';
export abstract class Persistent implements Assignable, Serializable {
private _data: Record<string, unknown> = {};
private _properties: Record<string, string>;
protected readonly properties: Record<string, string>;
public constructor (props: Record<string, string>) {
this._properties = props;
public constructor () {
this.properties = this.define_properties ();
}
protected abstract define_properties(): Record<string, string>;
public assign (a: Assignable): void {
this.assign_object (a.to_object ());
}
@ -18,7 +20,7 @@ export abstract class Persistent implements Assignable, Serializable {
public assign_object (obj: Record<string, unknown>): void {
for (const key of Object.keys (obj)) {
const prop = this._properties[key];
const prop = this.properties[key];
if (typeof prop !== 'undefined' && typeof obj[key] === prop)
this._data[key] = obj[key];
}
@ -35,7 +37,7 @@ export abstract class Persistent implements Assignable, Serializable {
}
public set (key: string, value: unknown): void {
const prop = this._properties[key];
const prop = this.properties[key];
if (typeof prop !== 'undefined' && typeof value === prop)
this._data[key] = value;
}