Permalink
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (43 sloc) 1.54 KB

Decimal128

You can define a Decimal128 type property as

// in model.json file
"aproperty": {
  "type": "String",
  "mongodb": {"dataType": "Decimal128"}
}

Suppose model Order has a decimal property called count. Read the sections below for the usage of CRUD operations.

Create

Order.create({count: '0.0005'}, function(err, order) {
  // order: {_id: '5bc8cc7f71cade4a8b5af886', count: '0.0005'}
});

The created record in the mongodb database will be:

{ "_id" : ObjectId("5bc8cc7f71cade4a8b5af886"), "count" : NumberDecimal("0.0005") }

The returned model instance is generated by the create method in loopback-datasource-juggler/lib/dao.js, connector only returns the id field. Therefore even the data is inserted as decimal, you still get a string in the callback function. If you need to continue processing with a decimal data, a workaround is doing the conversion manually:

var Decimal128 = require('mongodb').Decimal128;
Order.create({count: '0.0005'}, function(err, order) {
  // convert the string to decimal
  order.count = Decimal128.fromString(order.count);
});

Find

You can filter a decimal property like

OrderDecimal.find({where: {count: '0.0005'}}, function(err, orders) {
  // `orders` are all the data with `count` equivalent to NumberDecimal("0.0005")
});

The connector automatically converts the condition from string to decimal.

DestroyAll

You can destroy data with a filter contains decimal property. For example:

OrderDecimal.destroyAll({count: '0.0005'}, cb);