maggocnx
maggocnx

Reputation: 1602

Is there no option to map the column name in sequelize model

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
});;

Upvotes: 24

Views: 22703

Answers (3)

esc_rtn
esc_rtn

Reputation: 936

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

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

Upvotes: 58

user320487
user320487

Reputation:

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'
}
);

Upvotes: 3

BlaM
BlaM

Reputation: 28858

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.

Upvotes: 1

Related Questions