Compare commits

...

69 Commits

Author SHA1 Message Date
bb7344740e update
Some checks failed
continuous-integration/drone/push Build is failing
2021-05-24 15:04:35 +02:00
002f0c3715 update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
eslint: 7.7.0 ==> 7.8.1 minor
2020-09-07 13:32:13 +02:00
879fc564aa upgrade typescript
All checks were successful
continuous-integration/drone/push Build is passing
2020-08-21 13:13:07 +02:00
e6d69b135a update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
eslint: 7.6.0 ==> 7.7.0 minor
2020-08-19 08:23:45 +02:00
e45f9fd3b7 fix drone config
Some checks failed
continuous-integration/drone/push Build is failing
2020-08-07 08:12:54 +02:00
86fe138e66 update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
eslint: 7.5.0 ==> 7.6.0 minor
2020-08-04 12:55:03 +02:00
8fadff4a14 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.21 ==> 1.1.22 minor
2020-07-25 16:49:29 +02:00
ec4a5eece3 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.20 ==> 1.1.21 minor
@sapphirecode/utilities: 1.8.4 ==> 1.8.5 minor
2020-07-19 15:06:31 +02:00
887408a85c fix
Some checks failed
continuous-integration/drone/push Build is failing
2020-07-19 14:05:57 +02:00
72793baf36 update-scanner: automatic update
Some checks failed
continuous-integration/drone/push Build is failing
eslint: 7.4.0 ==> 7.5.0 minor
typescript: 3.9.6 ==> 3.9.7 minor
2020-07-19 12:10:40 +02:00
3a8bace490 update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.19 ==> 1.1.20 minor
2020-07-16 10:50:19 +02:00
12155578ac update-scanner: automatic update
All checks were successful
continuous-integration/drone/push Build is passing
@sapphirecode/eslint-config-ts: 1.1.17 ==> 1.1.19 minor
@sapphirecode/utilities: 1.7.2 ==> 1.8.4 minor
eslint: 7.3.1 ==> 7.4.0 minor
typescript: 3.9.5 ==> 3.9.6 minor
2020-07-10 12:31:25 +02:00
26d78a433f switch to drone
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-10 08:31:18 +02:00
796bb9ad5d update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.16 ==> 1.1.17 minor
@sapphirecode/utilities: 1.4.5 ==> 1.7.2 minor
2020-07-01 09:30:33 +02:00
8032878594 fix 2020-06-28 14:38:15 +02:00
4426ff6aa2 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.14 ==> 1.1.16 minor
@sapphirecode/utilities: 1.4.4 ==> 1.4.5 minor
eslint: 7.3.0 ==> 7.3.1 minor
2020-06-24 12:49:41 +02:00
63cd177757 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.13 ==> 1.1.14 minor
@sapphirecode/utilities: 1.4.3 ==> 1.4.4 minor
eslint: 7.2.0 ==> 7.3.0 minor
2020-06-22 08:20:12 +02:00
8270431e87 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.12 ==> 1.1.13 minor
@sapphirecode/utilities: 1.4.2 ==> 1.4.3 minor
2020-06-19 13:05:20 +02:00
c54ff294f1 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.11 ==> 1.1.12 minor
@sapphirecode/utilities: 1.3.5 ==> 1.4.2 minor
eslint: 7.1.0 ==> 7.2.0 minor
typescript: 3.9.3 ==> 3.9.5 minor
2020-06-11 20:27:13 +02:00
bbd1278e5a fix 2020-06-06 15:52:13 +02:00
e6321a6d07 control-model: restructuring 2020-06-06 15:48:43 +02:00
c79f71eb01 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.10 ==> 1.1.11 minor
@sapphirecode/utilities: 1.3.4 ==> 1.3.5 minor
2020-06-02 08:44:29 +02:00
43f831596f update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.9 ==> 1.1.10 minor
2020-05-28 08:46:54 +02:00
1ef264bcef update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.7 ==> 1.1.9 minor
@sapphirecode/utilities: 1.3.3 ==> 1.3.4 minor
eslint: 7.0.0 ==> 7.1.0 minor
typescript: 3.9.2 ==> 3.9.3 minor
2020-05-23 18:18:09 +02:00
510ad0ca77 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.1.4 ==> 1.1.7 minor
@sapphirecode/utilities: 1.3.2 ==> 1.3.3 minor
2020-05-17 19:35:48 +02:00
0840591d42 fix 2020-05-17 17:01:32 +02:00
434da3ce20 update 2020-05-17 16:59:34 +02:00
194195b4dc update jenkins.js 2020-05-15 13:16:00 +02:00
9738e4a837 adapt jenkins.js 2020-05-13 16:07:00 +02:00
93bff42997 don't serialize twice 2020-05-13 11:51:26 +02:00
328baeb35f update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.0.44 ==> 1.0.45 minor
eslint: 6.8.0 ==> 7.0.0 major
typescript: 3.8.3 ==> 3.9.2 minor
2020-05-13 11:05:51 +02:00
f6058eb50c update-scanner: automatic update
@sapphirecode/utilities: 1.0.39 ==> 1.0.40 minor
eslint: 6.8.0 ==> 7.0.0 major
2020-05-09 21:49:22 +02:00
b9381c2824 allow for arrays 2020-05-09 18:06:13 +02:00
432adc5b58 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.0.43 ==> 1.0.44 minor
2020-05-08 13:39:05 +02:00
0f26b14782 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.0.41 ==> 1.0.43 minor
2020-05-07 19:27:11 +02:00
16d4e68a8a fix 2020-05-07 18:59:27 +02:00
b00bf14bb4 update-scanner: automatic update
@sapphirecode/eslint-config-ts: 1.0.38 ==> 1.0.41 minor
2020-05-07 10:28:15 +02:00
d5272cf1cd update 2020-05-06 08:31:05 +02:00
15793afa98 update 2020-05-06 08:27:35 +02:00
5deecaea4c update 2020-05-06 08:22:47 +02:00
996694b6f4 update 2020-05-06 08:11:28 +02:00
d635aac840 update 2020-05-06 07:58:24 +02:00
11ec74e368 fix 2020-05-06 07:44:51 +02:00
607db269c5 fix 2020-05-06 07:33:04 +02:00
801c0920f6 fix publish 2020-05-05 19:50:08 +02:00
8b123ae924 move to @sapphirecode scope 2020-05-05 19:23:15 +02:00
433b2b5b27 fix 2020-05-05 11:48:46 +02:00
400b68daf0 fix 2020-05-05 11:41:00 +02:00
9895e1d386 better persistent model, fixes 2020-05-05 11:37:48 +02:00
ac3296387e improve persistent class 2020-05-05 10:46:30 +02:00
ac79bf8ef7 using TPersistent as example 2020-05-05 09:35:17 +02:00
3783f6b52c update-scanner: automatic update
@scode/eslint-config-ts: 1.0.29 ==> 1.0.31 minor
2020-05-04 21:07:42 +02:00
64fb775478 fix 2020-05-04 20:27:52 +02:00
dd5608afb7 fix 2020-05-04 20:25:57 +02:00
a94c741624 fix 2020-05-04 20:13:41 +02:00
3838c8c978 modelling 2020-05-04 20:04:38 +02:00
373635f0db interface modelling 2020-05-04 14:52:08 +02:00
6101f066cd update-scanner: automatic update
@scode/eslint-config-ts: 1.0.27 ==> 1.0.29 minor
2020-05-03 18:23:21 +02:00
df53948f69 apply methods 2020-05-03 15:35:29 +02:00
241f1dafac delete function 2020-05-02 21:26:51 +02:00
f6b6eb2142 fix 2020-05-02 21:20:01 +02:00
e0b1063df9 fix 2020-05-02 21:18:32 +02:00
161d4573c7 fix 2020-05-02 21:14:54 +02:00
174739ad17 fixes 2020-05-02 21:11:55 +02:00
702f7383cb implement abstracts 2020-05-02 21:07:15 +02:00
f2cfbe4f37 property for reading entire data stack 2020-05-02 21:01:08 +02:00
249316858f fix 2020-05-02 20:04:14 +02:00
d504f892c0 simplification 2020-05-02 20:03:14 +02:00
de5c397ed3 better structure 2020-05-02 20:00:41 +02:00
20 changed files with 1248 additions and 823 deletions

