简体   繁体   中英

Auto increment a property value in Strongloop loopback model

Is there a built-in way to auto increment a model's property value is Strongloop loopback ? This model has a property named orderNumber and I want it to start at 1 and increment by 1 every time a new model is created. This model is persisted to a mongo DB. If Strongloop loopback does not have a built in way, then what would be considered best practice using javaScript, Node and mongoDB ?

Thanks,

Ok, 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
        }
    });

Seems like there should be a built in LoopbackJS way of doing this. A lot to go through just to increment a value.

Thanks,

Warren Bell

Have same problem, but with Postgres. Fixed just using altering column 'id' manually in database (set type to SERIAL which means auto-increment in Postgres). I think problem could be resolved in this way on any database.

I want to share my approach, which is similar to the author's answer, but with using Loopback's methods. Here is the Sequence model I've created:

{
  "name": "sequence",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "value": {
      "type": "number",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

You can store as many sequences as you want in this model. Then I create a sequence I need with this script (in this case order sequence):

var app = require('./server/server');

var Sequence = app.models.Sequence;

Sequence.findOrCreate({
  where: {
    name: 'order'
  }
}, {
  name: 'order',
  value: 0
}, function (err) {
  if (err) {
    console.log(err);
  }
  process.exit();
});

Now everytime I need order number to create an order, first I update the order record in Sequence model by incrementing its value and then use it:

var Sequence = Order.app.models.Sequence;

Sequence.findOne({
  where: {
    name: 'order'
  }
})
.then(function (orderNumber) {
  orderNumber.value++;
  return orderNumber.save();
})
.then(function (orderNumber) {
  Order.upsert({
    orderNumber: orderNumber.value,
    ...
  });
});

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM