/** * Copyright (C) 2024 by Videsk - All Rights Reserved * @name @videsk/mongoose-dummy * @author Videsk * @license UNLICENSED * Written by Videsk * * 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 r=n(e);const{Schema:s,Types:a,SchemaType:o}=r.default;module.exports=class{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,r=(()=>!0)){const{paths:s}=e;for(const e of Object.values(s))this.constructor.canParse(e,this.config.dummyKey)&&this.constructor.query(e,r)&&e.path.split(".").reduce(((s,a,o,i)=>s[a]=s[a]||(o===i.length-1?this.evaluateDummy(e,n,t,r):{})),t);return t}evaluateDummy(e,t=0,n={},r=(()=>!0)){const{arrayLength:o=3,dummyKey:i="dummy"}=this.config||{},{instance:c}=e;if(t>2)return this.constructor.getFallbackValue(e);const{length:u=o}=e.options[i]||{};if(e.options[i]instanceof Function)try{return e.options[i].call(n,this.generators)}catch(t){return this.constructor.getFallbackValue(e)}else{if(e instanceof s.Types.ObjectId||"ObjectId"===c)return this.evaluateObjectId(e,r);if(e instanceof s.Types.DocumentArray||e instanceof s.Types.Array||"Array"===c)return[...Array(u)].map((()=>this.getArrayItem(e,n,r)));if(e instanceof a.Subdocument||e.schema?.paths||"Embedded"===c)return this.iterate(e.schema,{},t)}return this.constructor.getFallbackValue(e)}getArrayItem(e,t,n=(()=>!0)){if(e?.schema instanceof s)return this.iterate(e.schema,{},2);const r=e.caster;return r instanceof s.Types.ObjectId?this.evaluateObjectId(r,n):this.evaluateDummy(r,2,t,n)}evaluateObjectId(e,t=(()=>!0)){const{ref:n,populate:r}=e.options;return n&&r?this.iterate(this.getModel(n),{},2,t):this.constructor.canParse(e)?new a.ObjectId:void 0}static getFallbackValue(e){const{Mixed:n,ObjectId:r,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 a.Array:e instanceof a.Subdocument||e instanceof n||e instanceof u||"Embedded"===p?{}:e instanceof r||"ObjectId"===p?new a.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:r=3}=e;return Array.isArray(t)&&t.length>0?t[this.randomNumber(0,t.length-1)]:[...Array(Math.floor(Math.random()*(n-r+1))+r)].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)}};