14
.drone.yml Normal file
View File

@ -0,0 +1,14 @@
kind: pipeline
name: default
steps:
- name: setup
image: registry:5000/node-build
commands:
- yarn
- curl https://git.scode.ovh/Timo/standard/raw/branch/master/ci.js > ci.js
- name: build
image: registry:5000/node-build
commands:
- node ci.js

View File

@ -5,20 +5,18 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
'use strict';
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
es6: true,
node: true
},
extends: [
'@scode'
],
extends: [ '@sapphirecode' ],
globals: {
Atomics: 'readonly',
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018
}
}
parserOptions: { ecmaVersion: 2018 }
};

1
.npmrc
View File

@ -1 +0,0 @@
@scode:registry=https://npm.scode.ovh

10
CHANGELOG.md Normal file
View File

@ -0,0 +1,10 @@
# Changelog
## 1.1.0
Adapting ControlModel to be able to use the full Persistent-Type range and use
observers instead of baked in functions.
## 1.0.0
initial version

23
Jenkinsfile vendored
View File

@ -1,23 +0,0 @@
pipeline {
agent any
environment {
VERSION = VersionNumber([
versionNumberString:
'${BUILDS_ALL_TIME}',
versionPrefix: '1.0.',
worstResultForIncrement: 'SUCCESS'
])
}
stages {
stage('Building') {
steps {
script {
currentBuild.displayName = env.VERSION
}
sh 'yarn ci ${VERSION}'
}
}
}
}

