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.65 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. * */ import e from"mongoose";import{v4 as t}from"uuid";const{Schema:n,Types:a}=e;class s{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:r}=e;for(const e of Object.values(r))if(s.canParse(e,this.config.dummyKey)&&s.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,r={},o=(()=>!0)){const{arrayLength:i=3,dummyKey:c="dummy"}=this.config||{},{instance:h}=e;if(t>2)return s.getFallbackValue(e);const{length:m=i}=e.options[c]||{};if(e.options[c]instanceof Function)try{return e.options[c].call(r,this.generators)}catch{return s.getFallbackValue(e)}else{if(e instanceof n.Types.ObjectId||"ObjectId"===h)return this.evaluateObjectId(e,t,o);if(e instanceof n.Types.DocumentArray||e instanceof n.Types.Array||"Array"===h)return[...Array(m)].map((()=>this.getArrayItem(e,r,t,o)));if(e instanceof a.Subdocument||e.schema?.paths||"Embedded"===h)return this.iterate(e.schema,{},t)}return s.getFallbackValue(e)}getArrayItem(e,t,a=0,s=(()=>!0)){if(e?.schema instanceof n)return this.iterate(e.schema,{},a+1,s);const r=e.caster;return r instanceof n.Types.ObjectId?this.evaluateObjectId(r,a,s):this.evaluateDummy(r,a+1,t,s)}evaluateObjectId(e,t=0,n=(()=>!0)){const{ref:r,populate:o}=e.options;return r&&o?this.iterate(this.getModel(r),{},t+1,n):s.canParse(e)?new a.ObjectId:void 0}static getFallbackValue(e){const{Mixed:s,ObjectId:r,Number:o,Boolean:i,String:c,Map:h,Buffer:m,BigInt:u=o,DocumentArray:d,Decimal128:l}=n.Types,{max:f,min:g,default:p}=e.options,{instance:y}=e;return void 0!==p?p:e instanceof n.Types.Array||e instanceof d||"Array"===y?new a.Array:e instanceof a.Subdocument||e instanceof s||e instanceof h||"Embedded"===y?{}:e instanceof r||"ObjectId"===y?new a.ObjectId:e instanceof o||e instanceof u||e instanceof l||"Number"===y?this.randomNumber(g,f):e instanceof i||"Boolean"===y?Math.random()<.5:e instanceof c||e instanceof m||"String"===y?this.generateStringBasedOnSchemaOptions(e.options):e instanceof n.Types.Date||"Date"===y?new Date:e instanceof(n.Types.UUID||c)?t():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)}}export{s as default};