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)
Setup
Requirements
Modules
- Sequelize: ORM
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.
Key | Description |
---|---|
languageCodes | An 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 usebasicFormat
...
{ // 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