Overview
This module is a part of yocto node modules for NodeJS.
Please see our NPM repository for complete list of available tools (completed day after day).
This module provide a simple config validator tools for your node app.
Motivation
Create an easy and ready to use connector & model builder based on mongoose / redis / elasticsearch.
Folder structure
A specific folder structure was setup for database configuration :
- enums : Contains all enums defined for database
- models : Contains all model definition for an automatic build
- validators : Contains all validator function defined on model configuration files
- methods : Contains all methods defined on model configuration
All of these folders must be set up before any usage.
For sure it's possible to set your own directory for each items. (See How to setup path directory part)
Dependencies & Validator
- Validate rules MUST be build with joi package
- Promise was build with Q package
Model type & list defintiion
- String
- ObjectId
- Boolean
- Object
- Array or []
How to set directories path
A method was defined for each path
- models(path) : set directory for models files
- validators(path) : set directory for validators files
- methods(path) : set directory for methods files
- enums(path) : set directory for enums files
How to define a new model
For this example we want to define "Notify" model.
First you need to go in your models directory
Create your own folder (not mandatory)
Create a json file in created folder named like your model name (notify.json)
Add you model definition on it (see below for example)
{
"model" : {
"name" : "Notify",
"crud" : {
"enable" : true,
"exclude" : []
},
"properties" : {
"status" : {
"required" : true,
"type" : "String"
},
"notify_type" : {
"required" : true,
"type" : "String"
}
}
}
}
Save file, Build & Use (See How To use part)
How to define a validator / function / enums attached to the previous define model
How to add a new validator
First edit you notify.json file created before and set the validator properties by the name of needed function, for example my Function name is "testValidator"
{
"model" : {
"name" : "Notify",
.....
"validator" : "testValidator",
}
}
Create your structure of directory in validators directory (not mandatory).
Create your notify.js files that will be contain your validation rules.
Add into this files your new validation rules
'use strict';
var joi = require('joi');
exports.testValidator = function(data) {
return joi.object().keys({
a : joi.string()
});
};
Save file, Build & Use (See How To use part)
How to add new function(s)
First edit your notify.json file and add an "fn" properties, and add list of your methods :
{
"model" : {
"name" : "Notify",
.....
"validator" : "testValidator",
"fn" : [
{
"type" : "static",
"name" : "test1"
},
{
"type" : "method",
"name" : "test2"
}
}
}
Create your structure of directory in methods directory (not mandatory).
exports.test1 = function() {
console.log('test1')
};
exports.test2 = function() {
console.log('test2');
};
Save file, Build & Use (See How To use part)
!!! IMPORTANT !!! Each function will be executed in schema context, so crud or mongoose method was available.
Add enum value(s)
Create a "file.json" into enums directory
Add item like this :
[
{
"name" : "notify_type_list",
"value" : [ "email", "sms", "notification" ]
},
{
"name" : "notify_status_list",
"value" : [ "pending", "processed", "error" ]
}
]
Use it on your validator by injection dependencices to retreive a value :
exports.testValidator = function (enums) {
var status = enums().get('notify_status_list');
var type = enums().get('notify_type_list');
}
Access to mongooseTypes from enums
exports.testTypes = function (enums) {
console.log(enums().Types);
}
CRUD methods
All defined model have CRUD Method attached by mongoose static function.
See Below list of method & aliases :
Operation | Available Method | Alias(es) |
---|
Create | create | insert |
Read (Retrieve) | get | read |
Update (Modify | update | modify |
Delete (Destroy) | delete | destroy |
Elasticsearch implementation
Setting up one host or multiple hosts
You can provide to our package a multiple hosts connection for elasticsearch
part.
A method elasticHosts
are available to define which hosts to use on mongoosastic
.
var db = require('yocto-mongoose')();
db.connect('MONGO_URL').then(function() {
db.enableElasticsearch([ { host : '127.0.0.1', port : 9200 } ]);
});
Basic configuration
Elastic search implemantion are builed with mongoosastic package
Elastic search rules are setted up during model build.
You can define index directly on model definition. See a simple example below :
{
"model" : {
"name" :
"crud" : {
"enable" : true,
"exclude" : []
},
"elastic" : {
"enable" : true,
"options" : {}
},
"properties" : {
"status" : {
"required" : true,
"type" : "String"
"es_indexed" : true
"es_type" : "String"
"es_include_in_parent" : true
},
"notify_type" : {
"required" : true,
"type" : "String"
}
}
}
}
Usage
On each model, where elastic is enabled a method search was added.
To do an elasticsearch
query just do :
var query = {};
var hydrate = false;
var hydrateOptions = {};
account.esearch(query, hydrate, hydrateOptions).then(function(success) {
}).catch(function (error) {
});
List of available keys
For more complex implementation see mongoosastic package documentation to see available keys
Use ssl connection or other options
var db = require('yocto-mongoose')();
var options = {
ssl : {
ca: fs.readFileSync(__dirname + "/cert.pem"),
rejectUnauthorized: true
}
};
db.connect('MONGO_URL').then(function() {
db.elasticHosts([ { host : '127.0.0.1', port : 9200, protocol : 'https' } ], options);
});
To discover a complete list of options see : official elastic configuration
Redis implementation
Our redis implementation use ioredis module but only use set/get method with expire time for the moment.
Be careful : Redis > 2.6 is required to use this module. In fact expire time is not implemented on Redis < 2.6.
For more details please read official redis documentation
Setting up one host or multiple hosts
By default redis instance use single connection :
var db = require('yocto-mongoose')();
db.connect('MONGO_URL').then(function() {
db.enableRedis([ { host : '127.0.0.1', port : 6379 }, { host : '127.0.0.1', port : 6379 }]);
});
Setting up one host or multiple host in a cluster mode
Just add true on second parameters of enableRedis
method.
var db = require('yocto-mongoose')();
db.connect('MONGO_URL').then(function() {
db.enableRedis([ { host : '127.0.0.1', port : 6379 }, { host : '127.0.0.1', port : 6379 }], true);
});
Usage
Redis is implemented in two ways in this modules, from custom methods and crud methods.
Redis on Crud methods
To use redis on Crud method just extend your config file like this :
{
"model" : {
"name" : "Notify",
"crud" : {
"enable" : true,
"exclude" : [],
"redis" : {
"enable" : true,
"expire" : 10,
"include" : [ "get", "getOne" ]
}
},
"properties" : {
"status" : {
"required" : true,
"type" : "String"
},
"notify_type" : {
"required" : true,
"type" : "String"
}
}
}
}
This configuration enable to your current model redis on include
method (get or getOne).
On each request on include
method redis will be check if value exists before each mongo request.
In case of value was found, we return founded value, otherwise we store the value, with given expire time before process the mongo access.
The storage key is build automatically by our redis implementation
To see your data from a GUI tool download Medis
Redis on custom methods
To use redis on custom method just extend your config file like this :
{
"model" : {
"name" : "Notify",
.....
"validator" : "testValidator",
"fn" : [
{
"type" : "static",
"name" : "test1",
"redis" : {
"enable" : true,
"expire" : 10,
}
},
{
"type" : "method",
"name" : "test2"
}
}
}
After that your can access to the redis instance in your custom function and use add
, get
, delete
or flush
method.
An alias to the add method is provide by the set
method.
An alias to the delete method is provide by the remove
method.
exports.test1 = function(data) {
var redis = this.redis().instance;
var expire = this.redis().expire;
redis.add('aaa', { foo : 'abar' }, expire);
redis.set('aaa', { foo : 'abar' }, expire);
redis.get('aaa').then(function (success) {
}).catch(function (error) {
});
redis.delete('aaa');
redis.remove('aaa');
redis.flush().then(function (success) {
})
redis.flush('MY_PATTERN').then(function (success) {
})
};