@types/bson

  • Version 4.0.4
  • Published
  • 26.3 kB
  • 1 dependency
  • MIT license

Install

npm i @types/bson
yarn add @types/bson
pnpm add @types/bson

Overview

TypeScript definitions for bson

Index

Functions

function calculateObjectSize

calculateObjectSize: (
object: any,
options?: CalculateObjectSizeOptions
) => number;
  • Calculate the bson size for a passed in Javascript object.

    Parameter object

    the Javascript object to calculate the BSON byte size for.

    Parameter Options

    {Number} returns the number of bytes the BSON object will take up.

function deserialize

deserialize: (buffer: any, options?: DeserializeOptions) => any;
  • Deserialize data as BSON.

    Parameter buffer

    The buffer containing the serialized set of BSON documents.

    Parameter options

    Deserialize options.

    Returns

    The deserialized Javascript Object.

function deserializeStream

deserializeStream: (
data: any,
startIndex: number,
numberOfDocuments: number,
documents: Array<any>,
docStartIndex: number,
options?: DeserializeOptions
) => number;
  • Deserialize stream data as BSON documents.

    Parameter data

    The buffer containing the serialized set of BSON documents.

    Parameter startIndex

    The start index in the data Buffer where the deserialization is to start.

    Parameter numberOfDocuments

    Number of documents to deserialize

    Parameter documents

    An array where to store the deserialized documents

    Parameter docStartIndex

    The index in the documents array from where to start inserting documents

    Parameter options

    Additional options used for the deserialization

    Returns

    The next index in the buffer after deserialization of the numberOfDocuments

function serialize

serialize: (object: any, options?: SerializeOptions) => any;
  • Serialize a Javascript object.

    Parameter object

    The Javascript object to serialize.

    Parameter options

    Serialize options. The Buffer object containing the serialized object.

function serializeWithBufferAndIndex

serializeWithBufferAndIndex: (
object: any,
buffer: any,
options?: SerializeWithBufferAndIndexOptions
) => number;
  • Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.

    Parameter object

    The Javascript object to serialize.

    Parameter buffer

    The Buffer you pre-allocated to store the serialized BSON object.

    Parameter options

    Serialize options.

    Returns

    The index pointing to the last written byte in the buffer

Classes

class Binary

class Binary {}
  • A class representation of the BSON Binary type.

constructor

constructor(buffer: any, subType?: number);
  • Parameter buffer

    A buffer object containing the binary data

    Parameter subType

    Binary data subtype

property buffer

readonly buffer: any;
  • The underlying Buffer which stores the binary data.

property sub_type

readonly sub_type?: number;
  • Binary data subtype

property SUBTYPE_BYTE_ARRAY

static readonly SUBTYPE_BYTE_ARRAY: number;

    property SUBTYPE_DEFAULT

    static readonly SUBTYPE_DEFAULT: number;

      property SUBTYPE_FUNCTION

      static readonly SUBTYPE_FUNCTION: number;

        property SUBTYPE_MD5

        static readonly SUBTYPE_MD5: number;

          property SUBTYPE_USER_DEFINED

          static readonly SUBTYPE_USER_DEFINED: number;

            property SUBTYPE_UUID

            static readonly SUBTYPE_UUID: number;

              property SUBTYPE_UUID_OLD

              static readonly SUBTYPE_UUID_OLD: number;

                method length

                length: () => number;
                • The length of the binary.

                method put

                put: (byte_value: number | string) => void;
                • Updates this binary with byte_value

                method read

                read: (position: number, length: number) => any;
                • Reads length bytes starting at position.

                method value

                value: () => string;
                • Returns the value of this binary as a string.

                method write

                write: (buffer: Buffer | string, offset: number) => void;
                • Writes a buffer or string to the binary

                class BSONRegExp

                class BSONRegExp {}
                • A class representation of the BSON RegExp type.

                constructor

                constructor(pattern: string, options: string);

                  property options

                  readonly options: string;

                    property pattern

                    readonly pattern: string;

                      class Code

                      class Code {}
                      • A class representation of the BSON Code type.

                      constructor

                      constructor(code: string | Function, scope?: any);
                      • Parameter code

                        A string or function.

                        Parameter scope

                        An optional scope for the function.

                      property code

                      readonly code: string | Function;

                        property scope

                        readonly scope?: any;

                          class DBRef

                          class DBRef {}
                          • A class representation of the BSON DBRef type.

                          constructor

                          constructor(namespace: string, oid: ObjectId, db?: string);
                          • Parameter namespace

                            The collection name.

                            Parameter oid

                            The reference ObjectId.

                            Parameter db

                            Optional db name, if omitted the reference is local to the current db

                          property db

                          db?: string;

                            property namespace

                            namespace: string;

                              property oid

                              oid: ObjectId;

                                class Decimal128

                                class Decimal128 {}
                                • A class representation of the BSON Decimal128 type.

                                constructor

                                constructor(bytes: any);
                                • Parameter bytes

                                  A buffer containing the raw Decimal128 bytes.

                                property bytes

                                readonly bytes: any;
                                • A buffer containing the raw Decimal128 bytes.

                                method fromString

                                static fromString: (s: string) => Decimal128;
                                • Create a Decimal128 instance from a string representation.

                                method toJSON

                                toJSON: () => string;

                                  method toString

                                  toString: () => string;

                                    class Double

                                    class Double {}
                                    • A class representation of the BSON Double type.

                                    constructor

                                    constructor(value: number);
                                    • Parameter value

                                      The number we want to represent as a double.

                                    property value

                                    value: number;
                                    • https://github.com/mongodb/js-bson/blob/master/lib/double.js#L17

                                    method valueOf

                                    valueOf: () => number;

                                      class Int32

                                      class Int32 {}
                                      • A class representation of the BSON Int32 type.

                                      constructor

                                      constructor(value: number);
                                      • Parameter value

                                        The number we want to represent as an int32.

                                      method valueOf

                                      valueOf: () => number;

                                        class Long

                                        class Long extends LongLike<Long> {}
                                        • A class representation of the BSON Long type, a 64-bit two's-complement integer value, which faithfully simulates the behavior of a Java "Long". This implementation is derived from LongLib in GWT.

                                        property MAX_VALUE

                                        static readonly MAX_VALUE: Long;

                                          property MIN_VALUE

                                          static readonly MIN_VALUE: Long;

                                            property NEG_ONE

                                            static readonly NEG_ONE: Long;

                                              property ONE

                                              static readonly ONE: Long;

                                                property ZERO

                                                static readonly ZERO: Long;

                                                  method fromBits

                                                  static fromBits: (lowBits: number, highBits: number) => Long;
                                                  • Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.

                                                    Parameter lowBits

                                                    The low 32-bits.

                                                    Parameter highBits

                                                    The high 32-bits.

                                                  method fromInt

                                                  static fromInt: (i: number) => Long;
                                                  • Returns a Long representing the given (32-bit) integer value.

                                                  method fromNumber

                                                  static fromNumber: (n: number) => Long;
                                                  • Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.

                                                  method fromString

                                                  static fromString: (s: string, opt_radix?: number) => Long;
                                                  • Returns a Long representation of the given string

                                                    Parameter opt_radix

                                                    The radix in which the text is written. {default:10}

                                                  class MaxKey

                                                  class MaxKey {}
                                                  • A class representation of the BSON MaxKey type.

                                                  constructor

                                                  constructor();

                                                    class MinKey

                                                    class MinKey {}
                                                    • A class representation of the BSON MinKey type.

                                                    constructor

                                                    constructor();

                                                      class ObjectId

                                                      class ObjectId {}
                                                      • A class representation of the BSON ObjectId type.

                                                      constructor

                                                      constructor(id?: string | number | ObjectId);
                                                      • Create a new ObjectId instance

                                                        Parameter id

                                                        Can be a 24 byte hex string, 12 byte binary string or a Number.

                                                      property cacheHexString

                                                      static cacheHexString?: boolean;
                                                      • If true cache the hex string representation of ObjectId

                                                      property generationTime

                                                      generationTime: number;
                                                      • The generation time of this ObjectId instance

                                                      method createFromHexString

                                                      static createFromHexString: (hexString: string) => ObjectId;
                                                      • Creates an ObjectId from a hex string representation of an ObjectId.

                                                        Parameter hexString

                                                        create a ObjectId from a passed in 24 byte hexstring. {ObjectId} return the created ObjectId

                                                      method createFromTime

                                                      static createFromTime: (time: number) => ObjectId;
                                                      • Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.

                                                        Parameter time

                                                        an integer number representing a number of seconds. {ObjectId} return the created ObjectId

                                                      method equals

                                                      equals: (otherID: ObjectId | string) => boolean;
                                                      • Compares the equality of this ObjectId with otherID.

                                                        Parameter otherID

                                                        ObjectId instance to compare against. {boolean} the result of comparing two ObjectId's

                                                      method generate

                                                      static generate: (time?: number) => any;
                                                      • Generate a 12 byte id string used in ObjectId's

                                                        Parameter time

                                                        optional parameter allowing to pass in a second based timestamp. {string} return the 12 byte id binary string.

                                                      method getTimestamp

                                                      getTimestamp: () => Date;
                                                      • Returns the generation date (accurate up to the second) that this ID was generated. {Date} the generation date

                                                      method isValid

                                                      static isValid: (id: string | number | ObjectId) => boolean;
                                                      • Checks if a value is a valid bson ObjectId

                                                        {boolean} return true if the value is a valid bson ObjectId, return false otherwise.

                                                      method toHexString

                                                      toHexString: () => string;
                                                      • Return the ObjectId id as a 24 byte hex string representation {string} return the 24 byte hex string representation.

                                                      class ObjectID

                                                      class ObjectId {}
                                                      • A class representation of the BSON ObjectId type.

                                                      constructor

                                                      constructor(id?: string | number | ObjectId);
                                                      • Create a new ObjectId instance

                                                        Parameter id

                                                        Can be a 24 byte hex string, 12 byte binary string or a Number.

                                                      property cacheHexString

                                                      static cacheHexString?: boolean;
                                                      • If true cache the hex string representation of ObjectId

                                                      property generationTime

                                                      generationTime: number;
                                                      • The generation time of this ObjectId instance

                                                      method createFromHexString

                                                      static createFromHexString: (hexString: string) => ObjectId;
                                                      • Creates an ObjectId from a hex string representation of an ObjectId.

                                                        Parameter hexString

                                                        create a ObjectId from a passed in 24 byte hexstring. {ObjectId} return the created ObjectId

                                                      method createFromTime

                                                      static createFromTime: (time: number) => ObjectId;
                                                      • Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.

                                                        Parameter time

                                                        an integer number representing a number of seconds. {ObjectId} return the created ObjectId

                                                      method equals

                                                      equals: (otherID: ObjectId | string) => boolean;
                                                      • Compares the equality of this ObjectId with otherID.

                                                        Parameter otherID

                                                        ObjectId instance to compare against. {boolean} the result of comparing two ObjectId's

                                                      method generate

                                                      static generate: (time?: number) => any;
                                                      • Generate a 12 byte id string used in ObjectId's

                                                        Parameter time

                                                        optional parameter allowing to pass in a second based timestamp. {string} return the 12 byte id binary string.

                                                      method getTimestamp

                                                      getTimestamp: () => Date;
                                                      • Returns the generation date (accurate up to the second) that this ID was generated. {Date} the generation date

                                                      method isValid

                                                      static isValid: (id: string | number | ObjectId) => boolean;
                                                      • Checks if a value is a valid bson ObjectId

                                                        {boolean} return true if the value is a valid bson ObjectId, return false otherwise.

                                                      method toHexString

                                                      toHexString: () => string;
                                                      • Return the ObjectId id as a 24 byte hex string representation {string} return the 24 byte hex string representation.

                                                      class Symbol

                                                      class Symbol {}
                                                      • A class representation of the BSON Symbol type.

                                                        Deprecated

                                                      constructor

                                                      constructor(value: string);

                                                        method valueOf

                                                        valueOf: () => string;
                                                        • Access the wrapped string value.

                                                        class Timestamp

                                                        class Timestamp extends LongLike<Timestamp> {}
                                                        • A class representation of the BSON Timestamp type.

                                                        property MAX_VALUE

                                                        static readonly MAX_VALUE: Timestamp;

                                                          property MIN_VALUE

                                                          static readonly MIN_VALUE: Timestamp;

                                                            property NEG_ONE

                                                            static readonly NEG_ONE: Timestamp;

                                                              property ONE

                                                              static readonly ONE: Timestamp;

                                                                property ZERO

                                                                static readonly ZERO: Timestamp;

                                                                  method fromBits

                                                                  static fromBits: (lowBits: number, highBits: number) => Timestamp;
                                                                  • Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.

                                                                    Parameter lowBits

                                                                    The low 32-bits.

                                                                    Parameter highBits

                                                                    The high 32-bits.

                                                                  method fromInt

                                                                  static fromInt: (value: number) => Timestamp;
                                                                  • Returns a Timestamp represented by the given (32-bit) integer value

                                                                  method fromNumber

                                                                  static fromNumber: (value: number) => Timestamp;
                                                                  • Returns a Timestamp representing the given number value, provided that it is a finite number.

                                                                  method fromString

                                                                  static fromString: (str: string, opt_radix?: number) => Timestamp;
                                                                  • Returns a Timestamp from the given string.

                                                                    Parameter opt_radix

                                                                    The radix in which the text is written. {default:10}

                                                                  Interfaces

                                                                  interface CalculateObjectSizeOptions

                                                                  interface CalculateObjectSizeOptions {}

                                                                    property ignoreUndefined

                                                                    ignoreUndefined?: boolean | undefined;
                                                                    • {default:true}, ignore undefined fields.

                                                                    property serializeFunctions

                                                                    serializeFunctions?: boolean | undefined;
                                                                    • {default:false}, serialize the javascript functions

                                                                    interface DeserializeOptions

                                                                    interface DeserializeOptions {}

                                                                      property allowObjectSmallerThanBufferSize

                                                                      allowObjectSmallerThanBufferSize?: boolean | undefined;
                                                                      • {default:false}, allows the buffer to be larger than the parsed BSON object.

                                                                      property bsonRegExp

                                                                      bsonRegExp?: boolean | undefined;
                                                                      • {default:false}, return BSON regular expressions as BSONRegExp instances.

                                                                      property cacheFunctions

                                                                      cacheFunctions?: boolean | undefined;
                                                                      • {default:false}, cache evaluated functions for reuse.

                                                                      property cacheFunctionsCrc32

                                                                      cacheFunctionsCrc32?: boolean | undefined;
                                                                      • {default:false}, use a crc32 code for caching, otherwise use the string of the function.

                                                                      property evalFunctions

                                                                      evalFunctions?: boolean | undefined;
                                                                      • {default:false}, evaluate functions in the BSON document scoped to the object deserialized.

                                                                      property fieldsAsRaw

                                                                      fieldsAsRaw?: { readonly [fieldName: string]: boolean } | undefined;
                                                                      • {default:null}, allow to specify if there what fields we wish to return as unserialized raw buffer.

                                                                      property promoteBuffers

                                                                      promoteBuffers?: boolean | undefined;
                                                                      • {default:false}, deserialize Binary data directly into node.js Buffer object.

                                                                      property promoteLongs

                                                                      promoteLongs?: boolean | undefined;
                                                                      • {default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits.

                                                                      property promoteValues

                                                                      promoteValues?: boolean | undefined;
                                                                      • {default:false}, when deserializing will promote BSON values to their Node.js closest equivalent types.

                                                                      interface SerializeOptions

                                                                      interface SerializeOptions extends CommonSerializeOptions {}

                                                                        property minInternalBufferSize

                                                                        minInternalBufferSize?: number | undefined;
                                                                        • {default:1024*1024*17}, minimum size of the internal temporary serialization buffer.

                                                                        interface SerializeWithBufferAndIndexOptions

                                                                        interface SerializeWithBufferAndIndexOptions extends CommonSerializeOptions {}

                                                                          property index

                                                                          index?: number | undefined;
                                                                          • {default:0}, the index in the buffer where we wish to start serializing into.

                                                                          Namespaces

                                                                          namespace EJSON

                                                                          namespace EJSON {}
                                                                          • Functions for serializing JavaScript objects into Mongodb Extended JSON (EJSON). EJSON

                                                                          function deserialize

                                                                          deserialize: (ejson: {}, options?: { relaxed?: boolean | undefined }) => {};
                                                                          • Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types

                                                                            EJSON

                                                                            Parameter ejson

                                                                            The Extended JSON object to deserialize

                                                                            Parameter options

                                                                            Optional settings passed to the parse method {object}

                                                                          function parse

                                                                          parse: (text: string, options?: { relaxed?: boolean | undefined }) => {};
                                                                          • Parse an Extended JSON string, constructing the JavaScript value or object described by that string.

                                                                            EJSON

                                                                            Parameter text

                                                                            Parameter options

                                                                            Optional settings

                                                                            Parameter

                                                                            {boolean} [options.relaxed=true] Attempt to return native JS types where possible, rather than BSON types (if true) {object}

                                                                            Example 1

                                                                            const { EJSON } = require('bson'); const text = '{ "int32": { "$numberInt": "10" } }';

                                                                            // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } } console.log(EJSON.parse(text, { relaxed: false }));

                                                                            // prints { int32: 10 } console.log(EJSON.parse(text));

                                                                          function serialize

                                                                          serialize: (bson: {}, options?: { relaxed?: boolean | undefined }) => {};
                                                                          • Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.

                                                                            EJSON

                                                                            Parameter bson

                                                                            The object to serialize

                                                                            Parameter options

                                                                            Optional settings passed to the stringify function {object}

                                                                          function stringify

                                                                          stringify: {
                                                                          (value: {}, options?: { relaxed?: boolean | undefined }): string;
                                                                          (
                                                                          value: {},
                                                                          replacer: ((key: string, value: any) => any) | (string | number)[],
                                                                          options?: { relaxed?: boolean }
                                                                          ): string;
                                                                          (
                                                                          value: {},
                                                                          replacer: (string | number)[] | ((key: string, value: any) => any),
                                                                          indents?: string | number,
                                                                          options?: { relaxed?: boolean }
                                                                          ): string;
                                                                          };
                                                                          • Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

                                                                            EJSON

                                                                            Parameter value

                                                                            The value to convert to extended JSON

                                                                            Parameter replacer

                                                                            A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string

                                                                            Parameter space

                                                                            A String or Number object that's used to insert white space into the output JSON string for readability purposes.

                                                                            Parameter options

                                                                            Optional settings.

                                                                            Parameter

                                                                            {boolean} [options.relaxed=true] Enabled Extended JSON's relaxed mode

                                                                            Returns

                                                                            {string}

                                                                            Example 1

                                                                            const { EJSON, Int32 } = require('bson'); const doc = { int32: new Int32(10) };

                                                                            // prints '{"int32":{"$numberInt":"10"}}' console.log(EJSON.stringify(doc, { relaxed: false }));

                                                                            // prints '{"int32":10}' console.log(EJSON.stringify(doc));

                                                                          Package Files (1)

                                                                          Dependencies (1)

                                                                          Dev Dependencies (0)

                                                                          No dev dependencies.

                                                                          Peer Dependencies (0)

                                                                          No peer dependencies.

                                                                          Badge

                                                                          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/bson.

                                                                          • Markdown
                                                                            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/bson)
                                                                          • HTML
                                                                            <a href="https://www.jsdocs.io/package/@types/bson"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>