2016-04-06 4 views
1

Это мои таблицы (не все столбцы, включенных) и отношенийВыберите конкретные атрибуты из таблицы в SequelizeJS

var client = schema.define('client', { 
    name: { 
     type: Sequelize.STRING, 
     allowNull: false 
    }, 
} 

var task = schema.define('task', { 
    name: { 
     type: Sequelize.STRING, 
     unique: true, 
     allowNull: false 
    }, 
    description: { 
     type: Sequelize.STRING, 
    } 
} 

var clientTask = schema.define('clientTask', { 
    value: { 
     type: Sequelize.STRING, 
     allowNull: false, 
     defaultValue: false 
    }, 
} 

client.belongsToMany(task, { through: clientTask }); 
task.belongsToMany(client, { through: clientTask }); 

Я хочу, чтобы получить только name от задачи и value из clientTask, я поиск по идентификатору клиента и здесь что я пробовал до сих пор.

client.findAll({ 
    attributes: [], 
    where: {id: clientId}, 
    include: [{ 
     model: task, 
     attributes: ['name'] 
    }] 
}).then(function (clients) { 
    //client.tasks is array with task objects(models) with only name attribute 
    //client.tasks[0].clientTask is object(models) with all attributes but I want only `value` 
} 

В основном то, что я хочу с этим запросом

Select 
    tasks.name, 
    clienttasks.value 
From 
    clients Inner Join 
    clienttasks 
    On clienttasks.clientId = clients.id Inner Join 
    tasks 
    On clienttasks.taskId = tasks.id 
Where clients.id = ? 

ответ

0

Вы можете запрашивать у него, как это

clients.findById(1, { 
    attributes: ['id'], 
    include: [{ 
     model: tasks, 
     attributes: ['name'], 
     required: false 
    }] 
}).then(function(client) { 
    return client.tasks.map(function(task) { 
     return { 
      name: task.name, 
      value: task.clients_tasks.value 
     }; 
    }); 
}).then(function(result) { 
    console.log(result); 
    // The rest of you logic here... 
}); 
Смежные вопросы