javascript - sequelize.js relationship validation -
imagine have 2 models:
var movie = sequelize.define('movies', { /* model definition */ }) var genre = sequelize.define('genres', { /* model definition */ }); movie.hasmany(genre); genre.hasmany(movie); if wanted stipulate movie must have @ least 1 genre, how go doing that?
i've looked in obvious places. initial idea build(), validate() , save(), looking @ source .validate() accommodates fields defined in model definition.
e.g.
genre.find({where:{'name':'horror}) .success(function (horrorgenre) { var movie = movie.build({..}); movie.addgenre(horrorgenre); if (! movie.validate()) { // doesn't consider related data movie.save(); } }); so figure need implement kind of custom validation mechanism, i'm not entirely sure start.
note i'm maintaining own fork of sequelize, more of question of how might go modifying sequelize source want versus throwing hacky solid implementation.
you can try search genre objects in database , call addgenre movie
genre.findall({where:{'name':["genre1","genre1"]}) .success(function (genres) { if(genres.length==0){ console.log("genres not found!"); // exit somehow maybe res.json(200,{"msg","not ok"}); } var movie = movie.build({..}); var querychainer = new sequelize.utils.querychainer; for(var = 0 ; != genres.length ; i++){ querychainer.add(movie.addgenre(genres[i].id)); } querychainer.run().success(function(){}).error(function(){}); }); this way know @ least 1 genre added submitted movie!
Comments
Post a Comment