{"action":"created","issue":{"url":"https://api.github.com/repos/katharsis-project/katharsis-core/issues/168","labels_url":"https://api.github.com/repos/katharsis-project/katharsis-core/issues/168/labels{/name}","comments_url":"https://api.github.com/repos/katharsis-project/katharsis-core/issues/168/comments","events_url":"https://api.github.com/repos/katharsis-project/katharsis-core/issues/168/events","html_url":"https://github.com/katharsis-project/katharsis-core/issues/168","id":117919689,"number":168,"title":" Field operations issue with RDBMs","user":{"login":"amareen1988","id":12468439,"avatar_url":"https://avatars.githubusercontent.com/u/12468439?v=3","gravatar_id":"","url":"https://api.github.com/users/amareen1988","html_url":"https://github.com/amareen1988","followers_url":"https://api.github.com/users/amareen1988/followers","following_url":"https://api.github.com/users/amareen1988/following{/other_user}","gists_url":"https://api.github.com/users/amareen1988/gists{/gist_id}","starred_url":"https://api.github.com/users/amareen1988/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amareen1988/subscriptions","organizations_url":"https://api.github.com/users/amareen1988/orgs","repos_url":"https://api.github.com/users/amareen1988/repos","events_url":"https://api.github.com/users/amareen1988/events{/privacy}","received_events_url":"https://api.github.com/users/amareen1988/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-19T22:10:11Z","updated_at":"2015-11-25T19:53:59Z","closed_at":null,"body":"\r\n__NOTE: The case presented below is just an example, but it applies to all field resource methods.__\r\n\r\n__Problem__\r\nLet's assume we would like to perform a field POST, (i.e., __```POST .../parent/{parent_id}/child```__), then the following four steps are executed in order:\r\n   1. Save (Child) Resource --> via Child's Resource Repo\r\n   2. Find (Child) Resource which was just saved --> via Child's Resource Repo\r\n   3. Find (Parent) Resource using parent_id --> via Parent's Resource Repo\r\n   4. Add or Set Relation(s) depending on Cardinality --> via Parent-Child-Relationship-Repo\r\n\r\nThis fine granularity has manny issues:\r\n* It is not atomic i.e., a child can not be added to the parent at one shot\r\n* It is not transactional. What guarantees does katharsis provide If step 1 succeeds, and any subsequent step fails?\r\n* In an RDMS, a child typically has a foreign-key relationship with the parent. Accordingly, attempting to perform step 1 would result in an orphan child due to the absence of the parent's id.\r\n* At step 4, the Relationship Repository interface does not provide child and parent objects at the same time!! even though it is possible.\r\n* Katharsis should return the last state of the newly added child (i.e., return the child object) at step 4. \r\n\r\nIn short, Katharsis assumes that resources are completely independent, but does not take into account sub resources.\r\n\r\n__Suggested Solution__\r\nAdd a Field_Resource_Respository to handle field resource methods (GET, POST, PATCH, DELETE) which exposes the right types (i.e. parent_id and child object) and simply leave the implementation details up to the user. Below is a sample generic repo:\r\n\r\n```\r\npublic interface FieldResourceRepository<RESOURCE, RESOURCE_ID, FIELD, FIELD_ID>{\r\n\tFIELD addField(RESOURCE_ID resource, FIELD field, String fieldName, QueryParams queryParam);\r\n\r\n\tFIELD updateField(RESOURCE_ID resource, FIELD field, String fieldName, QueryParams queryParam);\r\n\r\n\tFIELD deleteField(RESOURCE_ID resource, String fieldName, QueryParams queryParam);\r\n\r\n\t/**\r\n\t * Delete for To-Many Relationships\r\n\t * @param resource\r\n\t * @param fieldName\r\n\t * @param queryParam\r\n\t * @return\r\n\t */\r\n\tIterable<FIELD> deleteFields(RESOURCE_ID resource, Iterable<FIELD_ID> targetIds, String fieldName, QueryParams queryParam);\r\n\r\n\tFIELD getField(RESOURCE_ID resource, String fieldName, QueryParams queryParam);\r\n\r\n\t/**\r\n\t * Get for To-Many Relationships\r\n\t * @param resource\r\n\t * @param fieldName\r\n\t * @param queryParam\r\n\t * @return\r\n\t */\r\n\tIterable<FIELD> getFields(RESOURCE_ID resource, String fieldName, QueryParams queryParam);\r\n}\r\n```\r\nIn the mean while, the existing relationship repository can remain intact to only handle relationship operations. That is, __parent/{parent_id}/relationships/child__"},"comment":{"url":"https://api.github.com/repos/katharsis-project/katharsis-core/issues/comments/159715300","html_url":"https://github.com/katharsis-project/katharsis-core/issues/168#issuecomment-159715300","issue_url":"https://api.github.com/repos/katharsis-project/katharsis-core/issues/168","id":159715300,"user":{"login":"corrspt","id":789263,"avatar_url":"https://avatars.githubusercontent.com/u/789263?v=3","gravatar_id":"","url":"https://api.github.com/users/corrspt","html_url":"https://github.com/corrspt","followers_url":"https://api.github.com/users/corrspt/followers","following_url":"https://api.github.com/users/corrspt/following{/other_user}","gists_url":"https://api.github.com/users/corrspt/gists{/gist_id}","starred_url":"https://api.github.com/users/corrspt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/corrspt/subscriptions","organizations_url":"https://api.github.com/users/corrspt/orgs","repos_url":"https://api.github.com/users/corrspt/repos","events_url":"https://api.github.com/users/corrspt/events{/privacy}","received_events_url":"https://api.github.com/users/corrspt/received_events","type":"User","site_admin":false},"created_at":"2015-11-25T19:53:59Z","updated_at":"2015-11-25T19:53:59Z","body":"I would like to +1 this issue. Or at least to know how to deal with a similar scenario. The canonical example the `Invoice` which has `InvoiceLines`. If I add a new InvoiceLine to the Invoice, at the database level I would like a transaction that saves the new invoice line *and* adds to the list of invoice lines."}}