I'm writing a SPA in Durandal 2.1. So far so good. However, I'm having great difficulties in selecting the right Data Persistence library to communicate with my PHP (Laravel) REST API.
The number one library for Durandal is BreezeJS. It is said that it supports API's without oData / ASP.NET Entity framework. I've searched for many days now, but I can't find any decent resource telling me how to use BreezeJS with my basic PHP REST API.
The wish-list is as follows:
- Persist data between views
- Validate Models
- State tracking (a.k.a. isDirty / cancelChanges)
- GET/POST/PUT/DELETE using REST API calls (in case of PUT, only send the dirty stuff)
The REST API is as follows. I'll use a Contact + Address + Country Model structure to explain how my API works.
- Contact many-to-many Address
- Address belongs-to Country a.k.a. Country has-many Address
The following GET-request:
GET /api/v1/contacts
+ Payload:
{
append_addresses : 1, // add addresses as nested data
append_addresses_country : 1, // add country as nested data of addresses
stack : 2, // return no more than 2 contacts (e.g. for pagination)
page : 1, // return the first page (so the first 2 contacts)
count : 1 // return the total number of existing contacts
}
Returns the following result:
{
"total": 100,
"data": [
{
"id": 1,
"name": "Turner PLC",
"addresses": [
{
"id" : 214,
"country_id" : 1,
"city" : "North Jason",
"country" : {
"id" : 1,
"name" : "Canada"
}
},
{
"id" : 203,
"country_id" : 2,
"city" : "West Lafayette",
"country" : {
"id" : 2,
"name" : "The Netherlands"
}
}
]
}
]
}
The API supports 2 ways to use POST/PUT:
POST/PUT data in the same structure as displayed above under the "data" attribute. Based on the
id
of a Model, the backend will decide if it needs to create OR update a Model. The backend also automatically detects if it needs to create/update/delete relationships, based on the supplied payload. Pretty cool, huh? :-)POST /api/v1/entities...
... with the following Payload:
[
{
"type" : "Contact",
"data" : {
"id" : null, // Will create a new Contact
"name" : "Malaysia"
}
},
{
"type" : "Contact",
"data" : {
"id" : 1, // Will update existing Contact
"name" : "Turner Example"
}
},
{
"type" : "Address",
"data" : {
"id" : 203, // Will update existing Address
"city" : "South Jason",
"contacts_id" : [1,3,5] // Will set/update the many-to-many relationship between Address ID 203 and Contact ID 1, 3 and 5
}
}
]
So now the questions I have:
Can this be done in BreezeJS or should I consider an alternative like JayData or Waterline?
BreezeJS seems te insist using a query like syntax. Am I missing its purpose or is it useless? If so, is it possible to omit the query like syntax and still use BreezeJS or an alternative?
How will BreezeJS (or the alternative) deal with the hasOne, hasMany, belongsTo and belongsToMany and Polymorphic relationships?
Does a "getting started" exist for a similar use case as mine? I've read a lot about BreezeJS and its alternatives, but the pieces of the puzzle are not coming together in my mind yet. In other words, I'm completely lost.