sequelize / express-example

A proposal for the usage of Sequelize within an Express.JS application.
MIT License
2.5k stars 770 forks source link

User model goes by the actual table name or model name? #44

Closed zenithtekla closed 7 years ago

zenithtekla commented 8 years ago

Hi, On the production floor (my workplace, proj is still pending development), I added new route directly to your app. Here how I tried with Assembly model

// model schema
"use strict";

module.exports = function(sequelize, DataTypes) {
  var Assembly = sequelize.define('orm_assembly_table', {
      customer_id: {
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false
      },
      number: DataTypes.STRING(250),
      revision: DataTypes.STRING(10),
      unique_key: {
        type: DataTypes.STRING(10),
        allowNull: false,
        unique: true
      }
    },
    {
      timestamps: false,
      freezeTableName: true

      /*, getterMethods   : {
       fullName       : function()  { return this.firstname + ' ' + this.lastname }
       },

       setterMethods   : {
       fullName       : function(value) {
       var names = value.split(' ');

       this.setDataValue('firstname', names.slice(0, -1).join(' '));
       this.setDataValue('lastname', names.slice(-1).join(' '));
       },
       },
       classMethods: {
       generateHash : function(password) {
       return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
       },
       },
       instanceMethods: {
       validPassword : function(password) {
       return bcrypt.compareSync(password, this.localpassword);
       }
       }

       */
    });

  return Assembly;
};
extends layout

block content
  h1= title
  p This is Sequelize greeting you :)

  h2 Assembly Listing

  ul
  each assembly in assemblys
    li
      = assembly.number
      |  
      | (
      a(href="/users/" + assembly.revision + "/destroy") Destroy
      | )
var models  = require('../models');
var express = require('express');
var router  = express.Router();

router.get('/', function(req, res) {
  models.orm_assembly_table.findAll().then(function(assemblys) {
    res.render('assembly', {
      title: 'Sequelize: Express Example',
      assemblys: assemblys
    });
  });
});

module.exports = router;

If all go like this, I get assemblys displayed correctly.

When I tried with Assembly in place of (au lieu de) _orm_assemblytable, I encountered error of undefined. So, in a router.get(), what should be used as property of models? I tried console.log(models) the models object in models/index.js and saw something like

models: { orm_assembly_table: orm_assembly_table }

 importCache: { 'C:\inetpub\wwwroot\development\phuc_NodeJS_MySQL\NodeMySQL\models\assembly.server.mod
el.js': orm_assembly_table }

Advices? Thanks

P/S: I like to write it models.Assembly than the actual table name (_orm_assemblytable, which is not nice) but I can't get it to work.

sushantdhiman commented 7 years ago

@zenithtekla You should be able to use tableName option for model, something like this -

var Assembly = sequelize.define('Assembly ', {
      customer_id: {
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false
      },
      number: DataTypes.STRING(250),
      revision: DataTypes.STRING(10)
    }, {
      timestamps: false,
      freezeTableName: true,
      tableName: 'orm_assembly_table'
    }
}