This object can be set as dbNumber property to specify configuration for using 3rd party number libary when additional information is required, as described in DBNumberConfig.

The only required property is Constructor. As menitoned, static and instance properties are only needed when the number library is not using common method names, or if you need to customize implementation, see NumberLibMethods.

An important note about precision and rounding:

Different number libraries have different options to round the results of arithmetic and other operations. For example, some libraries like decimal.js and decimal.js-light round to precision, which is total number of significant digits in the number. These libraries typically have a precision value specified as configuration setting in the constructor. Other libraries, like bignumber.js and big.js round to scale, or decimal places, which is number of digits after decimal point. These libraries typically have decimal places value as configuration setting in the constructor. In addition, some libraries, like decimal.js and bignumber.js do automatic rounding of some arithmetic operations and other libraries like big.js and decimal.js-light allow only manual rounding via methods such as round.

The above may have implication on some query results. On the server side, NoSQL database uses Java BigDecimal and rounds each arithmetic operation according to precision and rounding mode settings of MathContext, as mentioned in DBNumberConfig. The driver, on the other hand, does not perform rounding during query processing, other than the rounding performed automatically by some number libraries as mentioned above. This means that for queries that use arithmetic expressions and some aggregate functions, the query results may slightly differ when using different number libraries. From the libraries tested, decimal.js has the closest matching behavior to query processing on the server side because it automatically rounds arithmetic operations to precision using rounding mode setting.

You may also use the rounding methods from the number library to perform rounding after receiving query results. This may be useful when using number libraries that do not perform automatic rounding. In addition, number libraries may have other settings related to rounding. See documentation for the number library of your choice. If required, you can clone and customize the constructor and pass it as Constructor property. You may also customize arithmetic operations further using static and instance properties, see NumberLibMethods, in case non-standard behavior is needed.

The driver need to know precision and rounding mode in order to create MathContext for server-side query processing. It will try to infer these settings from the constructor as described in NumberLibPrecision and NumberLibRoundingMode. For libraries that round to scale, such as bignumber.js and big.js, it is not possible to infer precision from constructor, so you may need to set precision property, otherwise the driver will use the default value. For precision and roundingMode properties, if set, the driver will use their values instead of inferring them from the constructor.

Note that setting properties precision and roundingMode only affects how rounding is done on the server side. The driver will not set any properties of the constructor. If the number library does automatic rounding, it will do so based on the constructor as passed. If the number library supports precision and rounding mode settings and does automatic rounding/trucation, you can ensure that the same settings are used by the server and the client by letting the driver infer them from the constructor (by using getPrecision and getRoundingMode properties if necessary). E.g. for libraries decimal.js and decimal.js-light the driver will automatically infer and use precision and rounding mode settings.

Hierarchy

  • NumberLibConfig

Properties

Constructor: string | DBNumberConstructor

String representing number library module name, if the sole export is the constructor, or constructor function. Constructor function must be able to create instances from number's string representation or from Javascript number (although other options may also be provided by the number library). Note the upper case to disambiguate from Object's constructor property.

RoundingModes?: string | RoundingModesMap

Specifies mapping between rounding mode names and their constant values in the number library. If not set, the driver will try to infer the mapping from constuctor or module (see RoundingModesMap).

getPrecision?: NumberLibPrecision

Specifies how to get precision value from constructor. See NumberLibPrecision. If not set, the driver will use precision.

getRoundingMode?: NumberLibRoundingMode

Specifies how to get rounding mode from constructor. See NumberLibRoundingMode. If not set, the driver will use roundingMode.

instance?: NumberLibMethods

Instance method mappings, that is methods that are properties of the constructor's prototype. If not set, or for any required method not present in the mapping, the driver will try to infer it from constructor's prototype. See NumberLibMethods.

module?: string

In rare cases when the number library export is not the constructor, you may specify the module name. If this property is set and Constructor property is specified as a string, then the Constructor property specifies property name of the module.exports object instead of the module name.

precision?: number

Precision to use for rounding of server-side query calculations on datatype Number. If not set, the driver will try to infer the value from constructor (see NumberLibPrecision). If cannot be inferred, default precision of 20 is used.

roundingMode?: unknown

Rounding mode to use for rounding of server-side query calculations on datatype Number. See RoundingModesMap for details on supported roundings modes. This property can be specified either as rounding mode name string, such as 'DOWN', 'UP', 'HALF_DOWN', etc. (with or without ROUND_ prefix) or as number library-specific constant value, as long as the driver can find a mapping between rounding mode names and their values in the number library (see RoundingModesMap). If not set, the driver will try to infer the value from constructor (see NumberLibRoundingMode). If cannot be inferred, default value of ROUND_HALF_UP is used.

Static method mappings, that is methods that are properties of the constructor itself. If not set, or for any required method not present in the mapping, the driver will try to infer it from constructor. See NumberLibMethods.

Generated using TypeDoc