Skip to content

Commit 0b90f46

Browse files
committed
refactor(schema): store top-level primitive array paths in schema as 'arr.$', 'arr.$.$', etc.
Re: #6405
1 parent c79a649 commit 0b90f46

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

lib/schema.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,15 +542,17 @@ warnings.increment = '`increment` should not be used as a schema path name ' +
542542
*/
543543

544544
Schema.prototype.path = function(path, obj) {
545+
// Convert to '.$' to check subpaths re: gh-6405
546+
const cleanPath = path.replace(/\.\d+\./g, '.$.').replace(/\.\d+$/, '.$');
545547
if (obj === undefined) {
546548
if (this.paths.hasOwnProperty(path)) {
547549
return this.paths[path];
548550
}
549-
if (this.subpaths.hasOwnProperty(path)) {
550-
return this.subpaths[path];
551+
if (this.subpaths.hasOwnProperty(cleanPath)) {
552+
return this.subpaths[cleanPath];
551553
}
552-
if (this.singleNestedPaths.hasOwnProperty(path)) {
553-
return this.singleNestedPaths[path];
554+
if (this.singleNestedPaths.hasOwnProperty(cleanPath)) {
555+
return this.singleNestedPaths[cleanPath];
554556
}
555557

556558
// Look for maps
@@ -628,6 +630,10 @@ Schema.prototype.path = function(path, obj) {
628630
this.singleNestedPaths[path + '.' + key] =
629631
schemaType.schema.singleNestedPaths[key];
630632
}
633+
for (const key in schemaType.schema.subpaths) {
634+
this.singleNestedPaths[path + '.' + key] =
635+
schemaType.schema.subpaths[key];
636+
}
631637

632638
Object.defineProperty(schemaType.schema, 'base', {
633639
configurable: true,
@@ -656,6 +662,29 @@ Schema.prototype.path = function(path, obj) {
656662
});
657663
}
658664

665+
if (schemaType.$isMongooseArray && schemaType.caster instanceof SchemaType) {
666+
let arrayPath = path;
667+
let _schemaType = schemaType;
668+
669+
let toAdd = [];
670+
while (_schemaType.$isMongooseArray) {
671+
arrayPath = arrayPath + '.$';
672+
673+
// Skip arrays of document arrays
674+
if (_schemaType.$isMongooseDocumentArray) {
675+
toAdd = [];
676+
break;
677+
}
678+
_schemaType = _schemaType.caster.clone();
679+
_schemaType.path = arrayPath;
680+
toAdd.push(_schemaType);
681+
}
682+
683+
for (const _schemaType of toAdd) {
684+
this.subpaths[_schemaType.path] = _schemaType;
685+
}
686+
}
687+
659688
return this;
660689
};
661690

@@ -939,6 +968,9 @@ Schema.prototype.indexedPaths = function indexedPaths() {
939968
*/
940969

941970
Schema.prototype.pathType = function(path) {
971+
// Convert to '.$' to check subpaths re: gh-6405
972+
const cleanPath = path.replace(/\.\d+\./g, '.$.').replace(/\.\d+$/, '.$');
973+
942974
if (this.paths.hasOwnProperty(path)) {
943975
return 'real';
944976
}
@@ -948,10 +980,10 @@ Schema.prototype.pathType = function(path) {
948980
if (this.nested.hasOwnProperty(path)) {
949981
return 'nested';
950982
}
951-
if (this.subpaths.hasOwnProperty(path)) {
983+
if (this.subpaths.hasOwnProperty(cleanPath)) {
952984
return 'real';
953985
}
954-
if (this.singleNestedPaths.hasOwnProperty(path)) {
986+
if (this.singleNestedPaths.hasOwnProperty(cleanPath)) {
955987
return 'real';
956988
}
957989

@@ -1080,7 +1112,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
10801112
};
10811113

10821114
/*!
1083-
* ignore
1115+
* ignore. Deprecated re: #6405
10841116
*/
10851117

10861118
function getPositionalPathType(self, path) {

test/schema.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ describe('schema', function() {
406406
assert.ok(mixed[4] instanceof Date);
407407
assert.ok(mixed[5] instanceof DocumentObjectId);
408408

409+
// gh-6405
410+
assert.ok(Loki.path('dates.$') instanceof mongoose.Schema.Date);
411+
assert.ok(Loki.path('numbers.$') instanceof mongoose.Schema.Number);
412+
assert.ok(Loki.path('strings.$') instanceof mongoose.Schema.String);
413+
assert.ok(Loki.path('buffers.$') instanceof mongoose.Schema.Buffer);
414+
assert.ok(Loki.path('mixed.$') instanceof mongoose.Schema.Mixed);
415+
409416
done();
410417
});
411418

0 commit comments

Comments
 (0)