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

low level parent/ancestor manipulation #3

Closed Jcbobo closed 7 years ago

Jcbobo commented 7 years ago

for design reason i should handle a situation a little "strange"

  1. collection (tree) called test
  2. collection local
  3. collection called test-local

each time an entry is added to the local collection i need to replicate the entire collection test in the test-local collection (adding exstra information and manipulating some fields) each time an entry in the test collection is modified (basically on the tree structure) i need to update accordly the tree structure in test-local

if I understand correcly i can use the "create" nodes for handling the replication and when i need to update the tree structure (basically each time the moveNode) is called i retreave the test-local node (replicated from test node that i moved), retreate the test-local node (replicated from the test node where i'm moving) and call a moveNode accordly on the "test-local" collection,

there is some nice way to handle that? what is coming to my mind is, custom event (node created, node moved, node deleted)

if you have have some idea that can "simplify" my job will be great

mbouclas commented 7 years ago

You can always use the native loopback Model events to track operations that have to do with the DB. I also added some events here that might help. Other than that i can't think of anything else

Jcbobo commented 7 years ago

that's great, do you have the time to provide some example?

mbouclas commented 7 years ago

check the event bus here https://loopback.io/doc/en/lb2/Events.html

As for the mixin

app.on('lbTree.add.success', function(node){
console.log(node)
})
Jcbobo commented 7 years ago

so putting all together i can use the normal "save/update" event for handle the data replication and the evento of the tree for the tree operation, perfect thanks you

Jcbobo commented 7 years ago

@mbouclas after this modification calling the addNode method (from the explorer) give a validation error (parent is required) also if is passed

{ "error": { "statusCode": 400, "name": "Error", "message": "parent is a required argument", "stack": "Error: parent is a required argument\n" }

curl curl -X PUT --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'parent=%7B%22id%22%3A%20%2258c6b762189b4e1b380e0394%22%7D&node=%7B%20%20%20%20%20%22name%22%3A%20%22WWWWWWWWW%22%20%20%20%20%20%20%20%20%20%7D' 'http://localhost:3050/api/tests/addNode'