View File

@ -1,4 +1,4 @@
MIT License Copyright (c) 2020 Timo Hocker
MIT License Copyright (c) <year> <author>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

116
README.md Normal file
View File

@ -0,0 +1,116 @@
# @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
```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.
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>

View File

@ -1,38 +0,0 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
/* eslint-disable no-process-exit */
/* eslint-disable no-console */
/* eslint-disable no-sync */
'use strict';
const fs = require ('fs');
const child_process = require ('child_process');
const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8'));
[
,, pkg.version
] = process.argv;
fs.writeFileSync ('package.json', JSON.stringify (pkg, null, 2));
child_process.execSync ('yarn lint', { stdio: 'inherit' });
if (typeof pkg.scripts !== 'undefined' && typeof pkg.scripts.test === 'string')
child_process.execSync ('yarn test', { stdio: 'inherit' });
if (
typeof pkg.scripts !== 'undefined'
&& typeof pkg.scripts.compile === 'string'
)
child_process.execSync ('yarn compile', { stdio: 'inherit' });
child_process.exec ('git log -1 | grep \'\\[no publish\\]\'')
.addListener ('exit', (code) => {
if (code === 0) {
console.log ('build not marked for deployment');
process.exit (1);
}
else { child_process.execSync ('yarn publish'); }
});

View File

@ -5,20 +5,20 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
/* eslint-disable */
'use strict';
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
es6: true,
node: true
},
extends: [
'@scode/eslint-config-ts'
],
extends: [ '@sapphirecode/eslint-config-ts' ],
globals: {
Atomics: 'readonly',
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018
}
}
parserOptions: { ecmaVersion: 2018 }
};

View File

@ -5,13 +5,47 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
export abstract class ControlModel {
public abstract get object(): Record<string, unknown>;
public abstract set object(obj: Record<string, unknown>);
import { Persistent } from './Persistent';
import { PersistentType, ControlObserver, Observer } from './Types';
public update (): void {
this.verify ();
export abstract class ControlModel extends Persistent {
private _before_change: Record<string, ControlObserver[]> = {};
private _observers: Record<string, Observer[]> = {};
public constructor (obj?: Record<string, PersistentType>) {
super ();
this.define_properties ();
if (typeof obj !== 'undefined')
this.assign_object (obj);
}
public abstract verify(): void;
public set (key: string, value: PersistentType): void {
const prev = this.get (key);
if (typeof this._before_change[key] !== 'undefined') {
for (const obs of this._before_change[key]) {
if (!obs (value, prev, key))
return;
}
}
super.set (key, value);
if (typeof this._observers[key] !== 'undefined') {
for (const obs of this._observers[key])
obs (value, prev, key);
}
}
public before_change (key: string, func: ControlObserver): void {
if (typeof this._before_change[key] === 'undefined')
this._before_change[key] = [];
this._before_change[key].push (func);
}
public register_observer (key: string, func: Observer): void {
if (typeof this._observers[key] === 'undefined')
this._observers[key] = [];
this._observers[key].push (func);
}
protected abstract define_properties(): void;
}

View File

@ -5,28 +5,46 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
export abstract class DatabaseModel {
protected id?: number;
import { Persistent } from './Persistent';
public static async get<T extends DatabaseModel> (
id: number,
constructor: new () => T
): Promise<T> {
const dbm = (new constructor);
dbm.id = id;
await dbm.read ();
return dbm;
export abstract class DatabaseModel extends Persistent {
public get id (): number {
return this.get ('id') as number;
}
public get object (): Record<string, number | string | undefined> {
const obj = { id: this.id };
return obj;
public constructor (id?: number) {
super ();
this.properties.id = 'number';
this.define_properties ();
for (const prop of Object.keys (this.properties)) {
if ([
'string',
'number',
'boolean'
].indexOf (this.properties[prop]) < 0) {
throw new Error (
'property types have to be either string, number or boolean'
);
}
}
if (typeof id !== 'undefined')
this.set ('id', id);
}
public set object (obj: Record<string, number | string | undefined>) {
this.id = obj.id as number | undefined;
public get (key: string): string|number|boolean {
return super.get (key) as string|number|boolean;
}
public abstract read(): Promise<void>;
public abstract write(): Promise<void>;
public set (key: string, value: string|number|boolean): void {
super.set (key, value);
}
public to_object (): Record<string, string|number|boolean> {
return super.to_object () as Record<string, string|number|boolean>;
}
public abstract async read(): Promise<boolean>;
public abstract async write(): Promise<boolean>;
public abstract async delete(): Promise<boolean>;
protected abstract define_properties(): void;
}

60
lib/Persistent.ts Normal file
View File

@ -0,0 +1,60 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import { copy_object } from '@sapphirecode/utilities';
import { Assignable, Serializable } from './interfaces';
import { PersistentTypeString, PersistentType } from './Types';
export abstract class Persistent implements Assignable, Serializable {
private _data: Record<string, PersistentType> = {};
protected readonly properties: Record<string, PersistentTypeString> = {};
public assign (a: Assignable): void {
this.assign_object (a.to_object ());
}
public assign_to (a: Assignable): void {
a.assign (this);
}
public assign_object (obj: Record<string, unknown>): void {
for (const key of Object.keys (obj)) {
const prop = this.properties[key];
if (typeof prop !== 'undefined' && typeof obj[key] === prop)
this._data[key] = obj[key] as PersistentType;
}
}
public to_object (): Record<string, PersistentType> {
return copy_object (this._data) as Record<string, PersistentType>;
}
public serialize (formatted = false): string {
if (formatted)
return JSON.stringify (this._data, null, 2);
return JSON.stringify (this._data);
}
private check_type (value: unknown, prop: PersistentTypeString): boolean {
if (typeof prop === 'undefined')
return false;
if (prop === 'array')
return Array.isArray (value);
return typeof value === prop;
}
public set (key: string, value: unknown): void {
const prop = this.properties[key];
if (this.check_type (value, prop))
this._data[key] = value as PersistentType;
}
public get (key: string): PersistentType {
return this._data[key];
}
}

15
lib/Types.ts Normal file
View File

@ -0,0 +1,15 @@
export type PersistentTypeString = 'string'|'number'|'boolean'|'array';
export type PersistentPrimitive = string|number|boolean;
export type PersistentType = PersistentPrimitive|PersistentPrimitive[];
export type Observer = (
value: PersistentType,
prev: PersistentType,
key: string
) => void;
export type ControlObserver = (
value: PersistentType,
prev: PersistentType,
key: string
) => boolean;

View File

@ -7,3 +7,5 @@
export { ControlModel } from './ControlModel';
export { DatabaseModel } from './DatabaseModel';
export { Persistent } from './Persistent';
export * from './interfaces';

View File

@ -0,0 +1,12 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
export interface Assignable {
assign (a: Assignable): void;
assign_to (a: Assignable): void;
to_object (): Record<string, unknown>;
}

View File

@ -0,0 +1,10 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
export interface Serializable {
serialize(): string;
}

9
lib/interfaces/index.ts Normal file
View File

@ -0,0 +1,9 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
export { Assignable } from './Assignable';
export { Serializable } from './Serializable';

View File

@ -1,22 +1,37 @@
{
"name": "@scode/modelling",
"version": "1.0.0",
"name": "@sapphirecode/modelling",
"version": "1.1.13",
"main": "dist/index.js",
"author": "Timo Hocker <timo@scode.ovh>",
"author": {
"name": "Timo Hocker",
"email": "timo@scode.ovh"
},
"bugs": "https://redmine.scode.ovh/projects/modelling",
"license": "MIT",
"description": "base classes for controlling data",
"repository": {
"type": "git",
"url": "https://git.scode.ovh:timo/modelling.git"
},
"devDependencies": {
"@scode/eslint-config-ts": "^1.0.27",
"eslint": "^6.8.0",
"typescript": "^3.8.3"
"@sapphirecode/eslint-config-ts": "^1.1.4",
"eslint": "^7.0.0",
"typescript": "^4.0.2"
},
"scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
"test": "echo \"no test\"",
"ci": "yarn && node jenkins.js",
"compile": "tsc"
},
"files": [
"/dist/",
"LICENSE"
],
"dependencies": {
"@sapphirecode/utilities": "^1.3.2"
},
"keywords": [
"typescript",
"data control"
]
}

View File

@ -10,7 +10,7 @@
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./lib", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */

1590
yarn.lock

File diff suppressed because it is too large Load Diff