1.1.2 • Published 6 years ago

@grupojaque/model-lang-info v1.1.2

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
6 years ago

Model Lang Info

This hook is a helper for the local practice of having a secondary model for storing translatable information of certain model (e.g Service and ServiceInfo)

pipeline status coverage report

Setup

Requirements

Modules

Initialization

You need to require it after the sequelize startup, with the models created by Sequelize and its corresponding configuration.\ require('index.js')(models, config)

Configuration

Configuration values

The configuration file should have the following format.

KeyDescription
languageCodesAn array of the supported languages codes in the project

Example

  {
    "languageCodes": ["es", "en"]
  }
Additional configurations

You must specify in the model options the name of the secondary model as follows:

  • model.js Specify the name of the secondary model. optionally you can specify the name of the functions to be use for getting formats, if not specified, it will use basicFormat
  ...
  { // model options
    hasLangModel: "ModelInfo",
    formatFunction: "modelFormat", // optional
    infoFormatFunction: "secondaryModelFormat", // optional
  }
  ...

If no option is provided the hook will ignore the model.

Usage

Instance functions

formatWithInfo(isForLanding)

This function will take the format of the instance (it should be already defined) and will add the basic format of each modelinfo populated as an object with the corresponding language code as key.

  • isForLanding: if isForLanding exists instead of adding every populated modelInfo, it assign the format of the first modelInfo (as found in the populated array, it is also recomended to be the only one) into the basic format of the model
createInfoModels(creationData)

This function will create some modelInfos associated to this instace, it will take the language to be associated form the keys of the object.

  • creationData: Object with the creation data of the modelInfos to be created, it must have the objects with the creation data with keys corresponding to the language. Example:
  {
    "es": {
      title: "Título del modelo",
      description: "Descripción del modelo"
    },
    "en": {
      title: "Model title",
      description: "Model description"
    }
  }

updateInfoModels(updateData)

This funtion will update existent modelInfos, the update data will be in the same format of the creation function.

  • updateData: object with the same format of the creation function.

Scope

withInfo(langModelName, languageId)

Is a scope that includes the modelInfo, it's necessary to pass the name of the modelInfo, if languageId is provided it includes only the modelInfo with the matching language.

Contributors

  • Emilio Martínez
  • Jorge Álvarez
  • Susana Hahn