ForestAdmin/lumber

View on GitHub
templates/app/models/sequelize-model.hbs

Summary

Maintainability
Test Coverage
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const {{modelVariableName}} = sequelize.define('{{modelName}}', {
{{#each fields as |field|}}
    {{#if field.hasParenthesis}}'{{/if}}{{field.name}}{{#if field.hasParenthesis}}'{{/if}}: {
      type: DataTypes.{{{field.type}}},{{#if field.nameColumnUnconventional}}
      field: '{{field.nameColumn}}',{{/if}}{{#if field.primaryKey}}
      primaryKey: true,{{/if}}{{#if field.hasSafeDefaultValue}}
      defaultValue: {{{field.safeDefaultValue}}},{{/if}}{{#if field.isRequired}}
      allowNull: false,{{/if}}
    },
{{/each}}
  }, {
    tableName: '{{table}}',{{#if underscored}}
    underscored: true,{{/if}}{{#unless timestamps}}
    timestamps: false,{{/unless}}{{#if schema}}
    schema: process.env.DATABASE_SCHEMA,{{/if}}
  });
{{#if noId}}  {{modelVariableName}}.removeAttribute('id');{{/if}}
  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  {{modelVariableName}}.associate = (models) => {
    {{#each references as |reference|}}
    {{../modelVariableName}}.{{reference.association}}(models.{{reference.ref}}, {
    {{#if reference.isBelongsToMany}}
      through: '{{reference.through}}',
      foreignKey: '{{reference.foreignKey}}',
      otherKey: '{{reference.otherKey}}',
    {{else}}
      foreignKey: {
        name: '{{reference.foreignKeyName}}',
        field: '{{reference.foreignKey}}',
      },{{#if reference.targetKey}}
      targetKey: '{{reference.targetKey}}',{{/if}}{{#if reference.sourceKey}}
      sourceKey: '{{reference.sourceKey}}',{{/if}}
    {{/if}}
    {{#if reference.as}}
      as: '{{reference.as}}',
    {{/if}}
    });
    {{/each}}
  };

  return {{modelVariableName}};
};