A
A
Anton Anton2017-08-01 00:10:07
MySQL
Anton Anton, 2017-08-01 00:10:07

How to limit selection of fields from nested schemas in sequelize?

I can't restrict field selection from sequelize nested schemas, I try like this:

models.template.findById(req.params.id, {
        include: [{
            model: models.templateMessage, 
            separate: true, // это я уже от безысходности, и так и так не работает :(
            arrtibutes: {exclude: ['data']}, 
            order: ['order']
          }]
      });

models.template is, respectively, a model . There are a lot of fields, I would not want to list them by hand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Azamat Kasymbekov, 2017-08-10
@Fragster

const model = models.template;
// тут получаешь все поля объекта
const fields = Object.keys(model.attributes).map(key => model.attributes[key].field);
// можешь уже отсечь ненужное или отсортировать
...
const attributes = myFields;

// запрос
models.template.findById(req.params.id, {
        include: [{
            model: models.templateMessage, 
            // тут указано какие поля тебе нужны
            attributes, 
            order: ['order']
          }]
      });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question