How to use bson - 10 common examples

To help you get started, we’ve selected a few bson examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / bson / bson-tests.ts View on Github external
/// 

import * as bson from 'bson';

let BSON = new bson.BSONPure.BSON();
let Long = bson.BSONPure.Long;

let doc = {long: Long.fromNumber(100)}

// Serialize a document
let data = BSON.serialize(doc, false, true, false);
console.log("data:", data);

// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);


BSON = new bson.BSONNative.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
github DefinitelyTyped / DefinitelyTyped / bson / bson-tests.ts View on Github external
/// 

import * as bson from 'bson';

let BSON = new bson.BSONPure.BSON();
let Long = bson.BSONPure.Long;

let doc = {long: Long.fromNumber(100)}

// Serialize a document
let data = BSON.serialize(doc, false, true, false);
console.log("data:", data);

// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);


BSON = new bson.BSONNative.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
github DistributedObjectProtocol / dop / test / index.js View on Github external
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))
github hex7c0 / mongodb-backup / test / data.js View on Github external
docs.forEach(function(third) { // document

                var document = collection + '/' + third;
                assert.equal(extname(third), '.bson');
                var _id = third.split('.bson')[0];
                var data = BSON.deserialize(fs.readFileSync(document, {
                  encoding: null
                }));
                assert.deepEqual(data, DOCS[_id]);
                fs.unlinkSync(document);
              });
              fs.rmdirSync(collection);
github flow-typed / flow-typed / definitions / npm / bson_v1.x.x / test_bson-v1.js View on Github external
// @flow

import BSON from "bson";
import { ObjectId } from "bson";

const bson = new BSON();
bson.serialize({ foo: "bar" });

// $ExpectError missing arg to serialize
bson.serialize();
// $ExpectError missing method
bson.wrong();

const id = new ObjectId();

id.toString();
// $ExpectError
id.wrong();

// $ExpectError
const wrongId = new ObjectId({});
github mongodb / stitch-js-sdk / packages / core / services / aws-s3 / __tests__ / CoreAwsS3ServiceClientUnitTests.ts View on Github external
acl,
      contentType,
      bodyUintArray
    );
    expect(result.location).toEqual(expectedLocation);

    verify(
      serviceMock.callFunction(anything(), anything(), anything())
    ).times(4);
    const [funcNameArg4, funcArgsArg4, resultClassArg4]: any[] = capture(
      serviceMock.callFunction
    ).last();

    expect("put").toEqual(funcNameArg4);
    expect(1).toEqual(funcArgsArg4.length);
    expectedArgs.body = new BSON.Binary(new Buffer(bodyUintArray));
    expect(expectedArgs).toEqual(funcArgsArg4[0]);
    expect(ResultDecoders.PutObjectResultDecoder).toEqual(resultClassArg4);

    // Should pass along errors
    when(
      serviceMock.callFunction(anything(), anything(), anything())
    ).thenThrow(new Error("whoops"));

    try {
      await client.putObject(bucket, key, acl, contentType, body);
      fail();
    } catch (_) {
      // Do nothing
    }
  });
});
github mongodb-js / mongodb-core / test / mock / lib / request.js View on Github external
Request.prototype.reply = function(documents, options) {
  options = options || {};
  documents = Array.isArray(documents) ? documents : [documents];

  // Unpack any variables we need
  var cursorId = options.cursorId || Long.ZERO;
  var responseFlags = typeof options.responseFlags === 'number' ? options.responseFlags : 0;
  var startingFrom = typeof options.startingFrom === 'number' ? options.startingFrom : 0;
  var numberReturned = documents.length;

  // Additional response Options
  var killConnectionAfterNBytes =
    typeof options.killConnectionAfterNBytes === 'number'
      ? options.killConnectionAfterNBytes
      : null;

  // Create the Response document
  var response;
  if (options.compression) {
    response = new CompressedResponse(
      this.bson,
      {
github mongodb / stitch-js-sdk / packages / core / sdk / __tests__ / internal / net / StitchRequestClientUnitTests.ts View on Github external
.then(response => {
        expect(response.statusCode).toBe(200);
        const expected = {
          a: 42,
          hello: "world"
        };
        expect(expected).toEqual(EJSON.parse(response.body!, { relaxed: true }));

        // Error responses should be handled
        when(transportMock.roundTrip(anything())).thenResolve({
          headers: {},
          statusCode: 500
        });

        return stitchRequestClient.doRequest(builder.build());
      })
      .catch((error: StitchServiceError) => {
github mongodb / stitch-js-sdk / packages / core / sdk / __tests__ / internal / net / StitchRequestClientUnitTests.ts View on Github external
.then(response => {
        expect(response.statusCode).toEqual(200);
        const expected = {
          a: 42,
          hello: "world"
        };
        expect(EJSON.parse(response.body!, { relaxed: true })).toEqual(expected);

        // Error responses should be handled
        when(transportMock.roundTrip(anything())).thenResolve({
          headers: {},
          statusCode: 500
        });
        return stitchRequestClient.doRequest(builder.build());
      })
      .catch((error: StitchServiceError) => {
github mongodb / node-mongodb-native / test / mock / lib / wire_response.js View on Github external
// Unpack the cursor
  var lowBits =
    data[this.index] |
    (data[this.index + 1] << 8) |
    (data[this.index + 2] << 16) |
    (data[this.index + 3] << 24);
  this.index = this.index + 4;
  var highBits =
    data[this.index] |
    (data[this.index + 1] << 8) |
    (data[this.index + 2] << 16) |
    (data[this.index + 3] << 24);
  this.index = this.index + 4;
  // Create long object
  this.cursorId = new Long(lowBits, highBits);

  // Unpack the starting from
  this.startingFrom =
    data[this.index] |
    (data[this.index + 1] << 8) |
    (data[this.index + 2] << 16) |
    (data[this.index + 3] << 24);
  this.index = this.index + 4;

  // Unpack the number of objects returned
  this.numberReturned =
    data[this.index] |
    (data[this.index + 1] << 8) |
    (data[this.index + 2] << 16) |
    (data[this.index + 3] << 24);
  this.index = this.index + 4;