1.1.0 • Published 7 years ago

@alcadica/service.contract v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

@alcadica/service.contract

Install

npm i --save @alcadica/service.contract

This package provides a simple but powerful way to estabilish a contract between a datasource and an entity. A contract is used to handle data channeling between a caller (application layer) and an owner (data layer, contract provider).

Examples

import contract from '@alcadica/service.contract';

class MyEntity {
  public foo: string = '';
}

let instance = new MyEntity();

let MyEntityContract = contract.create(instance);

MyEntityContract.entity.foo.validate = entity => entity.foo.length > 0;

console.log(MyEntityContract.isFullfilled) // outputs false

instance.foo = '123';

console.log(MyEntityContract.isFullfilled) // outputs true

let newinstance = new MyEntity();

MyEntityContract.update(newinstance);

console.log(MyEntityContract.isFullfilled) // outputs false

Creating a custom validator

import contract from '@alcadica/service.contract';

class Test {
  public constructor (
    public bar: string,
    public foo: number
  ) {};
}

const testContract = contract.createFromClass(Test, 'hello', 100);

testContract.entity.foo.validate = () => {
  return (value: string) => {
    return contract.createValidatorOutput(value.length > 5, 'foo length must be greater than 5');
  }
}

Licence

MIT

1.1.0

7 years ago

1.0.2

7 years ago

1.0.0

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago