Skip to content

findOne convenience function #148

Closed
Closed

Description

@jogly

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.

Activity

  1. dresende commented on May 5, 2013

    @dresende
    Owner

    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.

  2. jogly commented on May 6, 2013

    @jogly
    CollaboratorAuthor

    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.

  3. dxg commented on May 6, 2013

    @dxg
    Collaborator

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

  4. dresende commented on May 6, 2013

    @dresende
    Owner

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

  5. dresende commented on May 6, 2013

    @dresende
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions