{"action":"created","issue":{"url":"https://api.github.com/repos/typicode/json-server/issues/736","repository_url":"https://api.github.com/repos/typicode/json-server","labels_url":"https://api.github.com/repos/typicode/json-server/issues/736/labels{/name}","comments_url":"https://api.github.com/repos/typicode/json-server/issues/736/comments","events_url":"https://api.github.com/repos/typicode/json-server/issues/736/events","html_url":"https://github.com/typicode/json-server/issues/736","id":303646134,"node_id":"MDU6SXNzdWUzMDM2NDYxMzQ=","number":736,"title":"PATCHing with a child array","user":{"login":"sigmazen","id":7496847,"node_id":"MDQ6VXNlcjc0OTY4NDc=","avatar_url":"https://avatars3.githubusercontent.com/u/7496847?v=4","gravatar_id":"","url":"https://api.github.com/users/sigmazen","html_url":"https://github.com/sigmazen","followers_url":"https://api.github.com/users/sigmazen/followers","following_url":"https://api.github.com/users/sigmazen/following{/other_user}","gists_url":"https://api.github.com/users/sigmazen/gists{/gist_id}","starred_url":"https://api.github.com/users/sigmazen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sigmazen/subscriptions","organizations_url":"https://api.github.com/users/sigmazen/orgs","repos_url":"https://api.github.com/users/sigmazen/repos","events_url":"https://api.github.com/users/sigmazen/events{/privacy}","received_events_url":"https://api.github.com/users/sigmazen/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-08T21:54:22Z","updated_at":"2018-12-07T10:50:26Z","closed_at":"2018-03-23T23:21:19Z","author_association":"NONE","body":"Hi\r\nQuestion about PATCHing with a child array.  My data is shown below.\r\n\r\nIf I run a simple `PATCH http://localhost:3000/users/2 { \"name\": \"Jim\" }` everything works as expected and Sam changes to Jim.\r\n\r\nNow if my PATCH value is thus:\r\n    { \"address_list\": [ { \"address_type\": \"Home\", \"address\": \"99 West Street\" } ] }\r\nduring the PATCH I want to update the `address` and add the `updated_date`\r\n\r\nSo I presume I need to intercept the PATCH and add a new element before the next().\r\n\r\nIn the following code, the new \"test\":\"Test\" is appended at the end but when the update occurs to the Home address it wipes all the address_list values out and replaces the entire array with the incoming address rather than updating only what is specified.\r\n\r\n    server.use(jsonServer.bodyParser)\r\n    server.use((req, res, next) => {\r\n      if (req.method === 'PATCH') {\r\n        req.body.test = \"Test\"\r\n        req.body.address_list[0].updated_date = moment().format()\r\n      }\r\n      next()\r\n    })\r\n\r\n\r\nIf I code it manually it works okay:\r\n\r\n    db.get('users')\r\n      .find({id: 2})\r\n      .get('address_list')\r\n      .filter((address) => {return address.address_type === 'Home'})\r\n      .get('[0]')\r\n      .assign({ address: reqBody.address_list[0].address })\r\n      .assign({ updated_date: moment().format() })\r\n      .write()\r\n\r\nDo I essentially need to write my own PATCH routine to accomplish this?\r\nThanks in advance\r\nCheers\r\n\r\n    {\r\n      \"users\": [\r\n        {\r\n          \"id\": 1,\r\n          \"name\": \"Joe\",\r\n          \"address_list\": [\r\n            {\r\n              \"address_type\": \"Home\",\r\n              \"address\": \"7 High Street\",\r\n              \"created_date\": \"2018-01-08\",\r\n              \"updated_date\": \"\"\r\n            },\r\n            {\r\n              \"address_type\": \"Business\",\r\n              \"address\": \"9 Middle Street\",\r\n              \"created_date\": \"2018-01-09\",\r\n              \"updated_date\": \"\"\r\n            }\r\n          ]\r\n        },\r\n        {\r\n          \"id\": 2,\r\n          \"name\": \"Sam\",\r\n          \"address_list\": [\r\n            {\r\n              \"address_type\": \"Home\",\r\n              \"address\": \"99 Portland Drive\",\r\n              \"created_date\": \"2018-01-08\",\r\n              \"updated_date\": \"\"\r\n            },\r\n            {\r\n              \"address_type\": \"Business\",\r\n              \"address\": \"99 Elm Road\",\r\n              \"created_date\": \"2018-01-09\",\r\n              \"updated_date\": \"\"\r\n            }\r\n          ]\r\n        }\r\n      ]\r\n    }"},"comment":{"url":"https://api.github.com/repos/typicode/json-server/issues/comments/445195691","html_url":"https://github.com/typicode/json-server/issues/736#issuecomment-445195691","issue_url":"https://api.github.com/repos/typicode/json-server/issues/736","id":445195691,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0NTE5NTY5MQ==","user":{"login":"sambokai","id":7689616,"node_id":"MDQ6VXNlcjc2ODk2MTY=","avatar_url":"https://avatars3.githubusercontent.com/u/7689616?v=4","gravatar_id":"","url":"https://api.github.com/users/sambokai","html_url":"https://github.com/sambokai","followers_url":"https://api.github.com/users/sambokai/followers","following_url":"https://api.github.com/users/sambokai/following{/other_user}","gists_url":"https://api.github.com/users/sambokai/gists{/gist_id}","starred_url":"https://api.github.com/users/sambokai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sambokai/subscriptions","organizations_url":"https://api.github.com/users/sambokai/orgs","repos_url":"https://api.github.com/users/sambokai/repos","events_url":"https://api.github.com/users/sambokai/events{/privacy}","received_events_url":"https://api.github.com/users/sambokai/received_events","type":"User","site_admin":false},"created_at":"2018-12-07T10:50:26Z","updated_at":"2018-12-07T10:50:26Z","author_association":"NONE","body":"> I ended up pulling the complete, original record from the db\r\n\r\nCan you share how you did that? Thanks!"}}
{"actor":{"display_login":"sambokai"}}