UNPKG

@videsk/mongoose-dummy

Version:

Random data generator based on mongoose schema, with very flexible options, populate feature and easily integrable with random data generators libraries.

12 lines (11 loc) 3.75 kB
/** * Copyright (C) 2025 by Videsk - All Rights Reserved * @name @videsk/mongoose-dummy * @author Videsk * @license UNLICENSED * Written by Videsk <devs@videsk.io> * * Random data generator based on mongoose schema, with very flexible options, populate feature and easily integrable with random data generators libraries. * */ "use strict";var e=require("mongoose"),t=require("uuid");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=n(e);const{Schema:s,Types:r}=a.default;class o{constructor(e){if(!e)throw new Error("Pass a valid mongoose instance.");this.mongooseInstance=e,this.schemas=e.models,this.config={generators:{}}}setup(e={}){return this.config=Object.assign(this.config,e),this}set generators(e){this.config.generators=Object.assign(this.config.generators,e)}get generators(){return this.config.generators}generate(e=(()=>!0)){return this.iterate(this.baseModel,{},0,e)}model(e=""){return this.baseModel=this.getModel(e),this}getModel(e){if(!(e in this.schemas))throw new Error(`The model name "${e}" is not present in schema models. Is case sensitive!`);return this.schemas[e].schema}get objectModel(){if(!this.baseModel)throw new Error("Set the model before try get the object with model() method.");return this.baseModel.obj}iterate(e,t={},n=0,a=(()=>!0)){const{paths:s}=e;for(const e of Object.values(s))if(o.canParse(e,this.config.dummyKey)&&o.query(e,a)){const s=e.path.split("."),r=s.pop();s.reduce(((e,t)=>e[t]=e[t]||{}),t)[r]=this.evaluateDummy(e,n,t,a)}return t}evaluateDummy(e,t=0,n={},a=(()=>!0)){const{arrayLength:i=3,dummyKey:c="dummy"}=this.config||{},{instance:u}=e;if(t>2)return o.getFallbackValue(e);const{length:h=i}=e.options[c]||{};if(e.options[c]instanceof Function)try{return e.options[c].call(n,this.generators)}catch{return o.getFallbackValue(e)}else{if(e instanceof s.Types.ObjectId||"ObjectId"===u)return this.evaluateObjectId(e,t,a);if(e instanceof s.Types.DocumentArray||e instanceof s.Types.Array||"Array"===u)return[...Array(h)].map((()=>this.getArrayItem(e,n,t,a)));if(e instanceof r.Subdocument||e.schema?.paths||"Embedded"===u)return this.iterate(e.schema,{},t)}return o.getFallbackValue(e)}getArrayItem(e,t,n=0,a=(()=>!0)){if(e?.schema instanceof s)return this.iterate(e.schema,{},n+1,a);const r=e.caster;return r instanceof s.Types.ObjectId?this.evaluateObjectId(r,n,a):this.evaluateDummy(r,n+1,t,a)}evaluateObjectId(e,t=0,n=(()=>!0)){const{ref:a,populate:s}=e.options;return a&&s?this.iterate(this.getModel(a),{},t+1,n):o.canParse(e)?new r.ObjectId:void 0}static getFallbackValue(e){const{Mixed:n,ObjectId:a,Number:o,Boolean:i,String:c,Map:u,Buffer:h,BigInt:m=o,DocumentArray:d,Decimal128:l}=s.Types,{max:f,min:g,default:y}=e.options,{instance:p}=e;return void 0!==y?y:e instanceof s.Types.Array||e instanceof d||"Array"===p?new r.Array:e instanceof r.Subdocument||e instanceof n||e instanceof u||"Embedded"===p?{}:e instanceof a||"ObjectId"===p?new r.ObjectId:e instanceof o||e instanceof m||e instanceof l||"Number"===p?this.randomNumber(g,f):e instanceof i||"Boolean"===p?Math.random()<.5:e instanceof c||e instanceof h||"String"===p?this.generateStringBasedOnSchemaOptions(e.options):e instanceof s.Types.Date||"Date"===p?new Date:e instanceof(s.Types.UUID||c)?t.v4():null}static randomNumber(e=0,t=99){return Math.floor(Math.random()*t)+e}static generateStringBasedOnSchemaOptions(e={}){const{enum:t,maxLength:n=99,minLength:a=3}=e;return Array.isArray(t)&&t.length>0?t[this.randomNumber(0,t.length-1)]:[...Array(Math.floor(Math.random()*(n-a+1))+a)].map((()=>Math.random().toString(36)[2])).join("")}static canParse(e,t="dummy"){return[t,"populate"].some((t=>e.options.hasOwnProperty(t)))}static query(e,t=(()=>!0)){return t(e.options)}}module.exports=o;