-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populate should throw upon detecting an ORM result. #144
Comments
Hey, I have figured out the issue, and I would like to voice my findings (and some opinions) on this: Most importantly (strong opinion) - I do not think we should force users to use Second most importantly - the // sequelize-raw-hook.js
function (hook) {
hook.params.sequelize = Object.assign({}, hook.params.sequelize, { raw: true });
return Promise.resolve(hook);
} The solution - the issue the ORM folks have been experiencing is solved by checking if an object implements var obj = {};
obj.toJSON = function () { return "hello"; };
JSON.stringify(obj); //-> "hello" I will be submitting a PR to fix this issue. Hopefully it makes it :) |
The problem with ORMs is not limited to We could change the hook utility daffl and I have talked a bunch about this issue and the conclusion was that feathers-sequelize and feathers-mongoose should default to I'm not sure if this has made it into Auk yet. I would talk this over with @daffl before submitting a PR. |
Ah, this makes sense - thanks for pointing out what I was not seeing. Then maybe I would flip my stance on this. The thing that doesn't really sit well with me is asking users to use
Then users can opt into ORM instantiation. The middleware is quite simple and could even ship with the adapters for ease of use: // feathers-sequelize/instantiate-hook.js
function (hook) {
if (!hook.type !== 'after') throw new Error('Instantiate hook must be used in "after" phase');
if (hook.result.data) {
hook.result.data = hook.result.data.map(item => this.Model.build(item));
} else {
hook.result = this.Model(hook.result);
}
return hook;
} |
That's exactly what we did already for Mongoose so it would only be consistent to also do it for Sequelize. Feathers expects plain objects and almost every issue that comes up in hooks is either a misuse of Promises or an ORM model acting strangely. |
Related #150 |
Implemented in #169 now that @DesignByOnyx 's PRs have been merged which result in feathers-sequelize return POJO by default. |
This means the best practice is not set raw? |
Configure raw mode when you configure the service. The sequelize adapter has some helper funcs to switch back to ORM only when you need to. |
A substantial percentage of populate issues come down to the DB not being confirmed to return plain JS objects. Mongoose and Serialize return ORM's particular to their DB.
The problem is not solved by having
populate
automatically convert these ORM's to JS objects because all hooks except JS objects. We could changeget-items.js
to convert the ORM's but Feathers in general expects JS objects.We have decided its more generic to change the defaults for Mongoose and Sequelize to return JS objects, so a user has to explicitly op-out to return an ORM. I'm not sure if this change has been pushed yet but its not prevelant in the field.
To save developers' and core-members' time,
populate
should check for ORM results and throw an explanatory message.The text was updated successfully, but these errors were encountered: