Skip to content

Commit

Permalink
fix: adding compatibility with mongoose 5 and later
Browse files Browse the repository at this point in the history
  • Loading branch information
matiaslopezd committed May 2, 2024
1 parent 182e750 commit 5fcdc05
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"branches": [
"main"
]
},
"dependencies": {
"uuid": "^9.0.1"
}
}
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Schema, Types } from 'mongoose';
import mongoose from 'mongoose';
import { v4 as uuid } from 'uuid';
const { Schema, Types } = mongoose;

class MongooseDummy {

Expand Down Expand Up @@ -91,7 +93,7 @@ class MongooseDummy {
return this.constructor.getFallbackValue(schema);
}
}
else if (schema instanceof Schema.Types.Subdocument) return this.iterate(schema.schema, {}, iteration);
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));
return this.constructor.getFallbackValue(schema);
Expand All @@ -112,17 +114,17 @@ class MongooseDummy {
}

static getFallbackValue(schema) {
const { Subdocument, Mixed, ObjectId, Number, Boolean, String, UUID, Map, Buffer, DocumentArray, BigInt, Decimal128 } = Schema.Types;
const { Mixed, ObjectId, Number, Boolean, String, UUID, Map, Buffer, DocumentArray, BigInt, Decimal128 } = Schema.Types;
const { max, min, default: defaultValue } = schema.options;
if (typeof defaultValue !== 'undefined') return defaultValue;
if (schema instanceof Schema.Types.Array || schema instanceof DocumentArray) return new Types.Array();
else if (schema instanceof Subdocument || schema instanceof Mixed || schema instanceof Map) return {};
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();
else if (schema instanceof UUID) return new Types.UUID();
else if (schema instanceof (Schema.Types.UUID || String)) return uuid();
return null;
}

Expand Down

0 comments on commit 5fcdc05

Please sign in to comment.