Created
November 3, 2016 19:52
-
-
Save RuairiSpain/6d9b704248d82d8562436693b9f6294f 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
var models = { | |
'user': User, | |
'todo': Todo | |
}; | |
server.get( '/:model', function(req, res, next) { | |
models[req.params.model] | |
.findAll({}) // .findAll({ include: [ req.body.include ] }) | |
.then(results => { res.send(200, results);}) /// need HTTP status codes | |
.catch(err { res.send(400, results);}); | |
}); | |
server.get( '/:model/:id', function(req, res, next) { | |
models[req.params.model] | |
.findById(req.params.id) // findById(req.params.id , { include: [ req.body.include ] }). | |
.then((results) => { console.log(results);}); | |
}); | |
server.post('/:model', function(req, res) { | |
models[req.params.model].create(req.body).then(results => { console.log(results);});); | |
}); | |
server.put('/:model/:id', function(req, res) { | |
models[req.params.model].findOne({ | |
where: { id: req.params.id } | |
}).then(function(res) { | |
if(res){ | |
res.updateAttributes(req.body).then(results => { console.log(results);});); | |
}); | |
router.delete('/:model/:id', function(req, res) { | |
models[req.params.model].destroy({ | |
where: { id: req.params.id } | |
}).then(results => { console.log(results);});); | |
}); | |
'use strict'; | |
module.exports = function(sequelize, DataTypes) { | |
var Todo = sequelize.define('Todo', { | |
title: DataTypes.STRING, | |
complete: DataTypes.BOOLEAN | |
}, { | |
classMethods: { | |
associate: function(models) { | |
Todo.belongsTo(models.User); | |
} | |
} | |
}); | |
return Todo; | |
}; | |
'use strict'; | |
module.exports = function(sequelize, DataTypes) { | |
var User = sequelize.define('User', { | |
email: DataTypes.STRING | |
}, { | |
classMethods: { | |
associate: function(models) { | |
User.hasMany(models.Todo); | |
} | |
} | |
}); | |
return User; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment