sequelize.js - Sequelize Subtype Example -
i trying replicate example here simple crud operations. have difficulty accessing shared foreign key in example , setting it. suggestions on how can access 'commentable' field of comment object? due {constraint: false} (foreign key constraints not being generated) it's not being making sense.
here working example:
var sequelize = require('sequelize'); var sequelize = new sequelize( 'my_test', // db name 'postgres', // username 'postgres', // password { host: 'localhost', dialect: 'postgres', port:5432, dialectoptions: { ssl: false }, logging: true, //verbose pool: { max: 5, min: 0, idle: 10000 } } ); // define models var image = sequelize.define('image', { title: sequelize.string }); var post = sequelize.define('post', { title: sequelize.string }); var comment = sequelize.define('comment', { title: sequelize.string, commentable: sequelize.string, commentable_id: sequelize.integer } , { instancemethods: { getitem: function() { console.log(this); console.log('===================='); console.log('===================='); //console.log(this.gettitle()); //console.log(this.getcommentable()); console.log('===================='); return this['get' + this.commentable.substr(0, 1).touppercase() + this.commentable.substr(1)](); //where definition of method? getimage/getpost. } } } ); // relations // post-comment post.hasmany(comment, { //adds fk in comment foreignkey: 'commentable_id', constraints: false, scope: { commentable: 'post' } }); comment.belongsto(post, { // fk on src (comment) foreignkey: 'commentable_id', constraints: false, as: 'post' }); // image-comment image.hasmany(comment, { //adds fk in comment foreignkey: 'commentable_id', //as:'commentableid', constraints: false, scope: { commentable: 'image' } }); comment.belongsto(image, { // fk on src (comment) foreignkey: 'commentable_id', constraints: false, as: 'image' }); //========= sequelize.sync() .then(function () { console.log('---------- sync'); var image1 = image.build({title: 'very_important_task.jpg'}); var image1promise = image1.save() .then(function(i){ var commentpromise=i.createcomment({ //create comment through image sets commentable , commentable_id title: 'my comment 999999999' }); // break previous promise abel access image1 when keep on adding chain commentpromise .then(function(c){ // comment saved. console.log('##################### comments: '); image1.getcomments() .then(function(results){ //results[0] first result console.log( results.map( function(item) { return item['title']; //or image.title item.commentable_id === image id } ) ); }); // after comment creation check accessing comentable reference of comment c.getitem() .then(function(i){ console.log(i.title) }); }); //creat lots of comments comment.bulkcreate([ {title: 'my comment 1', commentable_id:image1.id, commentable:'image'}, {title: 'my comment 2', commentable_id:image1.id, commentable:'image'} ]).then(function() { image1.getcomments() .then(function(results){ console.log( results.map( function(item) { return item['title']; } ) ); }); }); }); //save promise of image make sure created code accessing });
Comments
Post a Comment