Skip to content

Commit

Permalink
now test
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Jan 3, 2023
1 parent f71ef7a commit d05173c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const schemaStack = new WeakMap();

module.exports = function autopopulatePlugin(schema, options) {
const pathsToPopulate = getPathsToPopulate(schema);

Expand Down Expand Up @@ -152,7 +150,7 @@ function autopopulateDiscriminators(res) {

function getPathsToPopulate(schema) {
const pathsToPopulate = [];

const schemaStack = new WeakMap();
eachPathRecursive(schema, function(pathname, schemaType) {
let option;
if (schemaType.options && schemaType.options.autopopulate) {
Expand All @@ -171,7 +169,7 @@ function getPathsToPopulate(schema) {
autopopulate: option
});
}
});
}, null, schemaStack);

return pathsToPopulate;
}
Expand Down Expand Up @@ -222,7 +220,7 @@ function handleFunction(fn, options) {
return processOption.call(this, val, options);
}

function eachPathRecursive(schema, handler, path) {
function eachPathRecursive(schema, handler, path, schemaStack) {

if (schemaStack.has(schema)) {
return;
Expand All @@ -235,7 +233,7 @@ function eachPathRecursive(schema, handler, path) {
schema.eachPath(function(pathname, schemaType) {
path.push(pathname);
if (schemaType.schema) {
eachPathRecursive(schemaType.schema, handler, path);
eachPathRecursive(schemaType.schema, handler, path, schemaStack);

if (schemaType.schema.discriminators != null) {
for (const discriminatorName of Object.keys(schemaType.schema.discriminators)) {
Expand All @@ -247,11 +245,11 @@ function eachPathRecursive(schema, handler, path) {
schemaType = schemaType.$embeddedSchemaType;
}
if (schemaType != null && schemaType.$isMongooseDocumentArray) {
eachPathRecursive(schemaType.schema, handler, path);
eachPathRecursive(schemaType.schema, handler, path, schemaStack);

if (schemaType.schema.discriminators != null) {
for (const discriminatorName of Object.keys(schemaType.schema.discriminators)) {
eachPathRecursive(schemaType.schema.discriminators[discriminatorName], handler, path);
eachPathRecursive(schemaType.schema.discriminators[discriminatorName], handler, path, schemaStack);
}
}
}
Expand Down

0 comments on commit d05173c

Please sign in to comment.