Skip to content

Instantly share code, notes, and snippets.

@louisremi
Last active April 23, 2018 15:00
Show Gist options
  • Save louisremi/deca2c6da8a7992b84a1ed14a99e92dd to your computer and use it in GitHub Desktop.
Save louisremi/deca2c6da8a7992b84a1ed14a99e92dd to your computer and use it in GitHub Desktop.
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