Model Controller

The model-controller
module allows you to get a controller/model class or instance easily.
The module will search recursively the files in your root path inside the folders controllers/**
and models/**
.
Installation
npm install @janiscommerce/model-controller
API
Controller.get(string) static
- This method returns the controller class to use static methods or instance a controller.
Example: Controller.get('category');
Controller.getInstance(string) static
- This method returns the instance of a controller class.
Example: Controller.getInstance('category');
Controller.getModel(string) non-static
- This methods returns a Model Instance from a controller using his name. The method will cache the model to simple return it the next time.
Example: myController.getModel();
Controller.client(any) setter non-static
- This methods sets the client in the controller instance.
Example: myController.client = { id: 1 };
Example: myController.client = 1;
Example: myController.client = 'my-client-name';
Controller.client() getter non-static
- This methods returns the client if any
Example: myController.client;
Controller.getController(string) non-static
- This methods returns an Controller instance. It propagates the client if any.
Example: myController.getController('brand');
Model.get(string) static
- This method returns the model class to use static methods or instance a model.
Example: Model.get('category');
Model.getInstance(string) static
- This method returns the instance of a model class.
Example: Model.getInstance('category');
Usage
How to get a Product
class
const { Controller } = require('@janiscommerce/model-controller');
const ProductController = Controller.get('product');
How to get a Product
instance
const { Controller } = require('@janiscommerce/model-controller');
const productController = Controller.getInstance('product');
How to get a Product
model instance from a Product
instance
const { Controller } = require('@janiscommerce/model-controller');
const productController = Controller.getInstance('product');
const myProduct = productController.getModel();
How to get a Product
model
const { Model } = require('@janiscommerce/model-controller');
const ProductModel = Model.get('product');
How to get a Product
model instance
const { Model } = require('@janiscommerce/model-controller');
const productModel = Model.getInstance('product');
How to handle Client and propagation between controllers and models
const { Controller } = require('@janiscommerce/model-controller');
const productController = Controller.getInstance('product');
productController.client = {
id: 1,
name: 'my-client-name',
foo: 'bar'
};
const products = await productController.get();
const categoryController = productController.getController('category');
console.log(categoryController.client);
const categories = await categoryController.get();