Milkncookiez
Milkncookiez

Reputation: 7397

How to implement nesting of a model itself - Mongodb & Sails

So far I've got it figured out with these 3 models:

User model

module.exports = {
    schema: true,

    attributes: {
        // Relations
        maps: {
            collection: 'Map'
        },
    }
};

Map model

module.exports = {
    schema: true,

    attributes: {
        // Relations
        owner: {
            model: 'User'
        },

        spots: {
            collection: 'Spot',
            via: 'map'
        },
    }
};

Spot model

module.exports = {
    schema: true,

    attributes: {
        // Relations
        map: {
            model: 'Map'
        },

        parentSpot: {
            model: 'Spot'
        },

        subSpotss: {
            collection: 'Spot',
            via: 'parentSpot'
        },
    }
};

So, if I query a Map, by user, I will get, i.e. this:

{
  "owner": "1",
  "id": "1",
  "spots": [
    {
      "map": "1",
      "title": "Test Spot",
      "content_text": "asdasdasdasdasd",
      "createdAt": "2015-07-14T15:39:50.066Z",
      "updatedAt": "2015-07-14T15:39:50.066Z",
      "id": "1"
    },
    {
      "map": "1",
      "title": "Another Spot",
      "content_text": "hue hue hue hue hue",
      "createdAt": "2015-07-14T15:40:17.313Z",
      "updatedAt": "2015-07-14T15:40:17.313Z",
      "id": "2"
    }
  ],
  "createdAt": "2015-07-14T15:38:32.571Z",
  "updatedAt": "2015-07-14T15:38:32.571Z"
}

What I want, additionally, is to nest other spots inside spots, so I have a result like this:

{
      "owner": "1",
      "id": "1",
      "spots": [
        {
          "map": "1",
          "title": "Test Spot",
          "content_text": "asdasdasdasdasd",
          "spots": [
            {
              "map": "1",
              "title": "Nested Spot",
              "content_text": "dfgsdsfasdf",
              "spots": [
                {
                  "map": "1",
                  "title": "Another Nested Spot",
                  "content_text": "sometesxtisdjfiasj",
                  "spots": [
                    {
                       // more nested levels here
                    },
                    {
                       // more nested levels here
                    },
                  ],
                  "createdAt": "2015-07-14T15:39:50.066Z",
                  "updatedAt": "2015-07-14T15:39:50.066Z",
                  "id": "5"
                },
                {
                  // more nested levels here
                },
              ],
              "createdAt": "2015-07-14T15:39:50.066Z",
              "updatedAt": "2015-07-14T15:39:50.066Z",
              "id": "3"
            },
            {
              // another nested Spot which can have a collections of
              // more Nested spots
            },
            {
              // one more nested Spot which can have a collections of
              // more Nested spots
            }
          ],
          "createdAt": "2015-07-14T15:39:50.066Z",
          "updatedAt": "2015-07-14T15:39:50.066Z",
          "id": "1"
        },

So, basically, inside a single Map I want to have multiple "starting" Spots, which can have nested levels inside of them. I was searching for something, but could only find Tree-related examples, which go only left and right, and I want to have more than 2 options.

How can I write that in the Sails model? And is this feasible at all? A suggestion of better design is welcome.

Upvotes: 1

Views: 114

Answers (1)

Andi N. Dirgantara
Andi N. Dirgantara

Reputation: 2051

Your model is right. It's because sails currently don't support populate at nested model. You can override default query by doing like in this solution.

Upvotes: 1

Related Questions