sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

sequelize with Sequelize.VIRTUAL,

Open DigiGroundJohn opened this issue 7 years ago • 3 comments

Hi,

is there any way to achieve below in sequelize-typescript ?

const Pug = db.define('pugs', { firstName: { type: Sequelize.STRING, }, lastName: { type: Sequelize.STRING }, fullName: { type: Sequelize.VIRTUAL, get () { return this.getDataValue('firstName') + ' ' + this.getDataValue('lastName') } } })

Thanks

DigiGroundJohn avatar Nov 07 '19 08:11 DigiGroundJohn

Sure:

import {Table, Column, Model, DataType} from 'sequelize-typescript';

@Table({tableName: 'plugs'})
class Pug extends Model<Pug> {
  @Column
  firstName: string;

  @Column
  lastName: string;

  @Column(DataType.VIRTUAL)
  get fullName() {
    return this.getDataValue('firstName') + ' ' + this.getDataValue('lastName');
  }
}

Hope this helps!

RobinBuschmann avatar Nov 07 '19 09:11 RobinBuschmann

Thanks for the reply,

But the getDataValue always returning undefined.

"myFullName": "undefined undefined",

DigiGroundJohn avatar Nov 08 '19 00:11 DigiGroundJohn

Sorry got this, I was not selected firstName and lastName

Thanks

DigiGroundJohn avatar Nov 08 '19 00:11 DigiGroundJohn