const coll = db.collection('coll', {
serdes: {
enableBigNumbers(path, matches) {
if (path[0] === 'discount') {
return 'bigint';
}
if (matches(['items', '*', 'price'])) {
return 'bignumber';
}
return 'number';
},
}
});
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.
'*'
matches a single element in the path at that position.['foo', '*']
will match ['foo', 'bar']
, but not ['foo']
or ['foo', 'bar', 'baz']
.['foo', 1]
will not match ['foo', '1']
.'*'
, which will match any string or number.This function can then return any CollNumCoercion in order to coerce the value to the desired type.
You may simply use () => '<type>'
to return a single coercion type for all paths.
Overview
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.