{"action":"created","issue":{"url":"https://api.github.com/repos/vapor/fluent/issues/597","repository_url":"https://api.github.com/repos/vapor/fluent","labels_url":"https://api.github.com/repos/vapor/fluent/issues/597/labels{/name}","comments_url":"https://api.github.com/repos/vapor/fluent/issues/597/comments","events_url":"https://api.github.com/repos/vapor/fluent/issues/597/events","html_url":"https://github.com/vapor/fluent/issues/597","id":387797307,"node_id":"MDU6SXNzdWUzODc3OTczMDc=","number":597,"title":"Database may need Model information during encoding","user":{"login":"valeriomazzeo","id":1882080,"node_id":"MDQ6VXNlcjE4ODIwODA=","avatar_url":"https://avatars3.githubusercontent.com/u/1882080?v=4","gravatar_id":"","url":"https://api.github.com/users/valeriomazzeo","html_url":"https://github.com/valeriomazzeo","followers_url":"https://api.github.com/users/valeriomazzeo/followers","following_url":"https://api.github.com/users/valeriomazzeo/following{/other_user}","gists_url":"https://api.github.com/users/valeriomazzeo/gists{/gist_id}","starred_url":"https://api.github.com/users/valeriomazzeo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/valeriomazzeo/subscriptions","organizations_url":"https://api.github.com/users/valeriomazzeo/orgs","repos_url":"https://api.github.com/users/valeriomazzeo/repos","events_url":"https://api.github.com/users/valeriomazzeo/events{/privacy}","received_events_url":"https://api.github.com/users/valeriomazzeo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-12-05T15:02:12Z","updated_at":"2018-12-10T09:40:02Z","closed_at":null,"author_association":"NONE","body":"I am working on a mongo integration for Fluent.\r\n\r\nIn Fluent is possible to specify the `idKey` path for a specific `Model`.\r\nFor example:\r\n```swift\r\nstruct User: Model {\r\n  static var idKey: WritableKeyPath<User, UUID?> { return \\.id }\r\n  var id: UUID?\r\n}\r\n```\r\nProblem is, in mongo, documents have a fixed `_id` field.\r\n\r\nI want to `encode` my `User` model so that `id` becomes `_id` when encoded/decoded to the `Database.Output` type.\r\n\r\nIn general I think `Model`s should be free to define their `id` in whatever way they want, after all that's the purpose of having `fluentID` and `idKey` in the `Model`.\r\n\r\nThe database/driver should be in charge of transforming `id` to `_id` (in the above example).\r\nNow this is not possible at the moment, because `Codable` delegates the serialisation to the model itself.\r\n\r\nIt feels that the database should be aware of the specific `Model` during encoding/decoding. In order to be able to access the `idKey` and modify the generated output accordingly.\r\n\r\nFrom `QuerySupporting`:\r\n`static func queryEncode<E>(_ encodable: E, entity: String) throws -> Output where E: Encodable`\r\n\r\n@tanner0101 any idea?"},"comment":{"url":"https://api.github.com/repos/vapor/fluent/issues/comments/445751232","html_url":"https://github.com/vapor/fluent/issues/597#issuecomment-445751232","issue_url":"https://api.github.com/repos/vapor/fluent/issues/597","id":445751232,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0NTc1MTIzMg==","user":{"login":"valeriomazzeo","id":1882080,"node_id":"MDQ6VXNlcjE4ODIwODA=","avatar_url":"https://avatars3.githubusercontent.com/u/1882080?v=4","gravatar_id":"","url":"https://api.github.com/users/valeriomazzeo","html_url":"https://github.com/valeriomazzeo","followers_url":"https://api.github.com/users/valeriomazzeo/followers","following_url":"https://api.github.com/users/valeriomazzeo/following{/other_user}","gists_url":"https://api.github.com/users/valeriomazzeo/gists{/gist_id}","starred_url":"https://api.github.com/users/valeriomazzeo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/valeriomazzeo/subscriptions","organizations_url":"https://api.github.com/users/valeriomazzeo/orgs","repos_url":"https://api.github.com/users/valeriomazzeo/repos","events_url":"https://api.github.com/users/valeriomazzeo/events{/privacy}","received_events_url":"https://api.github.com/users/valeriomazzeo/received_events","type":"User","site_admin":false},"created_at":"2018-12-10T09:40:02Z","updated_at":"2018-12-10T09:40:02Z","author_association":"NONE","body":"Yes, but then you'd have to to specify all the keys. As far as I know, it is not possible to just instruct Swift to specify manually one key and automatically synthesise the rest.\r\n\r\nAfter talking with @tanner0101 he recommended to specify a custom model protocol that requires the `_id` variable."}}
{"actor":{"display_login":"valeriomazzeo"}}