diff --git a/lib/types/index.js b/lib/types/index.js index 1f67a5f9b8b..8babdf4b4d1 100644 --- a/lib/types/index.js +++ b/lib/types/index.js @@ -19,4 +19,4 @@ exports.Map = require('./map'); exports.Subdocument = require('./subdocument'); -exports.UUID = require('mongodb').BSON.UUID; +exports.UUID = require('./uuid'); diff --git a/lib/types/uuid.js b/lib/types/uuid.js new file mode 100644 index 00000000000..b3a01154758 --- /dev/null +++ b/lib/types/uuid.js @@ -0,0 +1,14 @@ +/** + * UUID type constructor + * + * #### Example: + * + * const id = new mongoose.Types.UUID(); + * + * @constructor UUID + */ + +'use strict'; + +module.exports = require('bson').UUID; + \ No newline at end of file diff --git a/package.json b/package.json index 36f12f68544..b47482d756b 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,9 @@ ], "license": "MIT", "dependencies": { - "bson": "^5.0.1", + "bson": "^5.2.0", "kareem": "2.5.1", - "mongodb": "5.1.0", + "mongodb": "5.3.0", "mpath": "0.9.0", "mquery": "5.0.0", "ms": "2.1.3", diff --git a/test/schema.uuid.test.js b/test/schema.uuid.test.js index 8292c233d35..e14d8e1fafd 100644 --- a/test/schema.uuid.test.js +++ b/test/schema.uuid.test.js @@ -129,6 +129,28 @@ describe('SchemaUUID', function() { assert.equal(organization, undefined); }); + it('handles built-in UUID type (gh-13103)', async function() { + const schema = new Schema({ + _id: { + type: Schema.Types.UUID + } + }, { _id: false }); + + db.deleteModel(/Test/); + const Test = db.model('Test', schema); + + const uuid = new mongoose.Types.UUID(); + let { _id } = await Test.create({ _id: uuid }); + assert.ok(_id); + assert.equal(typeof _id, 'string'); + assert.equal(_id, uuid.toString()); + + ({ _id } = await Test.findById(uuid)); + assert.ok(_id); + assert.equal(typeof _id, 'string'); + assert.equal(_id, uuid.toString()); + }); + // the following are TODOs based on SchemaUUID.prototype.$conditionalHandlers which are not tested yet it('should work with $bits* operators'); it('should work with $all operator');