123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- var Sequelize = require('sequelize');
- var sequelize = new Sequelize('test', 'root', '',
- {
- host: 'localhost',
- dialect: 'mysql',
- pool: {
- max: 5,
- min: 0,
- idle: 10000
- },
- //logging: false
- }
- );
- var Post = sequelize.define('post', {
- title: Sequelize.STRING,
- text: Sequelize.TEXT,
- },{
- getterMethods: {
- tagz() {
- return this.getTags().then(tagz => tagz.map(tag => tag.title))
- },
- age() {
- return (((new Date).getTime() - this.createdAt.getTime()) / 3600000).toFixed(0) + ' hrs ago';
- }
- },
- })
- var Comment = sequelize.define('comment',{
- text: Sequelize.TEXT
- })
- Post.hasMany(Comment)
- Comment.belongsTo(Post)
- Comment.hasMany(Comment)
- Comment.belongsTo(Comment)
- var Tag = sequelize.define('tag', {
- title: Sequelize.STRING
- })
- Tag.belongsToMany(Post, {through: 'PostTag'})
- Post.belongsToMany(Tag, {through: 'PostTag'})
- async function fillDB(){
- await sequelize.sync()
- var post1 = await Post.create( {
- title: 'First Post',
- text: 'First Post BLah-blah-blah'
- })
- var post2 = await Post.create(
- {
- title: 'Second Post',
- text: 'Second Post BLah-blah-blah'
- })
- var post3 = await Post.create(
- {
- title: 'Third Post',
- text: 'Third Post BLah-blah-blah'
- })
- var comment1 = await Comment.create({text: 'test comment for first post'})
- var comment11 = await Comment.create({text: 'test comment for first comment of first post'})
- var comment12 = await Comment.create({text: 'test comment 2 for first comment of first post'})
- comment1.addComment(comment11);
- comment1.addComment(comment12);
- post1.addComment(comment1)
- var comment2 = await Comment.create({text: 'test comment for second post'})
- var comment21 = await Comment.create({text: 'test comment for first comment of second post'})
- var comment22 = await Comment.create({text: 'test comment 2 for first comment of second post'})
- comment2.addComment(comment21);
- comment2.addComment(comment22);
- post2.addComment(comment2)
- var tag1 = await Tag.create({title: 'tag1'})
- var tag2 = await Tag.create({title: 'tag2'})
- var tag3 = await Tag.create({title: 'tag3'})
- tag1.addPosts([post1, post3])
- post1.addTags([tag2])
- post2.addTags([tag2, tag3])
- tag3.addPost(post3)
- }
- //fillDB()
- //
- //
- async function getData(){
- var tag1 = await Tag.findOne({
- where: {
- title: 'tag1'
- }
- })
- //console.log(await tag1.getPosts())
- for (let post of await tag1.getPosts()){
- console.log('TAG1', post.title)
- }
- var comment1 = await Comment.findOne({
- where: {
- id: 1
- }
- })
- for (let comment of await comment1.getComments()){
- console.log('comment1 sub comment:', comment.text)
- console.log('parent', (await comment.getComment()).text)
- }
- var post1 =(await comment1.getPost());
- console.log(`${post1.text} at ${post1.createdAt}`)
- console.log(await post1.tagz)
- console.log(post1.age)
- }
- getData()
- //sequelize.sync().then(function(){
- //return Post.create(
- //{
- //title: 'First Post',
- //text: 'First Post BLah-blah-blah'
- //})
- //}).then(function(){
- //return Post.create(
- //{
- //title: 'Second Post',
- //text: 'Second Post BLah-blah-blah'
- //})
- //}).then(function(){
- //return Post.create(
- //{
- //title: 'Third Post',
- //text: 'Third Post BLah-blah-blah'
- //})
- //}).t
- //.then(function() {
- //return User.create({
- //username: 'janedoe',
- //birthday: new Date(1980, 6, 20)
- //});
- //}).then(function(jane) {
- //console.log(jane.get({
- //plain: true
- //}));
- //});
|