backand

Backand - use own db row id as primary key


Is it possible to specify your own row ID in backand objects? I want to form one-to-one relationsships in database, where I have two objects: userPrivateData and userProfile

I would like to use the id of Backands own internal user object as primary key for both objects. So basically a userId. The reason for this: With REST api you can access single items with GET /1/objects/{name}/{id}

Since I don't know the row id I want to use another unique identiefier as primary key, that I know - my user id.

Is this possible? Maybe I have the concept all wrong, in their docs they talk a lot about one-to-many relationships and stuff, but not about one-to-one relationships.


Solution

  • I think your JSON model would look something like this.

        [
      {
        "name": "items",
        "fields": {
          "name": {
            "type": "string"
          },
          "description": {
            "type": "text"
          },
          "user": {
            "object": "users"
          }
        }
      },
      {
        "name": "users",
        "fields": {
          "items": {
            "collection": "items",
            "via": "user"
          },
          "allofuserprivatedata": {
            "collection": "userPrivateDataThings",
            "via": "userId"
          },
          "email": {
            "type": "string"
          },
          "firstName": {
            "type": "string"
          },
          "lastName": {
            "type": "string"
          },
          "userProfile": {
            "object": "userProfile"
          }
        }
      },
      {
        "name": "userProfile",
        "fields": {
          "userid": {
            "collection": "users",
            "via": "userProfile"
          },
          "fieldOne": {
            "type": "string"
          },
          "fieldTwo": {
            "type": "boolean"
          }
        }
      },
      {
        "name": "userPrivateDataThings",
        "fields": {
          "field1": {
            "type": "string"
          },
          "field2": {
            "type": "float"
          },
          "userId": {
            "object": "users"
          }
        }
      }
    ]
    

    Use a collection when you want 1 to many. Use an object when you want 1 to 1.

    When you do a GET on user with deep:true the nested relationship data will come back in the response.