Skip to content

relationships

Marian Paul edited this page Feb 23, 2016 · 2 revisions

WAMapping supports two kinds of relationships.

Classics:

{
    "id": 1,
    "first_name": "Marian",
    "enterprise": {
        "id": 1,
        "name": "Wasappli",
        "creation_date": "2013-10-01",
        "address": {
            "street_number": 5149
        }
    }
}
WARelationshipMapping *enterpriseRelationship = 
[WARelationshipMapping relationshipMappingFromSourceProperty:@"enterprise" toDestinationProperty:@"enterprise" withMapping:enterpriseMapping];
[employeeMapping addRelationshipMapping:enterpriseRelationship];

This is the very classic one when you have an object with a relationship description inside the object. Note that there is no limit in terms of hierarchies number.

With identification attribute only

{
    "enterprise": {
        "id": 1,
        "name": "Wasappli",
        "creation_date": "2013-10-01",
        "address": {
            "street_number": 5149
        },
        "chiefs": 1 # Could also be [1, 2, 3] 
    },
    "employees": [{
                  "id": 1,
                  "first_name": "Marian"
                  }]
}
WARelationshipMapping *chiefsRelationship = [WARelationshipMapping relationshipMappingFromSourceIdentificationAttribute:@"chiefs" toDestinationProperty:@"chiefs" withMapping:employeeMapping];
[enterpriseMapping addRelationshipMapping:chiefsRelationship];

This relationship is mostly used when you reuse some objects across your JSON to save bandwidth. For example, assuming a list of books and authors: you would have an array of books, and an array of authors. Each books would have a relationship with authors like "authors": [5, 45, 6]

Under the hood, WAMapping would, when mapping books, fetch the existing authors with the provided ids, assign if present or create if not. Then, when mapping the authors, it would update the authors properties.

Taking one more step:

Sometimes, you have a relationship where you need to care about the merge policy. WAMapping comes with three policies:

  • WARelationshipPolicyAssign -> Default. Replace the pointers without deleting objects from store
  • WARelationshipPolicyMerge -> Replace the object if 1to1 relation ship, or merge if collection
  • WARelationshipPolicyReplace -> Delete the previous object from store and assign new ones

For example, you could add an author to a book, and the server would return something like:

book/12/update

{"authors": [4]}

To tell WAMapping to add the author with an id of 4 to the book, you would have to update the relationship policy for WARelationshipPolicyMerge

authorsRelationship.relationshipPolicy = WARelationshipPolicyMerge;

An other example would be to replace the values. For example, assignees to a task and deleting the previous assignees from store.

assigneesRelationship.relationshipPolicy = WARelationshipPolicyReplace;

But if you want to set new values without deleting objects from store, then you are good to go by keeping WARelationshipPolicyAssign.

Clone this wiki locally