dresende / node-orm2

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

question: selecting specific attributes for related models #472

Open eshell opened 10 years ago

eshell commented 10 years ago

Hi, i'm looking to use an orm instead of doing all the sql manually as i have been. How would you select certain columns on related models?

For example.

// models
User('id','email','password')
Info('user_id','some','other','info')

What if you wanted all users, selecting only the email column, along with Info model selecting only 'some' and 'info' columns?

bvallelunga commented 10 years ago

To get only the email field from all users in the users model

User.all().only("email").run(function (error, users) {
    // NOTE: the users array of objects will contain a
    // skeleton of the model where only the email field has a value
    // meaning users array may look like
   [ {
     id: [Getter/Setter], // => null
     email: [Getter/Setter], // => "joe@bob.com"
     password: [Getter/Setter] // => null
   } ]
});

take a look at https://github.com/dresende/node-orm2/wiki/Finding-items