better persistent model, fixes
This commit is contained in:
parent
ac3296387e
commit
9895e1d386
@ -5,35 +5,37 @@
|
|||||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Assignable } from './interfaces/Assignable';
|
import { Persistent } from './Persistent';
|
||||||
|
|
||||||
export abstract class ControlModel extends Assignable {
|
export abstract class ControlModel extends Persistent {
|
||||||
public constructor (obj: Record<string, string|number|boolean>) {
|
public constructor (obj?: Record<string, string|number|boolean>) {
|
||||||
super ();
|
super ();
|
||||||
this.data = obj as Record<string, string|number|boolean>;
|
for (const prop of Object.keys (this.properties)) {
|
||||||
}
|
|
||||||
|
|
||||||
public apply_object (obj: Record<string, unknown>): void {
|
|
||||||
for (const key of Object.keys (obj)) {
|
|
||||||
if ([
|
if ([
|
||||||
'string',
|
'string',
|
||||||
'number',
|
'number',
|
||||||
'boolean'
|
'boolean'
|
||||||
].indexOf (typeof obj[key]) > -1)
|
].indexOf (this.properties[prop]) < 0) {
|
||||||
this.data[key] = obj[key];
|
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> {
|
public to_object (): Record<string, string|number|boolean> {
|
||||||
return super.get_data () as Record<string, string|number|boolean>;
|
return super.to_object () as Record<string, string|number|boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get (key: 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 {
|
public set (key: string, value: string|number|boolean): void {
|
||||||
this.data[key] = value;
|
super.set (key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public update (): void {
|
public update (): void {
|
||||||
|
@ -5,39 +5,41 @@
|
|||||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
* 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 {
|
public get id (): number {
|
||||||
return this.data.id as number;
|
return this.get ('id') as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
public apply_object (obj: Record<string, unknown>): void {
|
public constructor (id?: number) {
|
||||||
for (const key of Object.keys (obj)) {
|
super ();
|
||||||
|
this.properties.id = 'number';
|
||||||
|
for (const prop of Object.keys (this.properties)) {
|
||||||
if ([
|
if ([
|
||||||
'string',
|
'string',
|
||||||
'number',
|
'number',
|
||||||
'boolean'
|
'boolean'
|
||||||
].indexOf (typeof obj[key]) > -1)
|
].indexOf (this.properties[prop]) < 0) {
|
||||||
this.data[key] = obj[key];
|
throw new Error (
|
||||||
|
'property types have to be either string, number or boolean'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (typeof id !== 'undefined')
|
||||||
|
this.set ('id', id);
|
||||||
public constructor (id: number) {
|
|
||||||
super ();
|
|
||||||
this.data.id = id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get (key: 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 {
|
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> {
|
public to_object (): Record<string, string|number|boolean> {
|
||||||
return super.get_data () as Record<string, string|number|boolean>;
|
return super.to_object () as Record<string, string|number|boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract async read(): Promise<boolean>;
|
public abstract async read(): Promise<boolean>;
|
||||||
|
@ -2,12 +2,14 @@ import { Assignable, Serializable } from './interfaces';
|
|||||||
|
|
||||||
export abstract class Persistent implements Assignable, Serializable {
|
export abstract class Persistent implements Assignable, Serializable {
|
||||||
private _data: Record<string, unknown> = {};
|
private _data: Record<string, unknown> = {};
|
||||||
private _properties: Record<string, string>;
|
protected readonly properties: Record<string, string>;
|
||||||
|
|
||||||
public constructor (props: Record<string, string>) {
|
public constructor () {
|
||||||
this._properties = props;
|
this.properties = this.define_properties ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract define_properties(): Record<string, string>;
|
||||||
|
|
||||||
public assign (a: Assignable): void {
|
public assign (a: Assignable): void {
|
||||||
this.assign_object (a.to_object ());
|
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 {
|
public assign_object (obj: Record<string, unknown>): void {
|
||||||
for (const key of Object.keys (obj)) {
|
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)
|
if (typeof prop !== 'undefined' && typeof obj[key] === prop)
|
||||||
this._data[key] = obj[key];
|
this._data[key] = obj[key];
|
||||||
}
|
}
|
||||||
@ -35,7 +37,7 @@ export abstract class Persistent implements Assignable, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public set (key: string, value: unknown): void {
|
public set (key: string, value: unknown): void {
|
||||||
const prop = this._properties[key];
|
const prop = this.properties[key];
|
||||||
if (typeof prop !== 'undefined' && typeof value === prop)
|
if (typeof prop !== 'undefined' && typeof value === prop)
|
||||||
this._data[key] = value;
|
this._data[key] = value;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user