New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@janiscommerce/model-controller

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@janiscommerce/model-controller

The `model-controller` module allows you to get a controller/model class or instance easily

  • 1.5.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Model Controller

Build Status Coverage Status

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');

// To get the Product class from e.g. 'path/to/root/controllers/product.js'
const ProductController = Controller.get('product'); // this returns the product class

How to get a Product instance

const { Controller } = require('@janiscommerce/model-controller');

// To get the Product instance from e.g. 'path/to/root/controllers/product.js'
const productController = Controller.getInstance('product');

How to get a Product model instance from a Product instance

const { Controller } = require('@janiscommerce/model-controller');

// To get the Product instance from e.g. 'path/to/root/controllers/product.js'
const productController = Controller.getInstance('product');

// To get the Product Model class from e.g. 'path/to/root/models/product.js'
const myProduct = productController.getModel();

How to get a Product model

const { Model } = require('@janiscommerce/model-controller');

// To get the Product Model class from e.g. 'path/to/root/models/product.js'
const ProductModel = Model.get('product');

How to get a Product model instance

const { Model } = require('@janiscommerce/model-controller');

// To get the Product Model class from e.g. 'path/to/root/models/product.js'
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(); // get from DB using model + database-dispatcher. see @janiscommerce/database-dispatcher

const categoryController = productController.getController('category');

console.log(categoryController.client);

/** -- Expected output:
	{
		id: 1,
		name: 'my-client-name',
		foo: 'bar'
	}
*/

const categories = await categoryController.get(); // get from DB, should be the same DB than productsController

FAQs

Package last updated on 04 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc