mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 21:00:01 +08:00
* [@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
29 lines
851 B
TypeScript
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 }));
|