JayData and relationships

Author: Robert Bonay June 12th, 2013


JayData supports relationships since the first version, but it worths a short post to cover this feature.

If your application does more than saving and retrieving key-value pairs for configuration, you might arrive to a point when you want to manage relationships. JayData is able to manage relationships with navigations properties, so you don’t have to join the related tables by object identifiers. This feature – meaning the include() operation too – is available in the following data providers: OData, WebSQL/SQLite, WebSQL/SQLite Pro, MongoDB, MongoDB Pro.

Typed navigation properties can be defined easily by declaring the entity definitions.

1:N relationships

We will use the well-know Northwind example to understand the navigation properties.

Take notice of the inverseProperty in the declaration, which is necessary to be defined to help JayData to detect the other side of the relationship.

Once we instantiated our context, we can work with our database.

Try this code in a live example on JSFiddle!

In this snippet we access our database inside the onReady() of the context just to be sure that the context creation had finished. This example shows how to create a new product with an existing category by attaching the existing category to the context to track the changes of the objects and mark the state of category. Of course you can create a new category at a the same time, in this case you don’t have to use the attach() function.