node-orm2 icon indicating copy to clipboard operation
node-orm2 copied to clipboard

hasOne autoFetch reverse calls

Open co3moz opened this issue 9 years ago • 2 comments

Hello guys,

Is it possible that hasOne's autoFetch should work only "reverse" calls?

var User = require('./User');

var Message = db.define('Message', {
  id: {type: 'serial', key: true},
  subject: String,
  content: String,
  sentDate: Date,
  read: Boolean
});

Message.hasOne('user', User, {
  reverse: 'messages',
  field: 'userId',
  autoFetch: true
});

module.exports = Message;

I'm fetching user and i get all messages but i have some more relations with user so when i fetch user they came too. They have also other relations so it comes with big tree... Anyways to get big tree i'm changing autoFetchLimit but this time user-> message-> user -> message.. comes too.

I clone the main object and delete repeated objects.. It's very lame solution. Can we find simplier solution to this problem? Maybe there should be autoFetchOnlyOneToMany option? With this we just don't fetch message->user.

co3moz avatar Feb 21 '16 18:02 co3moz

I'm starting to think autoFetch isn't such a great idea. Something like explicit .eager is a lot safer.

dxg avatar Feb 25 '16 21:02 dxg

@dxg autoFetch in hasOne 's behavior is extremely bad, it consume much memory and even more slows down a lot. I am highly doubt if this orm has any optimization, because when I use raw sql it only takes 200ms work but using orm it usually takes 2000ms, the main reason is how autoFetch work. I think this orm is overrated by so many stars. And I am regret using it as my data layer for my production.

iceNuts avatar Mar 22 '16 02:03 iceNuts