2

I am learning sequelize js and I was wondering if there is a way to define a Model and have it get all the table columns automatically ?

We have a huge dataset and the table that I will base the Model on, has 200 columns. Even if I need only 50 from the 200 columns, it is still a lot.

Is there a way to define the Model and it will automatically get all the columns with their names, instead of me defining each columns by hand ? Something like

const HugeData = db.define('likeALotOfData', {
  //get all columns 
})

If this makes any difference, I am using mssql and tedious

Thanks

jarlh
  • 42,561
  • 8
  • 45
  • 63
codebot
  • 517
  • 8
  • 29
  • 55

2 Answers2

1

hmmm, are you defining attributes in your query? i use sequelize and it automatically fetches all columns if you do not specify the attributes in the find query... Or do you mean you don't want to have to type out all the columns that are in your database into your model? If so you can use sequelize-auto to generate that code for you... i do that in a separate project and copy and paste it into my model..

Greg Belyea
  • 858
  • 1
  • 5
  • 15
1

I know it's an old question but if you can get the table columns from a Sequelize model class like so:

console.log(Object.keys(MyModel.rawAttributes));
// outputs: ['id', 'name', 'createdAt', 'updatedAt']
Rodrigo Vasconcelos
  • 1,270
  • 2
  • 14
  • 26