Skip to content

Instantly share code, notes, and snippets.

@andrewjmead
Last active February 3, 2016 20:47
Show Gist options
  • Select an option

  • Save andrewjmead/1e8a1dc85679a440ed16 to your computer and use it in GitHub Desktop.

Select an option

Save andrewjmead/1e8a1dc85679a440ed16 to your computer and use it in GitHub Desktop.
Shena - Query Eager Load
// PART ONE - Query todos with user info included
Todo.findAll({include: [User]}).then(function(todos) {
todos.forEach(function (todo) {
console.log(todo.toJSON());
});
}, function() {});
// Printed to screen for each todo
// You can get the username by calling todo.user.userName
// {
// id: 1,
// description: 'Clean yard',
// completed: false,
// createdAt: Wed Feb 03 2016 15: 35: 25 GMT - 0500(EST),
// updatedAt: Wed Feb 03 2016 15: 35: 25 GMT - 0500(EST),
// userId: 1,
// user: {
// id: 1,
// email: 'andrew@example.com',
// userName: 'Andrew Mead',
// createdAt: '2016-02-03 20:35:25.855 +00:00',
// updatedAt: '2016-02-03 20:35:25.855 +00:00'
// }
// }
// PART TWO - Query for all todos whos user has a name of 'Andrew Mead'
Todo.findAll({
include: [{
model: User,
where: { userName: 'Andrew Mead' }
}]
}).then(function(todos) {
todos.forEach(function (todo) {
console.log(todo.toJSON());
});
}, function() {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment