Im working on an social app (multi-user) as well as the related backend (REST API) for that project.
Im wondering what is a good practice to handle the data-models. Since I thought i is the state of the art to use ember-data I based my whole construct so far on it.
Now im realizing, that Im getting into some trouble in the way I want to handle the data.
Lets say I have a route where I can create a new “Workgroup” or something. A “Workgroup” belongs to a “Office”-Record. My User has a List of many Workgroups.
User
name: DS.attr(‘string’), myGroups: DS.hasMany(‘workgroup’, {reverse: creator})
Workgroup
creator: DS.belongsTo(‘user’), office: DS.belongsTo(‘office’)
Office
workgroup: DS.hasMany(‘workgroup’)
On my route where I can create the new Workgroup, I have to chain a lot of steps to instanciate the models on the client side (in ember-data’s store) and save() them to the REST-Server. Like:
store.find('user', curentUserID)
.then(function(_currentUser){
var newWorkgroup = store.createRecord('Workgroup' function(), {
creator: _currentUser,
office: null
});
newWorkgroup.save().then(...);
});
This is getting more and more complex when you see that you have also create a record for office in the same step and put it as a reference in the new ‘workgroup.office’-field (in that case simply ‘null’).
However when Im acting like that, all the business-logic of creating and manipulating models is putted into the client and just “saved” to the Backend which is acting like a CRUD-Server if you want to say so. The problem is, that this is horrible unsecure in my opinion since the client could send whatever he wants as IDs in the “relationship between the models” fields (he could save ID’s of workgroups in his own user-records ‘myGroups’-field of workinggroup-records which belongs to a complete different person for example).
Also since all three models up there have two-ways-relationships I have to write a lot of code (an a very long and ugly “then”-chain) to
- get the user
- create the workgroup
- update the workgroup relationship (id) within the office-record
- update the myGroups relationship (id’s) within the user-record (myWorkgroups)
In that case it may be not so hard to realize that. But in more complex usecases I have to handle with a big bunch of models at once I fear.
Therefore Im wondering if ember-data is a good choice for such a project. Do you have Ideas about that? Do you use ember-data
or something else (maybe every model-action just running on server with an usecase fitting API like myserver.local:3000/api/createANewWorkgroup
)