Proper way to create a sequence on a model property

1,759 views
Skip to first unread message

Warren Bell

unread,
Dec 28, 2014, 11:52:44 AM12/28/14
to loopb...@googlegroups.com
I have a model named SpecialOrder with a property named orderNumber. I want it to start out with a value of 1 and increment each time a new model is created. The model is persisted in a mongo db. Does loopback have a way of doing this without me having to manually keep track of the value ?

Thanks,

Warren Bell

Warren Bell

unread,
Jan 2, 2015, 11:20:59 AM1/2/15
to loopb...@googlegroups.com

Ryan Schumacher

unread,
Jan 3, 2015, 4:22:51 PM1/3/15
to loopb...@googlegroups.com
Since you are using MongoDB you'd want to take this in consideration: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

Then I would suggest adding a `beforeCreate` hook: http://docs.strongloop.com/display/public/LB/Model+hooks#Modelhooks-beforeCreate

```

SpecialOrder.beforeCreate = function(next, model) {
  
  model.id = getNextSequence('SpecialOrder');

  next();

};

```

The getNextSequence function would be a function wrapping the MongoDB reference above.

Warren Bell

unread,
Jan 6, 2015, 4:37:38 PM1/6/15
to loopb...@googlegroups.com
Thanks for your response. I can't find any findAndModify method anywhere in Loopback. Still digging around in the mongodb connector or mongoose. How would I do this from Loopback ?

db.counters.findAndModify(
          {
            query: { _id: name },
            update: { $inc: { seq: 1 } },
            new: true
          }
   );
I know "counters" is the name of the collection in this example and my query and update would be different.

Thanks,

Warren Bell

Warren Bell

unread,
Jan 6, 2015, 11:09:06 PM1/6/15
to loopb...@googlegroups.com
Ok, I think I am getting close. This solution works but I am bypassing Loopback and the "mongoDB connector". Here is what I am doing.

Given a model named Sequence that looks like this:

{
    "_id": {
        "$oid": "54ab3ec0cc074e24144f26d7"
    },
    "collection": "SpecialOrder",
    "value": 113
}

I am doing this:

mongoConnector = app.dataSources.MyMongoDBConnectorName.connector;

mongoConnector.collection("Sequence").findAndModify({collection: 'SpecialOrder'}, [['_id','asc']], {$inc: { value: 1 }}, {new: true}, function(err, sequence) {
            if(err) {
                console.log(err.message);
            } else {
                // Do what I need to do with new incremented value sequence.value
            }
        });

Like I said, this is working, but I am still not sure if it is the correct way of doing it. Seems like there should be a built in LoopbackJS way.

Thanks,

Warren Bell

Ryan Schumacher

unread,
Jan 7, 2015, 12:10:28 AM1/7/15
to Warren Bell, loopb...@googlegroups.com
Since the Loopback connector is designed to work with MongoDB, MySQL, Oracle, etc. It looks like it's been designed to be agnostic of system specific methods in order to be interchangeable. (This is pure speculation)

That said, I think you are doing it the right way. Other developers will see your model code and know they must add a new method if they want it to work with MySQL, for example.

Ryan Hamilton-Schumacher

38pages / Application Development
--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/vJdNer-0fJM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lance Moore

unread,
Oct 6, 2015, 4:23:38 AM10/6/15
to LoopbackJS
At some point most database abstraction breaks down and you have to use raw queries. Unless loopback introduces this functionality into the connector I think you're doing it right. Thank you, this discussion has saved me a lot of time sifting through documentation. I'm giving this solution +1 for anyone else wanting to adopt it. While not pertinent to this challenge, loopback does allow some extended operations on model updates, check out the docs here: https://docs.strongloop.com/display/public/LB/MongoDB+connector#MongoDBconnector-UsingMongoDBoperatorsinupdateoperations
Reply all
Reply to author
Forward
0 new messages