1.1.13 • Published 4 years ago

@sapphirecode/modelling v1.1.13

Weekly downloads
4
License
MIT
Repository
-
Last release
4 years ago

@sapphirecode/modelling

version: 1.1.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

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.

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.

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.

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

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.30

4 years ago

1.0.27

4 years ago