From ac97396cafbe5da8272c39ccbb78eb7cabd3a63f Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 24 Apr 2023 11:37:22 -0400 Subject: [PATCH] feat: handle MongoDB's new UUID type, export `mongoose.Types.UUID` Fix #13103 --- lib/types/index.js | 2 +- lib/types/uuid.js | 14 ++++++++++++++ package.json | 4 ++-- test/schema.uuid.test.js | 22 ++++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 lib/types/uuid.js 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');