{"action":"created","comment":{"url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/comments/32270797","id":32270797,"diff_hunk":"@@ -623,6 +631,66 @@ Inclusion.include = function (objects, include, options, cb) {\n       }\n     }\n \n+\n+    /**\n+     * Handle Inclusion of Polymorphic HasOne relation\n+     * @param callback\n+     */\n+    function includePolymorphicHasOne(callback) {\n+      var sourceIds = [];\n+      //Map for Indexing objects by their id for faster retrieval\n+      var objIdMap = {};\n+      for (var i = 0; i < objs.length; i++) {\n+        var obj = objs[i];\n+        // one-to-one: foreign key reference is modelTo -> modelFrom.\n+        // use modelFrom.keyFrom in where filter later\n+        var sourceId = obj[relation.keyFrom];\n+        if (sourceId) {\n+          sourceIds.push(sourceId);\n+          objIdMap[sourceId.toString()] = obj;\n+        }\n+        // sourceId can be null. but cache empty data as result\n+        defineCachedRelations(obj);\n+        obj.__cachedRelations[relationName] = null;\n+      }\n+      filter.where[relation.keyTo] = {\n+        inq: uniq(sourceIds)\n+      };\n+      relation.applyScope(null, filter);\n+      relation.modelTo.find(filter, targetFetchHandler);\n+      /**\n+       * Process fetched related objects\n+       * @param err\n+       * @param {Array<Model>} targets\n+       * @returns {*}\n+       */\n+      function targetFetchHandler(err, targets) {\n+        if (err) {\n+          return callback(err);\n+        }\n+        var tasks = [];\n+        //simultaneously process subIncludes\n+        if (subInclude && targets) {\n+          tasks.push(function subIncludesTask(next) {\n+            relation.modelTo.include(targets, subInclude, options, next);\n+          });\n+        }\n+        //process each target object\n+        tasks.push(targetLinkingTask);\n+        function targetLinkingTask(next) {\n+          async.each(targets, linkOneToOne, next);\n+          function linkOneToOne(target, next) {\n+            var sourceId = target[relation.keyTo];\n+            var obj = objIdMap[sourceId.toString()];","path":"lib/include.js","position":97,"original_position":97,"commit_id":"052e22ab439abf2b26a958de27052b7d206ada0e","original_commit_id":"052e22ab439abf2b26a958de27052b7d206ada0e","user":{"login":"fabien","id":2749,"avatar_url":"https://avatars.githubusercontent.com/u/2749?v=3","gravatar_id":"","url":"https://api.github.com/users/fabien","html_url":"https://github.com/fabien","followers_url":"https://api.github.com/users/fabien/followers","following_url":"https://api.github.com/users/fabien/following{/other_user}","gists_url":"https://api.github.com/users/fabien/gists{/gist_id}","starred_url":"https://api.github.com/users/fabien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabien/subscriptions","organizations_url":"https://api.github.com/users/fabien/orgs","repos_url":"https://api.github.com/users/fabien/repos","events_url":"https://api.github.com/users/fabien/events{/privacy}","received_events_url":"https://api.github.com/users/fabien/received_events","type":"User","site_admin":false},"body":"I wonder if we should check that `obj` is actually defined at this stage (defensive coding ...)? There are probably other parts of this code where this might be appropriate.","created_at":"2015-06-11T21:32:13Z","updated_at":"2015-06-11T21:32:13Z","html_url":"https://github.com/strongloop/loopback-datasource-juggler/pull/626#discussion_r32270797","pull_request_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626","_links":{"self":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/comments/32270797"},"html":{"href":"https://github.com/strongloop/loopback-datasource-juggler/pull/626#discussion_r32270797"},"pull_request":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626"}}},"pull_request":{"url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626","id":37504933,"html_url":"https://github.com/strongloop/loopback-datasource-juggler/pull/626","diff_url":"https://github.com/strongloop/loopback-datasource-juggler/pull/626.diff","patch_url":"https://github.com/strongloop/loopback-datasource-juggler/pull/626.patch","issue_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues/626","number":626,"state":"open","locked":false,"title":"Fix for issues #622 & #623","user":{"login":"ningsuhen","id":6159372,"avatar_url":"https://avatars.githubusercontent.com/u/6159372?v=3","gravatar_id":"","url":"https://api.github.com/users/ningsuhen","html_url":"https://github.com/ningsuhen","followers_url":"https://api.github.com/users/ningsuhen/followers","following_url":"https://api.github.com/users/ningsuhen/following{/other_user}","gists_url":"https://api.github.com/users/ningsuhen/gists{/gist_id}","starred_url":"https://api.github.com/users/ningsuhen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ningsuhen/subscriptions","organizations_url":"https://api.github.com/users/ningsuhen/orgs","repos_url":"https://api.github.com/users/ningsuhen/repos","events_url":"https://api.github.com/users/ningsuhen/events{/privacy}","received_events_url":"https://api.github.com/users/ningsuhen/received_events","type":"User","site_admin":false},"body":"polymorphic hasOne needs separate handling and hasMany->referencesMany has a unique case which is fixed for MongoDB as suggested by @fabien.","created_at":"2015-06-11T20:26:54Z","updated_at":"2015-06-11T21:32:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b79ca7455ae9cd7b287df55e674aaf87b6f39efe","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626/commits","review_comments_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626/comments","review_comment_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/comments{/number}","comments_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues/626/comments","statuses_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/statuses/052e22ab439abf2b26a958de27052b7d206ada0e","head":{"label":"walkonsocial:include-issues","ref":"include-issues","sha":"052e22ab439abf2b26a958de27052b7d206ada0e","user":{"login":"walkonsocial","id":11752488,"avatar_url":"https://avatars.githubusercontent.com/u/11752488?v=3","gravatar_id":"","url":"https://api.github.com/users/walkonsocial","html_url":"https://github.com/walkonsocial","followers_url":"https://api.github.com/users/walkonsocial/followers","following_url":"https://api.github.com/users/walkonsocial/following{/other_user}","gists_url":"https://api.github.com/users/walkonsocial/gists{/gist_id}","starred_url":"https://api.github.com/users/walkonsocial/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/walkonsocial/subscriptions","organizations_url":"https://api.github.com/users/walkonsocial/orgs","repos_url":"https://api.github.com/users/walkonsocial/repos","events_url":"https://api.github.com/users/walkonsocial/events{/privacy}","received_events_url":"https://api.github.com/users/walkonsocial/received_events","type":"Organization","site_admin":false},"repo":{"id":34405446,"name":"loopback-datasource-juggler","full_name":"walkonsocial/loopback-datasource-juggler","owner":{"login":"walkonsocial","id":11752488,"avatar_url":"https://avatars.githubusercontent.com/u/11752488?v=3","gravatar_id":"","url":"https://api.github.com/users/walkonsocial","html_url":"https://github.com/walkonsocial","followers_url":"https://api.github.com/users/walkonsocial/followers","following_url":"https://api.github.com/users/walkonsocial/following{/other_user}","gists_url":"https://api.github.com/users/walkonsocial/gists{/gist_id}","starred_url":"https://api.github.com/users/walkonsocial/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/walkonsocial/subscriptions","organizations_url":"https://api.github.com/users/walkonsocial/orgs","repos_url":"https://api.github.com/users/walkonsocial/repos","events_url":"https://api.github.com/users/walkonsocial/events{/privacy}","received_events_url":"https://api.github.com/users/walkonsocial/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/walkonsocial/loopback-datasource-juggler","description":"Connect Loopback to various Data Sources","fork":true,"url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler","forks_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/forks","keys_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/keys{/key_id}","collaborators_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/teams","hooks_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/hooks","issue_events_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/issues/events{/number}","events_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/events","assignees_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/assignees{/user}","branches_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/branches{/branch}","tags_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/tags","blobs_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/git/refs{/sha}","trees_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/git/trees{/sha}","statuses_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/statuses/{sha}","languages_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/languages","stargazers_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/stargazers","contributors_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/contributors","subscribers_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/subscribers","subscription_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/subscription","commits_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/commits{/sha}","git_commits_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/git/commits{/sha}","comments_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/comments{/number}","issue_comment_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/issues/comments{/number}","contents_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/contents/{+path}","compare_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/compare/{base}...{head}","merges_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/merges","archive_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/downloads","issues_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/issues{/number}","pulls_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/pulls{/number}","milestones_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/milestones{/number}","notifications_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/labels{/name}","releases_url":"https://api.github.com/repos/walkonsocial/loopback-datasource-juggler/releases{/id}","created_at":"2015-04-22T17:34:32Z","updated_at":"2015-04-22T17:34:33Z","pushed_at":"2015-06-11T20:25:53Z","git_url":"git://github.com/walkonsocial/loopback-datasource-juggler.git","ssh_url":"git@github.com:walkonsocial/loopback-datasource-juggler.git","clone_url":"https://github.com/walkonsocial/loopback-datasource-juggler.git","svn_url":"https://github.com/walkonsocial/loopback-datasource-juggler","homepage":"www.strongloop.com","size":4473,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"strongloop:master","ref":"master","sha":"ec597ef207915893ed239bdc6ad10be9b3c012e8","user":{"login":"strongloop","id":3020012,"avatar_url":"https://avatars.githubusercontent.com/u/3020012?v=3","gravatar_id":"","url":"https://api.github.com/users/strongloop","html_url":"https://github.com/strongloop","followers_url":"https://api.github.com/users/strongloop/followers","following_url":"https://api.github.com/users/strongloop/following{/other_user}","gists_url":"https://api.github.com/users/strongloop/gists{/gist_id}","starred_url":"https://api.github.com/users/strongloop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/strongloop/subscriptions","organizations_url":"https://api.github.com/users/strongloop/orgs","repos_url":"https://api.github.com/users/strongloop/repos","events_url":"https://api.github.com/users/strongloop/events{/privacy}","received_events_url":"https://api.github.com/users/strongloop/received_events","type":"Organization","site_admin":false},"repo":{"id":10126908,"name":"loopback-datasource-juggler","full_name":"strongloop/loopback-datasource-juggler","owner":{"login":"strongloop","id":3020012,"avatar_url":"https://avatars.githubusercontent.com/u/3020012?v=3","gravatar_id":"","url":"https://api.github.com/users/strongloop","html_url":"https://github.com/strongloop","followers_url":"https://api.github.com/users/strongloop/followers","following_url":"https://api.github.com/users/strongloop/following{/other_user}","gists_url":"https://api.github.com/users/strongloop/gists{/gist_id}","starred_url":"https://api.github.com/users/strongloop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/strongloop/subscriptions","organizations_url":"https://api.github.com/users/strongloop/orgs","repos_url":"https://api.github.com/users/strongloop/repos","events_url":"https://api.github.com/users/strongloop/events{/privacy}","received_events_url":"https://api.github.com/users/strongloop/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/strongloop/loopback-datasource-juggler","description":"Connect Loopback to various Data Sources","fork":false,"url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler","forks_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/forks","keys_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/keys{/key_id}","collaborators_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/teams","hooks_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/hooks","issue_events_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues/events{/number}","events_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/events","assignees_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/assignees{/user}","branches_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/branches{/branch}","tags_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/tags","blobs_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/git/refs{/sha}","trees_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/git/trees{/sha}","statuses_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/statuses/{sha}","languages_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/languages","stargazers_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/stargazers","contributors_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/contributors","subscribers_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/subscribers","subscription_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/subscription","commits_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/commits{/sha}","git_commits_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/git/commits{/sha}","comments_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/comments{/number}","issue_comment_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues/comments{/number}","contents_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/contents/{+path}","compare_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/compare/{base}...{head}","merges_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/merges","archive_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/downloads","issues_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues{/number}","pulls_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls{/number}","milestones_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/milestones{/number}","notifications_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/labels{/name}","releases_url":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/releases{/id}","created_at":"2013-05-17T15:56:04Z","updated_at":"2015-05-26T09:32:16Z","pushed_at":"2015-06-11T20:26:55Z","git_url":"git://github.com/strongloop/loopback-datasource-juggler.git","ssh_url":"git@github.com:strongloop/loopback-datasource-juggler.git","clone_url":"https://github.com/strongloop/loopback-datasource-juggler.git","svn_url":"https://github.com/strongloop/loopback-datasource-juggler","homepage":"www.strongloop.com","size":11819,"stargazers_count":73,"watchers_count":73,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":77,"mirror_url":null,"open_issues_count":131,"forks":77,"open_issues":131,"watchers":73,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626"},"html":{"href":"https://github.com/strongloop/loopback-datasource-juggler/pull/626"},"issue":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues/626"},"comments":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/issues/626/comments"},"review_comments":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626/comments"},"review_comment":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/pulls/626/commits"},"statuses":{"href":"https://api.github.com/repos/strongloop/loopback-datasource-juggler/statuses/052e22ab439abf2b26a958de27052b7d206ada0e"}}}}