node.js - Node js Graph Search -
i creating social application using nodejs , mysql. have table named followers. fields are: follower , following.
for explain problem, create fake data in followers table: (follower, following)
amir, reza
amir, meghdad
amir, batman
amir, david
reza, mohammad
meghdad, mohammad
batman, bastani
mohammad, david
bastani, joseph
this network of 'amir' explain want:
result want: (user, level, connections):
reza, 0, 1
meghdad, 0, 1
batman, 0, 1
david, 0, 2
mohammad, 1, 2
bastani, 1, 1
joseph, 2, 1
i not mysql, try levelgraph database , confused me in levels after 3. can me?
finally own search works:
let getnetwork = ( user, maxlevel = 5 )=>{ let getlevel = ( user, level )=>{ return new promise( ( resolve, reject )=>{ let search = []; for( let = 0; < level; i++ ){ search.push( { subject: db.v( ), object: db.v( (i+1) ) } ); } search[0].filter = obj => obj.subject == user && !( level > 1 && obj.trust < 1 ); db.search(search, (error, results)=>{ resolve( object.keys( results ).map( i=> object.keys(results[i]).map( j=> results[i][j] ) ) ); }); }); } return new promise( ( resolve, reject )=>{ let ret = []; let getlevels = function (i=1){ if( > maxlevel ){ resolve( ret ); } else{ getlevel( user, ).then( (lvlx)=>{ ret = ret.concat( lvlx ); getlevels( i+1 ); }); } } getlevels(); }); } getnetwork('amir').then( console.log );
Comments
Post a Comment