Giter Site home page Giter Site logo

Comments (5)

janez89 avatar janez89 commented on August 20, 2024

Hy,
thank you for the feedback.
Good idea, I'll try to do it this week.

from mongoose-materialized.

janez89 avatar janez89 commented on August 20, 2024

Hello,
I had a little free time, so I made the requested function.

You can use npm tool for update and checkout the changelog and API reference.
https://github.com/janez89/mongoose-materialized#changelog

from mongoose-materialized.

DJWassink avatar DJWassink commented on August 20, 2024

Haha that was pretty damn quick! Awesome job man, Will check them out!

from mongoose-materialized.

DJWassink avatar DJWassink commented on August 20, 2024

Just tested the functions, really impressed with the quality work! Something did surprise me though. If you use

Cat.GetArrayTree('elemt ID', function (err, tree) {
    // ...
});

It returns the tree correctly but if you let's say input a id of 10 and 10 has a few parents it will also include parents, is this working as intended? (the way I did it now was with a findById and then ask the tree which returned only the tree starting from that ID).

Anyhow I am so thankful for the (quick) fulfilment of this feature! I also made a function that is able to import a arrayTree and write it to the db, maybe it is not perfectly written but I will leave it here if people ever need a feature that's similar.

importDataArray: function(db, json, name, callback) {
    var self = this;
    var counter = 0;

    json.forEach(function(node) {
        counter++;
        self.importDataObj(db, node, name, function(err) {
            if (err) {
                callback(err);
            } else {
                counter--;
                if (counter === 0) {
                    callback();
                }                   
            }
        });
    });
},
importDataObj: function(db, node, name, callback) {
    var self = this;
    var counter = 1;
    var callbackCheck = false;

    if (name) {
        node.data.name = name;
    }

    saveNode(node, null);

    function saveNode(json, parentId) {
        counter--;

        if(json.children !== undefined) {
            counter += json.children.length;
        }

        var newNode = {};
        newNode = extend(true, newNode, json);

        newNode.parentId = parentId;
        delete newNode._id;
        delete newNode.path;
        delete newNode.children;

        self.saveNode(db, newNode, function(err, result) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                if (json.children !== undefined) {
                    for (var i = 0; i < json.children.length; i++) {
                        saveNode(json.children[i], result._id);
                    }
                }
            }
            if (counter === 0 && !callbackCheck) {
                callbackCheck = true;
                callback();
            }
        });
    }
}

from mongoose-materialized.

andrzejs avatar andrzejs commented on August 20, 2024

Hi janez89 and Lailend,
I try to limit result of query

Cat.GetArrayTree('elemt ID', function (err, tree) {
    // ...
});

and have array with only one level of children node

{
  "items": [
    {
      "id": "53a97576bcdd8dbc905727ac",
      "text": "Cars",
      "children": [
        {
          "id": "543c35b78521e35432b790c6",
          "text": "VW"
        },
        {
          "id": "53a9958cbcdd8dbc905727ad",
          "text": "Volvo"
        }
      ]
    }
  ]
}

and function to return this

            Category.model.findOne({_id: nodeID}, function(err, node){
                node.getArrayTree(function(err, childs) {           
                sendResponse({

                                items: childs.map(function(i){
                                        return {
                                            id: i.id
                                            ,text: i.name
                                            ,children: i.children.map(function(j){
                                                return {
                                                    id: j.id
                                                    ,text: j.name
                                                }
                                            })
                                        }

                                    })

                                })
                                                    })

How reduce resultset of function GetArrayTree to some fields and have all children levels?
Plase help.
Andrzej

from mongoose-materialized.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.