114 lines
2.5 KiB
Markdown
114 lines
2.5 KiB
Markdown
|
# @sapphirecode/modelling
|
||
|
|
||
|
version: 1.0.x
|
||
|
|
||
|
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.
|
||
|
|
||
|
verify is meant to check and modify data.
|
||
|
|
||
|
update should be called when data was modified, by default it just calls verify,
|
||
|
but can also be used to report updates to other components
|
||
|
|
||
|
### 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>
|