mbouclas / loopback-ds-tree-mixin

Transform a loopback model into a tree using the Array of Ancestors pattern
MIT License
6 stars 4 forks source link

Creating a root node #1

Closed martin-spinks closed 7 years ago

martin-spinks commented 8 years ago

Firstly thanks so much for the mixin, having installed the mixin and played with it a little on a test env, what the is the proposed approach you recommend for creating root nodes (nodes that have no parent)?

At the moment; the addNode endpoint requires that a parent is specified; which makes sense if you're adding a node it needs to be attached to something; should there be an addRoot endpoint perhaps to allow nodes to be created with a parent of null?

How do you implement a root node at the moment if you're using the mixin, are you assuming that there is already an item in the DB (I'm using mongo also) with a parent of null?

More than happy to make a pull request so add this if you'd like?

ahmed-abdulmoniem commented 8 years ago

You can just pass empty object in the parent parameter this will make the node you are inserting as a root node or parent.

mbouclas commented 8 years ago

As far as root nodes are concerned, what i do now is just create a simple model and save it. As this is a root node, no need for any special handling. You could also pass an empty object and it will figure out that you are refering to a root node.

I know that i should probably have added a "addRoot()" method, but it is kind of useless in some ways.

Jcbobo commented 7 years ago

I have issue creating a root node, passing {} as parent and a {"name":"testnode"} (name is the only property i have in my model right not) does not work, i obtain this error:

{ "error": { "statusCode": 500, "name": "Error", "message": "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters", "stack": "Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters\n " } }

mbouclas commented 7 years ago

@Jcbobo88 can you provide some code?

Jcbobo commented 7 years ago

i pushed the test project at this url

this is the curl generated from the explorer when i try to add a root node curl -X PUT --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'parent=%7B%7D&node=%7B%22name%22%3A%22aaaa%22%7D' 'http://localhost:3050/api/tests/addNode'

mbouclas commented 7 years ago

If i got this right, it might be a logic error here. You add a node to an existing parent. If you need to create a root node, you need to just use the create method and simply create a record. You then use the addNode using as a parent the node you just created.

Example, according to the documentation

var Category = app.models.Category;
//add a root node
Category.create({name : 'parent'})
.then(function(rootNode){
   rootNode.addNode({name: 'parent'},
    {
      category : 'A sub category',
      permalink : 'a-sub-category',
      active : true
    })
    .then(function (newItem) {
      console.log(newItem);
    })
    .catch(function (err) {
      console.log('Err', err);
    });
});

Same with the api. First create, then a second call to the addNode

Jcbobo commented 7 years ago

if i make a post call (so a create) i get an other error

{ "error": { "statusCode": 500, "name": "TypeError", "message": "Cannot read property 'toString' of undefined", "stack": "TypeError: Cannot read property 'toString' of undefined\n } }

the url is curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ "name": "test" \ }' 'http://localhost:3000/api/tests'

if i pass parent as empty object instead

{ "error": { "statusCode": 500, "name": "Error", "message": "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters", } }

mbouclas commented 7 years ago

ok, i pushed a fix, so you can have a look. I tested like so

var T = app.models.test;
    T.create({name : 'parent'})
    .then(function(parent){
      T.addNode(parent, {name : 'child'})
      .then(function(child){
        console.log(child)
      })
    });

Then i did

    T.asTree({name : 'parent'},{withParent:true})
  .then(function (results) {
  console.log(results);
});

and sure enough i got the tree back

Jcbobo commented 7 years ago

pushed also on npm ?

mbouclas commented 7 years ago

Yeah, npm should be at 0.0.13 now

On 13 March 2017 at 17:11, Jcbobo88 notifications@github.com wrote:

pushed also on npm ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mbouclas/loopback-ds-tree-mixin/issues/1#issuecomment-286137029, or mute the thread https://github.com/notifications/unsubscribe-auth/AAnOtuC8alrIYLbEIcHgOihd_P87XEdSks5rlVy5gaJpZM4IwtZB .

Jcbobo commented 7 years ago

now it works thanks (i will open an other issue as "question" thanks

Jcbobo commented 7 years ago

@mbouclas i think this can be closed (maybe after a readme update)