Model Structure
model-structure
helps you to create Models based on a Schema's Object.
It can:
- Set a custom repository to create, update, get and delete
- Get Swagger models
- Get db-migrate JSON
- Nested objects support (validations too!)
- Call validations before create, update or standalone
- Custom async validations based on Schema
- Custom validation messages, by validation or attribute
- Schema Declaration
- Using Repositories
- Validating Models
- Swagger
- Node DB Migrate
- Messages
- Data/Object Definition
Installation
$ npm install model-structure
Model.init(constructor, schema)
;
Used to bind prototype properties in object constructor
constructor
- Object constructorschema
- object schema with properties details
Model.instantiate(instance, data, options)
;
Used inside object constructor
instance
-this
referencedata
- object data attributesoptions
- Model options-
### options
repository
- Repository Object - contains object with crud functionsvalidator
- Validators Array - an array with validators to be used against object data
-
Usage
var Model = ; { Model;} var schema = properties: "id" : "type": "integer" "primaryKey": true "autoIncrement": true "name" : "type": "string" "maximum": 30 "email" : "type": "email" "message": "%s field is not a valid email!!" Model; var data = name: 'Kurosaki Ichigo' email: 'ichi@soulsociety.com';var lead = data;lead;
Schema Declaration
var schema = {};
schema.messages = {}
Schema error messages based on Data Type.
schemamessages = "integer": "Integer error message" "float": "Float error message"
schema.notInstantiate = false
If true
, gets exactly the return data from Repository.
schema.properties = {}
Property | Type | Description |
---|---|---|
type |
String - Data Type |
Required. |
primaryKey |
Boolean |
|
autoIncrement |
Boolean |
|
minimum |
Number |
If type is a Number , minimum value. If it's a String , minimum length. |
maximum |
Number |
If type is a Number , maximum value. If it's a String , maximum length. |
values |
Object - ENUM |
. |
model |
Object - Model Ref |
schemaproperties = "id": "type": "integer" "primaryKey": true "autoIncrement": true
Using Repositories
The Model calls repository's functions passing object with this
context.
var datas = {};// Simple repository to use memory to save data { Repositoryprototype { thisid = ; datasthisid = this; if typeof callback === 'function' ; }; Repositoryprototype { var data = ; for var i in datas data; ; }; Repositoryprototype { args = args || {}; var data = argsdata || {}; if argsid data = datasargsid; ; }; Repositoryprototype { datasthisid = this; if typeof callback === 'function' ; }; Repositoryprototype { delete datasthisid; if typeof callback === 'function' ; };} var data = name: 'Kurosaki Ichigo' email: 'ichi@soulsociety.com';var options = {};optionsrepository = ;var lead = data options;lead;
Validating Models
The validations methods are fired before create
or update
methods. But you may trigger it directly:
var data = name: 'Kurosaki Ichigo' email: 'ichi@soulsociety.com';optionsrepository = ;var lead = data options;lead;
Custom Validations
var Validator = ModelValidator;var validators = ;var validator = validate: firstLetterLowerCase;var expect = ;var error = message: "Name field must be first letter in lowercase" field: 'name';; { if thisname0 === thisname0 ; else ; }validators;lead;// ORValidator;
//TODO More examples
Swagger
Get Swagger Model schema
var swaggerSchema = "apiVersion": "0.0.1" "swaggerVersion": "1.2" "basePath": "http://localhost:1214" "resourcePath": "/lead" "apis": "path": "/lead/" "operations": "description": "Get all leads" "notes": "Returns all leads." "summary": "Get leads" "method": "GET" "type": "Lead" "nickname": "getAllLeads" "models": Lead }
Node DB Migrate
If you are using node-db-migrate to manager your migrations, you can get the migration schema directly from Model.getSchema('dbMigrate', [YOUR_SCHEMA])
.
var schema = "properties": "id" : "type": "integer" "primaryKey": true "autoIncrement": true "name" : "type": "string" "minimum": 3 "maximum": 30 "email" : "type": "email" "required": true "unique": true "minimum": 7 Model; // returns the node-db-migrate schema
Messages
Add custom error messages to field or validation
var expect = ; var lead = id:"not a valid integer"; Model; Model; lead;
Data/Object Definition
Data Types
Currently Supported Datatypes:
String
Char
Decimal
Float
Integer
Boolean
Date
Datetime
Enum
Array
Email
Nested Objects
ENUM Object
Property | Type | Description |
---|---|---|
ref |
Array/Object |
Reference Values |
type |
String - Data Type |
Model Ref Object
Property | Type | Description |
---|---|---|
ref |
Object - Model Structure |
A Model Structure instance |
Browser Support
You can use on client-side too!
IE 9+ ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
Running Tests
To run the test suite, first invoke the following command within the repo, installing the development dependencies:
$ npm install
Then run the tests:
$ npm test
To run the coverage report:
$ npm run cover