Last active
April 23, 2018 15:00
-
-
Save louisremi/deca2c6da8a7992b84a1ed14a99e92dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sequelize = new Sequelize('database', 'username', 'password', { | |
dialect: 'mysql', | |
host: "my.server.tld", | |
port: 9821, | |
}) | |
const Project = sequelize.define('project', { | |
title: Sequelize.STRING, | |
description: Sequelize.TEXT, | |
}); | |
const Task = sequelize.define('task', { | |
title: Sequelize.STRING, | |
description: Sequelize.TEXT, | |
deadline: Sequelize.DATE, | |
}); | |
Project.hasMany(Task); | |
Task.belongsTo(Project); | |
await Project.create({ | |
title: 'todo list', | |
description: 'the best todo manager ever', | |
}); | |
const projectTodo = await Project.create({ | |
title: 'todo list', | |
description: 'the best todo manager ever', | |
}); | |
const task1 = await Task.create({ | |
title: 'foo', | |
description: 'bar', | |
deadline: new Date(), | |
projectId: projectTodo.id, | |
}); | |
const backofficeProject = await Project.findOne({ | |
where: { title: 'backoffice' }, | |
}); | |
const overdueProjects = await Project.findAll({ | |
include: [{ | |
model: Task, | |
where: { deadline: { [Op.lt]: new Date() } }, | |
required: true, // → inner join | |
}], | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment