How to use the mongodb.Binary function in mongodb

To help you get started, we’ve selected a few mongodb 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 xbrowsersync / api / dist / core / db.js View on Github external
DB.idIsValid = function (id) {
        var binary;
        var base64Str;
        if (!id) {
            throw new exception_1.InvalidSyncIdException();
        }
        try {
            binary = new mongodb.Binary(Buffer.from(id, 'hex'), 4);
            base64Str = binary.buffer.toString('base64');
        }
        catch (err) {
            throw new exception_1.InvalidSyncIdException();
        }
        if (!binary || !base64Str) {
            throw new exception_1.InvalidSyncIdException();
        }
    };
    // Closes the database connection
github mongo-express / mongo-express / lib / router.js View on Github external
appRouter.param('document', function (req, res, next, id) {
    if (id === 'undefined' || id === undefined) {
      req.session.error = 'Document lacks an _id!';
      return res.redirect(res.locals.baseHref + 'db/' + req.dbName + '/' + req.collectionName);
    }

    id = JSON.parse(decodeURIComponent(id));
    let _id;

    try {
      // Case 1 : Object ID
      _id = new mongodb.ObjectID.createFromHexString(id);
    } catch (err) {
      // Case 2 : BinaryID (only subtype 3 and 4)
      if (('subtype' in req.query) && [3, 4].indexOf(req.query.subtype)) {
        _id = new mongodb.Binary(Buffer.from(id, 'base64'), req.query.subtype);
      } else {
        // Case 3 : Try as raw ID
        _id = id;
      }
    }

    let findStraighId = function (id) {
      // No document found with obj_id, try again with straight id
      req.collection.findOne({ _id: id }, function (err, doc) {
        if (err) {
          req.session.error = 'Error: ' + err;
          return res.redirect(res.locals.baseHref + 'db/' + req.dbName + '/' + req.collectionName);
        }

        if (doc === null) {
          req.session.error = 'Document not found!';
github artifacthealth / hydrate-mongodb / tests / mapping / bufferMapping.tests.ts View on Github external
it('returns a buffer for the bson Binary data', () => {

            var mapping = new BufferMapping();

            var testValue = "test";
            var result = mapping.read(new ReadContext(null), new Binary(new Buffer(testValue))).toString("utf8");

            assert.equal(result, testValue);
        });
    });
github watson / json2mongo / index.js View on Github external
module.exports = function (obj) {
  var key, val
  for (key in obj) {
    if (!obj.hasOwnProperty(key)) continue
    val = obj[key]
    switch (key) {
      case '$binary':
      case '$type':
        return new mongo.Binary(obj.$binary, obj.$type)
      case '$date':
        return new Date(val)
      case '$decimal128':
        return new mongo.Decimal128(Buffer.from(val))
      case '$timestamp':
        return new mongo.Timestamp(val.t, val.i)
      case '$regex':
      case '$options':
        return new RegExp(obj.$regex, obj.$options)
      case '$oid':
        return new mongo.ObjectID(val)
      case '$ref':
      case '$id':
      case '$db':
        var id = obj.$id._bsontype ? obj.$id : mongo.ObjectID(obj.$id.$oid)
        return new mongo.DBRef(obj.$ref, id, obj.$db)
github cdimascio / uuid-mongodb / lib / index.js View on Github external
from(uuid) {
    if (typeof uuid === 'string') {
      return generateUUID(uuid);
    } else if (
      (uuid instanceof Binary || uuid._bsontype === 'Binary') &&
      uuid.sub_type === Binary.SUBTYPE_UUID
    ) {
      if (uuid.length() !== 16) throw Err.InvalidUUID;
      return apply(
        new Binary(uuid.read(0, uuid.length()), Binary.SUBTYPE_UUID)
      );
    } else {
      throw Err.UnexpectedUUIDType;
    }
  },
};
github cdimascio / uuid-mongodb / lib / index.js View on Github external
function generateUUID(uuid, v = 1) {
  if (v <= 0 || v > 4 || v === 2 || v === 3) throw Err.UnsupportedUUIDVersion;

  let uuidv = uuidv1;
  if (v === 4) uuidv = uuidv4;

  if (uuid) {
    if (!validateUuid(uuid)) throw Err.InvalidUUID;
    const normalized = normalize(uuid);
    if (!normalized) throw Err.InvalidHexString;
    return apply(
      new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID)
    );
  } else {
    const uuid = uuidv(null, Buffer.alloc(16));
    return apply(Binary(uuid, Binary.SUBTYPE_UUID));
  }
}
github christkv / mongodb-schema-simulator / lib / common / scenarios / carts_no_reservation.js View on Github external
var createProduct = function(i, callback) {
    new Product(cols
      , i
      , f('product_%s', i)
      , {bin: new Binary(new Buffer(sizeOfProductsInBytes))}).create(function(err, p) {
        if(err) return callback(err);
        new Inventory(cols, i, quantityPrItem).create(function(err, i) {
          if(err) return callback(err);
          callback();
        });
    });
  }
github parse-community / parse-server / src / Adapters / Storage / Mongo / MongoTransform.js View on Github external
JSONToDatabase(json) {
    return new mongodb.Binary(new Buffer(json.base64, 'base64'));
  },
github christkv / mongodb-schema-simulator / lib / common / scenarios / carts_reservations.js View on Github external
var createProduct = function(i, callback) {
    new Product(cols
      , i
      , f('product_%s', i)
      , {bin: new Binary(new Buffer(sizeOfProductsInBytes))}).create(function(err, p) {
        if(err) return callback(err);

        new Inventory(cols, i, quantityPrItem).create(function(err, i) {
          if(err) return callback(err);
          callback();
        });
    });
  }
github reactioncommerce / reaction-devtools / data / images.js View on Github external
.png()
        .toBuffer();
      
    } else {
      img = await sharp({
        create: {
            width: 600,
            height: 600,
            channels: 3,
            background: { r, g, b }
        }
        })
        .jpeg()
        .toBuffer();
    }
    return Binary(img);
  }