Skip to content
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

findOne convenience function #148

Closed
jogly opened this issue May 5, 2013 · 5 comments
Closed

findOne convenience function #148

jogly opened this issue May 5, 2013 · 5 comments

Comments

@jogly
Copy link
Collaborator

jogly commented May 5, 2013

If you can fit a quick nice feature into your next release, I would like a findOne function in addition to the find(...).limit(1) functionality. This would make integration with user validation/authentication a little cleaner IMO.

Thank you, I'm very appreciative of this library. There are not very many good ORM libraries for MySQL.

@dresende
Copy link
Owner

dresende commented May 5, 2013

There is a Model.all so probably I'll create a Model.one. In your case it shouldn't be possible because you should have at most 1 result or your authentication might have a breach :) I'll commit it soon and release it this week.

@jogly
Copy link
Collaborator Author

jogly commented May 6, 2013

Haha, yes, having more than one result would be indicative of an issue. More along the lines of what I hoped to solve with this idea is eliminating the var user = users[0] from the following snippet:

User.find({ username: username }, function (err, users) {
  var user = users[0];
  ...

with

// findOne or one, syntax is obviously up to you. 
// I'm familiar with the findOne vs. find from the Mongoose API.
User.[findOne | one]({ username: username }, function (err, user) {
  // no need to access the first element.
  ...

Will the alias Model.one() cover this concept? From my understanding (which may very well be incorrect), even if it is a limit of 1 it will still return an array, albeit with a length of 1.

@dxg
Copy link
Collaborator

dxg commented May 6, 2013

Could also call it 'first()'.
i too would find it useful if it just returned the item rather than an array.

@dresende
Copy link
Owner

dresende commented May 6, 2013

Yes, the simple implementation I added will return an Array of results. I'll have to change it.

@dresende
Copy link
Owner

dresende commented May 6, 2013

For now it forces a callback, you can't do this:

Model.one(....).run(function (...) {
});

For now, only:

Model.one(...., function (...) {
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants