Skip to content

Commit

Permalink
feat: handle MongoDB's new UUID type, export mongoose.Types.UUID
Browse files Browse the repository at this point in the history
Fix #13103
  • Loading branch information
vkarpov15 committed Apr 24, 2023
1 parent 72ef315 commit ac97396
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
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');
14 changes: 14 additions & 0 deletions lib/types/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* UUID type constructor
*
* #### Example:
*
* const id = new mongoose.Types.UUID();
*
* @constructor UUID
*/

'use strict';

module.exports = require('bson').UUID;

Check failure on line 14 in lib/types/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check warning on line 14 in lib/types/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Newline required at end of file but not found

Check failure on line 14 in lib/types/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check warning on line 14 in lib/types/uuid.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Newline required at end of file but not found
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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());

Check failure on line 147 in test/schema.uuid.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 147 in test/schema.uuid.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
({ _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 ac97396

Please sign in to comment.