Skip to content

Commit 14a7473

Browse files
authored
feat!(NODE-4461): remove Decimal128 toObject transformer (#526)
1 parent a99d176 commit 14a7473

File tree

3 files changed

+5
-39
lines changed

3 files changed

+5
-39
lines changed

docs/upgrade-to-v5.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ We have set our typescript compilation target to `es2020` which aligns with our
7979
> **TL;DR**: TODO
8080
8181
TODO(NODE-4771): serializeFunctions bug fix makes function names outside the ascii range get serialized correctly
82-
> This will preserve newer ECMAScript 2020 features like optional chaining, nullish coalescing, export * as ns, and dynamic import(...) syntax. It also means bigint literals now have a stable target below esnext.
8382

8483
### Remove `Map` export
8584

8685
This library no longer polyfills [ES Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and the export "Map" was removed. Users should migrate to using the global Map constructor available in all supported JS environments.
86+
87+
### `Decimal128` `toObject()` mapper support removed
88+
89+
`Decimal128` can no longer have a `toObject()` method added on to its prototype for mapping to a custom value. This feature was undocumented and inconsistent with the rest of our BSON types. At this time there is no direct migration: cursors in the driver support transformations via `.map`, otherwise the `Decimal128` instances will require manual transformation. There is a plan to provide a better mechanism for consistently transforming BSON values tracked in [NODE-4680](https://jira.mongodb.org/browse/NODE-4680), please feel free to add a vote or comment with a use case to help us land the feature in the most useful form.

src/parser/deserializer.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,7 @@ function deserializeObject(
373373
// Update index
374374
index = index + 16;
375375
// Assign the new Decimal128 value
376-
const decimal128 = new Decimal128(bytes) as Decimal128 | { toObject(): unknown };
377-
// If we have an alternative mapper use that
378-
if ('toObject' in decimal128 && typeof decimal128.toObject === 'function') {
379-
value = decimal128.toObject();
380-
} else {
381-
value = decimal128;
382-
}
376+
value = new Decimal128(bytes);
383377
} else if (elementType === constants.BSON_DATA_BINARY) {
384378
let binarySize =
385379
buffer[index++] |

test/node/decimal128_tests.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,37 +1174,6 @@ describe('Decimal128', function () {
11741174
done();
11751175
});
11761176

1177-
it('Support toBSON and toObject methods for custom mapping', function (done) {
1178-
// Create a custom object
1179-
var MyCustomDecimal = function (value) {
1180-
this.value = value instanceof Decimal128 ? value.toString() : value;
1181-
};
1182-
1183-
MyCustomDecimal.prototype.toBSON = function () {
1184-
return Decimal128.fromString(this.value);
1185-
};
1186-
1187-
// Add a custom mapper for the type
1188-
const saveToObject = Decimal128.prototype.toObject;
1189-
try {
1190-
Decimal128.prototype.toObject = function () {
1191-
return new MyCustomDecimal(this);
1192-
};
1193-
1194-
// Test all methods around a simple serialization at object top level
1195-
var doc = { value: new MyCustomDecimal('1') };
1196-
var buffer = BSON.serialize(doc);
1197-
var back = BSON.deserialize(buffer);
1198-
expect(back.value instanceof MyCustomDecimal).to.be.ok;
1199-
expect('1').to.equal(back.value.value);
1200-
} finally {
1201-
// prevent this test from breaking later tests which may re-use the same class
1202-
Decimal128.prototype.toObject = saveToObject;
1203-
}
1204-
1205-
done();
1206-
});
1207-
12081177
it('accepts strings in the constructor', () => {
12091178
expect(new Decimal128('0').toString()).to.equal('0');
12101179
expect(new Decimal128('00').toString()).to.equal('0');

0 commit comments

Comments
 (0)