RoModel
RoModel is a simple, lightweight, unopinionated, read-only, immutable, model based data access library.
DOES NOT SUPPORT Node.js < 4.0
Install
npm install --save romodel
Usage
Basic
const MyModel = Model; const a = id: 1 foo: 'bar'; // Accessing 'id' valueaid === 1; // directly accessible since declared in 'fields' a === 1; // access via calling a class method a === 1; // accessible via pre-defined '$get' method a$dataid === 1; // accessible via '$data' method that returns the raw data // Accessing 'foo' valueafoo === undefined; // not directly accessible since not declared in 'fields' a === 'bar' // accessible via '$get' though not declared a$datafoo === 'bar' // or via '$data'
Class Methods / Getters / Static
const Plus = Model; const a = value: 4; Plus === 5; // static method a === 6; // member method aplus3 === 7 // getter
Class Inheritance
const ShapeModel = Model; const ShapeWithColorModel = Model; const s = name: 'box'; s instanceof ShapeModel === true;s instanceof ShapeWithColorModel === false; // staticShapeModel === false;ShapeModel === true;ShapeModelclassId === 1; sname === 'box';s === 'box';s === 'Shape'; const c = name: 'red box' color: 'red'; c instanceof ShapeModel === true;c instanceof ShapeWithColorModel === true; // staticShapeWithColorModel === false;ShapeWithColorModel === true;ShapeWithColorModel === true;ShapeWithColorModel === false;ShapeWithColorModel === false;ShapeWithColorModel === true;ShapeWithColorModelclassId === 2; // Inherited field and methodcname === 'red box';c === 'red box'; // Extended field and methodccolor === 'red';c === 'red'; // Method overridec === 'ShapeWithColor';
Parent / Child
const ChildModel = Model; const ParentModel = Model; const p = child: name: 'a' children: name: 'b' name: 'c'; pchild instanceof ChildModel === true;pchildren0 instanceof ChildModel === true;pchildren1 instanceof ChildModel === true; pchildname === 'a';pchild === 'a'; pchildren0name === 'b';pchildren0 === 'b';
Advanced
Take a look at test file.
TODO
- super.superMethod();
LICENSE
The MIT License (MIT)
Copyright (c) 2016 Joon Ho Cho
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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.