Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with mongoose 5.x and later #23

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading