Skip to content

Commit 21aded5

Browse files
authored
Merge pull request #15326 from Automattic/8.13
8.13
2 parents adc17f2 + 4bdcdfb commit 21aded5

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

lib/mongoose.js

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
179179
}
180180
}
181181

182+
if (driver.SchemaTypes != null) {
183+
Object.assign(mongoose.Schema.Types, driver.SchemaTypes);
184+
}
185+
182186
const Connection = driver.Connection;
183187
const oldDefaultConnection = _mongoose.connections[0];
184188
_mongoose.connections = [new Connection(_mongoose)];

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dependencies": {
2323
"bson": "^6.10.3",
2424
"kareem": "2.6.3",
25-
"mongodb": "~6.14.0",
25+
"mongodb": "~6.15.0",
2626
"mpath": "0.9.0",
2727
"mquery": "5.0.0",
2828
"ms": "2.1.3",

test/driver.test.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('driver', function() {
1313

1414
class Collection {
1515
findOne(filter, options) { // eslint-disable-line no-unused-vars
16-
return Promise.resolve({ answer: 42 });
16+
return Promise.resolve({ answer: 42, date: '2023-10-01' });
1717
}
1818
}
1919
class Connection extends EventEmitter {
@@ -35,18 +35,34 @@ describe('driver', function() {
3535
const driver = {
3636
Collection,
3737
Connection,
38-
plugins: [foo]
38+
plugins: [foo],
39+
SchemaTypes: {
40+
DateString: class DateString extends mongoose.SchemaType {
41+
constructor(key, options) {
42+
super(key, options, 'DateString');
43+
}
44+
45+
cast(val) {
46+
if (typeof val !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(val)) {
47+
throw new Error('DateString must be in format yyyy-mm-dd');
48+
}
49+
return val;
50+
}
51+
}
52+
}
3953
};
4054

4155
m.setDriver(driver);
4256
assert.deepStrictEqual(m.plugins.slice(mongoose.plugins.length), [[foo, undefined]]);
4357

4458
await m.connect();
4559

46-
const Test = m.model('Test', m.Schema({ answer: Number }));
60+
const Test = m.model('Test', m.Schema({ answer: Number, date: 'DateString' }));
4761

4862
const res = await Test.findOne();
49-
assert.deepEqual(res.toObject(), { answer: 42 });
63+
assert.deepEqual(res.toObject(), { answer: 42, date: '2023-10-01' });
64+
65+
await assert.rejects(Test.create({ answer: 12, date: '2025-01' }), /path "date"/);
5066

5167
function foo() {}
5268
});

0 commit comments

Comments
 (0)