Type alias CollNumCoercionFn

CollNumCoercionFn: ((path, matches) => CollNumCoercion)
Overview

💡Tip: read the documentation for CollectionSerDesConfig.enableBigNumbers before reading this.

This method of configuring the numerical deserialization behavior uses a function that takes the path of the field being deserialized, and returns the coercion type to be used for that path.

If you'd prefer to use a more declarative approach, you can use the CollNumCoercionCfg type to define the coercion for each path.

Type declaration

Example

const coll = db.collection('coll', {
serdes: {
enableBigNumbers(path, matches) {
if (path[0] === 'discount') {
return 'bigint';
}

if (matches(['items', '*', 'price'])) {
return 'bignumber';
}

return 'number';
},
}
});

Using the function

The function is called for each field being deserialized, and it receives two arguments:

  • path: the path of the field being deserialized, as an array of strings.
  • matches: a utility function that takes a "path matcher" as an argument and returns true if it matches the current path being deserialized.

The matcher must be an array of strings and numbers, but it may also contain wildcards ('*') to match any single field.

  • The wildcard '*' matches a single element in the path at that position.
    • However, it will not match multiple elements (or no elements) in the path.
    • For example, ['foo', '*'] will match ['foo', 'bar'], but not ['foo'] or ['foo', 'bar', 'baz'].
  • Strings and numbers are strictly compared.
    • For example, ['foo', 1] will not match ['foo', '1'].
    • The exception is the wildcard '*', which will match any string or number.

This function can then return any CollNumCoercion in order to coerce the value to the desired type.


Using a single CollNumCoercion

You may simply use () => '<type>' to return a single coercion type for all paths.

See

  • CollectionSerDesConfig.enableBigNumbers
  • CollNumCoercionCfg
  • CollNumCoercion