From 9895e1d3864753b55846a330a5aba172b2c1fe5e Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Tue, 5 May 2020 11:37:48 +0200 Subject: [PATCH] better persistent model, fixes --- lib/ControlModel.ts | 30 ++++++++++++++++-------------- lib/DatabaseModel.ts | 34 ++++++++++++++++++---------------- lib/Persistent.ts | 12 +++++++----- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/ControlModel.ts b/lib/ControlModel.ts index 6810a6c..165bf6b 100644 --- a/lib/ControlModel.ts +++ b/lib/ControlModel.ts @@ -5,35 +5,37 @@ * Created by Timo Hocker , May 2020 */ -import { Assignable } from './interfaces/Assignable'; +import { Persistent } from './Persistent'; -export abstract class ControlModel extends Assignable { - public constructor (obj: Record) { +export abstract class ControlModel extends Persistent { + public constructor (obj?: Record) { super (); - this.data = obj as Record; - } - - public apply_object (obj: Record): 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 { - return super.get_data () as Record; + public to_object (): Record { + return super.to_object () as Record; } 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 { diff --git a/lib/DatabaseModel.ts b/lib/DatabaseModel.ts index 3cdcd5d..97468a8 100644 --- a/lib/DatabaseModel.ts +++ b/lib/DatabaseModel.ts @@ -5,39 +5,41 @@ * Created by Timo Hocker , 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): 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 { - return super.get_data () as Record; + public to_object (): Record { + return super.to_object () as Record; } public abstract async read(): Promise; diff --git a/lib/Persistent.ts b/lib/Persistent.ts index 0e429d7..851d452 100644 --- a/lib/Persistent.ts +++ b/lib/Persistent.ts @@ -2,12 +2,14 @@ import { Assignable, Serializable } from './interfaces'; export abstract class Persistent implements Assignable, Serializable { private _data: Record = {}; - private _properties: Record; + protected readonly properties: Record; - public constructor (props: Record) { - this._properties = props; + public constructor () { + this.properties = this.define_properties (); } + protected abstract define_properties(): Record; + 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): 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; }