23

I have a given database with long, cumbersome columnnames. Isn't there any way to map the tablenames to shorter and more descriptive propertyNames in the model ? something like

var Employee = sql.define('Employee', {
    id : {type : Sequelize.INTEGER , primaryKey: true, map : "veryLongNameForJustTheId"}
},{
     tableName: 'cumbersomeTableName',
     timestamps: false
});;
maggocnx
  • 1,512
  • 3
  • 11
  • 14

3 Answers3

58
id : {
    field: 'some_long_name_that_is_terrible_thanks_dba_guy',
    type : Sequelize.INTEGER ,
    primaryKey: true
}

Specify a 'field' attribute ... Like that ^

esc_rtn
  • 936
  • 9
  • 7
  • 1
    This is the official answer. The document has an example: http://docs.sequelizejs.com/en/latest/docs/models-definition/#definition – Ning Liu Jan 25 '16 at 22:21
3

You can specify a table name by supplying the name as the first parameter to the define() call. For example:

var User = sequelize.define(
'a_long_cumbersone_users_table_name',
{
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING
    },
    email: {
        type: Sequelize.STRING
    },
    password: {
        type: Sequelize.STRING
    },
    rememberToken: {
        type: Sequelize.STRING,
        field: 'remember_token'
    }
},
{
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
}
);
1

So far the best solution I found to do this is to use getters and setters.

They will - however - not affect results in object.values or object.toJSON(). You'll need your own serialization methods if you want to use these.

BlaM
  • 28,465
  • 32
  • 91
  • 105