Loopback Model Binder for ES6
Install with npm
npm install loopback-model-binder --save
import { modelBootstrap } from 'loopback-model-binder';
import * as path from 'path';
module.exports = (app) => {
// the path should be the root path of your model schema
modelBootstrap(app, path.join(__dirname, '../models'));
};
When creating model json file, it should have -model.json suffix.
{
"name": "Mongo",
"base": "PersistedModel",
"mongodb": {
"collections": ["MaritalStatus", "Occupations"]
},
"http": {
"path": "mongo"
}
}
{
"name": "RestMongo",
"plural": "RestMongos",
"base": "PersistedModel",
"http": {
"path": "rest-mongo"
},
"extends": {
"RestMongoData": {
"collections": ["SeveralArtist"]
}
}
}
{
"MongoData": {
"connector": "mongodb",
"debug": "true",
"host": "localhost",
"database": "sampledb",
"port": 27017,
"username": "root",
"password": "*****"
}
}
{
"MongoRestData": {
"connector": "mongodb",
"debug": "true",
"host": "localhost",
"database": "mongorest",
"port": 27017,
"username": "root",
"password": ""
},
"mongoSpotifyRest": {
"connector": "rest",
"debug": "true",
"options": {
"headers": {
"content-type": "application/json"
},
"strictSSL": false
},
"operations": [
{
"template": {
"method": "GET",
"url": "https://api.spotify.com/v1/artists?ids={ids}"
},
"functions": {
"getSpotifySeveralArtists": ["ids"]
}
}
]
}
}
Please see in folder demo/mongo-data/mongo-boot.js
/// By default, it will read binder.config.js file, if exist.
/// else loopback-model-binder has default configs.
/// Please see in folder src/model-boot.js
get configs(){
/// create a copy of the base configs
let _configs = JSON.parse(JSON.stringify(super.configs));
/// set the seed.isSeed value to false
/// setting to false will not execute the seed
_configs.seed.isSeed = false;
/// return the new configs
return _configs;
}
import { modelBootstrap, ModelBoot, EntityBase, Model, ModelSeed } 'loopback-model-binder';
app
{Object}
strongloop/loopback app object.bootRootDir
{String}
root Directory of your custom boot file.isEnable
{Boolean}
Enable or Disable remoteMethods by default = falseonInit
{Observable} initialize the datasources and Modelconfigs
{Object} configurations of model binderonInit
{void} Attach custom functionalities to Model from your entity.Model.instance.create
{void} create a modelModel.instance.[YourModel]
{Object} you access the Loopback Model functionalitiesexecute
{void} execute the seeding, must be implement in the child class.migrate
{void} migrate each object from array data.
data
{Object} on each array.filter
{Object} check if data is existoptions
{Object} default null
modelName
{String} name of the Modellength
{number} length of the data array