Skip to content

Commit

Permalink
Merge pull request #13323 from Automattic/vkarpov15/gh-13103
Browse files Browse the repository at this point in the history
feat: handle MongoDB's new UUID type, export `mongoose.Types.UUID`
  • Loading branch information
vkarpov15 committed Apr 25, 2023
2 parents 881c5ab + 00953e1 commit 0b7323b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ exports.Map = require('./map');

exports.Subdocument = require('./subdocument');

exports.UUID = require('mongodb').BSON.UUID;
exports.UUID = require('./uuid');
13 changes: 13 additions & 0 deletions lib/types/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* UUID type constructor
*
* #### Example:
*
* const id = new mongoose.Types.UUID();
*
* @constructor UUID
*/

'use strict';

module.exports = require('bson').UUID;
22 changes: 22 additions & 0 deletions test/schema.uuid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 0b7323b

Please sign in to comment.