S
S
Sergey2017-04-26 20:18:45
Node.js
Sergey, 2017-04-26 20:18:45

SequelizeJs how to implement tree view?

Good hour and straight to the point.
Let's say there is a list of combinations in the database:

combs -> [id:1, name:'noname', weight:100500]
combs -> [id:2, name:'noname', weight:100500]
combs -> [id:3, name:'noname', weight:100500]

and there is a config list:
conf -> [id:1, name:'noname', combs:[1, 2, 3]]
conf -> [id:2, name:'noname', combs:[3, 4, 5]]
conf -> [id:3, name:'noname', combs:[3, 1, 4]]

the essence of the idea, pull out configurations with already filled combinations, example:
confModel.findAll( ).then(function(items:any):any {
    var tree = items.map(function(item:any):any {
        return item.get({plain: true});
    });
    console.log(tree);
    // [{
    // id:1,
    // name:'noname',
    // combs:[
    //     {id:1, weight:100500},
    //     {id:2, weight:100500},
    //     {id:3, weight:100500}
    // },{
    // id:2,
    // name:'noname',
    // combs:[
    //     {id:3, weight:100500},
    //     {id:4, weight:100500},
    //     {id:5, weight:100500}
    // }];
});

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2017-04-26
@stweet

In php, everything is simpler, because it uses the database synchronously.
This is where connections need to be made.
Methods: hasMany is not suitable because the brainchild of JOIN.
And here it is necessary to make a repeated request for each pass, but to the "combs" table.

I
Ivan, 2017-04-26
@LiguidCool

Well, since there is no nesting, then just bypass the array and substitute values ​​:)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question