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)

  1. amir, reza

  2. amir, meghdad

  3. amir, batman

  4. amir, david

  5. reza, mohammad

  6. meghdad, mohammad

  7. batman, bastani

  8. mohammad, david

  9. bastani, joseph

this network of 'amir' explain want: graph:

result want: (user, level, connections):

  1. reza, 0, 1

  2. meghdad, 0, 1

  3. batman, 0, 1

  4. david, 0, 2

  5. mohammad, 1, 2

  6. bastani, 1, 1

  7. 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

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -