Files
DefinitelyTyped/types/bson/bson-tests.ts
Andrey Tselishchev eb735f86b6 Update [@types/bson] to match js-bson@1.0.6 (#27098)
* [@types/bson] Updated BSON class definitions

* Updated BSON.serialize (changed the way how options are passed)
* Updated BSON.deserialize (added missing options, removed `isArray` argument)
* Added BSON.serializeWithBufferAndIndex
* Added BSON.deserializeStream

* [@types/bson] Updated BSON types

* Binary: Made static constants 'readonly', added comments
* ObjectID: 'equals' can accept string, added comments
* Code, DBRef, Double, Decimal128, MaxKey, MinKey, ObjectID, BSONRegExp, Symbol: Added comments

* [@types/bson] Updated Long & Timestamp BSON types

* In original js-node@1.0.x code 'Timestamp' is a 100% copy-paste of 'Long'
  with 'Long' replaced by 'Timestamp' (changed to inheritance in js-node@2.0.0).
  Do avoid duplication in typings a base class 'LongLike' was introduced and both 'Long'
  and 'Timestamp' are inherited from 'LongLike'.
* Made static constants 'readonly', fixed return-type for greaterThan and greaterThanOrEqual
  (boolean), renamed argument for shiftLeft, shiftRight, shiftRightUnsigned, added comments

* [@types/bson] Changed header version number to 1.0.6
2018-07-06 11:55:43 -07:00

29 lines
851 B
TypeScript

import * as bson from 'bson';
// enable hex string caching
bson.ObjectID.cacheHexString = true
let BSON = new bson.BSON();
let Long = bson.Long;
let doc = { long: Long.fromNumber(100) }
// Serialize a document
let data = BSON.serialize(doc);
console.log("data:", data);
// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);
BSON = new bson.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
// Calculate Object Size
BSON = new bson.BSON();
console.log("Calculated Object size - no options object:", BSON.calculateObjectSize(doc));
console.log("Calculated Object size - empty options object:", BSON.calculateObjectSize(doc, {}));
console.log("Calculated Object size - custom options object:", BSON.calculateObjectSize(doc, { ignoreUndefined: false, serializeFunctions: true }));