Ivan Asmer %!s(int64=6) %!d(string=hai) anos
achega
14ab837e2b
Modificáronse 2 ficheiros con 175 adicións e 0 borrados
  1. 160 0
      index.js
  2. 15 0
      package.json

+ 160 - 0
index.js

@@ -0,0 +1,160 @@
+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
+  //}));
+//});

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "seq",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "mysql2": "^1.5.3",
+    "sequelize": "^4.37.10"
+  }
+}