dresende / node-orm2

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

How do you implement the find_or_create function? #757

Open ronald2wing opened 7 years ago

ronald2wing commented 7 years ago

I find it difficult to implement this functionality. Has anyone done it before?

ronald2wing commented 7 years ago
    Chat.count({ id: chatId }, function(err, count) {
      if (count === 0) {
        Chat.create({ id: chatId }, (err) => {
          console.log(err);
        });
      }
    });

Right now I am combining count and create to create a similar method. Is there a better way to do this?