Skip to content

Commit

Permalink
refactor(schema): store top-level primitive array paths in schema as …
Browse files Browse the repository at this point in the history
…'arr.$', 'arr.$.$', etc.

Re: #6405
  • Loading branch information
vkarpov15 committed Jul 28, 2019
1 parent c79a649 commit 0b90f46
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
46 changes: 39 additions & 7 deletions lib/schema.js
Expand Up @@ -542,15 +542,17 @@ warnings.increment = '`increment` should not be used as a schema path name ' +
*/

Schema.prototype.path = function(path, obj) {
// Convert to '.$' to check subpaths re: gh-6405
const cleanPath = path.replace(/\.\d+\./g, '.$.').replace(/\.\d+$/, '.$');
if (obj === undefined) {
if (this.paths.hasOwnProperty(path)) {
return this.paths[path];
}
if (this.subpaths.hasOwnProperty(path)) {
return this.subpaths[path];
if (this.subpaths.hasOwnProperty(cleanPath)) {
return this.subpaths[cleanPath];
}
if (this.singleNestedPaths.hasOwnProperty(path)) {
return this.singleNestedPaths[path];
if (this.singleNestedPaths.hasOwnProperty(cleanPath)) {
return this.singleNestedPaths[cleanPath];
}

// Look for maps
Expand Down Expand Up @@ -628,6 +630,10 @@ Schema.prototype.path = function(path, obj) {
this.singleNestedPaths[path + '.' + key] =
schemaType.schema.singleNestedPaths[key];
}
for (const key in schemaType.schema.subpaths) {
this.singleNestedPaths[path + '.' + key] =
schemaType.schema.subpaths[key];
}

Object.defineProperty(schemaType.schema, 'base', {
configurable: true,
Expand Down Expand Up @@ -656,6 +662,29 @@ Schema.prototype.path = function(path, obj) {
});
}

if (schemaType.$isMongooseArray && schemaType.caster instanceof SchemaType) {
let arrayPath = path;
let _schemaType = schemaType;

let toAdd = [];
while (_schemaType.$isMongooseArray) {
arrayPath = arrayPath + '.$';

// Skip arrays of document arrays
if (_schemaType.$isMongooseDocumentArray) {
toAdd = [];
break;
}
_schemaType = _schemaType.caster.clone();
_schemaType.path = arrayPath;
toAdd.push(_schemaType);
}

for (const _schemaType of toAdd) {
this.subpaths[_schemaType.path] = _schemaType;
}
}

return this;
};

Expand Down Expand Up @@ -939,6 +968,9 @@ Schema.prototype.indexedPaths = function indexedPaths() {
*/

Schema.prototype.pathType = function(path) {
// Convert to '.$' to check subpaths re: gh-6405
const cleanPath = path.replace(/\.\d+\./g, '.$.').replace(/\.\d+$/, '.$');

if (this.paths.hasOwnProperty(path)) {
return 'real';
}
Expand All @@ -948,10 +980,10 @@ Schema.prototype.pathType = function(path) {
if (this.nested.hasOwnProperty(path)) {
return 'nested';
}
if (this.subpaths.hasOwnProperty(path)) {
if (this.subpaths.hasOwnProperty(cleanPath)) {
return 'real';
}
if (this.singleNestedPaths.hasOwnProperty(path)) {
if (this.singleNestedPaths.hasOwnProperty(cleanPath)) {
return 'real';
}

Expand Down Expand Up @@ -1080,7 +1112,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
};

/*!
* ignore
* ignore. Deprecated re: #6405
*/

function getPositionalPathType(self, path) {
Expand Down
7 changes: 7 additions & 0 deletions test/schema.test.js
Expand Up @@ -406,6 +406,13 @@ describe('schema', function() {
assert.ok(mixed[4] instanceof Date);
assert.ok(mixed[5] instanceof DocumentObjectId);

// gh-6405
assert.ok(Loki.path('dates.$') instanceof mongoose.Schema.Date);
assert.ok(Loki.path('numbers.$') instanceof mongoose.Schema.Number);
assert.ok(Loki.path('strings.$') instanceof mongoose.Schema.String);
assert.ok(Loki.path('buffers.$') instanceof mongoose.Schema.Buffer);
assert.ok(Loki.path('mixed.$') instanceof mongoose.Schema.Mixed);

done();
});

Expand Down

0 comments on commit 0b90f46

Please sign in to comment.