简体   繁体   中英

sails js and create relationship between nodes in neo4j

I have 2 nodes, first one is Users and second one is Posts. I want to create relationship between Auser in Users to auserPost in Posts. I start using Sailsjs and have a problem to create relationship between in two nodes. My database in neo4j. any Idea?


Actually I would like to convert below code to sailsjs api:

   CREATE (matrix:Movie { title:"The Matrix",released:1997 })
   CREATE (cloudAtlas:Movie { title:"Cloud Atlas",released:2012 })
   CREATE (forrestGump:Movie { title:"Forrest Gump",released:1994 })
   CREATE (keanu:Person { name:"Keanu Reeves", born:1964 })
   CREATE (robert:Person { name:"Robert Zemeckis", born:1951 })
   CREATE (tom:Person { name:"Tom Hanks", born:1956 })
   CREATE (tom)-[:ACTED_IN { roles: ["Forrest"]}]->(forrestGump)
   CREATE (tom)-[:ACTED_IN { roles: ['Zachry']}]->(cloudAtlas)
   CREATE (robert)-[:DIRECTED]->(forrestGump)

Add this code to adapter

runQuery: function(connection , collection , myQuery, params , cb){
        query(connection, collection, myQuery, params, cb, true);
    }

and in your controller use this:

getTestData:function(req,res){
var lang  = req.param("lang");
var skip = req.param("skip");
//get user id from session
var userId=req.session.userId;
var limit = 10;

//relations.posted is a string
var q = [
  'match (u:'+Users.tablename+')-[:'+relations.posted+']->(p:'+Posts.tablename+')-[:'+relations.posted_to+']->(h:'+Headlines.tablename+'),',
  '(h)-[:'+relations.cover_image+']->(i:'+Files.tablename+'),(p)-[:'+relations.sound_file+']->(s:'+Files.tablename+'),(hp:'+Posts.tablename+')-[:'+relations.posted_to+']->(h),',
  '(u)-[:'+relations.profile_picture+']->(ui:'+Files.tablename+')',
  'where p.isDeleted = false and hp.isDeleted = false',
  'return distinct(h) as headlines,count(hp) as postCount,max(hp.createdAt) as lastPostDate,hp as lastPost ,i as headlineImage,s as lastPostSound,u as user,ui as userProfileImg',
  'order by postCount desc',
  'skip '+skip,
  'limit '+limit
];
var params={};
Headlines.runQuery(q,params,function(err,result){
  var feeds = [];
  if(err == null){
    for (var i = 0; i < result.length; i++) {
      var obj = result[i];
      var feed = {};
      feed.post={
        id:obj.lastPost.id,
        uid:obj.lastPost.data.uid,
        url:obj.lastPostSound.data.url
      };

      feeds.push(feed);
    }
  }
  return res.json({
    err:err,
    result:feeds
  });
});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM