Skip to content

Commit

Permalink
Compatibility with mongoose 5.x and later (#23)
Browse files Browse the repository at this point in the history
Certain `SchemaTypes` are not present in older version, so now using
`instance` property of each `schema` (`schema.instance`) is able to get
the name as `String` to compare correctly, but as a fallback when
instanceof can't be compare with undefined `SchemaType`.
  • Loading branch information
matiaslopezd authored May 14, 2024
2 parents 8bdcf06 + 6a87760 commit e3ad4b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';
import { v4 as uuid } from 'uuid';
const { Schema, Types } = mongoose;
const { Schema, Types, SchemaType } = mongoose;

class MongooseDummy {

Expand Down Expand Up @@ -83,6 +83,7 @@ class MongooseDummy {

evaluateDummy(schema, iteration = 0, output = {}, filter = () => true) {
const { arrayLength = 3, dummyKey = 'dummy' } = this.config || {};
const { instance } = schema;
if (iteration > 2) return this.constructor.getFallbackValue(schema);
const { length = arrayLength } = schema.options[dummyKey] || {};

Expand All @@ -93,9 +94,9 @@ class MongooseDummy {
return this.constructor.getFallbackValue(schema);
}
}
else if (schema instanceof Types.Subdocument) return this.iterate(schema.schema, {}, iteration);
else if (schema instanceof Schema.Types.ObjectId) return this.evaluateObjectId(schema, filter);
else if (schema instanceof Schema.Types.DocumentArray || schema instanceof Schema.Types.Array) return [...Array(length)].map(() => this.getArrayItem(schema, output, filter));
else if (schema instanceof Schema.Types.ObjectId || instance === 'ObjectId') return this.evaluateObjectId(schema, filter);
else if (schema instanceof Schema.Types.DocumentArray || schema instanceof Schema.Types.Array || instance === 'Array') return [...Array(length)].map(() => this.getArrayItem(schema, output, filter));
else if (schema instanceof Types.Subdocument || schema.schema?.paths || instance === 'Embedded') return this.iterate(schema.schema, {}, iteration);
return this.constructor.getFallbackValue(schema);
}

Expand All @@ -114,16 +115,17 @@ class MongooseDummy {
}

static getFallbackValue(schema) {
const { Mixed, ObjectId, Number, Boolean, String, Map, Buffer, DocumentArray, BigInt, Decimal128 } = Schema.Types;
const { Mixed, ObjectId, Number, Boolean, String, Map, Buffer, BigInt = Number, DocumentArray, Decimal128 } = Schema.Types;
const { max, min, default: defaultValue } = schema.options;
const { instance } = schema;
if (typeof defaultValue !== 'undefined') return defaultValue;
if (schema instanceof Schema.Types.Array || schema instanceof DocumentArray) return new Types.Array();
else if (schema instanceof Types.Subdocument || schema instanceof Mixed || schema instanceof Map) return {};
else if (schema instanceof ObjectId) return new Types.ObjectId();
else if (schema instanceof Number || schema instanceof BigInt || schema instanceof Decimal128) return this.randomNumber(min, max);
else if (schema instanceof Boolean) return Math.random() < 0.5;
else if (schema instanceof String || schema instanceof Buffer) return this.generateStringBasedOnSchemaOptions(schema.options);
else if (schema instanceof Schema.Types.Date) return new Date();
if (schema instanceof Schema.Types.Array || schema instanceof DocumentArray || instance === 'Array') return new Types.Array();
else if (schema instanceof Types.Subdocument || schema instanceof Mixed || schema instanceof Map || instance === 'Embedded') return {};
else if (schema instanceof ObjectId || instance === 'ObjectId') return new Types.ObjectId();
else if (schema instanceof Number || schema instanceof BigInt || schema instanceof Decimal128 || instance === 'Number') return this.randomNumber(min, max);
else if (schema instanceof Boolean || instance === 'Boolean') return Math.random() < 0.5;
else if (schema instanceof String || schema instanceof Buffer || instance === 'String') return this.generateStringBasedOnSchemaOptions(schema.options);
else if (schema instanceof Schema.Types.Date || instance === 'Date') return new Date();
else if (schema instanceof (Schema.Types.UUID || String)) return uuid();
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ describe('Test methods of MongooseDummy', function () {
expect('name' in output).to.be.equal(true);
expect('org' in output).to.be.equal(true);
expect(typeof output.name).to.be.equal('string');
expect(typeof output.credentials).to.be.equal('object');
expect(output.credentials).to.have.property('subCredentials')
expect(output.credentials).to.have.property('secretName')
});

it('Iterate model deeper', async () => {
Expand Down

0 comments on commit e3ad4b6

Please sign in to comment.