backand

Backand (Back&) schema setup for user favorites


I am struggling with setting up the notion of user favorites in the Backand schema editor. An example for what I am trying to do is as follows:

Say I have a website selling cars. Users are authenticated and on any car they like they can "favorite" that car. When favorited, that specific car object or id should be added to the users favorites property so that the user can return to or call upon their favorites list at any time.

In Back&s schema editor I have added a collection property to my user object but I am completely confused because this just creates a 1 to many relationship.

The Back& documentation is pretty lacking at this point so I figured I'd bring it up here so others who run into this can see it as well. What is the best way to accomplish this common functionality in Back&? Any help would be greatly appreciated.


Solution

  • I think what you are actually trying to do needs a many-to-many relation which is well documents here and has a code pen example here.

    i.e. each user can favorite many cars and each car can be a favorite of many users so the json would look somthing like this

    [
    {
        "name": "user_car",
        "fields": {
    
          "user": {
            "object": "user"
          },
          "car": {
            "object": "car"
          }
        }
      },
      {
        "name": "user",
        "fields": {
          "user_car": {
            "collection": "user_car",
            "via": "car"
          },
    
          "email": {
            "type": "string"
          },
          "firstName": {
            "type": "string"
          },
          "lastName": {
            "type": "string"
          }
        }
      },
      {
        "name": "car",
        "fields": {
          "user_car": {
            "collection": "user_car",
            "via": "car"
          },
          "name": {
            "type": "string"
          },
          "model": {
            "type": "int"
          }
        }
      }
    }
    ]