Created
September 21, 2017 07:51
-
-
Save yooouuri/83fd2b9a4133e8303fa5782927532560 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // task model | |
| 'use strict'; | |
| module.exports = (sequelize, DataTypes) => { | |
| const Task = sequelize.define('Task', { | |
| name: DataTypes.STRING | |
| }); | |
| Task.associations = function (models) { | |
| Task.belongsTo(models.User, { | |
| as: 'owner' | |
| }); | |
| }; | |
| return Task; | |
| }; | |
| // user model | |
| 'use strict'; | |
| module.exports = (sequelize, DataTypes) => { | |
| const User = sequelize.define('User', { | |
| first_name: DataTypes.STRING, | |
| last_name: DataTypes.STRING, | |
| email: DataTypes.STRING | |
| }, { | |
| underscored: true | |
| }); | |
| User.associations = function (models) { | |
| User.hasMany(models.Task, { | |
| as: 'tasks' | |
| }); | |
| }; | |
| return User; | |
| }; | |
| // users route | |
| const models = require('./../models'); | |
| const express = require('express'); | |
| const router = express.Router(); | |
| router.get('', function (req, res) { | |
| models.User.findAll({ | |
| include: [ | |
| { | |
| model: models.Task, | |
| as: 'tasks' | |
| } | |
| ] | |
| }).then(function (users) { | |
| res.send(users); | |
| }); | |
| }); | |
| module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment