2020-05-17 16:59:34 +02:00
|
|
|
# @sapphirecode/modelling
|
|
|
|
|
2020-06-06 15:48:43 +02:00
|
|
|
version: 1.1.x
|
2020-05-17 16:59:34 +02:00
|
|
|
|
|
|
|
base classes for controlling data
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
npm:
|
|
|
|
|
|
|
|
> npm i --save @sapphirecode/modelling
|
|
|
|
|
|
|
|
yarn:
|
|
|
|
|
|
|
|
> yarn add @sapphirecode/modelling
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
### Classes
|
|
|
|
|
|
|
|
#### Persistent
|
|
|
|
|
|
|
|
Persistent allows easy data transfer between classes that extend Persistent, as
|
|
|
|
long as they have overlapping properties.
|
|
|
|
|
|
|
|
to create a persistent type, create a class that extends Persistent and in the
|
|
|
|
constructor, define all properties you want to store.
|
|
|
|
|
|
|
|
Property types can be string, boolean, number or array
|
|
|
|
|
|
|
|
```js
|
|
|
|
import { Persistent } from '@sapphirecode/modelling';
|
|
|
|
|
|
|
|
class Foo extends Persistent {
|
|
|
|
public constructor() {
|
|
|
|
super();
|
|
|
|
this.properties.foo = 'string';
|
|
|
|
this.properties.baz = 'boolean';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar extends Persistent {
|
|
|
|
public constructor() {
|
|
|
|
super();
|
|
|
|
this.properties.bar = 'string';
|
|
|
|
this.properties.baz = 'boolean';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const f = new Foo();
|
|
|
|
f.set('foo','bar');
|
|
|
|
f.set('baz', true);
|
|
|
|
|
|
|
|
const b = new Bar();
|
|
|
|
b.assign(f); // f.assign_to(b) will do the same
|
|
|
|
// now the property 'baz' of b will also be true
|
|
|
|
console.log(b.serialize()); // Persistent also implements Serializable
|
|
|
|
```
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
|
|
|
|
- assign, assign_to, assign_object: (implementation of Assignable)
|
|
|
|
- to_object: return data as plain object
|
|
|
|
- serialize: return data as json string (Implementation of Serializable)
|
|
|
|
- set(key, value): set property to a specific value
|
|
|
|
- get(key): get a property
|
|
|
|
|
|
|
|
#### DatabaseModel
|
|
|
|
|
|
|
|
Extends Persistent and has the abstract methods read, write, delete and
|
|
|
|
define_properties to allow working with databases
|
|
|
|
|
|
|
|
in define_properties, set all the properties you need, id is automatically added
|
|
|
|
and has to be a number.
|
|
|
|
|
|
|
|
read, write and delete specify the actions needed to interact with the database
|
|
|
|
itself
|
|
|
|
|
|
|
|
#### ControlModel
|
|
|
|
|
|
|
|
The ControlModel is there to verify incoming data and modify it if needed. It
|
|
|
|
also extends Persistent.
|
|
|
|
|
|
|
|
The method define_properties lets you define the data you will store.
|
|
|
|
|
2020-06-06 15:48:43 +02:00
|
|
|
before_change allows you to register observers, that are able to cancel changes.
|
|
|
|
the registered observers are called with the following parameters: (new_value,
|
|
|
|
old_value, property_name). By returning false they can cancel the change that
|
|
|
|
was about to happen.
|
2020-05-17 16:59:34 +02:00
|
|
|
|
2020-06-06 15:48:43 +02:00
|
|
|
register_observer does the same thing, but those observers are not able to
|
|
|
|
cancel the change and they are called after the change already happened.
|
2020-05-17 16:59:34 +02:00
|
|
|
|
|
|
|
### Interfaces
|
|
|
|
|
|
|
|
#### Assignable
|
|
|
|
|
|
|
|
Base interface of Persistent
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
|
|
|
|
- assign(b): assign data of another assingable object (b)
|
|
|
|
- assign_to(b): assing data to b
|
|
|
|
- assign_object(obj): assign data of plain object obj
|
|
|
|
|
|
|
|
#### Serializable
|
|
|
|
|
|
|
|
Interface to allow serialization of different data structures
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
|
|
|
|
- serialize: serialize into a string
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
MIT © Timo Hocker <timo@scode.ovh>
|