Skip to content

Commit

Permalink
Merge pull request #13538 from Automattic/IslandRhythms/gh-13510
Browse files Browse the repository at this point in the history
Island rhythms/gh 13510
  • Loading branch information
vkarpov15 authored Jun 28, 2023
2 parents 5e18d6b + a5a0be3 commit bc5643d
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ Model.prototype.$__handleSave = function(options, callback) {
_setIsNew(this, false);
// Make it possible to retry the insert
this.$__.inserting = true;

return;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ SubdocumentPath.defaultOptions = {};

SubdocumentPath.set = SchemaType.set;

SubdocumentPath.setters = [];

/**
* Attaches a getter for all SubdocumentPath instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ SchemaArray.defaultOptions = {};
*/
SchemaArray.set = SchemaType.set;

SchemaArray.setters = [];

/**
* Attaches a getter for all Array instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ SchemaBigInt._cast = castBigInt;

SchemaBigInt.set = SchemaType.set;

SchemaBigInt.setters = [];

/**
* Attaches a getter for all BigInt instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ SchemaBoolean._cast = castBoolean;

SchemaBoolean.set = SchemaType.set;

SchemaBoolean.setters = [];

/**
* Attaches a getter for all Boolean instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ SchemaBuffer._checkRequired = v => !!(v && v.length);

SchemaBuffer.set = SchemaType.set;

SchemaBuffer.setters = [];

/**
* Attaches a getter for all Buffer instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ SchemaDate._cast = castDate;

SchemaDate.set = SchemaType.set;

SchemaDate.setters = [];

/**
* Attaches a getter for all Date instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Decimal128._cast = castDecimal128;

Decimal128.set = SchemaType.set;

Decimal128.setters = [];

/**
* Attaches a getter for all Decimal128 instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ DocumentArrayPath.defaultOptions = {};

DocumentArrayPath.set = SchemaType.set;

DocumentArrayPath.setters = [];

/**
* Attaches a getter for all DocumentArrayPath instances
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Mixed.get = SchemaType.get;

Mixed.set = SchemaType.set;

Mixed.setters = [];

/**
* Casts `val` for Mixed.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ SchemaNumber.get = SchemaType.get;

SchemaNumber.set = SchemaType.set;

SchemaNumber.setters = [];

/*!
* ignore
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ ObjectId.get = SchemaType.get;

ObjectId.set = SchemaType.set;

ObjectId.setters = [];

/**
* Adds an auto-generated ObjectId default if turnOn is true.
* @param {Boolean} turnOn auto generated ObjectId defaults
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ SchemaString.get = SchemaType.get;

SchemaString.set = SchemaType.set;

SchemaString.setters = [];

/*!
* ignore
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ SchemaUUID.get = SchemaType.get;

SchemaUUID.set = SchemaType.set;

SchemaUUID.setters = [];

/**
* Get/set the function used to cast arbitrary values to UUIDs.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function SchemaType(path, options, instance) {
this.getters = this.constructor.hasOwnProperty('getters') ?
this.constructor.getters.slice() :
[];
this.setters = [];
this.setters = this.constructor.hasOwnProperty('setters') ?
this.constructor.setters.slice() :
[];

this.splitPath();

Expand Down
6 changes: 5 additions & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7615,7 +7615,11 @@ describe('document', function() {

schema.path('createdAt').immutable(true);
assert.ok(schema.path('createdAt').$immutable);
assert.equal(schema.path('createdAt').setters.length, 1);
assert.equal(
schema.path('createdAt').setters.length,
1,
schema.path('createdAt').setters.map(setter => setter.toString())
);

schema.path('createdAt').immutable(false);
assert.ok(!schema.path('createdAt').$immutable);
Expand Down
1 change: 1 addition & 0 deletions test/query.toconstructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe('Query:', function() {
});

const Test = db.model('Test', schema);
await Test.init();
await Test.deleteMany({});
const test = new Test({ name: 'Romero' });
const Q = Test.findOne({}).toConstructor();
Expand Down
23 changes: 22 additions & 1 deletion test/schematype.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* Module dependencies.
*/

const mongoose = require('./common').mongoose;
const start = require('./common');

const mongoose = start.mongoose;

const assert = require('assert');

Expand Down Expand Up @@ -211,6 +213,25 @@ describe('schematype', function() {
it('SchemaType.set, is a function', () => {
assert.equal(typeof mongoose.SchemaType.set, 'function');
});
it('should allow setting values to a given property gh-13510', async function() {
const m = new mongoose.Mongoose();
await m.connect(start.uri);
m.SchemaTypes.Date.setters.push(v => typeof v === 'string' && /^\d{8}$/.test(v) ? new Date(v.slice(0, 4), +v.slice(4, 6) - 1, v.slice(6, 8)) : v);
const testSchema = new m.Schema({
myDate: Date
});
const Test = m.model('Test', testSchema);
await Test.deleteMany({});
const doc = new Test();
doc.myDate = '20220601';
await doc.save();
await m.connections[0].close();
assert(doc.myDate instanceof Date);
});

after(() => {
mongoose.SchemaTypes.Date.setters = [];
});
});

const typesToTest = Object.values(mongoose.SchemaTypes).
Expand Down

0 comments on commit bc5643d

Please sign in to comment.