1

So here is the scenario, I have 3 beacons (BEACON_TABLE) and some products (PRODUCT_TABLE) where each product is associated with one of the beacon (one to many relationship). The data model is working fine with SQL where, I just use a foreign key in my PRODUCT_TABLE to reference a particular beacon id from BEACON_TABLE. I did that in the Entity store (Full stack / no wrapper), where I added a property to the PRODUCT_TABLE and set Reference typed to BEACON_TABLE. When I run the GET method on the BEACON_TABLE resource, it just displays the beacon data, but I was expecting all the products associated with the beacon with the response. A JSON response something like this:

[
   {"beacon_id":"beacon1", "products": [
                                        {"id":"prod1","price":"1.50"},
                                        {"id":"prod2","price":"2.50"}
                                     ]
   },
   {"beacon_id":"beacon2", "products": [
                                        {"id":"prod30","price":"3.95"},
                                        {"id":"prod40","price":"5.15"}
                                     ]
   }
]

How can I achieve it in the Entity Store? I went through the user guide with the user-comment scenario which was pretty unclear to me. Some help will be highly appreciated.

rubikskube
  • 372
  • 1
  • 5
  • 22

1 Answers1

0

APISpark doesn't currently support this feature out of the box this feature. I mean getting elements with its dependencies defined within other entities. I guess that you come from the relational world ;-)

The approach I suggested you (and the one recommended by APISpark) is to denormalize your entity store leveraging the feature "composite field" of APISpark. This approach is common pattern in the noSQL world (APISpark is based by default on a noSQL database) since there is no SQL-like join support.

Using this approach has the advantage of performance since only one request to the database is necessary instead of N+1 (actually more than one).

Here is the way to do for your use case:

  • Entity Beacon
    • Primitive field beacon_id
    • Composite field products (list = true) with sub fields:
    • Primitive field id
    • Primitive field price
  • Entity Product
    • Primitive field id
    • Primitive field price

When adding a field for an entity store, you have the ability to specify the type composite in the dropdown "Type". After that, you will be able to create sub fields directly on this field (icon + nearby the field name).

The main drawback is that you need to make two updates when you want to update products. I guess that it's something not really frequent, so this approach can be acceptable.

Feel free to ask me more questions if it's not clear!

Hope it will help you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Thank you very much @thierry for your quick response, yes you are right, i was thinking from the relational viewpoint, i will soon try your approach.. – rubikskube Apr 30 '15 at 13:47