summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js
blob: ee6d5985356082d065590f42b04683d5d5962a5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * Module dependencies.
 */

var ObjectId = require('mongodb').BSONPure.ObjectID;

/**
 * Constructor export
 *
 * @api private
 */

var ObjectIdToString = ObjectId.toString.bind(ObjectId);

module.exports = exports = ObjectId;
/**
 * Creates an ObjectID for this driver
 *
 * @param {Object} hex string or ObjectId
 * @api private
 */

exports.fromString = function(str){
  // patch native driver bug in V0.9.6.4
  if (!('string' === typeof str && 24 === str.length)) {
    throw new Error("Invalid ObjectId");
  }

  return ObjectId.createFromHexString(str);
};

/**
 * Gets an ObjectId and converts it to string.
 *
 * @param {ObjectId} -native objectid
 * @api private
 */

exports.toString = function(oid){
  if (!arguments.length) return ObjectIdToString();
  return oid.toHexString();
};