cekoya
v0.2.0
Published
Cekoya is a class template for modeling object with custom attributes it is oriented as a Model
Downloads
2
Maintainers
Readme
Cekoya
Purposes
I wroked hard on building another library called modelizejs. These two share the same targets except that Cekoya should plugginable. I worked on projet when I wanted to use modelizejs but didn't want some feature that I thought were overwhelming for my use case. With Cekoya, I do a second attempt of building something everyone would enjoy. A Model library that is DRY.
Note The whole package was building under ES6 JavaScript standards. I recommend using them when working on new JavaScript. This said, the package comes with firenpm which has a basic webpack config to build the package if you want to deploy it at home.
Installation
npm install --save cekoya
// or
yarn add cekoya
Usage
Usage by proxy
Something I wanted to achieve long time ago with modelizejs was overriding default getter on class. I discovered Proxies.
const user = Cekoya.create({
firstName: 'John',
lastName: 'Doe',
age: 20,
});
console.log(user.firstName) // Outputs "John";
console.log(user.get('firstName')) // Outputs "John";
If you instanciate a new Cekoya object, Proxies won't be applied to the object. Calling user.firstName
will then return undefined
. And that's bad.
Extension
This package should be used by extension. You create models that extends this one. You could create getters and setters just as any model.
Getters
Let's say I want to display my firstName attribute in upper case because I like the yelling style of the upper case.
class User extends Cekoya {
getFirstNameAttribute() {
return this.getAttribute('firstName').toUpperCase();
}
}
const user = User.create({
firstName: 'John',
lastName: 'Doe',
age: 20,
});
console.log(user.firstName) // Outputs "JOHN";
console.log(user.get('firstName')) // Outputs "JOHN";
console.log(user.getAttribute('firstName')) // Outputs "John";
Setters
Let's say I want to set my age to at least 18 and pretend I'm an adult (Which I am, in real life).
const MINIMUM_AGE = 18;
class User extends Cekoya {
setAgeAttribute(value) {
return (value >= MINIMUM_AGE) ? value : MINIMUM_AGE;
}
}
const user = User.create({
firstName: 'John',
lastName: 'Doe',
age: 10,
});
console.log(user.age) // Outputs "18";
Plugins
To add a plugins to the package, simply override the static method plugins
on your class to return an array of Plugin classes.
Note Plugin classes must have a static method attach
which will receive the instance in parameters
class GetAttributesPlugin {
static attach(model) {
model.constructor.prototype.getAttributes = () => model._attributes;
}
}
Main purpose of this is adding plugins like :
- Resource package for calling REST api
- Relationnal casting between models
- Casting of properties to other things
- ...
This is a new projet so this is a only a starts but I think it go far away.