0

I want to create a Tree like storage structure to be used with my app, but can't find enough documentation on how to create a tree model using Waterline attributes.

Case is simple. I do need to have a set of folders, that can have multiple levels of subfolders and in the end, files. What you usually do in mySQL for this kind of data is to add a parent_id field to your model as a foreign key to the model itself.

How can this be done using attributes in sailsjs/waterline model?

I've tried doing something like the following, which generates quite a bit of redundant and orphaned data:

--

attributes: {
    name: {
       type: 'string'
    },
    parentFolder: {
      model: 'Folder'
    },
    childFolders: {
      model: 'Folder',
      via: 'parentItem'
    }
}

-- Any ideas?

And by the way, if this is possible, let's say for example using mySQL as a backend. How will it replicate to say, mongoDB?

afterxleep
  • 622
  • 1
  • 5
  • 17
  • can you explain what exactly is failing you with your current method? How is redundant / orphaned data being created? – Meeker Oct 14 '15 at 00:49

1 Answers1

1

This seemed to work:

    name: {
        type: 'string',
        maxLength: 255,
        required: true
    },

    parent: {
        model: 'folder'
    },

    childs: {
        collection: 'folder',
        via: 'parent'
    }

I do believe duplicates were being generated by posting data directly via GET in the browser. I'm posting data with a client via POST and it seems to work as expected. (At least from what I see in mySQL)

afterxleep
  • 622
  • 1
  • 5
  • 17