Open
Description
In current implementation data models highly coupled with data storage. I think it will be better if we will use POJO augmented with decorators for data models. I.e. all methods that change data in underlying storage (currently, save()
) should be moved from JsonApiModel
to JsonApiDataStore
.
Typical interaction with data storage can look like:
// create record
let author = new AuthorModel();
storage.saveRecord(author);
// updating record
storage.findRecord(BookModel, '1').subscribe(
(book) => {
book.title = "New title";
storage.saveRecord(book);
}
);
// deleting record
storage.findRecord(BookModel, '1').subscribe(
(book) => storage.deleteRecord(book);
);
This approach have following advantages:
- data models can be plain old javascript objects without dependency on
JsonApiDataStorage
. It can be easily created and tested. - in the future we can easily support multiple operations in the single request (Support for multiple operations in a single request json-api/json-api#795). Something like:
storage.beginTransaction();
/* posts: BlogPostModel[] */
posts.forEach((post) => {
post.published = true;
storage.saveRecord(post); // request will be delayed because of transaction
});
storage.commit(); // update all posts in underlying storage using single request