dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

hasOne autoFetch reverse calls #706

Open co3moz opened 8 years ago

co3moz commented 8 years ago

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.

dxg commented 8 years ago

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

iceNuts commented 8 years ago

@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.