浏览代码

pre clear

Ivan Asmer 5 年之前
父节点
当前提交
82cf273c47
共有 2 个文件被更改,包括 66 次插入40 次删除
  1. 65 40
      index.js
  2. 1 0
      package.json

+ 65 - 40
index.js

@@ -18,17 +18,16 @@ var Post = sequelize.define('post', {
     text:  Sequelize.TEXT,
     key:  {type: Sequelize.STRING, 
             unique: true,
-            //get: function(){
-                //var currentValue = this.getDataValue('key')
-                //if (!currentValue){
-                    //var shajs = require('sha.js')
-                    //this.setDataValue('key',shajs('sha256').update(`${Math.random}${(new Date()).toString()}${this.text}${this.title}`).digest('hex'))
-                //}
-                //return this.getDataValue('key')
-            //},
-            //set: function(){
-            //}}
-    }
+            get: function(){
+                var currentValue = this.getDataValue('key')
+                if (!currentValue){
+                    var shajs = require('sha.js')
+                    this.setDataValue('key',shajs('sha256').update(`${Math.random}${(new Date()).toString()}${this.text}${this.title}`).digest('hex'))
+                }
+                return this.getDataValue('key')
+            },
+            set: function(){
+            }}
 },{
  getterMethods: {
     tagz() {
@@ -66,6 +65,8 @@ Tag.belongsToMany(Post, {through: 'PostTag'})
 Post.belongsToMany(Tag, {through: 'PostTag'})
 
 
+
+
 async function fillDB(){
     await sequelize.sync()
     var post1 = await Post.create( {
@@ -119,35 +120,44 @@ async function getData(){
     var tag1 = await Tag.findOne({
         where: {
             title: 'tag1'
-        }
+        },
+        //include: [
+            //{model: Post,
+                //limit: 1}
+        //]
     })
 
-    //console.log(await tag1.getPosts())
+    console.log(await tag1.getPosts({limit: 1, offset: 1}))
 
-    for (let post of await tag1.getPosts()){
-        console.log('TAG1', post.title)
-    }
+    //for (let post of await tag1.getPosts()){
+        //console.log('TAG1', post.title)
+    //}
 
-    var comment1 = await Comment.findOne({
-        where: {
-            id: 1
-        }
-    })
+    //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)
-    }
+    //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());
+    //var post1 =(await comment1.getPost());
 
-    console.log(`${post1.text} at ${post1.createdAt}`)
-    console.log(await post1.tagz)
-    console.log(post1.age)
+    //console.log(`${post1.text} at ${post1.createdAt}`)
+    //console.log(await post1.tagz)
+    //console.log(post1.age)
 }
 
 getData()
 
+var cookieSession = require('cookie-session')
+var express = require('express')
+ 
+ 
+
 var express = require('express');
 const cors  = require('cors')
 var express_graphql = require('express-graphql');
@@ -184,23 +194,27 @@ var schema = buildSchema(`
     }
 `);
 
-async function getPost({id}){
+async function getPost({id}, {session}){
+    console.log(session)
+    session.lastId = id;
+    console.log(session)
     //return Post.findById(id).then( post => (post.comments = post.getComments(), post) )
     let post = await Post.findOne({
         where: {
-            key: id
+             id
         }
     })
-    if (post){
-        post.comments = await post.getComments()
-        post.timestamp = post.createdAt.getTime()/1000
-    }
+    console.log(post.age)
+    //if (post){
+        //post.comments = await post.getComments()
+        //post.timestamp = post.createdAt.getTime()/1000
+    //}
     //console.log(post.createdAt, typeof post.createdAt, post.createdAt.getTime())
     return post;
 }
 
 
-async function getPosts(args){
+async function getPosts(args, {session}){
     let posts = await Post.findAll({})
     for (let post of posts){
         post.timestamp = post.createdAt.getTime()/1000
@@ -249,13 +263,24 @@ var root = {
 
 // Create an express server and a GraphQL endpoint
 var app = express();
-app.use(cors())
+app.use(cors({origin: 'http://localhost:3000',
+				credentials: true}))
+
+
+app.use(cookieSession({
+  name: 'session',
+  keys: ['SuperSecretKey'],
+ 
+  // Cookie Options
+  maxAge: 24 * 60 * 60 * 1000 // 24 hours
+}))
 
-app.use('/graphql', express_graphql({
+app.use('/graphql', express_graphql(req => ({
     schema: schema,
     rootValue: root,
-    graphiql: true
-}));
+    graphiql: true,
+    context: (console.log(req.session),req.session.counter = (req.session.counter || 0) +1, ({session: req.session}))
+})));
 
 app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
 

+ 1 - 0
package.json

@@ -9,6 +9,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "cookie-session": "^2.0.0-beta.3",
     "cors": "^2.8.4",
     "express": "^4.16.3",
     "express-graphql": "^0.6.12",