index.cjs 237 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  9. var __glob = (map) => (path) => {
  10. var fn = map[path];
  11. if (fn) return fn();
  12. throw new Error("Module not found in bundle: " + path);
  13. };
  14. var __esm = (fn, res) => function __init() {
  15. return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
  16. };
  17. var __export = (target, all) => {
  18. for (var name in all)
  19. __defProp(target, name, { get: all[name], enumerable: true });
  20. };
  21. var __copyProps = (to, from, except, desc) => {
  22. if (from && typeof from === "object" || typeof from === "function") {
  23. for (let key of __getOwnPropNames(from))
  24. if (!__hasOwnProp.call(to, key) && key !== except)
  25. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  26. }
  27. return to;
  28. };
  29. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  30. // If the importer is in node compatibility mode or this is not an ESM
  31. // file that has been converted to a CommonJS file using a Babel-
  32. // compatible transform (i.e. "__esModule" has not been set), then set
  33. // "default" to the CommonJS "module.exports" for node compatibility.
  34. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  35. mod
  36. ));
  37. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  38. // node_modules/@e22m4u/js-format/src/utils/is-class.js
  39. function isClass(value) {
  40. if (!value) return false;
  41. return typeof value === "function" && /^class\s/.test(Function.prototype.toString.call(value));
  42. }
  43. var init_is_class = __esm({
  44. "node_modules/@e22m4u/js-format/src/utils/is-class.js"() {
  45. __name(isClass, "isClass");
  46. }
  47. });
  48. // node_modules/@e22m4u/js-format/src/utils/index.js
  49. var init_utils = __esm({
  50. "node_modules/@e22m4u/js-format/src/utils/index.js"() {
  51. init_is_class();
  52. }
  53. });
  54. // node_modules/@e22m4u/js-format/src/value-to-string.js
  55. function valueToString(input) {
  56. if (input == null) return String(input);
  57. if (typeof input === "string") return `"${input}"`;
  58. if (typeof input === "number" || typeof input === "boolean")
  59. return String(input);
  60. if (isClass(input)) return input.name ? input.name : "Class";
  61. if (input.constructor && input.constructor.name)
  62. return BASE_CTOR_NAMES.includes(input.constructor.name) ? input.constructor.name : `${input.constructor.name} (instance)`;
  63. if (typeof input === "object" && input.constructor == null) return "Object";
  64. return String(input);
  65. }
  66. var BASE_CTOR_NAMES;
  67. var init_value_to_string = __esm({
  68. "node_modules/@e22m4u/js-format/src/value-to-string.js"() {
  69. init_utils();
  70. BASE_CTOR_NAMES = [
  71. "String",
  72. "Number",
  73. "Boolean",
  74. "Object",
  75. "Array",
  76. "Function",
  77. "Symbol",
  78. "Map",
  79. "Set",
  80. "Date"
  81. ];
  82. __name(valueToString, "valueToString");
  83. }
  84. });
  85. // node_modules/@e22m4u/js-format/src/array-to-list.js
  86. function arrayToList(input) {
  87. if (Array.isArray(input) && input.length)
  88. return input.map(valueToString).join(SEPARATOR);
  89. return valueToString(input);
  90. }
  91. var SEPARATOR;
  92. var init_array_to_list = __esm({
  93. "node_modules/@e22m4u/js-format/src/array-to-list.js"() {
  94. init_value_to_string();
  95. SEPARATOR = ", ";
  96. __name(arrayToList, "arrayToList");
  97. }
  98. });
  99. // node_modules/@e22m4u/js-format/src/format.js
  100. function format(pattern) {
  101. if (pattern instanceof Date) {
  102. pattern = pattern.toISOString();
  103. } else if (typeof pattern !== "string") {
  104. pattern = String(pattern);
  105. }
  106. const re = /(%?)(%([sdjvl]))/g;
  107. const args = Array.prototype.slice.call(arguments, 1);
  108. if (args.length) {
  109. pattern = pattern.replace(re, function(match, escaped, ptn, flag) {
  110. let arg = args.shift();
  111. switch (flag) {
  112. case "s":
  113. arg = String(arg);
  114. break;
  115. case "d":
  116. arg = Number(arg);
  117. break;
  118. case "j":
  119. arg = JSON.stringify(arg);
  120. break;
  121. case "v":
  122. arg = valueToString(arg);
  123. break;
  124. case "l":
  125. arg = arrayToList(arg);
  126. break;
  127. }
  128. if (!escaped) return arg;
  129. args.unshift(arg);
  130. return match;
  131. });
  132. }
  133. if (args.length) pattern += " " + args.join(" ");
  134. pattern = pattern.replace(/%{2}/g, "%");
  135. return "" + pattern;
  136. }
  137. var init_format = __esm({
  138. "node_modules/@e22m4u/js-format/src/format.js"() {
  139. init_array_to_list();
  140. init_value_to_string();
  141. __name(format, "format");
  142. }
  143. });
  144. // node_modules/@e22m4u/js-format/src/errorf.js
  145. var _Errorf, Errorf;
  146. var init_errorf = __esm({
  147. "node_modules/@e22m4u/js-format/src/errorf.js"() {
  148. init_format();
  149. _Errorf = class _Errorf extends Error {
  150. /**
  151. * Constructor.
  152. *
  153. * @param {string|undefined} pattern
  154. * @param {any} args
  155. */
  156. constructor(pattern = void 0, ...args) {
  157. const message = pattern != null ? format(pattern, ...args) : void 0;
  158. super(message);
  159. }
  160. };
  161. __name(_Errorf, "Errorf");
  162. Errorf = _Errorf;
  163. }
  164. });
  165. // node_modules/@e22m4u/js-format/src/index.js
  166. var init_src = __esm({
  167. "node_modules/@e22m4u/js-format/src/index.js"() {
  168. init_format();
  169. init_errorf();
  170. }
  171. });
  172. // node_modules/@e22m4u/js-service/src/errors/invalid-argument-error.js
  173. var _InvalidArgumentError, InvalidArgumentError;
  174. var init_invalid_argument_error = __esm({
  175. "node_modules/@e22m4u/js-service/src/errors/invalid-argument-error.js"() {
  176. init_src();
  177. _InvalidArgumentError = class _InvalidArgumentError extends Errorf {
  178. };
  179. __name(_InvalidArgumentError, "InvalidArgumentError");
  180. InvalidArgumentError = _InvalidArgumentError;
  181. }
  182. });
  183. // node_modules/@e22m4u/js-service/src/errors/index.js
  184. var init_errors = __esm({
  185. "node_modules/@e22m4u/js-service/src/errors/index.js"() {
  186. init_invalid_argument_error();
  187. }
  188. });
  189. // node_modules/@e22m4u/js-service/src/service-container.js
  190. var _ServiceContainer, ServiceContainer;
  191. var init_service_container = __esm({
  192. "node_modules/@e22m4u/js-service/src/service-container.js"() {
  193. init_service();
  194. init_errors();
  195. _ServiceContainer = class _ServiceContainer {
  196. /**
  197. * Services map.
  198. *
  199. * @type {Map<any, any>}
  200. * @private
  201. */
  202. _services = /* @__PURE__ */ new Map();
  203. /**
  204. * Parent container.
  205. *
  206. * @type {ServiceContainer}
  207. * @private
  208. */
  209. _parent;
  210. /**
  211. * Constructor.
  212. *
  213. * @param {ServiceContainer|undefined} parent
  214. */
  215. constructor(parent = void 0) {
  216. if (parent != null) {
  217. if (!(parent instanceof _ServiceContainer))
  218. throw new InvalidArgumentError(
  219. 'The provided parameter "parent" of ServicesContainer.constructor must be an instance ServiceContainer, but %v given.',
  220. parent
  221. );
  222. this._parent = parent;
  223. }
  224. }
  225. /**
  226. * Получить существующий или новый экземпляр.
  227. *
  228. * @param {*} ctor
  229. * @param {*} args
  230. * @return {*}
  231. */
  232. get(ctor, ...args) {
  233. if (!ctor || typeof ctor !== "function")
  234. throw new InvalidArgumentError(
  235. "The first argument of ServicesContainer.get must be a class constructor, but %v given.",
  236. ctor
  237. );
  238. if (!this._services.has(ctor) && this._parent && this._parent.has(ctor)) {
  239. return this._parent.get(ctor);
  240. }
  241. let service = this._services.get(ctor);
  242. if (!service || args.length) {
  243. service = "prototype" in ctor && ctor.prototype instanceof Service ? new ctor(this, ...args) : new ctor(...args);
  244. this._services.set(ctor, service);
  245. } else if (typeof service === "function") {
  246. service = service();
  247. this._services.set(ctor, service);
  248. }
  249. return service;
  250. }
  251. /**
  252. * Проверка существования конструктора в контейнере.
  253. *
  254. * @param {*} ctor
  255. * @return {boolean}
  256. */
  257. has(ctor) {
  258. if (this._services.has(ctor)) return true;
  259. if (this._parent) return this._parent.has(ctor);
  260. return false;
  261. }
  262. /**
  263. * Добавить конструктор в контейнер.
  264. *
  265. * @param {*} ctor
  266. * @param {*} args
  267. * @return {this}
  268. */
  269. add(ctor, ...args) {
  270. if (!ctor || typeof ctor !== "function")
  271. throw new InvalidArgumentError(
  272. "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
  273. ctor
  274. );
  275. const factory = /* @__PURE__ */ __name(() => ctor.prototype instanceof Service ? new ctor(this, ...args) : new ctor(...args), "factory");
  276. this._services.set(ctor, factory);
  277. return this;
  278. }
  279. /**
  280. * Добавить конструктор и создать экземпляр.
  281. *
  282. * @param {*} ctor
  283. * @param {*} args
  284. * @return {this}
  285. */
  286. use(ctor, ...args) {
  287. if (!ctor || typeof ctor !== "function")
  288. throw new InvalidArgumentError(
  289. "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
  290. ctor
  291. );
  292. const service = ctor.prototype instanceof Service ? new ctor(this, ...args) : new ctor(...args);
  293. this._services.set(ctor, service);
  294. return this;
  295. }
  296. /**
  297. * Добавить конструктор и связанный экземпляр.
  298. *
  299. * @param {*} ctor
  300. * @param {*} service
  301. * @return {this}
  302. */
  303. set(ctor, service) {
  304. if (!ctor || typeof ctor !== "function")
  305. throw new InvalidArgumentError(
  306. "The first argument of ServicesContainer.set must be a class constructor, but %v given.",
  307. ctor
  308. );
  309. if (!service || typeof service !== "object" || Array.isArray(service))
  310. throw new InvalidArgumentError(
  311. "The second argument of ServicesContainer.set must be an Object, but %v given.",
  312. service
  313. );
  314. this._services.set(ctor, service);
  315. return this;
  316. }
  317. };
  318. __name(_ServiceContainer, "ServiceContainer");
  319. ServiceContainer = _ServiceContainer;
  320. }
  321. });
  322. // node_modules/@e22m4u/js-service/src/service.js
  323. var _Service, Service;
  324. var init_service = __esm({
  325. "node_modules/@e22m4u/js-service/src/service.js"() {
  326. init_service_container();
  327. _Service = class _Service {
  328. /**
  329. * Container.
  330. *
  331. * @type {ServiceContainer}
  332. */
  333. container;
  334. /**
  335. * Constructor.
  336. *
  337. * @param {ServiceContainer|undefined} container
  338. */
  339. constructor(container = void 0) {
  340. this.container = container instanceof ServiceContainer ? container : new ServiceContainer();
  341. }
  342. /**
  343. * Получить существующий или новый экземпляр.
  344. *
  345. * @param {*} ctor
  346. * @param {*} args
  347. * @return {*}
  348. */
  349. getService(ctor, ...args) {
  350. return this.container.get(ctor, ...args);
  351. }
  352. /**
  353. * Проверка существования конструктора в контейнере.
  354. *
  355. * @param {*} ctor
  356. * @return {boolean}
  357. */
  358. hasService(ctor) {
  359. return this.container.has(ctor);
  360. }
  361. /**
  362. * Добавить конструктор в контейнер.
  363. *
  364. * @param {*} ctor
  365. * @param {*} args
  366. * @return {this}
  367. */
  368. addService(ctor, ...args) {
  369. this.container.add(ctor, ...args);
  370. return this;
  371. }
  372. /**
  373. * Добавить конструктор и создать экземпляр.
  374. *
  375. * @param {*} ctor
  376. * @param {*} args
  377. * @return {this}
  378. */
  379. useService(ctor, ...args) {
  380. this.container.use(ctor, ...args);
  381. return this;
  382. }
  383. /**
  384. * Добавить конструктор и связанный экземпляр.
  385. *
  386. * @param {*} ctor
  387. * @param {*} service
  388. * @return {this}
  389. */
  390. setService(ctor, service) {
  391. this.container.set(ctor, service);
  392. return this;
  393. }
  394. };
  395. __name(_Service, "Service");
  396. Service = _Service;
  397. }
  398. });
  399. // node_modules/@e22m4u/js-service/src/index.js
  400. var init_src2 = __esm({
  401. "node_modules/@e22m4u/js-service/src/index.js"() {
  402. init_service();
  403. init_service_container();
  404. }
  405. });
  406. // src/errors/not-implemented-error.js
  407. var _NotImplementedError, NotImplementedError;
  408. var init_not_implemented_error = __esm({
  409. "src/errors/not-implemented-error.js"() {
  410. "use strict";
  411. init_src();
  412. _NotImplementedError = class _NotImplementedError extends Errorf {
  413. };
  414. __name(_NotImplementedError, "NotImplementedError");
  415. NotImplementedError = _NotImplementedError;
  416. }
  417. });
  418. // src/errors/invalid-argument-error.js
  419. var _InvalidArgumentError2, InvalidArgumentError2;
  420. var init_invalid_argument_error2 = __esm({
  421. "src/errors/invalid-argument-error.js"() {
  422. "use strict";
  423. init_src();
  424. _InvalidArgumentError2 = class _InvalidArgumentError2 extends Errorf {
  425. };
  426. __name(_InvalidArgumentError2, "InvalidArgumentError");
  427. InvalidArgumentError2 = _InvalidArgumentError2;
  428. }
  429. });
  430. // src/errors/invalid-operator-value-error.js
  431. var _InvalidOperatorValueError, InvalidOperatorValueError;
  432. var init_invalid_operator_value_error = __esm({
  433. "src/errors/invalid-operator-value-error.js"() {
  434. "use strict";
  435. init_src();
  436. _InvalidOperatorValueError = class _InvalidOperatorValueError extends Error {
  437. /**
  438. * Constructor.
  439. *
  440. * @param {string} operator
  441. * @param {string} expected
  442. * @param {*} value
  443. */
  444. constructor(operator, expected, value) {
  445. super(
  446. format(
  447. "Condition of {%s: ...} should have %s, but %v given.",
  448. operator,
  449. expected,
  450. value
  451. )
  452. );
  453. }
  454. };
  455. __name(_InvalidOperatorValueError, "InvalidOperatorValueError");
  456. InvalidOperatorValueError = _InvalidOperatorValueError;
  457. }
  458. });
  459. // src/errors/index.js
  460. var init_errors2 = __esm({
  461. "src/errors/index.js"() {
  462. "use strict";
  463. init_not_implemented_error();
  464. init_invalid_argument_error2();
  465. init_invalid_operator_value_error();
  466. }
  467. });
  468. // src/filter/slice-clause-tool.js
  469. var _SliceClauseTool, SliceClauseTool;
  470. var init_slice_clause_tool = __esm({
  471. "src/filter/slice-clause-tool.js"() {
  472. "use strict";
  473. init_src2();
  474. init_errors2();
  475. _SliceClauseTool = class _SliceClauseTool extends Service {
  476. /**
  477. * Slice.
  478. *
  479. * @param {object[]} entities
  480. * @param {number|undefined} skip
  481. * @param {number|undefined} limit
  482. * @returns {object[]}
  483. */
  484. slice(entities, skip = void 0, limit = void 0) {
  485. if (!Array.isArray(entities))
  486. throw new InvalidArgumentError2(
  487. "The first argument of SliceClauseTool.slice should be an Array, but %v given.",
  488. entities
  489. );
  490. if (skip != null && typeof skip !== "number")
  491. throw new InvalidArgumentError2(
  492. 'The provided option "skip" should be a Number, but %v given.',
  493. skip
  494. );
  495. if (limit != null && typeof limit !== "number")
  496. throw new InvalidArgumentError2(
  497. 'The provided option "limit" should be a Number, but %v given.',
  498. limit
  499. );
  500. skip = skip || 0;
  501. limit = limit || entities.length;
  502. return entities.slice(skip, skip + limit);
  503. }
  504. /**
  505. * Validate skip clause.
  506. *
  507. * @param {number|undefined} skip
  508. */
  509. static validateSkipClause(skip) {
  510. if (skip == null) return;
  511. if (typeof skip !== "number")
  512. throw new InvalidArgumentError2(
  513. 'The provided option "skip" should be a Number, but %v given.',
  514. skip
  515. );
  516. }
  517. /**
  518. * Validate limit clause.
  519. *
  520. * @param {number|undefined} limit
  521. */
  522. static validateLimitClause(limit) {
  523. if (limit == null) return;
  524. if (typeof limit !== "number")
  525. throw new InvalidArgumentError2(
  526. 'The provided option "limit" should be a Number, but %v given.',
  527. limit
  528. );
  529. }
  530. };
  531. __name(_SliceClauseTool, "SliceClauseTool");
  532. SliceClauseTool = _SliceClauseTool;
  533. }
  534. });
  535. // src/utils/is-ctor.js
  536. function isCtor(value) {
  537. if (!value) return false;
  538. return typeof value === "function" && "prototype" in value;
  539. }
  540. var init_is_ctor = __esm({
  541. "src/utils/is-ctor.js"() {
  542. "use strict";
  543. __name(isCtor, "isCtor");
  544. }
  545. });
  546. // src/utils/is-promise.js
  547. function isPromise(value) {
  548. if (!value) return false;
  549. if (typeof value !== "object") return false;
  550. return typeof value.then === "function";
  551. }
  552. var init_is_promise = __esm({
  553. "src/utils/is-promise.js"() {
  554. "use strict";
  555. __name(isPromise, "isPromise");
  556. }
  557. });
  558. // src/utils/capitalize.js
  559. function capitalize(string) {
  560. if (!string || typeof string !== "string") return string;
  561. return string.charAt(0).toUpperCase() + string.slice(1);
  562. }
  563. var init_capitalize = __esm({
  564. "src/utils/capitalize.js"() {
  565. "use strict";
  566. __name(capitalize, "capitalize");
  567. }
  568. });
  569. // src/utils/clone-deep.js
  570. function cloneDeep(value) {
  571. if (!value) return value;
  572. const types = [Number, String, Boolean];
  573. let result;
  574. types.forEach((type) => {
  575. if (value instanceof type) result = type(value);
  576. });
  577. if (result === void 0) {
  578. if (Array.isArray(value)) {
  579. result = [];
  580. value.forEach((child, index) => {
  581. result[index] = cloneDeep(child);
  582. });
  583. } else if (typeof value === "object") {
  584. if ("nodeType" in value && value.nodeType && "cloneNode" in value && typeof value.cloneNode === "function") {
  585. result = value.cloneNode(true);
  586. } else if (!("prototype" in value) || !value.prototype) {
  587. if (value instanceof Date) {
  588. result = new Date(value);
  589. } else if (value.constructor && value.constructor.name === "Object") {
  590. result = {};
  591. for (const key in value) {
  592. result[key] = cloneDeep(value[key]);
  593. }
  594. } else {
  595. result = value;
  596. }
  597. } else {
  598. result = value;
  599. }
  600. } else {
  601. result = value;
  602. }
  603. }
  604. return result;
  605. }
  606. var init_clone_deep = __esm({
  607. "src/utils/clone-deep.js"() {
  608. "use strict";
  609. __name(cloneDeep, "cloneDeep");
  610. }
  611. });
  612. // src/utils/singularize.js
  613. function singularize(noun) {
  614. if (!noun || typeof noun !== "string") return noun;
  615. const endings = {
  616. ves: "fe",
  617. ies: "y",
  618. i: "us",
  619. zes: "ze",
  620. ses: "s",
  621. es: "e",
  622. s: ""
  623. };
  624. return noun.replace(
  625. new RegExp(`(${Object.keys(endings).join("|")})$`),
  626. (r) => endings[r]
  627. );
  628. }
  629. var init_singularize = __esm({
  630. "src/utils/singularize.js"() {
  631. "use strict";
  632. __name(singularize, "singularize");
  633. }
  634. });
  635. // src/utils/is-deep-equal.js
  636. function isDeepEqual(firstValue, secondValue) {
  637. const cached = /* @__PURE__ */ new WeakMap();
  638. const compare = /* @__PURE__ */ __name((a, b) => {
  639. if (a === null || b === null) return a === b;
  640. if (typeof a !== "object" || typeof b !== "object") return a === b;
  641. const dataTypeA = Array.isArray(a) ? "array" : "object";
  642. const dataTypeB = Array.isArray(b) ? "array" : "object";
  643. if (dataTypeA !== dataTypeB) return false;
  644. const keysA = Object.keys(a);
  645. const keysB = Object.keys(b);
  646. if (keysA.length !== keysB.length) return false;
  647. const symbolsA = Object.getOwnPropertySymbols(a);
  648. const symbolsB = Object.getOwnPropertySymbols(b);
  649. if (symbolsA.length !== symbolsB.length) return false;
  650. let setForA = cached.get(a);
  651. if (setForA == null) {
  652. setForA = /* @__PURE__ */ new Set();
  653. cached.set(a, setForA);
  654. } else if (setForA.has(b)) {
  655. return true;
  656. }
  657. setForA.add(b);
  658. let setForB = cached.get(b);
  659. if (setForB == null) {
  660. setForB = /* @__PURE__ */ new Set();
  661. cached.set(b, setForB);
  662. } else if (setForB.has(a)) {
  663. return true;
  664. }
  665. setForB.add(a);
  666. const propertyNamesA = [...keysA, ...symbolsA];
  667. for (const propertyNameA of propertyNamesA) {
  668. if (!Object.prototype.hasOwnProperty.call(b, propertyNameA)) return false;
  669. const propertyValueA = a[propertyNameA];
  670. const propertyValueB = b[propertyNameA];
  671. if (!compare(propertyValueA, propertyValueB)) return false;
  672. }
  673. return true;
  674. }, "compare");
  675. return compare(firstValue, secondValue);
  676. }
  677. var init_is_deep_equal = __esm({
  678. "src/utils/is-deep-equal.js"() {
  679. "use strict";
  680. __name(isDeepEqual, "isDeepEqual");
  681. }
  682. });
  683. // src/utils/get-ctor-name.js
  684. function getCtorName(value) {
  685. if (value === null) return "Null";
  686. if (value === void 0) return "Undefined";
  687. return value.constructor && value.constructor.name || void 0;
  688. }
  689. var init_get_ctor_name = __esm({
  690. "src/utils/get-ctor-name.js"() {
  691. "use strict";
  692. __name(getCtorName, "getCtorName");
  693. }
  694. });
  695. // src/utils/is-pure-object.js
  696. function isPureObject(value) {
  697. return Boolean(
  698. typeof value === "object" && value && !Array.isArray(value) && (!value.constructor || value.constructor && value.constructor.name === "Object")
  699. );
  700. }
  701. var init_is_pure_object = __esm({
  702. "src/utils/is-pure-object.js"() {
  703. "use strict";
  704. __name(isPureObject, "isPureObject");
  705. }
  706. });
  707. // src/utils/string-to-regexp.js
  708. function stringToRegexp(pattern, flags = void 0) {
  709. if (pattern instanceof RegExp) {
  710. return new RegExp(pattern, flags);
  711. }
  712. let regex = "";
  713. for (let i = 0, n = pattern.length; i < n; i++) {
  714. const char = pattern.charAt(i);
  715. if (char === "%") {
  716. regex += ".*";
  717. } else {
  718. regex += char;
  719. }
  720. }
  721. return new RegExp(regex, flags);
  722. }
  723. var init_string_to_regexp = __esm({
  724. "src/utils/string-to-regexp.js"() {
  725. "use strict";
  726. __name(stringToRegexp, "stringToRegexp");
  727. }
  728. });
  729. // src/utils/get-value-by-path.js
  730. function getValueByPath(obj, path, orElse = void 0) {
  731. if (!obj || typeof obj !== "object") return orElse;
  732. if (!path || typeof path !== "string") return orElse;
  733. const keys = path.split(".");
  734. let value = obj;
  735. for (const key of keys) {
  736. if (typeof value === "object" && value !== null && key in value) {
  737. value = value[key];
  738. } else {
  739. value = orElse;
  740. break;
  741. }
  742. }
  743. return value;
  744. }
  745. var init_get_value_by_path = __esm({
  746. "src/utils/get-value-by-path.js"() {
  747. "use strict";
  748. __name(getValueByPath, "getValueByPath");
  749. }
  750. });
  751. // src/utils/transform-promise.js
  752. function transformPromise(valueOrPromise, transformer) {
  753. return isPromise(valueOrPromise) ? valueOrPromise.then(transformer) : transformer(valueOrPromise);
  754. }
  755. var init_transform_promise = __esm({
  756. "src/utils/transform-promise.js"() {
  757. "use strict";
  758. init_is_promise();
  759. __name(transformPromise, "transformPromise");
  760. }
  761. });
  762. // src/utils/select-object-keys.js
  763. function selectObjectKeys(obj, keys) {
  764. if (!obj || typeof obj !== "object" || Array.isArray(obj))
  765. throw new InvalidArgumentError2(
  766. "The first argument of selectObjectKeys should be an Object, but %v given.",
  767. obj
  768. );
  769. if (!Array.isArray(keys))
  770. throw new InvalidArgumentError2(
  771. "The second argument of selectObjectKeys should be an Array of String, but %v given.",
  772. keys
  773. );
  774. keys.forEach((key) => {
  775. if (typeof key !== "string")
  776. throw new InvalidArgumentError2(
  777. "The second argument of selectObjectKeys should be an Array of String, but %v given.",
  778. key
  779. );
  780. });
  781. const result = {};
  782. const allKeys = Object.keys(obj);
  783. allKeys.forEach((key) => {
  784. if (keys.includes(key)) result[key] = obj[key];
  785. });
  786. return result;
  787. }
  788. var init_select_object_keys = __esm({
  789. "src/utils/select-object-keys.js"() {
  790. "use strict";
  791. init_errors2();
  792. __name(selectObjectKeys, "selectObjectKeys");
  793. }
  794. });
  795. // src/utils/exclude-object-keys.js
  796. function excludeObjectKeys(obj, keys) {
  797. if (typeof obj !== "object" || !obj || Array.isArray(obj))
  798. throw new InvalidArgumentError2(
  799. "Cannot exclude keys from a non-Object value, %v given.",
  800. obj
  801. );
  802. const result = { ...obj };
  803. keys = Array.isArray(keys) ? keys : [keys];
  804. keys.forEach((key) => delete result[key]);
  805. return result;
  806. }
  807. var init_exclude_object_keys = __esm({
  808. "src/utils/exclude-object-keys.js"() {
  809. "use strict";
  810. init_errors2();
  811. __name(excludeObjectKeys, "excludeObjectKeys");
  812. }
  813. });
  814. // src/utils/get-decorator-target-type.js
  815. function getDecoratorTargetType(target, propertyKey, descriptorOrIndex) {
  816. const isCtor2 = typeof target === "function";
  817. const isParameter = typeof descriptorOrIndex === "number";
  818. const isProperty = propertyKey != null && descriptorOrIndex == null;
  819. const isMethod = propertyKey != null && descriptorOrIndex != null;
  820. const D = DecoratorTargetType;
  821. if (isCtor2) {
  822. if (isParameter)
  823. return propertyKey ? D.STATIC_METHOD_PARAMETER : D.CONSTRUCTOR_PARAMETER;
  824. if (isProperty) return D.STATIC_PROPERTY;
  825. if (isMethod) return D.STATIC_METHOD;
  826. return D.CONSTRUCTOR;
  827. } else {
  828. if (isParameter) return D.INSTANCE_METHOD_PARAMETER;
  829. if (isProperty) return D.INSTANCE_PROPERTY;
  830. if (isMethod) return D.INSTANCE_METHOD;
  831. return D.INSTANCE;
  832. }
  833. }
  834. var DecoratorTargetType;
  835. var init_get_decorator_target_type = __esm({
  836. "src/utils/get-decorator-target-type.js"() {
  837. "use strict";
  838. DecoratorTargetType = {
  839. CONSTRUCTOR: "constructor",
  840. INSTANCE: "instance",
  841. STATIC_METHOD: "staticMethod",
  842. INSTANCE_METHOD: "instanceMethod",
  843. STATIC_PROPERTY: "staticProperty",
  844. INSTANCE_PROPERTY: "instanceProperty",
  845. CONSTRUCTOR_PARAMETER: "constructorParameter",
  846. STATIC_METHOD_PARAMETER: "staticMethodParameter",
  847. INSTANCE_METHOD_PARAMETER: "instanceMethodParameter"
  848. };
  849. __name(getDecoratorTargetType, "getDecoratorTargetType");
  850. }
  851. });
  852. // src/utils/index.js
  853. var init_utils2 = __esm({
  854. "src/utils/index.js"() {
  855. "use strict";
  856. init_is_ctor();
  857. init_is_promise();
  858. init_capitalize();
  859. init_clone_deep();
  860. init_singularize();
  861. init_is_deep_equal();
  862. init_get_ctor_name();
  863. init_is_pure_object();
  864. init_string_to_regexp();
  865. init_get_value_by_path();
  866. init_transform_promise();
  867. init_select_object_keys();
  868. init_exclude_object_keys();
  869. init_get_decorator_target_type();
  870. }
  871. });
  872. // src/filter/order-clause-tool.js
  873. function compareFn(a, b) {
  874. let undefinedA, undefinedB;
  875. for (let i = 0, l = this.length; i < l; i++) {
  876. const aVal = getValueByPath(a, this[i].key);
  877. const bVal = getValueByPath(b, this[i].key);
  878. undefinedB = bVal === void 0 && aVal !== void 0;
  879. undefinedA = aVal === void 0 && bVal !== void 0;
  880. if (undefinedB || aVal > bVal) {
  881. return this[i].reverse;
  882. } else if (undefinedA || aVal < bVal) {
  883. return -1 * this[i].reverse;
  884. }
  885. }
  886. return 0;
  887. }
  888. var _OrderClauseTool, OrderClauseTool;
  889. var init_order_clause_tool = __esm({
  890. "src/filter/order-clause-tool.js"() {
  891. "use strict";
  892. init_src2();
  893. init_utils2();
  894. init_errors2();
  895. _OrderClauseTool = class _OrderClauseTool extends Service {
  896. /**
  897. * Sort.
  898. *
  899. * @param {object[]} entities
  900. * @param {string|string[]|undefined} clause
  901. */
  902. sort(entities, clause) {
  903. if (clause == null) return;
  904. if (Array.isArray(clause) === false) clause = [clause];
  905. if (!clause.length) return;
  906. const mapping = [];
  907. clause.forEach((key, index) => {
  908. if (!key || typeof key !== "string")
  909. throw new InvalidArgumentError2(
  910. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
  911. key
  912. );
  913. let reverse = 1;
  914. const matches = key.match(/\s+(A|DE)SC$/i);
  915. if (matches) {
  916. key = key.replace(/\s+(A|DE)SC/i, "");
  917. if (matches[1].toLowerCase() === "de") reverse = -1;
  918. }
  919. mapping[index] = { key, reverse };
  920. });
  921. entities.sort(compareFn.bind(mapping));
  922. }
  923. /**
  924. * Validate order clause.
  925. *
  926. * @param {string|string[]|undefined} clause
  927. */
  928. static validateOrderClause(clause) {
  929. if (clause == null) return;
  930. if (Array.isArray(clause) === false) clause = [clause];
  931. if (!clause.length) return;
  932. clause.forEach((field) => {
  933. if (!field || typeof field !== "string")
  934. throw new InvalidArgumentError2(
  935. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
  936. field
  937. );
  938. });
  939. }
  940. /**
  941. * Normalize order clause.
  942. *
  943. * @param {string|string[]|undefined} clause
  944. * @returns {string[]|undefined}
  945. */
  946. static normalizeOrderClause(clause) {
  947. if (clause == null) return;
  948. if (Array.isArray(clause) === false) clause = [clause];
  949. if (!clause.length) return;
  950. clause.forEach((field) => {
  951. if (!field || typeof field !== "string")
  952. throw new InvalidArgumentError2(
  953. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
  954. field
  955. );
  956. });
  957. return clause;
  958. }
  959. };
  960. __name(_OrderClauseTool, "OrderClauseTool");
  961. OrderClauseTool = _OrderClauseTool;
  962. __name(compareFn, "compareFn");
  963. }
  964. });
  965. // src/filter/operator-clause-tool.js
  966. var _OperatorClauseTool, OperatorClauseTool;
  967. var init_operator_clause_tool = __esm({
  968. "src/filter/operator-clause-tool.js"() {
  969. "use strict";
  970. init_src2();
  971. init_utils2();
  972. init_errors2();
  973. init_errors2();
  974. _OperatorClauseTool = class _OperatorClauseTool extends Service {
  975. /**
  976. * Compare.
  977. *
  978. * @param {*} val1 The 1st value
  979. * @param {*} val2 The 2nd value
  980. * @returns {number} 0: =, positive: >, negative <
  981. */
  982. compare(val1, val2) {
  983. if (val1 == null || val2 == null) {
  984. return val1 == val2 ? 0 : NaN;
  985. }
  986. if (typeof val1 === "number") {
  987. if (typeof val2 === "number" || typeof val2 === "string" || typeof val2 === "boolean") {
  988. if (val1 === val2) return 0;
  989. return val1 - Number(val2);
  990. }
  991. return NaN;
  992. }
  993. if (typeof val1 === "string") {
  994. const isDigits = /^\d+$/.test(val1);
  995. if (isDigits) return this.compare(Number(val1), val2);
  996. try {
  997. if (val1 > val2) return 1;
  998. if (val1 < val2) return -1;
  999. if (val1 == val2) return 0;
  1000. } catch (e) {
  1001. }
  1002. return NaN;
  1003. }
  1004. if (typeof val1 === "boolean") {
  1005. return Number(val1) - Number(val2);
  1006. }
  1007. return val1 === val2 ? 0 : NaN;
  1008. }
  1009. /**
  1010. * Test all operators.
  1011. *
  1012. * @param {object} clause
  1013. * @param {*} value
  1014. * @returns {boolean|undefined}
  1015. */
  1016. testAll(clause, value) {
  1017. if (!clause || typeof clause !== "object" || Array.isArray(clause))
  1018. throw new InvalidArgumentError2(
  1019. "The first argument of OperatorUtils.testAll should be an Object, but %v given.",
  1020. clause
  1021. );
  1022. const eqNeqTest = this.testEqNeq(clause, value);
  1023. if (eqNeqTest !== void 0) return eqNeqTest;
  1024. const gtLtTest = this.testGtLt(clause, value);
  1025. if (gtLtTest !== void 0) return gtLtTest;
  1026. const incTest = this.testInq(clause, value);
  1027. if (incTest !== void 0) return incTest;
  1028. const ninTest = this.testNin(clause, value);
  1029. if (ninTest !== void 0) return ninTest;
  1030. const betweenTest = this.testBetween(clause, value);
  1031. if (betweenTest !== void 0) return betweenTest;
  1032. const existsTest = this.testExists(clause, value);
  1033. if (existsTest !== void 0) return existsTest;
  1034. const likeTest = this.testLike(clause, value);
  1035. if (likeTest !== void 0) return likeTest;
  1036. const nlikeTest = this.testNlike(clause, value);
  1037. if (nlikeTest !== void 0) return nlikeTest;
  1038. const ilikeTest = this.testIlike(clause, value);
  1039. if (ilikeTest !== void 0) return ilikeTest;
  1040. const nilikeTest = this.testNilike(clause, value);
  1041. if (nilikeTest !== void 0) return nilikeTest;
  1042. const regExpTest = this.testRegexp(clause, value);
  1043. if (regExpTest !== void 0) return regExpTest;
  1044. }
  1045. /**
  1046. * Test eq/neq operator.
  1047. *
  1048. * @example
  1049. * ```ts
  1050. * {
  1051. * eq: 'foo',
  1052. * }
  1053. * ```
  1054. *
  1055. * @example
  1056. * ```ts
  1057. * {
  1058. * neq: 'foo',
  1059. * }
  1060. * ```
  1061. *
  1062. * @param {object} clause
  1063. * @param {*} value
  1064. * @returns {boolean|undefined}
  1065. */
  1066. testEqNeq(clause, value) {
  1067. if (!clause || typeof clause !== "object")
  1068. throw new InvalidArgumentError2(
  1069. "The first argument of OperatorUtils.testEqNeq should be an Object, but %v given.",
  1070. clause
  1071. );
  1072. if ("eq" in clause) return this.compare(clause.eq, value) === 0;
  1073. if ("neq" in clause) return this.compare(clause.neq, value) !== 0;
  1074. }
  1075. /**
  1076. * Test lt/gt/lte/gte operator.
  1077. *
  1078. * @example
  1079. * ```ts
  1080. * {
  1081. * lt: 10,
  1082. * }
  1083. * ```
  1084. *
  1085. * @example
  1086. * ```ts
  1087. * {
  1088. * lte: 10,
  1089. * }
  1090. * ```
  1091. *
  1092. * @example
  1093. * ```ts
  1094. * {
  1095. * gt: 10,
  1096. * }
  1097. * ```
  1098. *
  1099. * @example
  1100. * ```ts
  1101. * {
  1102. * gte: 10,
  1103. * }
  1104. * ```
  1105. *
  1106. * @param {object} clause
  1107. * @param {*} value
  1108. * @returns {boolean|undefined}
  1109. */
  1110. testGtLt(clause, value) {
  1111. if (!clause || typeof clause !== "object")
  1112. throw new InvalidArgumentError2(
  1113. "The first argument of OperatorUtils.testGtLt should be an Object, but %v given.",
  1114. clause
  1115. );
  1116. if ("gt" in clause) return this.compare(value, clause.gt) > 0;
  1117. if ("gte" in clause) return this.compare(value, clause.gte) >= 0;
  1118. if ("lt" in clause) return this.compare(value, clause.lt) < 0;
  1119. if ("lte" in clause) return this.compare(value, clause.lte) <= 0;
  1120. }
  1121. /**
  1122. * Test inc operator.
  1123. *
  1124. * @example
  1125. * ```ts
  1126. * {
  1127. * inc: ['foo', 'bar'],
  1128. * }
  1129. * ```
  1130. *
  1131. * @param {object} clause
  1132. * @param {*} value
  1133. * @returns {boolean|undefined}
  1134. */
  1135. testInq(clause, value) {
  1136. if (!clause || typeof clause !== "object")
  1137. throw new InvalidArgumentError2(
  1138. "The first argument of OperatorUtils.testInq should be an Object, but %v given.",
  1139. clause
  1140. );
  1141. if ("inq" in clause && clause.inq !== void 0) {
  1142. if (!clause.inq || !Array.isArray(clause.inq)) {
  1143. throw new InvalidOperatorValueError(
  1144. "inq",
  1145. "an Array of possible values",
  1146. clause.inq
  1147. );
  1148. }
  1149. for (let i = 0; i < clause.inq.length; i++) {
  1150. if (clause.inq[i] == value) return true;
  1151. }
  1152. return false;
  1153. }
  1154. }
  1155. /**
  1156. * Test nin operator.
  1157. *
  1158. * @example
  1159. * ```ts
  1160. * {
  1161. * nin: ['foo', 'bar'],
  1162. * }
  1163. * ```
  1164. *
  1165. * @param {object} clause
  1166. * @param {*} value
  1167. * @returns {boolean|undefined}
  1168. */
  1169. testNin(clause, value) {
  1170. if (!clause || typeof clause !== "object")
  1171. throw new InvalidArgumentError2(
  1172. "The first argument of OperatorUtils.testNin should be an Object, but %v given.",
  1173. clause
  1174. );
  1175. if ("nin" in clause && clause.nin !== void 0) {
  1176. if (!clause.nin || !Array.isArray(clause.nin)) {
  1177. throw new InvalidOperatorValueError(
  1178. "nin",
  1179. "an Array of possible values",
  1180. clause.nin
  1181. );
  1182. }
  1183. for (let i = 0; i < clause.nin.length; i++) {
  1184. if (clause.nin[i] == value) return false;
  1185. }
  1186. return true;
  1187. }
  1188. }
  1189. /**
  1190. * Test between operator.
  1191. *
  1192. * @example
  1193. * ```ts
  1194. * {
  1195. * between: [10, 20],
  1196. * }
  1197. * ```
  1198. *
  1199. * @param {object} clause
  1200. * @param {*} value
  1201. * @returns {boolean|undefined}
  1202. */
  1203. testBetween(clause, value) {
  1204. if (!clause || typeof clause !== "object")
  1205. throw new InvalidArgumentError2(
  1206. "The first argument of OperatorUtils.testBetween should be an Object, but %v given.",
  1207. clause
  1208. );
  1209. if ("between" in clause && clause.between !== void 0) {
  1210. if (!Array.isArray(clause.between) || clause.between.length !== 2) {
  1211. throw new InvalidOperatorValueError(
  1212. "between",
  1213. "an Array of 2 elements",
  1214. clause.between
  1215. );
  1216. }
  1217. return this.testGtLt({ gte: clause.between[0] }, value) && this.testGtLt({ lte: clause.between[1] }, value);
  1218. }
  1219. }
  1220. /**
  1221. * Test exists operator.
  1222. *
  1223. * @example
  1224. * ```ts
  1225. * {
  1226. * exists: true,
  1227. * }
  1228. * ```
  1229. *
  1230. * @param {object} clause
  1231. * @param {*} value
  1232. * @returns {boolean|undefined}
  1233. */
  1234. testExists(clause, value) {
  1235. if (!clause || typeof clause !== "object")
  1236. throw new InvalidArgumentError2(
  1237. "The first argument of OperatorUtils.testExists should be an Object, but %v given.",
  1238. clause
  1239. );
  1240. if ("exists" in clause && clause.exists !== void 0) {
  1241. if (typeof clause.exists !== "boolean") {
  1242. throw new InvalidOperatorValueError(
  1243. "exists",
  1244. "a Boolean",
  1245. clause.exists
  1246. );
  1247. }
  1248. return clause.exists ? value !== void 0 : value === void 0;
  1249. }
  1250. }
  1251. /**
  1252. * Test like operator.
  1253. *
  1254. * @example
  1255. * ```ts
  1256. * {
  1257. * like: 'foo',
  1258. * }
  1259. * ```
  1260. *
  1261. * @param {object} clause
  1262. * @param {*} value
  1263. * @returns {boolean|undefined}
  1264. */
  1265. testLike(clause, value) {
  1266. if (!clause || typeof clause !== "object")
  1267. throw new InvalidArgumentError2(
  1268. "The first argument of OperatorUtils.testLike should be an Object, but %v given.",
  1269. clause
  1270. );
  1271. if ("like" in clause && clause.like !== void 0) {
  1272. if (typeof clause.like !== "string" && !(clause.like instanceof RegExp))
  1273. throw new InvalidOperatorValueError("like", "a String", clause.like);
  1274. return stringToRegexp(clause.like).test(value);
  1275. }
  1276. }
  1277. /**
  1278. * Test nlike operator.
  1279. *
  1280. * @example
  1281. * ```ts
  1282. * {
  1283. * nlike: 'foo',
  1284. * }
  1285. * ```
  1286. *
  1287. * @param {object} clause
  1288. * @param {*} value
  1289. * @returns {boolean|undefined}
  1290. */
  1291. testNlike(clause, value) {
  1292. if (!clause || typeof clause !== "object")
  1293. throw new InvalidArgumentError2(
  1294. "The first argument of OperatorUtils.testNlike should be an Object, but %v given.",
  1295. clause
  1296. );
  1297. if ("nlike" in clause && clause.nlike !== void 0) {
  1298. if (typeof clause.nlike !== "string" && !(clause.nlike instanceof RegExp)) {
  1299. throw new InvalidOperatorValueError("nlike", "a String", clause.nlike);
  1300. }
  1301. return !stringToRegexp(clause.nlike).test(value);
  1302. }
  1303. }
  1304. /**
  1305. * Test ilike operator.
  1306. *
  1307. * @example
  1308. * ```ts
  1309. * {
  1310. * ilike: 'foo',
  1311. * }
  1312. * ```
  1313. *
  1314. * @param {object} clause
  1315. * @param {*} value
  1316. * @returns {boolean|undefined}
  1317. */
  1318. testIlike(clause, value) {
  1319. if (!clause || typeof clause !== "object")
  1320. throw new InvalidArgumentError2(
  1321. "The first argument of OperatorUtils.testIlike should be an Object, but %v given.",
  1322. clause
  1323. );
  1324. if ("ilike" in clause && clause.ilike !== void 0) {
  1325. if (typeof clause.ilike !== "string" && !(clause.ilike instanceof RegExp)) {
  1326. throw new InvalidOperatorValueError("ilike", "a String", clause.ilike);
  1327. }
  1328. return stringToRegexp(clause.ilike, "i").test(value);
  1329. }
  1330. }
  1331. /**
  1332. * Test nilike operator.
  1333. *
  1334. * @example
  1335. * ```ts
  1336. * {
  1337. * nilike: 'foo',
  1338. * }
  1339. * ```
  1340. *
  1341. * @param {object} clause
  1342. * @param {*} value
  1343. * @returns {boolean|undefined}
  1344. */
  1345. testNilike(clause, value) {
  1346. if (!clause || typeof clause !== "object")
  1347. throw new InvalidArgumentError2(
  1348. "The first argument of OperatorUtils.testNilike should be an Object, but %v given.",
  1349. clause
  1350. );
  1351. if ("nilike" in clause && clause.nilike !== void 0) {
  1352. if (typeof clause.nilike !== "string" && !(clause.nilike instanceof RegExp)) {
  1353. throw new InvalidOperatorValueError(
  1354. "nilike",
  1355. "a String",
  1356. clause.nilike
  1357. );
  1358. }
  1359. return !stringToRegexp(clause.nilike, "i").test(value);
  1360. }
  1361. }
  1362. /**
  1363. * Test regexp.
  1364. *
  1365. * @example
  1366. * ```ts
  1367. * {
  1368. * regexp: 'foo.*',
  1369. * }
  1370. * ```
  1371. *
  1372. * @example
  1373. * ```ts
  1374. * {
  1375. * regexp: 'foo.*',
  1376. * flags: 'i',
  1377. * }
  1378. * ```
  1379. *
  1380. * @param {object} clause
  1381. * @param {*} value
  1382. * @returns {boolean|undefined}
  1383. */
  1384. testRegexp(clause, value) {
  1385. if (!clause || typeof clause !== "object")
  1386. throw new InvalidArgumentError2(
  1387. "The first argument of OperatorUtils.testRegexp should be an Object, but %v given.",
  1388. clause
  1389. );
  1390. if ("regexp" in clause && clause.regexp !== void 0) {
  1391. if (typeof clause.regexp !== "string" && !(clause.regexp instanceof RegExp)) {
  1392. throw new InvalidOperatorValueError(
  1393. "regexp",
  1394. "a String",
  1395. clause.regexp
  1396. );
  1397. }
  1398. const flags = clause.flags || void 0;
  1399. if (flags && typeof flags !== "string")
  1400. throw new InvalidArgumentError2(
  1401. "RegExp flags should be a String, but %v given.",
  1402. clause.flags
  1403. );
  1404. if (!value || typeof value !== "string") return false;
  1405. const regExp = stringToRegexp(clause.regexp, flags);
  1406. return !!value.match(regExp);
  1407. }
  1408. }
  1409. };
  1410. __name(_OperatorClauseTool, "OperatorClauseTool");
  1411. OperatorClauseTool = _OperatorClauseTool;
  1412. }
  1413. });
  1414. // src/filter/where-clause-tool.js
  1415. var _WhereClauseTool, WhereClauseTool;
  1416. var init_where_clause_tool = __esm({
  1417. "src/filter/where-clause-tool.js"() {
  1418. "use strict";
  1419. init_src2();
  1420. init_utils2();
  1421. init_errors2();
  1422. init_operator_clause_tool();
  1423. _WhereClauseTool = class _WhereClauseTool extends Service {
  1424. /**
  1425. * Filter by where clause.
  1426. *
  1427. * @example
  1428. * ```
  1429. * const entities = [
  1430. * {foo: 1, bar: 'a'},
  1431. * {foo: 2, bar: 'b'},
  1432. * {foo: 3, bar: 'b'},
  1433. * {foo: 4, bar: 'b'},
  1434. * ];
  1435. *
  1436. * const result = filterByWhereClause(entities, {
  1437. * foo: {gt: 2},
  1438. * bar: 'b',
  1439. * });
  1440. *
  1441. * console.log(result);
  1442. * // [
  1443. * // {foo: 3, bar: 'b'},
  1444. * // {foo: 4, bar: 'b'},
  1445. * // ];
  1446. *
  1447. * ```
  1448. *
  1449. * @param {object[]} entities
  1450. * @param {WhereClause|undefined} where
  1451. * @returns {object[]}
  1452. */
  1453. filter(entities, where = void 0) {
  1454. if (!Array.isArray(entities))
  1455. throw new InvalidArgumentError2(
  1456. "The first argument of WhereClauseTool.filter should be an Array of Object, but %v given.",
  1457. entities
  1458. );
  1459. if (where == null) return entities;
  1460. return entities.filter(this._createFilter(where));
  1461. }
  1462. /**
  1463. * Create where filter.
  1464. *
  1465. * @param {WhereClause} whereClause
  1466. * @returns {Function}
  1467. */
  1468. _createFilter(whereClause) {
  1469. if (typeof whereClause !== "object" || Array.isArray(whereClause))
  1470. throw new InvalidArgumentError2(
  1471. 'The provided option "where" should be an Object, but %v given.',
  1472. whereClause
  1473. );
  1474. const keys = Object.keys(whereClause);
  1475. return (data) => {
  1476. if (typeof data !== "object")
  1477. throw new InvalidArgumentError2(
  1478. "The first argument of WhereClauseTool.filter should be an Array of Object, but %v given.",
  1479. data
  1480. );
  1481. return keys.every((key) => {
  1482. if (key === "and" && key in whereClause) {
  1483. const andClause = whereClause[key];
  1484. if (Array.isArray(andClause))
  1485. return andClause.every((clause) => this._createFilter(clause)(data));
  1486. } else if (key === "or" && key in whereClause) {
  1487. const orClause = whereClause[key];
  1488. if (Array.isArray(orClause))
  1489. return orClause.some((clause) => this._createFilter(clause)(data));
  1490. }
  1491. const value = getValueByPath(data, key);
  1492. const matcher = whereClause[key];
  1493. if (Array.isArray(value)) {
  1494. if (typeof matcher === "object" && matcher !== null && "neq" in matcher && matcher.neq !== void 0) {
  1495. if (value.length === 0) return true;
  1496. return value.every((el, index) => {
  1497. const where = {};
  1498. where[index] = matcher;
  1499. return this._createFilter(where)({ ...value });
  1500. });
  1501. }
  1502. return value.some((el, index) => {
  1503. const where = {};
  1504. where[index] = matcher;
  1505. return this._createFilter(where)({ ...value });
  1506. });
  1507. }
  1508. if (this._test(matcher, value)) return true;
  1509. });
  1510. };
  1511. }
  1512. /**
  1513. * Value testing.
  1514. *
  1515. * @param {*} example
  1516. * @param {*} value
  1517. * @returns {boolean}
  1518. */
  1519. _test(example, value) {
  1520. if (example === null) {
  1521. return value === null;
  1522. }
  1523. if (example === void 0) {
  1524. return value === void 0;
  1525. }
  1526. if (example instanceof RegExp) {
  1527. if (typeof value === "string") return !!value.match(example);
  1528. return false;
  1529. }
  1530. if (typeof example === "object") {
  1531. const operatorsTest = this.getService(OperatorClauseTool).testAll(
  1532. example,
  1533. value
  1534. );
  1535. if (operatorsTest !== void 0) return operatorsTest;
  1536. }
  1537. return example == value;
  1538. }
  1539. /**
  1540. * Validate where clause.
  1541. *
  1542. * @param {WhereClause|undefined} clause
  1543. */
  1544. static validateWhereClause(clause) {
  1545. if (clause == null || typeof clause === "function") return;
  1546. if (typeof clause !== "object" || Array.isArray(clause))
  1547. throw new InvalidArgumentError2(
  1548. 'The provided option "where" should be an Object, but %v given.',
  1549. clause
  1550. );
  1551. }
  1552. };
  1553. __name(_WhereClauseTool, "WhereClauseTool");
  1554. WhereClauseTool = _WhereClauseTool;
  1555. }
  1556. });
  1557. // src/definition/model/relations/relation-type.js
  1558. var RelationType;
  1559. var init_relation_type = __esm({
  1560. "src/definition/model/relations/relation-type.js"() {
  1561. "use strict";
  1562. RelationType = {
  1563. BELONGS_TO: "belongsTo",
  1564. HAS_ONE: "hasOne",
  1565. HAS_MANY: "hasMany",
  1566. REFERENCES_MANY: "referencesMany"
  1567. };
  1568. }
  1569. });
  1570. // src/definition/model/relations/relation-definition.js
  1571. var init_relation_definition = __esm({
  1572. "src/definition/model/relations/relation-definition.js"() {
  1573. "use strict";
  1574. }
  1575. });
  1576. // src/definition/model/relations/relations-definition-validator.js
  1577. var _RelationsDefinitionValidator, RelationsDefinitionValidator;
  1578. var init_relations_definition_validator = __esm({
  1579. "src/definition/model/relations/relations-definition-validator.js"() {
  1580. "use strict";
  1581. init_src2();
  1582. init_relation_type();
  1583. init_relation_type();
  1584. init_errors2();
  1585. _RelationsDefinitionValidator = class _RelationsDefinitionValidator extends Service {
  1586. /**
  1587. * Validate.
  1588. *
  1589. * @param {string} modelName
  1590. * @param {object} relDefs
  1591. */
  1592. validate(modelName, relDefs) {
  1593. if (!modelName || typeof modelName !== "string")
  1594. throw new InvalidArgumentError2(
  1595. "The first argument of RelationsDefinitionValidator.validate should be a non-empty String, but %v given.",
  1596. modelName
  1597. );
  1598. if (!relDefs || typeof relDefs !== "object" || Array.isArray(relDefs))
  1599. throw new InvalidArgumentError2(
  1600. 'The provided option "relations" of the model %v should be an Object, but %v given.',
  1601. modelName,
  1602. relDefs
  1603. );
  1604. const relNames = Object.keys(relDefs);
  1605. relNames.forEach((relName) => {
  1606. const relDef = relDefs[relName];
  1607. this._validateRelation(modelName, relName, relDef);
  1608. });
  1609. }
  1610. /**
  1611. * Validate relation.
  1612. *
  1613. * @param {string} modelName
  1614. * @param {string} relName
  1615. * @param {object} relDef
  1616. */
  1617. _validateRelation(modelName, relName, relDef) {
  1618. if (!modelName || typeof modelName !== "string")
  1619. throw new InvalidArgumentError2(
  1620. "The first argument of RelationsDefinitionValidator._validateRelation should be a non-empty String, but %v given.",
  1621. modelName
  1622. );
  1623. if (!relName || typeof relName !== "string")
  1624. throw new InvalidArgumentError2(
  1625. "The relation name of the model %v should be a non-empty String, but %v given.",
  1626. modelName,
  1627. relName
  1628. );
  1629. if (!relDef || typeof relDef !== "object" || Array.isArray(relDef))
  1630. throw new InvalidArgumentError2(
  1631. "The relation %v of the model %v should be an Object, but %v given.",
  1632. relName,
  1633. modelName,
  1634. relDef
  1635. );
  1636. if (!relDef.type || !Object.values(RelationType).includes(relDef.type))
  1637. throw new InvalidArgumentError2(
  1638. 'The relation %v of the model %v requires the option "type" to have one of relation types: %l, but %v given.',
  1639. relName,
  1640. modelName,
  1641. Object.values(RelationType),
  1642. relDef.type
  1643. );
  1644. this._validateBelongsTo(modelName, relName, relDef);
  1645. this._validateHasOne(modelName, relName, relDef);
  1646. this._validateHasMany(modelName, relName, relDef);
  1647. this._validateReferencesMany(modelName, relName, relDef);
  1648. }
  1649. /**
  1650. * Validate "belongsTo".
  1651. *
  1652. * @example The regular "belongsTo" relation.
  1653. * ```
  1654. * {
  1655. * type: RelationType.BELONGS_TO,
  1656. * model: 'model',
  1657. * foreignKey: 'modelId', // optional
  1658. * }
  1659. * ```
  1660. *
  1661. * @example The polymorphic "belongsTo" relation.
  1662. * ```
  1663. * {
  1664. * type: RelationType.BELONGS_TO,
  1665. * polymorphic: true,
  1666. * foreignKey: 'referenceId', // optional
  1667. * discriminator: 'referenceType', // optional
  1668. * }
  1669. * ```
  1670. *
  1671. * @param {string} modelName
  1672. * @param {string} relName
  1673. * @param {object} relDef
  1674. * @private
  1675. */
  1676. _validateBelongsTo(modelName, relName, relDef) {
  1677. if (relDef.type !== RelationType.BELONGS_TO) return;
  1678. if (relDef.polymorphic) {
  1679. if (typeof relDef.polymorphic !== "boolean")
  1680. throw new InvalidArgumentError2(
  1681. 'The relation %v of the model %v has the type "belongsTo", so it expects the option "polymorphic" to be a Boolean, but %v given.',
  1682. relName,
  1683. modelName,
  1684. relDef.polymorphic
  1685. );
  1686. if (relDef.foreignKey && typeof relDef.foreignKey !== "string")
  1687. throw new InvalidArgumentError2(
  1688. 'The relation %v of the model %v is a polymorphic "belongsTo" relation, so it expects the provided option "foreignKey" to be a String, but %v given.',
  1689. relName,
  1690. modelName,
  1691. relDef.foreignKey
  1692. );
  1693. if (relDef.discriminator && typeof relDef.discriminator !== "string")
  1694. throw new InvalidArgumentError2(
  1695. 'The relation %v of the model %v is a polymorphic "belongsTo" relation, so it expects the provided option "discriminator" to be a String, but %v given.',
  1696. relName,
  1697. modelName,
  1698. relDef.discriminator
  1699. );
  1700. } else {
  1701. if (!relDef.model || typeof relDef.model !== "string")
  1702. throw new InvalidArgumentError2(
  1703. 'The relation %v of the model %v has the type "belongsTo", so it requires the option "model" to be a non-empty String, but %v given.',
  1704. relName,
  1705. modelName,
  1706. relDef.model
  1707. );
  1708. if (relDef.foreignKey && typeof relDef.foreignKey !== "string")
  1709. throw new InvalidArgumentError2(
  1710. 'The relation %v of the model %v has the type "belongsTo", so it expects the provided option "foreignKey" to be a String, but %v given.',
  1711. relName,
  1712. modelName,
  1713. relDef.foreignKey
  1714. );
  1715. if (relDef.discriminator)
  1716. throw new InvalidArgumentError2(
  1717. 'The relation %v of the model %v is a non-polymorphic "belongsTo" relation, so it should not have the option "discriminator" to be provided.',
  1718. relName,
  1719. modelName
  1720. );
  1721. }
  1722. }
  1723. /**
  1724. * Validate "hasOne".
  1725. *
  1726. * @example The regular "hasOne" relation.
  1727. * ```
  1728. * {
  1729. * type: RelationType.HAS_ONE,
  1730. * model: 'model',
  1731. * foreignKey: 'modelId',
  1732. * }
  1733. * ```
  1734. *
  1735. * @example The polymorphic "hasOne" relation with a target relation name.
  1736. * ```
  1737. * {
  1738. * type: RelationType.HAS_ONE,
  1739. * model: 'model',
  1740. * polymorphic: 'reference',
  1741. * }
  1742. * ```
  1743. *
  1744. * @example The polymorphic "hasOne" relation with target relation keys.
  1745. * ```
  1746. * {
  1747. * type: RelationType.HAS_ONE,
  1748. * model: 'model',
  1749. * polymorphic: true,
  1750. * foreignKey: 'referenceId',
  1751. * discriminator: 'referenceType',
  1752. * }
  1753. * ```
  1754. *
  1755. * @param {string} modelName
  1756. * @param {string} relName
  1757. * @param {object} relDef
  1758. * @private
  1759. */
  1760. _validateHasOne(modelName, relName, relDef) {
  1761. if (relDef.type !== RelationType.HAS_ONE) return;
  1762. if (!relDef.model || typeof relDef.model !== "string")
  1763. throw new InvalidArgumentError2(
  1764. 'The relation %v of the model %v has the type "hasOne", so it requires the option "model" to be a non-empty String, but %v given.',
  1765. relName,
  1766. modelName,
  1767. relDef.model
  1768. );
  1769. if (relDef.polymorphic) {
  1770. if (typeof relDef.polymorphic === "string") {
  1771. if (relDef.foreignKey)
  1772. throw new InvalidArgumentError2(
  1773. 'The relation %v of the model %v has the option "polymorphic" with a String value, so it should not have the option "foreignKey" to be provided.',
  1774. relName,
  1775. modelName
  1776. );
  1777. if (relDef.discriminator)
  1778. throw new InvalidArgumentError2(
  1779. 'The relation %v of the model %v has the option "polymorphic" with a String value, so it should not have the option "discriminator" to be provided.',
  1780. relName,
  1781. modelName
  1782. );
  1783. } else if (typeof relDef.polymorphic === "boolean") {
  1784. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1785. throw new InvalidArgumentError2(
  1786. 'The relation %v of the model %v has the option "polymorphic" with "true" value, so it requires the option "foreignKey" to be a non-empty String, but %v given.',
  1787. relName,
  1788. modelName,
  1789. relDef.foreignKey
  1790. );
  1791. if (!relDef.discriminator || typeof relDef.discriminator !== "string")
  1792. throw new InvalidArgumentError2(
  1793. 'The relation %v of the model %v has the option "polymorphic" with "true" value, so it requires the option "discriminator" to be a non-empty String, but %v given.',
  1794. relName,
  1795. modelName,
  1796. relDef.discriminator
  1797. );
  1798. } else {
  1799. throw new InvalidArgumentError2(
  1800. 'The relation %v of the model %v has the type "hasOne", so it expects the provided option "polymorphic" to be a String or a Boolean, but %v given.',
  1801. relName,
  1802. modelName,
  1803. relDef.polymorphic
  1804. );
  1805. }
  1806. } else {
  1807. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1808. throw new InvalidArgumentError2(
  1809. 'The relation %v of the model %v has the type "hasOne", so it requires the option "foreignKey" to be a non-empty String, but %v given.',
  1810. relName,
  1811. modelName,
  1812. relDef.foreignKey
  1813. );
  1814. if (relDef.discriminator)
  1815. throw new InvalidArgumentError2(
  1816. 'The relation %v of the model %v is a non-polymorphic "hasOne" relation, so it should not have the option "discriminator" to be provided.',
  1817. relName,
  1818. modelName
  1819. );
  1820. }
  1821. }
  1822. /**
  1823. * Validate "hasMany".
  1824. *
  1825. * @example The regular "hasMany" relation.
  1826. * ```
  1827. * {
  1828. * type: RelationType.HAS_MANY,
  1829. * model: 'model',
  1830. * foreignKey: 'modelId',
  1831. * }
  1832. * ```
  1833. *
  1834. * @example The polymorphic "hasMany" relation with a target relation name.
  1835. * ```
  1836. * {
  1837. * type: RelationType.HAS_MANY,
  1838. * model: 'model',
  1839. * polymorphic: 'reference',
  1840. * }
  1841. * ```
  1842. *
  1843. * @example The polymorphic "hasMany" relation with target relation keys.
  1844. * ```
  1845. * {
  1846. * type: RelationType.HAS_MANY,
  1847. * model: 'model',
  1848. * polymorphic: true,
  1849. * foreignKey: 'referenceId',
  1850. * discriminator: 'referenceType',
  1851. * }
  1852. * ```
  1853. *
  1854. * @param {string} modelName
  1855. * @param {string} relName
  1856. * @param {object} relDef
  1857. * @private
  1858. */
  1859. _validateHasMany(modelName, relName, relDef) {
  1860. if (relDef.type !== RelationType.HAS_MANY) return;
  1861. if (!relDef.model || typeof relDef.model !== "string")
  1862. throw new InvalidArgumentError2(
  1863. 'The relation %v of the model %v has the type "hasMany", so it requires the option "model" to be a non-empty String, but %v given.',
  1864. relName,
  1865. modelName,
  1866. relDef.model
  1867. );
  1868. if (relDef.polymorphic) {
  1869. if (typeof relDef.polymorphic === "string") {
  1870. if (relDef.foreignKey)
  1871. throw new InvalidArgumentError2(
  1872. 'The relation %v of the model %v has the option "polymorphic" with a String value, so it should not have the option "foreignKey" to be provided.',
  1873. relName,
  1874. modelName
  1875. );
  1876. if (relDef.discriminator)
  1877. throw new InvalidArgumentError2(
  1878. 'The relation %v of the model %v has the option "polymorphic" with a String value, so it should not have the option "discriminator" to be provided.',
  1879. relName,
  1880. modelName
  1881. );
  1882. } else if (typeof relDef.polymorphic === "boolean") {
  1883. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1884. throw new InvalidArgumentError2(
  1885. 'The relation %v of the model %v has the option "polymorphic" with "true" value, so it requires the option "foreignKey" to be a non-empty String, but %v given.',
  1886. relName,
  1887. modelName,
  1888. relDef.foreignKey
  1889. );
  1890. if (!relDef.discriminator || typeof relDef.discriminator !== "string")
  1891. throw new InvalidArgumentError2(
  1892. 'The relation %v of the model %v has the option "polymorphic" with "true" value, so it requires the option "discriminator" to be a non-empty String, but %v given.',
  1893. relName,
  1894. modelName,
  1895. relDef.discriminator
  1896. );
  1897. } else {
  1898. throw new InvalidArgumentError2(
  1899. 'The relation %v of the model %v has the type "hasMany", so it expects the provided option "polymorphic" to be a String or a Boolean, but %v given.',
  1900. relName,
  1901. modelName,
  1902. relDef.polymorphic
  1903. );
  1904. }
  1905. } else {
  1906. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1907. throw new InvalidArgumentError2(
  1908. 'The relation %v of the model %v has the type "hasMany", so it requires the option "foreignKey" to be a non-empty String, but %v given.',
  1909. relName,
  1910. modelName,
  1911. relDef.foreignKey
  1912. );
  1913. if (relDef.discriminator)
  1914. throw new InvalidArgumentError2(
  1915. 'The relation %v of the model %v is a non-polymorphic "hasMany" relation, so it should not have the option "discriminator" to be provided.',
  1916. relName,
  1917. modelName
  1918. );
  1919. }
  1920. }
  1921. /**
  1922. * Validate "referencesMany".
  1923. *
  1924. * @example
  1925. * ```
  1926. * {
  1927. * type: RelationType.REFERENCES_MANY,
  1928. * model: 'model',
  1929. * foreignKey: 'modelIds', // optional
  1930. * }
  1931. * ```
  1932. *
  1933. * @param {string} modelName
  1934. * @param {string} relName
  1935. * @param {object} relDef
  1936. * @private
  1937. */
  1938. _validateReferencesMany(modelName, relName, relDef) {
  1939. if (relDef.type !== RelationType.REFERENCES_MANY) return;
  1940. if (!relDef.model || typeof relDef.model !== "string")
  1941. throw new InvalidArgumentError2(
  1942. 'The relation %v of the model %v has the type "referencesMany", so it requires the option "model" to be a non-empty String, but %v given.',
  1943. relName,
  1944. modelName,
  1945. relDef.model
  1946. );
  1947. if (relDef.foreignKey && typeof relDef.foreignKey !== "string")
  1948. throw new InvalidArgumentError2(
  1949. 'The relation %v of the model %v has the type "referencesMany", so it expects the provided option "foreignKey" to be a String, but %v given.',
  1950. relName,
  1951. modelName,
  1952. relDef.foreignKey
  1953. );
  1954. if (relDef.discriminator)
  1955. throw new InvalidArgumentError2(
  1956. 'The relation %v of the model %v has the type "referencesMany", so it should not have the option "discriminator" to be provided.',
  1957. relName,
  1958. modelName
  1959. );
  1960. }
  1961. };
  1962. __name(_RelationsDefinitionValidator, "RelationsDefinitionValidator");
  1963. RelationsDefinitionValidator = _RelationsDefinitionValidator;
  1964. }
  1965. });
  1966. // src/definition/model/relations/index.js
  1967. var init_relations = __esm({
  1968. "src/definition/model/relations/index.js"() {
  1969. "use strict";
  1970. init_relation_type();
  1971. init_relation_definition();
  1972. init_relations_definition_validator();
  1973. }
  1974. });
  1975. // src/definition/model/properties/data-type.js
  1976. var DataType;
  1977. var init_data_type = __esm({
  1978. "src/definition/model/properties/data-type.js"() {
  1979. "use strict";
  1980. DataType = {
  1981. ANY: "any",
  1982. STRING: "string",
  1983. NUMBER: "number",
  1984. BOOLEAN: "boolean",
  1985. ARRAY: "array",
  1986. OBJECT: "object"
  1987. };
  1988. }
  1989. });
  1990. // src/definition/model/properties/property-definition.js
  1991. var init_property_definition = __esm({
  1992. "src/definition/model/properties/property-definition.js"() {
  1993. "use strict";
  1994. }
  1995. });
  1996. // src/definition/model/properties/property-uniqueness.js
  1997. var PropertyUniqueness;
  1998. var init_property_uniqueness = __esm({
  1999. "src/definition/model/properties/property-uniqueness.js"() {
  2000. "use strict";
  2001. PropertyUniqueness = {
  2002. STRICT: "strict",
  2003. SPARSE: "sparse",
  2004. NON_UNIQUE: "nonUnique"
  2005. };
  2006. }
  2007. });
  2008. // src/definition/model/properties/empty-values-definer.js
  2009. var _EmptyValuesDefiner, EmptyValuesDefiner;
  2010. var init_empty_values_definer = __esm({
  2011. "src/definition/model/properties/empty-values-definer.js"() {
  2012. "use strict";
  2013. init_data_type();
  2014. init_src2();
  2015. init_utils2();
  2016. init_errors2();
  2017. _EmptyValuesDefiner = class _EmptyValuesDefiner extends Service {
  2018. /**
  2019. * Empty values map.
  2020. *
  2021. * @type {Map<string, *[]>}
  2022. */
  2023. _emptyValuesMap = /* @__PURE__ */ new Map([
  2024. [DataType.ANY, [void 0, null]],
  2025. [DataType.STRING, [void 0, null, ""]],
  2026. [DataType.NUMBER, [void 0, null, 0]],
  2027. [DataType.BOOLEAN, [void 0, null]],
  2028. [DataType.ARRAY, [void 0, null, []]],
  2029. [DataType.OBJECT, [void 0, null, {}]]
  2030. ]);
  2031. /**
  2032. * Set empty values of data type.
  2033. *
  2034. * @param {string} dataType
  2035. * @param {*[]} emptyValues
  2036. * @returns {EmptyValuesDefiner}
  2037. */
  2038. setEmptyValuesOf(dataType, emptyValues) {
  2039. if (!Object.values(DataType).includes(dataType))
  2040. throw new InvalidArgumentError2(
  2041. 'The argument "dataType" of the EmptyValuesDefiner.setEmptyValuesOf must be one of data types: %l, but %v given.',
  2042. Object.values(DataType),
  2043. dataType
  2044. );
  2045. if (!Array.isArray(emptyValues))
  2046. throw new InvalidArgumentError2(
  2047. 'The argument "emptyValues" of the EmptyValuesDefiner.setEmptyValuesOf must be an Array, but %v given.',
  2048. emptyValues
  2049. );
  2050. this._emptyValuesMap.set(dataType, emptyValues);
  2051. return this;
  2052. }
  2053. /**
  2054. * Is empty.
  2055. *
  2056. * @param {string} dataType
  2057. * @param {*} value
  2058. * @returns {boolean}
  2059. */
  2060. isEmpty(dataType, value) {
  2061. if (!Object.values(DataType).includes(dataType))
  2062. throw new InvalidArgumentError2(
  2063. 'The argument "dataType" of the EmptyValuesDefiner.isEmpty must be one of data types: %l, but %v given.',
  2064. Object.values(DataType),
  2065. dataType
  2066. );
  2067. return this._emptyValuesMap.get(dataType).some((v) => isDeepEqual(v, value));
  2068. }
  2069. };
  2070. __name(_EmptyValuesDefiner, "EmptyValuesDefiner");
  2071. EmptyValuesDefiner = _EmptyValuesDefiner;
  2072. }
  2073. });
  2074. // src/definition/model/properties/property-validator/property-validator.js
  2075. var init_property_validator = __esm({
  2076. "src/definition/model/properties/property-validator/property-validator.js"() {
  2077. "use strict";
  2078. }
  2079. });
  2080. // src/definition/model/properties/property-validator/builtin/regexp-validator.js
  2081. function regexpValidator(value, options, context) {
  2082. if (value == null || options === false) return true;
  2083. if (typeof options !== "string" && !(options instanceof RegExp))
  2084. throw new InvalidArgumentError2(
  2085. 'The validator %v requires the "options" argument as a String or RegExp, but %v given.',
  2086. context.validatorName,
  2087. options
  2088. );
  2089. if (typeof value === "string") {
  2090. const regexp = stringToRegexp(options);
  2091. return regexp.test(value);
  2092. }
  2093. throw new InvalidArgumentError2(
  2094. "The property validator %v requires a String value, but %v given.",
  2095. context.validatorName,
  2096. value
  2097. );
  2098. }
  2099. var init_regexp_validator = __esm({
  2100. "src/definition/model/properties/property-validator/builtin/regexp-validator.js"() {
  2101. "use strict";
  2102. init_utils2();
  2103. init_errors2();
  2104. __name(regexpValidator, "regexpValidator");
  2105. }
  2106. });
  2107. // src/definition/model/properties/property-validator/builtin/max-length-validator.js
  2108. function maxLengthValidator(value, options, context) {
  2109. if (value == null || options === false) return true;
  2110. if (typeof options !== "number")
  2111. throw new InvalidArgumentError2(
  2112. 'The validator %v requires the "options" argument as a Number, but %v given.',
  2113. context.validatorName,
  2114. options
  2115. );
  2116. if (typeof value === "string" || Array.isArray(value))
  2117. return value.length <= options;
  2118. throw new InvalidArgumentError2(
  2119. "The property validator %v requires a String or an Array value, but %v given.",
  2120. context.validatorName,
  2121. value
  2122. );
  2123. }
  2124. var init_max_length_validator = __esm({
  2125. "src/definition/model/properties/property-validator/builtin/max-length-validator.js"() {
  2126. "use strict";
  2127. init_errors2();
  2128. __name(maxLengthValidator, "maxLengthValidator");
  2129. }
  2130. });
  2131. // src/definition/model/properties/property-validator/builtin/min-length-validator.js
  2132. function minLengthValidator(value, options, context) {
  2133. if (value == null || options === false) return true;
  2134. if (typeof options !== "number")
  2135. throw new InvalidArgumentError2(
  2136. 'The validator %v requires the "options" argument as a Number, but %v given.',
  2137. context.validatorName,
  2138. options
  2139. );
  2140. if (typeof value === "string" || Array.isArray(value))
  2141. return value.length >= options;
  2142. throw new InvalidArgumentError2(
  2143. "The property validator %v requires a String or an Array value, but %v given.",
  2144. context.validatorName,
  2145. value
  2146. );
  2147. }
  2148. var init_min_length_validator = __esm({
  2149. "src/definition/model/properties/property-validator/builtin/min-length-validator.js"() {
  2150. "use strict";
  2151. init_errors2();
  2152. __name(minLengthValidator, "minLengthValidator");
  2153. }
  2154. });
  2155. // src/definition/model/properties/property-validator/builtin/index.js
  2156. var init_builtin = __esm({
  2157. "src/definition/model/properties/property-validator/builtin/index.js"() {
  2158. "use strict";
  2159. init_regexp_validator();
  2160. init_max_length_validator();
  2161. init_min_length_validator();
  2162. }
  2163. });
  2164. // src/definition/model/properties/property-validator/property-validator-registry.js
  2165. var _PropertyValidatorRegistry, PropertyValidatorRegistry;
  2166. var init_property_validator_registry = __esm({
  2167. "src/definition/model/properties/property-validator/property-validator-registry.js"() {
  2168. "use strict";
  2169. init_src2();
  2170. init_builtin();
  2171. init_builtin();
  2172. init_builtin();
  2173. init_errors2();
  2174. _PropertyValidatorRegistry = class _PropertyValidatorRegistry extends Service {
  2175. /**
  2176. * Validators.
  2177. *
  2178. * @type {object}
  2179. */
  2180. _validators = {
  2181. maxLength: maxLengthValidator,
  2182. minLength: minLengthValidator,
  2183. regexp: regexpValidator
  2184. };
  2185. /**
  2186. * Add validator.
  2187. *
  2188. * @param {string} name
  2189. * @param {Function} validator
  2190. * @returns {PropertyValidatorRegistry}
  2191. */
  2192. addValidator(name, validator) {
  2193. if (!name || typeof name !== "string")
  2194. throw new InvalidArgumentError2(
  2195. "A name of the property validator must be a non-empty String, but %v given.",
  2196. name
  2197. );
  2198. if (name in this._validators)
  2199. throw new InvalidArgumentError2(
  2200. "The property validator %v is already defined.",
  2201. name
  2202. );
  2203. if (typeof validator !== "function")
  2204. throw new InvalidArgumentError2(
  2205. "The property validator %v must be a Function, but %v given.",
  2206. name,
  2207. validator
  2208. );
  2209. this._validators[name] = validator;
  2210. return this;
  2211. }
  2212. /**
  2213. * Has validator.
  2214. *
  2215. * @param {string} name
  2216. * @returns {boolean}
  2217. */
  2218. hasValidator(name) {
  2219. return Boolean(this._validators[name]);
  2220. }
  2221. /**
  2222. * Get validator.
  2223. *
  2224. * @param {string} name
  2225. * @returns {Function}
  2226. */
  2227. getValidator(name) {
  2228. const validator = this._validators[name];
  2229. if (!validator)
  2230. throw new InvalidArgumentError2(
  2231. "The property validator %v is not defined.",
  2232. name
  2233. );
  2234. return validator;
  2235. }
  2236. };
  2237. __name(_PropertyValidatorRegistry, "PropertyValidatorRegistry");
  2238. PropertyValidatorRegistry = _PropertyValidatorRegistry;
  2239. }
  2240. });
  2241. // src/definition/model/properties/property-validator/index.js
  2242. var init_property_validator2 = __esm({
  2243. "src/definition/model/properties/property-validator/index.js"() {
  2244. "use strict";
  2245. init_property_validator();
  2246. init_property_validator_registry();
  2247. }
  2248. });
  2249. // src/definition/model/properties/property-transformer/property-transformer.js
  2250. var init_property_transformer = __esm({
  2251. "src/definition/model/properties/property-transformer/property-transformer.js"() {
  2252. "use strict";
  2253. }
  2254. });
  2255. // src/definition/model/properties/property-transformer/builtin/trim-transformer.js
  2256. function trimTransformer(value, options, context) {
  2257. if (value == null) return value;
  2258. if (typeof value === "string") return value.trim();
  2259. throw new InvalidArgumentError2(
  2260. "The property transformer %v requires a String value, but %v given.",
  2261. context.transformerName,
  2262. value
  2263. );
  2264. }
  2265. var init_trim_transformer = __esm({
  2266. "src/definition/model/properties/property-transformer/builtin/trim-transformer.js"() {
  2267. "use strict";
  2268. init_errors2();
  2269. __name(trimTransformer, "trimTransformer");
  2270. }
  2271. });
  2272. // src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.js
  2273. function toLowerCaseTransformer(value, options, context) {
  2274. if (value == null) return value;
  2275. if (typeof value === "string") return value.toLowerCase();
  2276. throw new InvalidArgumentError2(
  2277. "The property transformer %v requires a String value, but %v given.",
  2278. context.transformerName,
  2279. value
  2280. );
  2281. }
  2282. var init_to_lower_case_transformer = __esm({
  2283. "src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.js"() {
  2284. "use strict";
  2285. init_errors2();
  2286. __name(toLowerCaseTransformer, "toLowerCaseTransformer");
  2287. }
  2288. });
  2289. // src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.js
  2290. function toUpperCaseTransformer(value, options, context) {
  2291. if (value == null) return value;
  2292. if (typeof value === "string") return value.toUpperCase();
  2293. throw new InvalidArgumentError2(
  2294. "The property transformer %v requires a String value, but %v given.",
  2295. context.transformerName,
  2296. value
  2297. );
  2298. }
  2299. var init_to_upper_case_transformer = __esm({
  2300. "src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.js"() {
  2301. "use strict";
  2302. init_errors2();
  2303. __name(toUpperCaseTransformer, "toUpperCaseTransformer");
  2304. }
  2305. });
  2306. // src/definition/model/properties/property-transformer/builtin/to-title-case-transformer.js
  2307. function toTitleCaseTransformer(value, options, context) {
  2308. if (value == null) return value;
  2309. if (typeof value === "string")
  2310. return value.replace(new RegExp("\\p{L}\\S*", "gu"), (text) => {
  2311. return text.charAt(0).toUpperCase() + text.substring(1).toLowerCase();
  2312. });
  2313. throw new InvalidArgumentError2(
  2314. "The property transformer %v requires a String value, but %v given.",
  2315. context.transformerName,
  2316. value
  2317. );
  2318. }
  2319. var init_to_title_case_transformer = __esm({
  2320. "src/definition/model/properties/property-transformer/builtin/to-title-case-transformer.js"() {
  2321. "use strict";
  2322. init_errors2();
  2323. __name(toTitleCaseTransformer, "toTitleCaseTransformer");
  2324. }
  2325. });
  2326. // src/definition/model/properties/property-transformer/builtin/index.js
  2327. var init_builtin2 = __esm({
  2328. "src/definition/model/properties/property-transformer/builtin/index.js"() {
  2329. "use strict";
  2330. init_trim_transformer();
  2331. init_to_lower_case_transformer();
  2332. init_to_upper_case_transformer();
  2333. init_to_title_case_transformer();
  2334. }
  2335. });
  2336. // src/definition/model/properties/property-transformer/property-transformer-registry.js
  2337. var _PropertyTransformerRegistry, PropertyTransformerRegistry;
  2338. var init_property_transformer_registry = __esm({
  2339. "src/definition/model/properties/property-transformer/property-transformer-registry.js"() {
  2340. "use strict";
  2341. init_src2();
  2342. init_builtin2();
  2343. init_builtin2();
  2344. init_builtin2();
  2345. init_builtin2();
  2346. init_errors2();
  2347. _PropertyTransformerRegistry = class _PropertyTransformerRegistry extends Service {
  2348. /**
  2349. * Transformers.
  2350. *
  2351. * @type {object}
  2352. */
  2353. _transformers = {
  2354. trim: trimTransformer,
  2355. toUpperCase: toUpperCaseTransformer,
  2356. toLowerCase: toLowerCaseTransformer,
  2357. toTitleCase: toTitleCaseTransformer
  2358. };
  2359. /**
  2360. * Add transformer.
  2361. *
  2362. * @param {string} name
  2363. * @param {Function} transformer
  2364. * @returns {PropertyTransformerRegistry}
  2365. */
  2366. addTransformer(name, transformer) {
  2367. if (!name || typeof name !== "string")
  2368. throw new InvalidArgumentError2(
  2369. "A name of the property transformer must be a non-empty String, but %v given.",
  2370. name
  2371. );
  2372. if (name in this._transformers)
  2373. throw new InvalidArgumentError2(
  2374. "The property transformer %v is already defined.",
  2375. name
  2376. );
  2377. if (typeof transformer !== "function")
  2378. throw new InvalidArgumentError2(
  2379. "The property transformer %v must be a Function, but %v given.",
  2380. name,
  2381. transformer
  2382. );
  2383. this._transformers[name] = transformer;
  2384. return this;
  2385. }
  2386. /**
  2387. * Has transformer.
  2388. *
  2389. * @param {string} name
  2390. * @returns {boolean}
  2391. */
  2392. hasTransformer(name) {
  2393. return Boolean(this._transformers[name]);
  2394. }
  2395. /**
  2396. * Get transformer.
  2397. *
  2398. * @param {string} name
  2399. * @returns {Function}
  2400. */
  2401. getTransformer(name) {
  2402. const transformer = this._transformers[name];
  2403. if (!transformer)
  2404. throw new InvalidArgumentError2(
  2405. "The property transformer %v is not defined.",
  2406. name
  2407. );
  2408. return transformer;
  2409. }
  2410. };
  2411. __name(_PropertyTransformerRegistry, "PropertyTransformerRegistry");
  2412. PropertyTransformerRegistry = _PropertyTransformerRegistry;
  2413. }
  2414. });
  2415. // src/definition/model/properties/property-transformer/index.js
  2416. var init_property_transformer2 = __esm({
  2417. "src/definition/model/properties/property-transformer/index.js"() {
  2418. "use strict";
  2419. init_property_transformer();
  2420. init_property_transformer_registry();
  2421. }
  2422. });
  2423. // src/definition/definition-registry.js
  2424. var _DefinitionRegistry, DefinitionRegistry;
  2425. var init_definition_registry = __esm({
  2426. "src/definition/definition-registry.js"() {
  2427. "use strict";
  2428. init_src2();
  2429. init_errors2();
  2430. init_model();
  2431. init_definition();
  2432. _DefinitionRegistry = class _DefinitionRegistry extends Service {
  2433. /**
  2434. * Datasources.
  2435. *
  2436. * @type {object}
  2437. */
  2438. _datasources = {};
  2439. /**
  2440. * Models.
  2441. *
  2442. * @type {object}
  2443. */
  2444. _models = {};
  2445. /**
  2446. * Add datasource.
  2447. *
  2448. * @param {object} datasourceDef
  2449. */
  2450. addDatasource(datasourceDef) {
  2451. this.getService(DatasourceDefinitionValidator).validate(datasourceDef);
  2452. const name = datasourceDef.name;
  2453. if (name in this._datasources)
  2454. throw new InvalidArgumentError2(
  2455. "The datasource %v is already defined.",
  2456. name
  2457. );
  2458. this._datasources[name] = datasourceDef;
  2459. }
  2460. /**
  2461. * Has datasource.
  2462. *
  2463. * @param {string} name
  2464. * @returns {boolean}
  2465. */
  2466. hasDatasource(name) {
  2467. return Boolean(this._datasources[name]);
  2468. }
  2469. /**
  2470. * Get datasource.
  2471. *
  2472. * @param {string} name
  2473. * @returns {object}
  2474. */
  2475. getDatasource(name) {
  2476. const datasourceDef = this._datasources[name];
  2477. if (!datasourceDef)
  2478. throw new InvalidArgumentError2("The datasource %v is not defined.", name);
  2479. return datasourceDef;
  2480. }
  2481. /**
  2482. * Add model.
  2483. *
  2484. * @param {object} modelDef
  2485. */
  2486. addModel(modelDef) {
  2487. this.getService(ModelDefinitionValidator).validate(modelDef);
  2488. const name = modelDef.name;
  2489. if (name in this._models)
  2490. throw new InvalidArgumentError2("The model %v is already defined.", name);
  2491. this._models[name] = modelDef;
  2492. }
  2493. /**
  2494. * Has model.
  2495. *
  2496. * @param {string} name
  2497. * @returns {boolean}
  2498. */
  2499. hasModel(name) {
  2500. return Boolean(this._models[name]);
  2501. }
  2502. /**
  2503. * Get model.
  2504. *
  2505. * @param {string} name
  2506. * @returns {object}
  2507. */
  2508. getModel(name) {
  2509. const modelDef = this._models[name];
  2510. if (!modelDef)
  2511. throw new InvalidArgumentError2("The model %v is not defined.", name);
  2512. return modelDef;
  2513. }
  2514. };
  2515. __name(_DefinitionRegistry, "DefinitionRegistry");
  2516. DefinitionRegistry = _DefinitionRegistry;
  2517. }
  2518. });
  2519. // src/definition/model/model-definition-utils.js
  2520. var DEFAULT_PRIMARY_KEY_PROPERTY_NAME, _ModelDefinitionUtils, ModelDefinitionUtils;
  2521. var init_model_definition_utils = __esm({
  2522. "src/definition/model/model-definition-utils.js"() {
  2523. "use strict";
  2524. init_src2();
  2525. init_properties();
  2526. init_utils2();
  2527. init_utils2();
  2528. init_properties();
  2529. init_errors2();
  2530. init_definition_registry();
  2531. DEFAULT_PRIMARY_KEY_PROPERTY_NAME = "id";
  2532. _ModelDefinitionUtils = class _ModelDefinitionUtils extends Service {
  2533. /**
  2534. * Get primary key as property name.
  2535. *
  2536. * @param {string} modelName
  2537. * @returns {string}
  2538. */
  2539. getPrimaryKeyAsPropertyName(modelName) {
  2540. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2541. const propNames = Object.keys(propDefs).filter((propName) => {
  2542. const propDef = propDefs[propName];
  2543. return propDef && typeof propDef === "object" && propDef.primaryKey;
  2544. });
  2545. if (propNames.length < 1) {
  2546. const isDefaultPrimaryKeyAlreadyInUse = Object.keys(propDefs).includes(
  2547. DEFAULT_PRIMARY_KEY_PROPERTY_NAME
  2548. );
  2549. if (isDefaultPrimaryKeyAlreadyInUse)
  2550. throw new InvalidArgumentError2(
  2551. 'The property name %v of the model %v is defined as a regular property. In this case, a primary key should be defined explicitly. Do use the option "primaryKey" to specify the primary key.',
  2552. DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  2553. modelName
  2554. );
  2555. return DEFAULT_PRIMARY_KEY_PROPERTY_NAME;
  2556. }
  2557. return propNames[0];
  2558. }
  2559. /**
  2560. * Get primary key as column name.
  2561. *
  2562. * @param {string} modelName
  2563. * @returns {string}
  2564. */
  2565. getPrimaryKeyAsColumnName(modelName) {
  2566. const pkPropName = this.getPrimaryKeyAsPropertyName(modelName);
  2567. let pkColName;
  2568. try {
  2569. pkColName = this.getColumnNameByPropertyName(modelName, pkPropName);
  2570. } catch (error) {
  2571. if (!(error instanceof InvalidArgumentError2)) throw error;
  2572. }
  2573. if (pkColName === void 0) return pkPropName;
  2574. return pkColName;
  2575. }
  2576. /**
  2577. * Get table name by model name.
  2578. *
  2579. * @param {string} modelName
  2580. * @returns {string}
  2581. */
  2582. getTableNameByModelName(modelName) {
  2583. var _a;
  2584. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  2585. return (_a = modelDef.tableName) != null ? _a : modelName;
  2586. }
  2587. /**
  2588. * Get column name by property name.
  2589. *
  2590. * @param {string} modelName
  2591. * @param {string} propertyName
  2592. * @returns {string}
  2593. */
  2594. getColumnNameByPropertyName(modelName, propertyName) {
  2595. var _a;
  2596. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2597. const propDef = propDefs[propertyName];
  2598. if (!propDef)
  2599. throw new InvalidArgumentError2(
  2600. "The model %v does not have the property %v.",
  2601. modelName,
  2602. propertyName
  2603. );
  2604. if (propDef && typeof propDef === "object")
  2605. return (_a = propDef.columnName) != null ? _a : propertyName;
  2606. return propertyName;
  2607. }
  2608. /**
  2609. * Get default property value.
  2610. *
  2611. * @param {string} modelName
  2612. * @param {string} propertyName
  2613. * @returns {*}
  2614. */
  2615. getDefaultPropertyValue(modelName, propertyName) {
  2616. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2617. const propDef = propDefs[propertyName];
  2618. if (!propDef)
  2619. throw new InvalidArgumentError2(
  2620. "The model %v does not have the property %v.",
  2621. modelName,
  2622. propertyName
  2623. );
  2624. if (propDef && typeof propDef === "object")
  2625. return propDef.default instanceof Function ? propDef.default() : propDef.default;
  2626. }
  2627. /**
  2628. * Set default values for empty properties.
  2629. *
  2630. * @param {string} modelName
  2631. * @param {object} modelData
  2632. * @param {boolean|undefined} onlyProvidedProperties
  2633. * @returns {object}
  2634. */
  2635. setDefaultValuesToEmptyProperties(modelName, modelData, onlyProvidedProperties = false) {
  2636. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2637. const propNames = onlyProvidedProperties ? Object.keys(modelData) : Object.keys(propDefs);
  2638. const extendedData = cloneDeep(modelData);
  2639. const emptyValueDefiner = this.getService(EmptyValuesDefiner);
  2640. propNames.forEach((propName) => {
  2641. const propDef = propDefs[propName];
  2642. const propValue = extendedData[propName];
  2643. const propType = propDef != null ? this.getDataTypeFromPropertyDefinition(propDef) : DataType.ANY;
  2644. const isEmpty = emptyValueDefiner.isEmpty(propType, propValue);
  2645. if (!isEmpty) return;
  2646. if (propDef && typeof propDef === "object" && propDef.default !== void 0) {
  2647. extendedData[propName] = this.getDefaultPropertyValue(
  2648. modelName,
  2649. propName
  2650. );
  2651. }
  2652. });
  2653. return extendedData;
  2654. }
  2655. /**
  2656. * Convert property names to column names.
  2657. *
  2658. * @param {string} modelName
  2659. * @param {object} modelData
  2660. * @returns {object}
  2661. */
  2662. convertPropertyNamesToColumnNames(modelName, modelData) {
  2663. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2664. const propNames = Object.keys(propDefs);
  2665. const convertedData = cloneDeep(modelData);
  2666. propNames.forEach((propName) => {
  2667. if (!(propName in convertedData)) return;
  2668. const colName = this.getColumnNameByPropertyName(modelName, propName);
  2669. if (propName === colName) return;
  2670. const propValue = convertedData[propName];
  2671. delete convertedData[propName];
  2672. convertedData[colName] = propValue;
  2673. });
  2674. return convertedData;
  2675. }
  2676. /**
  2677. * Convert column names to property names.
  2678. *
  2679. * @param {string} modelName
  2680. * @param {object} tableData
  2681. * @returns {object}
  2682. */
  2683. convertColumnNamesToPropertyNames(modelName, tableData) {
  2684. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2685. const propNames = Object.keys(propDefs);
  2686. const convertedData = cloneDeep(tableData);
  2687. propNames.forEach((propName) => {
  2688. const colName = this.getColumnNameByPropertyName(modelName, propName);
  2689. if (!(colName in convertedData) || colName === propName) return;
  2690. const colValue = convertedData[colName];
  2691. delete convertedData[colName];
  2692. convertedData[propName] = colValue;
  2693. });
  2694. return convertedData;
  2695. }
  2696. /**
  2697. * Get data type by property name.
  2698. *
  2699. * @param {string} modelName
  2700. * @param {string} propertyName
  2701. * @returns {string}
  2702. */
  2703. getDataTypeByPropertyName(modelName, propertyName) {
  2704. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2705. const propDef = propDefs[propertyName];
  2706. if (!propDef) {
  2707. const pkPropName = this.getPrimaryKeyAsPropertyName(modelName);
  2708. if (pkPropName === propertyName) return DataType.ANY;
  2709. throw new InvalidArgumentError2(
  2710. "The model %v does not have the property %v.",
  2711. modelName,
  2712. propertyName
  2713. );
  2714. }
  2715. if (typeof propDef === "string") return propDef;
  2716. return propDef.type;
  2717. }
  2718. /**
  2719. * Get data type from property definition.
  2720. *
  2721. * @param {object} propDef
  2722. * @returns {string}
  2723. */
  2724. getDataTypeFromPropertyDefinition(propDef) {
  2725. if ((!propDef || typeof propDef !== "object") && !Object.values(DataType).includes(propDef)) {
  2726. throw new InvalidArgumentError2(
  2727. 'The argument "propDef" of the ModelDefinitionUtils.getDataTypeFromPropertyDefinition should be an Object or the DataType enum, but %v given.',
  2728. propDef
  2729. );
  2730. }
  2731. if (typeof propDef === "string") return propDef;
  2732. const dataType = propDef.type;
  2733. if (!Object.values(DataType).includes(dataType))
  2734. throw new InvalidArgumentError2(
  2735. 'The given Object to the ModelDefinitionUtils.getDataTypeFromPropertyDefinition should have the "type" property with one of values: %l, but %v given.',
  2736. Object.values(DataType),
  2737. propDef.type
  2738. );
  2739. return dataType;
  2740. }
  2741. /**
  2742. * Get own properties definition of primary keys.
  2743. *
  2744. * @param {string} modelName
  2745. * @returns {object}
  2746. */
  2747. getOwnPropertiesDefinitionOfPrimaryKeys(modelName) {
  2748. var _a;
  2749. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  2750. const propDefs = (_a = modelDef.properties) != null ? _a : {};
  2751. const pkPropNames = Object.keys(propDefs).filter((propName) => {
  2752. const propDef = propDefs[propName];
  2753. return typeof propDef === "object" && propDef.primaryKey;
  2754. });
  2755. return pkPropNames.reduce((a, k) => ({ ...a, [k]: propDefs[k] }), {});
  2756. }
  2757. /**
  2758. * Get own properties definition without primary keys.
  2759. *
  2760. * @param {string} modelName
  2761. * @returns {object}
  2762. */
  2763. getOwnPropertiesDefinitionWithoutPrimaryKeys(modelName) {
  2764. var _a;
  2765. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  2766. const propDefs = (_a = modelDef.properties) != null ? _a : {};
  2767. return Object.keys(propDefs).reduce((result, propName) => {
  2768. const propDef = propDefs[propName];
  2769. if (typeof propDef === "object" && propDef.primaryKey) return result;
  2770. return { ...result, [propName]: propDef };
  2771. }, {});
  2772. }
  2773. /**
  2774. * Get properties definition in base model hierarchy.
  2775. *
  2776. * @param {string} modelName
  2777. * @returns {object}
  2778. */
  2779. getPropertiesDefinitionInBaseModelHierarchy(modelName) {
  2780. let result = {};
  2781. let pkPropDefs = {};
  2782. const recursion = /* @__PURE__ */ __name((currModelName, prevModelName = void 0) => {
  2783. if (currModelName === prevModelName)
  2784. throw new InvalidArgumentError2(
  2785. "The model %v has a circular inheritance.",
  2786. currModelName
  2787. );
  2788. if (Object.keys(pkPropDefs).length === 0) {
  2789. pkPropDefs = this.getOwnPropertiesDefinitionOfPrimaryKeys(currModelName);
  2790. result = { ...result, ...pkPropDefs };
  2791. }
  2792. const regularPropDefs = this.getOwnPropertiesDefinitionWithoutPrimaryKeys(currModelName);
  2793. result = { ...regularPropDefs, ...result };
  2794. const modelDef = this.getService(DefinitionRegistry).getModel(currModelName);
  2795. if (modelDef.base) recursion(modelDef.base, currModelName);
  2796. }, "recursion");
  2797. recursion(modelName);
  2798. return result;
  2799. }
  2800. /**
  2801. * Get own relations definition.
  2802. *
  2803. * @param {string} modelName
  2804. * @returns {object}
  2805. */
  2806. getOwnRelationsDefinition(modelName) {
  2807. var _a;
  2808. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  2809. return (_a = modelDef.relations) != null ? _a : {};
  2810. }
  2811. /**
  2812. * Get relations definition in base model hierarchy.
  2813. *
  2814. * @param {string} modelName
  2815. * @returns {object}
  2816. */
  2817. getRelationsDefinitionInBaseModelHierarchy(modelName) {
  2818. let result = {};
  2819. const recursion = /* @__PURE__ */ __name((currModelName, prevModelName = void 0) => {
  2820. var _a;
  2821. if (currModelName === prevModelName)
  2822. throw new InvalidArgumentError2(
  2823. "The model %v has a circular inheritance.",
  2824. currModelName
  2825. );
  2826. const modelDef = this.getService(DefinitionRegistry).getModel(currModelName);
  2827. const ownRelDefs = (_a = modelDef.relations) != null ? _a : {};
  2828. result = { ...ownRelDefs, ...result };
  2829. if (modelDef.base) recursion(modelDef.base, currModelName);
  2830. }, "recursion");
  2831. recursion(modelName);
  2832. return result;
  2833. }
  2834. /**
  2835. * Get relation definition by name.
  2836. *
  2837. * @param {string} modelName
  2838. * @param {string} relationName
  2839. * @returns {object}
  2840. */
  2841. getRelationDefinitionByName(modelName, relationName) {
  2842. const relDefs = this.getRelationsDefinitionInBaseModelHierarchy(modelName);
  2843. const relNames = Object.keys(relDefs);
  2844. let foundDef;
  2845. for (const relName of relNames) {
  2846. if (relName === relationName) {
  2847. foundDef = relDefs[relName];
  2848. break;
  2849. }
  2850. }
  2851. if (!foundDef)
  2852. throw new InvalidArgumentError2(
  2853. "The model %v does not have relation name %v.",
  2854. modelName,
  2855. relationName
  2856. );
  2857. return foundDef;
  2858. }
  2859. /**
  2860. * Exclude object keys by relation names.
  2861. *
  2862. * @param {string} modelName
  2863. * @param {object} modelData
  2864. * @returns {object}
  2865. */
  2866. excludeObjectKeysByRelationNames(modelName, modelData) {
  2867. if (!modelData || typeof modelData !== "object" || Array.isArray(modelData))
  2868. throw new InvalidArgumentError2(
  2869. "The second argument of ModelDefinitionUtils.excludeObjectKeysByRelationNames should be an Object, but %v given.",
  2870. modelData
  2871. );
  2872. const relDefs = this.getRelationsDefinitionInBaseModelHierarchy(modelName);
  2873. const relNames = Object.keys(relDefs);
  2874. return excludeObjectKeys(modelData, relNames);
  2875. }
  2876. };
  2877. __name(_ModelDefinitionUtils, "ModelDefinitionUtils");
  2878. ModelDefinitionUtils = _ModelDefinitionUtils;
  2879. }
  2880. });
  2881. // src/definition/model/properties/property-uniqueness-validator.js
  2882. var _PropertyUniquenessValidator, PropertyUniquenessValidator;
  2883. var init_property_uniqueness_validator = __esm({
  2884. "src/definition/model/properties/property-uniqueness-validator.js"() {
  2885. "use strict";
  2886. init_data_type();
  2887. init_src2();
  2888. init_utils2();
  2889. init_property_uniqueness();
  2890. init_empty_values_definer();
  2891. init_errors2();
  2892. init_model_definition_utils();
  2893. _PropertyUniquenessValidator = class _PropertyUniquenessValidator extends Service {
  2894. /**
  2895. * Validate.
  2896. *
  2897. * @param {Function} countMethod
  2898. * @param {string} methodName
  2899. * @param {string} modelName
  2900. * @param {object} modelData
  2901. * @param {*} modelId
  2902. * @returns {Promise<undefined>}
  2903. */
  2904. async validate(countMethod, methodName, modelName, modelData, modelId = void 0) {
  2905. if (typeof countMethod !== "function")
  2906. throw new InvalidArgumentError2(
  2907. 'The parameter "countMethod" of the PropertyUniquenessValidator must be a Function, but %v given.',
  2908. countMethod
  2909. );
  2910. if (!methodName || typeof methodName !== "string")
  2911. throw new InvalidArgumentError2(
  2912. 'The parameter "methodName" of the PropertyUniquenessValidator must be a non-empty String, but %v given.',
  2913. methodName
  2914. );
  2915. if (!modelName || typeof modelName !== "string")
  2916. throw new InvalidArgumentError2(
  2917. 'The parameter "modelName" of the PropertyUniquenessValidator must be a non-empty String, but %v given.',
  2918. modelName
  2919. );
  2920. if (!isPureObject(modelData))
  2921. throw new InvalidArgumentError2(
  2922. "The data of the model %v should be an Object, but %v given.",
  2923. modelName,
  2924. modelData
  2925. );
  2926. const propDefs = this.getService(
  2927. ModelDefinitionUtils
  2928. ).getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2929. const isPartial = methodName === "patch" || methodName === "patchById";
  2930. const propNames = Object.keys(isPartial ? modelData : propDefs);
  2931. const idProp = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  2932. modelName
  2933. );
  2934. const createError = /* @__PURE__ */ __name((propName, propValue) => new InvalidArgumentError2(
  2935. "An existing document of the model %v already has the property %v with the value %v and should be unique.",
  2936. modelName,
  2937. propName,
  2938. propValue
  2939. ), "createError");
  2940. let willBeReplaced = void 0;
  2941. const emptyValuesDefiner = this.getService(EmptyValuesDefiner);
  2942. for (const propName of propNames) {
  2943. const propDef = propDefs[propName];
  2944. if (!propDef || typeof propDef === "string" || !propDef.unique || propDef.unique === PropertyUniqueness.NON_UNIQUE) {
  2945. continue;
  2946. }
  2947. const propValue = modelData[propName];
  2948. if (propDef.unique === PropertyUniqueness.SPARSE) {
  2949. const propType = propDef.type || DataType.ANY;
  2950. const isEmpty = emptyValuesDefiner.isEmpty(propType, propValue);
  2951. if (isEmpty) continue;
  2952. }
  2953. if (methodName === "create") {
  2954. const count = await countMethod({ [propName]: propValue });
  2955. if (count > 0) throw createError(propName, propValue);
  2956. } else if (methodName === "replaceById") {
  2957. const count = await countMethod({
  2958. [idProp]: { neq: modelId },
  2959. [propName]: propValue
  2960. });
  2961. if (count > 0) throw createError(propName, propValue);
  2962. } else if (methodName === "replaceOrCreate") {
  2963. const idFromData = modelData[idProp];
  2964. if (willBeReplaced == null && idFromData != null) {
  2965. const count = await countMethod({ [idProp]: idFromData });
  2966. willBeReplaced = count > 0;
  2967. }
  2968. if (willBeReplaced) {
  2969. const count = await countMethod({
  2970. [idProp]: { neq: idFromData },
  2971. [propName]: propValue
  2972. });
  2973. if (count > 0) throw createError(propName, propValue);
  2974. } else {
  2975. const count = await countMethod({ [propName]: propValue });
  2976. if (count > 0) throw createError(propName, propValue);
  2977. }
  2978. } else if (methodName === "patch") {
  2979. const count = await countMethod({ [propName]: propValue });
  2980. if (count > 0) throw createError(propName, propValue);
  2981. } else if (methodName === "patchById") {
  2982. const count = await countMethod({
  2983. [idProp]: { neq: modelId },
  2984. [propName]: propValue
  2985. });
  2986. if (count > 0) throw createError(propName, propValue);
  2987. } else {
  2988. throw new InvalidArgumentError2(
  2989. "The PropertyUniquenessValidator does not support the adapter method %v.",
  2990. methodName
  2991. );
  2992. }
  2993. }
  2994. }
  2995. };
  2996. __name(_PropertyUniquenessValidator, "PropertyUniquenessValidator");
  2997. PropertyUniquenessValidator = _PropertyUniquenessValidator;
  2998. }
  2999. });
  3000. // src/definition/model/properties/primary-keys-definition-validator.js
  3001. var _PrimaryKeysDefinitionValidator, PrimaryKeysDefinitionValidator;
  3002. var init_primary_keys_definition_validator = __esm({
  3003. "src/definition/model/properties/primary-keys-definition-validator.js"() {
  3004. "use strict";
  3005. init_src2();
  3006. init_errors2();
  3007. init_model_definition_utils();
  3008. _PrimaryKeysDefinitionValidator = class _PrimaryKeysDefinitionValidator extends Service {
  3009. /**
  3010. * Validate.
  3011. *
  3012. * @param {string} modelName
  3013. * @param {object} propDefs
  3014. */
  3015. validate(modelName, propDefs) {
  3016. const propNames = Object.keys(propDefs).filter((propName) => {
  3017. const propDef = propDefs[propName];
  3018. return propDef && typeof propDef === "object" && propDef.primaryKey;
  3019. });
  3020. if (propNames.length < 1) {
  3021. const isDefaultPrimaryKeyAlreadyInUse = Object.keys(propDefs).includes(DEFAULT_PRIMARY_KEY_PROPERTY_NAME);
  3022. if (isDefaultPrimaryKeyAlreadyInUse)
  3023. throw new InvalidArgumentError2(
  3024. 'The property name %v of the model %v is defined as a regular property. In this case, a primary key should be defined explicitly. Do use the option "primaryKey" to specify the primary key.',
  3025. DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  3026. modelName
  3027. );
  3028. return;
  3029. }
  3030. if (propNames.length > 1)
  3031. throw new InvalidArgumentError2(
  3032. "The model definition %v should not have multiple primary keys, but %v keys given.",
  3033. modelName,
  3034. propNames.length
  3035. );
  3036. const pkPropName = propNames[0];
  3037. const pkPropDef = propDefs[pkPropName];
  3038. if (pkPropDef && typeof pkPropDef === "object" && pkPropDef.default !== void 0) {
  3039. throw new InvalidArgumentError2(
  3040. "Do not specify a default value for the primary key %v of the model %v.",
  3041. pkPropName,
  3042. modelName
  3043. );
  3044. }
  3045. }
  3046. };
  3047. __name(_PrimaryKeysDefinitionValidator, "PrimaryKeysDefinitionValidator");
  3048. PrimaryKeysDefinitionValidator = _PrimaryKeysDefinitionValidator;
  3049. }
  3050. });
  3051. // src/definition/model/properties/properties-definition-validator.js
  3052. var _PropertiesDefinitionValidator, PropertiesDefinitionValidator;
  3053. var init_properties_definition_validator = __esm({
  3054. "src/definition/model/properties/properties-definition-validator.js"() {
  3055. "use strict";
  3056. init_src2();
  3057. init_data_type();
  3058. init_utils2();
  3059. init_property_uniqueness();
  3060. init_errors2();
  3061. init_property_validator2();
  3062. init_property_transformer2();
  3063. init_primary_keys_definition_validator();
  3064. _PropertiesDefinitionValidator = class _PropertiesDefinitionValidator extends Service {
  3065. /**
  3066. * Validate.
  3067. *
  3068. * @param {string} modelName
  3069. * @param {object} propDefs
  3070. */
  3071. validate(modelName, propDefs) {
  3072. if (!modelName || typeof modelName !== "string")
  3073. throw new InvalidArgumentError2(
  3074. "The first argument of PropertiesDefinitionValidator.validate should be a non-empty String, but %v given.",
  3075. modelName
  3076. );
  3077. if (!propDefs || typeof propDefs !== "object" || Array.isArray(propDefs)) {
  3078. throw new InvalidArgumentError2(
  3079. 'The provided option "properties" of the model %v should be an Object, but %v given.',
  3080. modelName,
  3081. propDefs
  3082. );
  3083. }
  3084. const propNames = Object.keys(propDefs);
  3085. propNames.forEach((propName) => {
  3086. const propDef = propDefs[propName];
  3087. this._validateProperty(modelName, propName, propDef);
  3088. });
  3089. this.getService(PrimaryKeysDefinitionValidator).validate(
  3090. modelName,
  3091. propDefs
  3092. );
  3093. }
  3094. /**
  3095. * Validate property.
  3096. *
  3097. * @param {string} modelName
  3098. * @param {string} propName
  3099. * @param {object} propDef
  3100. */
  3101. _validateProperty(modelName, propName, propDef) {
  3102. if (!modelName || typeof modelName !== "string")
  3103. throw new InvalidArgumentError2(
  3104. "The first argument of PropertiesDefinitionValidator._validateProperty should be a non-empty String, but %v given.",
  3105. modelName
  3106. );
  3107. if (!propName || typeof propName !== "string")
  3108. throw new InvalidArgumentError2(
  3109. "The property name of the model %v should be a non-empty String, but %v given.",
  3110. modelName,
  3111. propName
  3112. );
  3113. if (!propDef)
  3114. throw new InvalidArgumentError2(
  3115. "The property %v of the model %v should have a property definition, but %v given.",
  3116. propName,
  3117. modelName,
  3118. propDef
  3119. );
  3120. if (typeof propDef === "string") {
  3121. if (!Object.values(DataType).includes(propDef))
  3122. throw new InvalidArgumentError2(
  3123. "In case of a short property definition, the property %v of the model %v should have one of data types: %l, but %v given.",
  3124. propName,
  3125. modelName,
  3126. Object.values(DataType),
  3127. propDef
  3128. );
  3129. return;
  3130. }
  3131. if (!propDef || typeof propDef !== "object" || Array.isArray(propDef)) {
  3132. throw new InvalidArgumentError2(
  3133. "In case of a full property definition, the property %v of the model %v should be an Object, but %v given.",
  3134. propName,
  3135. modelName,
  3136. propDef
  3137. );
  3138. }
  3139. if (!propDef.type || !Object.values(DataType).includes(propDef.type))
  3140. throw new InvalidArgumentError2(
  3141. 'The property %v of the model %v requires the option "type" to have one of data types: %l, but %v given.',
  3142. propName,
  3143. modelName,
  3144. Object.values(DataType),
  3145. propDef.type
  3146. );
  3147. if (propDef.itemType && !Object.values(DataType).includes(propDef.itemType)) {
  3148. throw new InvalidArgumentError2(
  3149. 'The provided option "itemType" of the property %v in the model %v should have one of data types: %l, but %v given.',
  3150. propName,
  3151. modelName,
  3152. Object.values(DataType),
  3153. propDef.itemType
  3154. );
  3155. }
  3156. if (propDef.model && typeof propDef.model !== "string")
  3157. throw new InvalidArgumentError2(
  3158. 'The provided option "model" of the property %v in the model %v should be a String, but %v given.',
  3159. propName,
  3160. modelName,
  3161. propDef.model
  3162. );
  3163. if (propDef.primaryKey && typeof propDef.primaryKey !== "boolean")
  3164. throw new InvalidArgumentError2(
  3165. 'The provided option "primaryKey" of the property %v in the model %v should be a Boolean, but %v given.',
  3166. propName,
  3167. modelName,
  3168. propDef.primaryKey
  3169. );
  3170. if (propDef.columnName && typeof propDef.columnName !== "string")
  3171. throw new InvalidArgumentError2(
  3172. 'The provided option "columnName" of the property %v in the model %v should be a String, but %v given.',
  3173. propName,
  3174. modelName,
  3175. propDef.columnName
  3176. );
  3177. if (propDef.columnType && typeof propDef.columnType !== "string")
  3178. throw new InvalidArgumentError2(
  3179. 'The provided option "columnType" of the property %v in the model %v should be a String, but %v given.',
  3180. propName,
  3181. modelName,
  3182. propDef.columnType
  3183. );
  3184. if (propDef.required && typeof propDef.required !== "boolean")
  3185. throw new InvalidArgumentError2(
  3186. 'The provided option "required" of the property %v in the model %v should be a Boolean, but %v given.',
  3187. propName,
  3188. modelName,
  3189. propDef.required
  3190. );
  3191. if (propDef.required && propDef.default !== void 0)
  3192. throw new InvalidArgumentError2(
  3193. 'The property %v of the model %v is a required property, so it should not have the option "default" to be provided.',
  3194. propName,
  3195. modelName
  3196. );
  3197. if (propDef.primaryKey && propDef.required)
  3198. throw new InvalidArgumentError2(
  3199. 'The property %v of the model %v is a primary key, so it should not have the option "required" to be provided.',
  3200. propName,
  3201. modelName
  3202. );
  3203. if (propDef.primaryKey && propDef.default !== void 0)
  3204. throw new InvalidArgumentError2(
  3205. 'The property %v of the model %v is a primary key, so it should not have the option "default" to be provided.',
  3206. propName,
  3207. modelName
  3208. );
  3209. if (propDef.itemType && propDef.type !== DataType.ARRAY)
  3210. throw new InvalidArgumentError2(
  3211. 'The property %v of the model %v has the non-array type, so it should not have the option "itemType" to be provided.',
  3212. propName,
  3213. modelName,
  3214. propDef.type
  3215. );
  3216. if (propDef.model && propDef.type !== DataType.OBJECT && propDef.itemType !== DataType.OBJECT) {
  3217. if (propDef.type !== DataType.ARRAY) {
  3218. throw new InvalidArgumentError2(
  3219. 'The option "model" is not supported for %s property type, so the property %v of the model %v should not have the option "model" to be provided.',
  3220. capitalize(propDef.type),
  3221. propName,
  3222. modelName
  3223. );
  3224. } else {
  3225. throw new InvalidArgumentError2(
  3226. 'The option "model" is not supported for Array property type of %s, so the property %v of the model %v should not have the option "model" to be provided.',
  3227. capitalize(propDef.itemType),
  3228. propName,
  3229. modelName
  3230. );
  3231. }
  3232. }
  3233. if (propDef.validate != null) {
  3234. const propertyValidatorRegistry = this.getService(
  3235. PropertyValidatorRegistry
  3236. );
  3237. if (propDef.validate && typeof propDef.validate === "string") {
  3238. if (!propertyValidatorRegistry.hasValidator(propDef.validate))
  3239. throw new InvalidArgumentError2(
  3240. "The property validator %v is not found.",
  3241. propDef.validate
  3242. );
  3243. } else if (Array.isArray(propDef.validate)) {
  3244. for (const validatorName of propDef.validate) {
  3245. if (typeof validatorName !== "string")
  3246. throw new InvalidArgumentError2(
  3247. 'The provided option "validate" of the property %v in the model %v has an Array value that should have a non-empty String, but %v given.',
  3248. propName,
  3249. modelName,
  3250. validatorName
  3251. );
  3252. if (!propertyValidatorRegistry.hasValidator(validatorName))
  3253. throw new InvalidArgumentError2(
  3254. "The property validator %v is not found.",
  3255. validatorName
  3256. );
  3257. }
  3258. } else if (typeof propDef.validate === "object") {
  3259. for (const validatorName in propDef.validate) {
  3260. if (!propertyValidatorRegistry.hasValidator(validatorName))
  3261. throw new InvalidArgumentError2(
  3262. "The property validator %v is not found.",
  3263. validatorName
  3264. );
  3265. }
  3266. } else {
  3267. throw new InvalidArgumentError2(
  3268. 'The provided option "validate" of the property %v in the model %v should be a non-empty String, an Array of String or an Object, but %v given.',
  3269. propName,
  3270. modelName,
  3271. propDef.validate
  3272. );
  3273. }
  3274. }
  3275. if (propDef.transform != null) {
  3276. const propertyTransformerRegistry = this.getService(
  3277. PropertyTransformerRegistry
  3278. );
  3279. if (propDef.transform && typeof propDef.transform === "string") {
  3280. if (!propertyTransformerRegistry.hasTransformer(propDef.transform))
  3281. throw new InvalidArgumentError2(
  3282. "The property transformer %v is not found.",
  3283. propDef.transform
  3284. );
  3285. } else if (Array.isArray(propDef.transform)) {
  3286. for (const transformerName of propDef.transform) {
  3287. if (typeof transformerName !== "string")
  3288. throw new InvalidArgumentError2(
  3289. 'The provided option "transform" of the property %v in the model %v has an Array value that should have a non-empty String, but %v given.',
  3290. propName,
  3291. modelName,
  3292. transformerName
  3293. );
  3294. if (!propertyTransformerRegistry.hasTransformer(transformerName))
  3295. throw new InvalidArgumentError2(
  3296. "The property transformer %v is not found.",
  3297. transformerName
  3298. );
  3299. }
  3300. } else if (typeof propDef.transform === "object") {
  3301. for (const transformerName in propDef.transform) {
  3302. if (!propertyTransformerRegistry.hasTransformer(transformerName))
  3303. throw new InvalidArgumentError2(
  3304. "The property transformer %v is not found.",
  3305. transformerName
  3306. );
  3307. }
  3308. } else {
  3309. throw new InvalidArgumentError2(
  3310. 'The provided option "transform" of the property %v in the model %v should be a non-empty String, an Array of String or an Object, but %v given.',
  3311. propName,
  3312. modelName,
  3313. propDef.transform
  3314. );
  3315. }
  3316. }
  3317. if (propDef.unique) {
  3318. if (typeof propDef.unique !== "boolean" && !Object.values(PropertyUniqueness).includes(propDef.unique)) {
  3319. throw new InvalidArgumentError2(
  3320. 'The provided option "unique" of the property %v in the model %v should be a Boolean or one of values: %l, but %v given.',
  3321. propName,
  3322. modelName,
  3323. Object.values(PropertyUniqueness),
  3324. propDef.unique
  3325. );
  3326. }
  3327. }
  3328. if (propDef.unique && propDef.primaryKey)
  3329. throw new InvalidArgumentError2(
  3330. 'The property %v of the model %v is a primary key, so it should not have the option "unique" to be provided.',
  3331. propName,
  3332. modelName
  3333. );
  3334. }
  3335. };
  3336. __name(_PropertiesDefinitionValidator, "PropertiesDefinitionValidator");
  3337. PropertiesDefinitionValidator = _PropertiesDefinitionValidator;
  3338. }
  3339. });
  3340. // src/definition/model/properties/index.js
  3341. var init_properties = __esm({
  3342. "src/definition/model/properties/index.js"() {
  3343. "use strict";
  3344. init_data_type();
  3345. init_property_definition();
  3346. init_property_uniqueness();
  3347. init_empty_values_definer();
  3348. init_property_validator2();
  3349. init_property_transformer2();
  3350. init_property_uniqueness_validator();
  3351. init_properties_definition_validator();
  3352. init_primary_keys_definition_validator();
  3353. }
  3354. });
  3355. // src/definition/model/model-definition.js
  3356. var init_model_definition = __esm({
  3357. "src/definition/model/model-definition.js"() {
  3358. "use strict";
  3359. }
  3360. });
  3361. // src/definition/model/model-data-validator.js
  3362. var _ModelDataValidator, ModelDataValidator;
  3363. var init_model_data_validator = __esm({
  3364. "src/definition/model/model-data-validator.js"() {
  3365. "use strict";
  3366. init_src2();
  3367. init_properties();
  3368. init_utils2();
  3369. init_utils2();
  3370. init_properties();
  3371. init_errors2();
  3372. init_properties();
  3373. init_model_definition_utils();
  3374. _ModelDataValidator = class _ModelDataValidator extends Service {
  3375. /**
  3376. * Validate.
  3377. *
  3378. * @param {string} modelName
  3379. * @param {object} modelData
  3380. * @param {boolean} isPartial
  3381. * @returns {undefined}
  3382. */
  3383. validate(modelName, modelData, isPartial = false) {
  3384. if (!isPureObject(modelData))
  3385. throw new InvalidArgumentError2(
  3386. "The data of the model %v should be an Object, but %v given.",
  3387. modelName,
  3388. modelData
  3389. );
  3390. const propDefs = this.getService(
  3391. ModelDefinitionUtils
  3392. ).getPropertiesDefinitionInBaseModelHierarchy(modelName);
  3393. const propNames = Object.keys(isPartial ? modelData : propDefs);
  3394. propNames.forEach((propName) => {
  3395. const propDef = propDefs[propName];
  3396. if (!propDef) return;
  3397. this._validatePropertyValue(
  3398. modelName,
  3399. propName,
  3400. propDef,
  3401. modelData[propName]
  3402. );
  3403. });
  3404. }
  3405. /**
  3406. * Validate property value.
  3407. *
  3408. * @param {string} modelName
  3409. * @param {string} propName
  3410. * @param {string|object} propDef
  3411. * @param {*} propValue
  3412. * @returns {undefined}
  3413. */
  3414. _validatePropertyValue(modelName, propName, propDef, propValue) {
  3415. const propType = this.getService(ModelDefinitionUtils).getDataTypeFromPropertyDefinition(
  3416. propDef
  3417. );
  3418. const isEmpty = this.getService(EmptyValuesDefiner).isEmpty(
  3419. propType,
  3420. propValue
  3421. );
  3422. if (isEmpty) {
  3423. const isRequired = typeof propDef === "string" ? false : Boolean(propDef.required);
  3424. if (!isRequired) return;
  3425. throw new InvalidArgumentError2(
  3426. "The property %v of the model %v is required, but %v given.",
  3427. propName,
  3428. modelName,
  3429. propValue
  3430. );
  3431. }
  3432. this._validateValueByPropertyValidators(
  3433. modelName,
  3434. propName,
  3435. propDef,
  3436. propValue
  3437. );
  3438. this._validateValueByPropertyType(modelName, propName, propDef, propValue);
  3439. }
  3440. /**
  3441. * Validate value by property type.
  3442. *
  3443. * @param {string} modelName
  3444. * @param {string} propName
  3445. * @param {string|object} propDef
  3446. * @param {*} propValue
  3447. * @param {boolean} isArrayValue
  3448. * @returns {undefined}
  3449. */
  3450. _validateValueByPropertyType(modelName, propName, propDef, propValue, isArrayValue = false) {
  3451. var _a;
  3452. let expectingType;
  3453. if (isArrayValue) {
  3454. if (typeof propDef === "object") {
  3455. expectingType = (_a = propDef.itemType) != null ? _a : DataType.ANY;
  3456. } else {
  3457. expectingType = DataType.ANY;
  3458. }
  3459. } else {
  3460. expectingType = typeof propDef !== "string" ? propDef.type : propDef;
  3461. }
  3462. const createError = /* @__PURE__ */ __name((expected) => {
  3463. const pattern = isArrayValue ? "The array property %v of the model %v must have %s element, but %s given." : "The property %v of the model %v must have %s, but %s given.";
  3464. const ctorName = getCtorName(propValue);
  3465. const givenStr = ctorName != null ? ctorName : typeof propValue;
  3466. return new InvalidArgumentError2(
  3467. pattern,
  3468. propName,
  3469. modelName,
  3470. expected,
  3471. givenStr
  3472. );
  3473. }, "createError");
  3474. switch (expectingType) {
  3475. // STRING
  3476. case DataType.STRING:
  3477. if (typeof propValue !== "string") throw createError("a String");
  3478. break;
  3479. // NUMBER
  3480. case DataType.NUMBER:
  3481. if (typeof propValue !== "number") throw createError("a Number");
  3482. break;
  3483. // BOOLEAN
  3484. case DataType.BOOLEAN:
  3485. if (typeof propValue !== "boolean") throw createError("a Boolean");
  3486. break;
  3487. // ARRAY
  3488. case DataType.ARRAY:
  3489. if (!Array.isArray(propValue)) throw createError("an Array");
  3490. propValue.forEach(
  3491. (value) => this._validateValueByPropertyType(
  3492. modelName,
  3493. propName,
  3494. propDef,
  3495. value,
  3496. true
  3497. )
  3498. );
  3499. break;
  3500. // OBJECT
  3501. case DataType.OBJECT:
  3502. if (!isPureObject(propValue)) throw createError("an Object");
  3503. if (typeof propDef === "object" && propDef.model)
  3504. this.validate(propDef.model, propValue);
  3505. break;
  3506. }
  3507. }
  3508. /**
  3509. * Validate value by property validators.
  3510. *
  3511. * @param {string} modelName
  3512. * @param {string} propName
  3513. * @param {string|object} propDef
  3514. * @param {*} propValue
  3515. * @returns {undefined}
  3516. */
  3517. _validateValueByPropertyValidators(modelName, propName, propDef, propValue) {
  3518. if (typeof propDef === "string" || propDef.validate == null) return;
  3519. const validateDef = propDef.validate;
  3520. const validatorRegistry = this.getService(PropertyValidatorRegistry);
  3521. const createError = /* @__PURE__ */ __name((validatorName) => new InvalidArgumentError2(
  3522. "The property %v of the model %v has an invalid value %v that caught by the validator %v.",
  3523. propName,
  3524. modelName,
  3525. propValue,
  3526. validatorName
  3527. ), "createError");
  3528. const validateBy = /* @__PURE__ */ __name((validatorName, validatorOptions = void 0) => {
  3529. const validator = validatorRegistry.getValidator(validatorName);
  3530. const context = { validatorName, modelName, propName };
  3531. const valid = validator(propValue, validatorOptions, context);
  3532. if (valid instanceof Promise) {
  3533. throw new InvalidArgumentError2(
  3534. "Asynchronous property validators are not supported, but the property validator %v returns a Promise.",
  3535. validatorName
  3536. );
  3537. } else if (valid !== true) {
  3538. throw createError(validatorName);
  3539. }
  3540. }, "validateBy");
  3541. if (validateDef && typeof validateDef === "string") {
  3542. validateBy(validateDef);
  3543. } else if (Array.isArray(validateDef)) {
  3544. validateDef.forEach((validatorName) => validateBy(validatorName));
  3545. } else if (validateDef !== null && typeof validateDef === "object") {
  3546. Object.keys(validateDef).forEach((validatorName) => {
  3547. if (Object.prototype.hasOwnProperty.call(validateDef, validatorName)) {
  3548. const validatorOptions = validateDef[validatorName];
  3549. validateBy(validatorName, validatorOptions);
  3550. }
  3551. });
  3552. } else {
  3553. throw new InvalidArgumentError2(
  3554. 'The provided option "validate" of the property %v in the model %v should be a non-empty String, an Array of String or an Object, but %v given.',
  3555. propName,
  3556. modelName,
  3557. validateDef
  3558. );
  3559. }
  3560. }
  3561. };
  3562. __name(_ModelDataValidator, "ModelDataValidator");
  3563. ModelDataValidator = _ModelDataValidator;
  3564. }
  3565. });
  3566. // src/definition/model/model-data-sanitizer.js
  3567. var _ModelDataSanitizer, ModelDataSanitizer;
  3568. var init_model_data_sanitizer = __esm({
  3569. "src/definition/model/model-data-sanitizer.js"() {
  3570. "use strict";
  3571. init_src2();
  3572. init_errors2();
  3573. init_model_definition_utils();
  3574. _ModelDataSanitizer = class _ModelDataSanitizer extends Service {
  3575. /**
  3576. * Validate.
  3577. *
  3578. * @param {string} modelName
  3579. * @param {object} modelData
  3580. * @returns {object}
  3581. */
  3582. sanitize(modelName, modelData) {
  3583. if (!modelName || typeof modelName !== "string")
  3584. throw new InvalidArgumentError2(
  3585. "The first argument of ModelDataSanitizer.sanitize should be a string, but %v given.",
  3586. modelName
  3587. );
  3588. if (!modelData || typeof modelData !== "object")
  3589. throw new InvalidArgumentError2(
  3590. "The second argument of ModelDataSanitizer.sanitize should be an Object, but %v given.",
  3591. modelData
  3592. );
  3593. return this.getService(
  3594. ModelDefinitionUtils
  3595. ).excludeObjectKeysByRelationNames(modelName, modelData);
  3596. }
  3597. };
  3598. __name(_ModelDataSanitizer, "ModelDataSanitizer");
  3599. ModelDataSanitizer = _ModelDataSanitizer;
  3600. }
  3601. });
  3602. // src/definition/model/model-data-transformer.js
  3603. var _ModelDataTransformer, ModelDataTransformer;
  3604. var init_model_data_transformer = __esm({
  3605. "src/definition/model/model-data-transformer.js"() {
  3606. "use strict";
  3607. init_src2();
  3608. init_utils2();
  3609. init_utils2();
  3610. init_utils2();
  3611. init_properties();
  3612. init_errors2();
  3613. init_model_definition_utils();
  3614. init_properties();
  3615. _ModelDataTransformer = class _ModelDataTransformer extends Service {
  3616. /**
  3617. * Transform.
  3618. *
  3619. * @param {string} modelName
  3620. * @param {object} modelData
  3621. * @param {boolean} isPartial
  3622. * @returns {object|Promise<object>}
  3623. */
  3624. transform(modelName, modelData, isPartial = false) {
  3625. if (!isPureObject(modelData))
  3626. throw new InvalidArgumentError2(
  3627. "The data of the model %v should be an Object, but %v given.",
  3628. modelName,
  3629. modelData
  3630. );
  3631. const emptyValuesDefiner = this.getService(EmptyValuesDefiner);
  3632. const modelDefinitionUtils = this.getService(ModelDefinitionUtils);
  3633. const propDefs = modelDefinitionUtils.getPropertiesDefinitionInBaseModelHierarchy(
  3634. modelName
  3635. );
  3636. const propNames = Object.keys(isPartial ? modelData : propDefs);
  3637. const transformedData = cloneDeep(modelData);
  3638. return propNames.reduce((transformedDataOrPromise, propName) => {
  3639. const propDef = propDefs[propName];
  3640. if (!propDef) return transformedDataOrPromise;
  3641. const propType = modelDefinitionUtils.getDataTypeFromPropertyDefinition(propDef);
  3642. const propValue = modelData[propName];
  3643. const isEmpty = emptyValuesDefiner.isEmpty(propType, propValue);
  3644. if (isEmpty) return transformedDataOrPromise;
  3645. const newPropValueOrPromise = this._transformPropertyValue(
  3646. modelName,
  3647. propName,
  3648. propDef,
  3649. propValue
  3650. );
  3651. return transformPromise(newPropValueOrPromise, (newPropValue) => {
  3652. return transformPromise(transformedDataOrPromise, (resolvedData) => {
  3653. if (newPropValue !== propValue) resolvedData[propName] = newPropValue;
  3654. return resolvedData;
  3655. });
  3656. });
  3657. }, transformedData);
  3658. }
  3659. /**
  3660. * Transform property value.
  3661. *
  3662. * @param {string} modelName
  3663. * @param {string} propName
  3664. * @param {string|object} propDef
  3665. * @param {*} propValue
  3666. * @returns {*}
  3667. */
  3668. _transformPropertyValue(modelName, propName, propDef, propValue) {
  3669. if (typeof propDef === "string" || propDef.transform == null)
  3670. return propValue;
  3671. const transformDef = propDef.transform;
  3672. const transformerRegistry = this.getService(PropertyTransformerRegistry);
  3673. const transformFn = /* @__PURE__ */ __name((value, transformerName, transformerOptions = void 0) => {
  3674. const transformer = transformerRegistry.getTransformer(transformerName);
  3675. const context = { transformerName, modelName, propName };
  3676. return transformer(value, transformerOptions, context);
  3677. }, "transformFn");
  3678. if (transformDef && typeof transformDef === "string") {
  3679. return transformFn(propValue, transformDef);
  3680. } else if (Array.isArray(transformDef)) {
  3681. return transformDef.reduce((valueOrPromise, transformerName) => {
  3682. return transformPromise(valueOrPromise, (value) => {
  3683. return transformFn(value, transformerName);
  3684. });
  3685. }, propValue);
  3686. } else if (transformDef !== null && typeof transformDef === "object") {
  3687. return Object.keys(transformDef).reduce(
  3688. (valueOrPromise, transformerName) => {
  3689. const transformerOptions = transformDef[transformerName];
  3690. return transformPromise(valueOrPromise, (value) => {
  3691. return transformFn(value, transformerName, transformerOptions);
  3692. });
  3693. },
  3694. propValue
  3695. );
  3696. } else {
  3697. throw new InvalidArgumentError2(
  3698. 'The provided option "transform" of the property %v in the model %v should be a non-empty String, an Array of String or an Object, but %v given.',
  3699. propName,
  3700. modelName,
  3701. transformDef
  3702. );
  3703. }
  3704. }
  3705. };
  3706. __name(_ModelDataTransformer, "ModelDataTransformer");
  3707. ModelDataTransformer = _ModelDataTransformer;
  3708. }
  3709. });
  3710. // src/definition/model/model-definition-validator.js
  3711. var _ModelDefinitionValidator, ModelDefinitionValidator;
  3712. var init_model_definition_validator = __esm({
  3713. "src/definition/model/model-definition-validator.js"() {
  3714. "use strict";
  3715. init_src2();
  3716. init_errors2();
  3717. init_relations();
  3718. init_properties();
  3719. _ModelDefinitionValidator = class _ModelDefinitionValidator extends Service {
  3720. /**
  3721. * Validate.
  3722. *
  3723. * @param {object} modelDef
  3724. */
  3725. validate(modelDef) {
  3726. if (!modelDef || typeof modelDef !== "object" || Array.isArray(modelDef))
  3727. throw new InvalidArgumentError2(
  3728. "The model definition should be an Object, but %v given.",
  3729. modelDef
  3730. );
  3731. if (!modelDef.name || typeof modelDef.name !== "string")
  3732. throw new InvalidArgumentError2(
  3733. 'The model definition requires the option "name" as a non-empty String, but %v given.',
  3734. modelDef.name
  3735. );
  3736. if (modelDef.datasource && typeof modelDef.datasource !== "string")
  3737. throw new InvalidArgumentError2(
  3738. 'The provided option "datasource" of the model %v should be a String, but %v given.',
  3739. modelDef.name,
  3740. modelDef.datasource
  3741. );
  3742. if (modelDef.base && typeof modelDef.base !== "string")
  3743. throw new InvalidArgumentError2(
  3744. 'The provided option "base" of the model %v should be a String, but %v given.',
  3745. modelDef.name,
  3746. modelDef.base
  3747. );
  3748. if (modelDef.tableName && typeof modelDef.tableName !== "string")
  3749. throw new InvalidArgumentError2(
  3750. 'The provided option "tableName" of the model %v should be a String, but %v given.',
  3751. modelDef.name,
  3752. modelDef.tableName
  3753. );
  3754. if (modelDef.properties) {
  3755. if (typeof modelDef.properties !== "object" || Array.isArray(modelDef.properties)) {
  3756. throw new InvalidArgumentError2(
  3757. 'The provided option "properties" of the model %v should be an Object, but %v given.',
  3758. modelDef.name,
  3759. modelDef.properties
  3760. );
  3761. }
  3762. this.getService(PropertiesDefinitionValidator).validate(
  3763. modelDef.name,
  3764. modelDef.properties
  3765. );
  3766. }
  3767. if (modelDef.relations) {
  3768. if (typeof modelDef.relations !== "object" || Array.isArray(modelDef.relations)) {
  3769. throw new InvalidArgumentError2(
  3770. 'The provided option "relations" of the model %v should be an Object, but %v given.',
  3771. modelDef.name,
  3772. modelDef.relations
  3773. );
  3774. }
  3775. this.getService(RelationsDefinitionValidator).validate(
  3776. modelDef.name,
  3777. modelDef.relations
  3778. );
  3779. }
  3780. }
  3781. };
  3782. __name(_ModelDefinitionValidator, "ModelDefinitionValidator");
  3783. ModelDefinitionValidator = _ModelDefinitionValidator;
  3784. }
  3785. });
  3786. // src/definition/model/index.js
  3787. var init_model = __esm({
  3788. "src/definition/model/index.js"() {
  3789. "use strict";
  3790. init_relations();
  3791. init_properties();
  3792. init_model_definition();
  3793. init_model_data_validator();
  3794. init_model_data_sanitizer();
  3795. init_model_data_transformer();
  3796. init_model_definition_utils();
  3797. init_model_definition_validator();
  3798. }
  3799. });
  3800. // src/definition/datasource/datasource-definition-validator.js
  3801. var _DatasourceDefinitionValidator, DatasourceDefinitionValidator;
  3802. var init_datasource_definition_validator = __esm({
  3803. "src/definition/datasource/datasource-definition-validator.js"() {
  3804. "use strict";
  3805. init_src2();
  3806. init_errors2();
  3807. _DatasourceDefinitionValidator = class _DatasourceDefinitionValidator extends Service {
  3808. /**
  3809. * Validate.
  3810. *
  3811. * @param {object} datasourceDef
  3812. */
  3813. validate(datasourceDef) {
  3814. if (!datasourceDef || typeof datasourceDef !== "object")
  3815. throw new InvalidArgumentError2(
  3816. "The datasource definition should be an Object, but %v given.",
  3817. datasourceDef
  3818. );
  3819. if (!datasourceDef.name || typeof datasourceDef.name !== "string")
  3820. throw new InvalidArgumentError2(
  3821. 'The datasource definition requires the option "name" as a non-empty String, but %v given.',
  3822. datasourceDef.name
  3823. );
  3824. if (!datasourceDef.adapter || typeof datasourceDef.adapter !== "string")
  3825. throw new InvalidArgumentError2(
  3826. 'The datasource %v requires the option "adapter" as a non-empty String, but %v given.',
  3827. datasourceDef.name,
  3828. datasourceDef.adapter
  3829. );
  3830. }
  3831. };
  3832. __name(_DatasourceDefinitionValidator, "DatasourceDefinitionValidator");
  3833. DatasourceDefinitionValidator = _DatasourceDefinitionValidator;
  3834. }
  3835. });
  3836. // src/definition/datasource/index.js
  3837. var init_datasource = __esm({
  3838. "src/definition/datasource/index.js"() {
  3839. "use strict";
  3840. init_datasource_definition_validator();
  3841. }
  3842. });
  3843. // src/definition/index.js
  3844. var init_definition = __esm({
  3845. "src/definition/index.js"() {
  3846. "use strict";
  3847. init_model();
  3848. init_datasource();
  3849. init_definition_registry();
  3850. }
  3851. });
  3852. // src/filter/fields-clause-tool.js
  3853. var _FieldsClauseTool, FieldsClauseTool;
  3854. var init_fields_clause_tool = __esm({
  3855. "src/filter/fields-clause-tool.js"() {
  3856. "use strict";
  3857. init_src2();
  3858. init_utils2();
  3859. init_errors2();
  3860. init_definition();
  3861. _FieldsClauseTool = class _FieldsClauseTool extends Service {
  3862. /**
  3863. * Filter.
  3864. *
  3865. * @param {object|object[]} input
  3866. * @param {string} modelName
  3867. * @param {string|string[]|undefined} clause
  3868. * @returns {object|object[]}
  3869. */
  3870. filter(input, modelName, clause) {
  3871. const isArray = Array.isArray(input);
  3872. let entities = isArray ? input : [input];
  3873. entities.forEach((entity) => {
  3874. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  3875. throw new InvalidArgumentError2(
  3876. "The first argument of FieldsClauseTool.filter should be an Object or an Array of Object, but %v given.",
  3877. entity
  3878. );
  3879. });
  3880. if (!modelName || typeof modelName !== "string")
  3881. throw new InvalidArgumentError2(
  3882. "The second argument of FieldsClauseTool.filter should be a non-empty String, but %v given.",
  3883. modelName
  3884. );
  3885. if (clause == null) return input;
  3886. const fields = Array.isArray(clause) ? clause.slice() : [clause];
  3887. if (!fields.length) return input;
  3888. fields.forEach((field) => {
  3889. if (!field || typeof field !== "string")
  3890. throw new InvalidArgumentError2(
  3891. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v given.',
  3892. field
  3893. );
  3894. });
  3895. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3896. modelName
  3897. );
  3898. if (fields.indexOf(pkPropName) === -1) fields.push(pkPropName);
  3899. entities = entities.map((entity) => selectObjectKeys(entity, fields));
  3900. return isArray ? entities : entities[0];
  3901. }
  3902. /**
  3903. * Validate fields clause.
  3904. *
  3905. * @param {string|string[]|undefined} clause
  3906. */
  3907. static validateFieldsClause(clause) {
  3908. if (clause == null) return;
  3909. const fields = Array.isArray(clause) ? clause : [clause];
  3910. if (!fields.length) return;
  3911. fields.forEach((field) => {
  3912. if (!field || typeof field !== "string")
  3913. throw new InvalidArgumentError2(
  3914. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v given.',
  3915. field
  3916. );
  3917. });
  3918. }
  3919. /**
  3920. * Normalize fields clause.
  3921. *
  3922. * @param {string|string[]|undefined} clause
  3923. * @returns {string[]|undefined}
  3924. */
  3925. static normalizeFieldsClause(clause) {
  3926. if (clause == null) return;
  3927. const fields = Array.isArray(clause) ? clause : [clause];
  3928. if (!fields.length) return;
  3929. fields.forEach((field) => {
  3930. if (!field || typeof field !== "string")
  3931. throw new InvalidArgumentError2(
  3932. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v given.',
  3933. field
  3934. );
  3935. });
  3936. return fields;
  3937. }
  3938. };
  3939. __name(_FieldsClauseTool, "FieldsClauseTool");
  3940. FieldsClauseTool = _FieldsClauseTool;
  3941. }
  3942. });
  3943. // src/relations/has-one-resolver.js
  3944. var _HasOneResolver, HasOneResolver;
  3945. var init_has_one_resolver = __esm({
  3946. "src/relations/has-one-resolver.js"() {
  3947. "use strict";
  3948. init_src2();
  3949. init_utils2();
  3950. init_definition();
  3951. init_errors2();
  3952. init_repository2();
  3953. init_definition();
  3954. _HasOneResolver = class _HasOneResolver extends Service {
  3955. /**
  3956. * Include to.
  3957. *
  3958. * @param {object[]} entities
  3959. * @param {string} sourceName
  3960. * @param {string} targetName
  3961. * @param {string} relationName
  3962. * @param {string} foreignKey
  3963. * @param {object|undefined} scope
  3964. * @returns {Promise<void>}
  3965. */
  3966. async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
  3967. if (!entities || !Array.isArray(entities))
  3968. throw new InvalidArgumentError2(
  3969. 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v given.',
  3970. entities
  3971. );
  3972. if (!sourceName || typeof sourceName !== "string")
  3973. throw new InvalidArgumentError2(
  3974. 'The parameter "sourceName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
  3975. sourceName
  3976. );
  3977. if (!targetName || typeof targetName !== "string")
  3978. throw new InvalidArgumentError2(
  3979. 'The parameter "targetName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
  3980. targetName
  3981. );
  3982. if (!relationName || typeof relationName !== "string")
  3983. throw new InvalidArgumentError2(
  3984. 'The parameter "relationName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
  3985. relationName
  3986. );
  3987. if (!foreignKey || typeof foreignKey !== "string")
  3988. throw new InvalidArgumentError2(
  3989. 'The parameter "foreignKey" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
  3990. foreignKey
  3991. );
  3992. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  3993. throw new InvalidArgumentError2(
  3994. 'The provided parameter "scope" of HasOneResolver.includeTo should be an Object, but %v given.',
  3995. scope
  3996. );
  3997. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3998. sourceName
  3999. );
  4000. const sourceIds = [];
  4001. entities.forEach((entity) => {
  4002. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4003. throw new InvalidArgumentError2(
  4004. 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v given.',
  4005. entity
  4006. );
  4007. const sourceId = entity[sourcePkPropName];
  4008. if (sourceIds.includes(sourceId)) return;
  4009. sourceIds.push(sourceId);
  4010. });
  4011. const promises = [];
  4012. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4013. scope = scope ? cloneDeep(scope) : {};
  4014. const targetBySourceId = /* @__PURE__ */ new Map();
  4015. sourceIds.forEach((sourceId) => {
  4016. const filter = cloneDeep(scope);
  4017. filter.where = {
  4018. and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
  4019. };
  4020. filter.limit = 1;
  4021. promises.push(
  4022. targetRepository.find(filter).then((result) => {
  4023. if (result.length) targetBySourceId.set(sourceId, result[0]);
  4024. })
  4025. );
  4026. });
  4027. await Promise.all(promises);
  4028. Array.from(targetBySourceId.keys()).forEach((sourceId) => {
  4029. const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
  4030. sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
  4031. });
  4032. }
  4033. /**
  4034. * Include polymorphic to.
  4035. *
  4036. * @param {object[]} entities
  4037. * @param {string} sourceName
  4038. * @param {string} targetName
  4039. * @param {string} relationName
  4040. * @param {string} foreignKey
  4041. * @param {string} discriminator
  4042. * @param {object|undefined} scope
  4043. * @returns {Promise<void>}
  4044. */
  4045. async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
  4046. if (!entities || !Array.isArray(entities))
  4047. throw new InvalidArgumentError2(
  4048. 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v given.',
  4049. entities
  4050. );
  4051. if (!sourceName || typeof sourceName !== "string")
  4052. throw new InvalidArgumentError2(
  4053. 'The parameter "sourceName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4054. sourceName
  4055. );
  4056. if (!targetName || typeof targetName !== "string")
  4057. throw new InvalidArgumentError2(
  4058. 'The parameter "targetName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4059. targetName
  4060. );
  4061. if (!relationName || typeof relationName !== "string")
  4062. throw new InvalidArgumentError2(
  4063. 'The parameter "relationName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4064. relationName
  4065. );
  4066. if (!foreignKey || typeof foreignKey !== "string")
  4067. throw new InvalidArgumentError2(
  4068. 'The parameter "foreignKey" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4069. foreignKey
  4070. );
  4071. if (!discriminator || typeof discriminator !== "string")
  4072. throw new InvalidArgumentError2(
  4073. 'The parameter "discriminator" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4074. discriminator
  4075. );
  4076. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4077. throw new InvalidArgumentError2(
  4078. 'The provided parameter "scope" of HasOneResolver.includePolymorphicTo should be an Object, but %v given.',
  4079. scope
  4080. );
  4081. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4082. sourceName
  4083. );
  4084. const sourceIds = [];
  4085. entities.forEach((entity) => {
  4086. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4087. throw new InvalidArgumentError2(
  4088. 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v given.',
  4089. entity
  4090. );
  4091. const sourceId = entity[sourcePkPropName];
  4092. if (sourceIds.includes(sourceId)) return;
  4093. sourceIds.push(sourceId);
  4094. });
  4095. const promises = [];
  4096. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4097. scope = scope ? cloneDeep(scope) : {};
  4098. const targetBySourceId = /* @__PURE__ */ new Map();
  4099. sourceIds.forEach((sourceId) => {
  4100. const filter = cloneDeep(scope);
  4101. filter.where = {
  4102. and: [
  4103. { [foreignKey]: sourceId, [discriminator]: sourceName },
  4104. ...scope.where ? [scope.where] : []
  4105. ]
  4106. };
  4107. filter.limit = 1;
  4108. promises.push(
  4109. targetRepository.find(filter).then((result) => {
  4110. if (result.length) targetBySourceId.set(sourceId, result[0]);
  4111. })
  4112. );
  4113. });
  4114. await Promise.all(promises);
  4115. Array.from(targetBySourceId.keys()).forEach((sourceId) => {
  4116. const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
  4117. sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
  4118. });
  4119. }
  4120. /**
  4121. * Include polymorphic by relation name.
  4122. *
  4123. * @param {object[]} entities
  4124. * @param {string} sourceName
  4125. * @param {string} targetName
  4126. * @param {string} relationName
  4127. * @param {string} targetRelationName
  4128. * @param {object|undefined} scope
  4129. * @returns {Promise<void>}
  4130. */
  4131. async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
  4132. if (!entities || !Array.isArray(entities))
  4133. throw new InvalidArgumentError2(
  4134. 'The parameter "entities" of HasOneResolver.includePolymorphicByRelationName requires an Array of Object, but %v given.',
  4135. entities
  4136. );
  4137. if (!sourceName || typeof sourceName !== "string")
  4138. throw new InvalidArgumentError2(
  4139. 'The parameter "sourceName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4140. sourceName
  4141. );
  4142. if (!targetName || typeof targetName !== "string")
  4143. throw new InvalidArgumentError2(
  4144. 'The parameter "targetName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4145. targetName
  4146. );
  4147. if (!relationName || typeof relationName !== "string")
  4148. throw new InvalidArgumentError2(
  4149. 'The parameter "relationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4150. relationName
  4151. );
  4152. if (!targetRelationName || typeof targetRelationName !== "string")
  4153. throw new InvalidArgumentError2(
  4154. 'The parameter "targetRelationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4155. targetRelationName
  4156. );
  4157. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4158. throw new InvalidArgumentError2(
  4159. 'The provided parameter "scope" of HasOneResolver.includePolymorphicByRelationName should be an Object, but %v given.',
  4160. scope
  4161. );
  4162. const targetRelationDef = this.getService(
  4163. ModelDefinitionUtils
  4164. ).getRelationDefinitionByName(targetName, targetRelationName);
  4165. if (targetRelationDef.type !== RelationType.BELONGS_TO)
  4166. throw new InvalidArgumentError2(
  4167. 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic "belongsTo", but %v type given.',
  4168. relationName,
  4169. sourceName,
  4170. targetRelationName,
  4171. targetRelationDef.type
  4172. );
  4173. if (!targetRelationDef.polymorphic)
  4174. throw new InvalidArgumentError2(
  4175. 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic too.',
  4176. relationName,
  4177. sourceName,
  4178. targetRelationName
  4179. );
  4180. const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
  4181. const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
  4182. return this.includePolymorphicTo(
  4183. entities,
  4184. sourceName,
  4185. targetName,
  4186. relationName,
  4187. foreignKey,
  4188. discriminator,
  4189. scope
  4190. );
  4191. }
  4192. };
  4193. __name(_HasOneResolver, "HasOneResolver");
  4194. HasOneResolver = _HasOneResolver;
  4195. }
  4196. });
  4197. // src/relations/has-many-resolver.js
  4198. var _HasManyResolver, HasManyResolver;
  4199. var init_has_many_resolver = __esm({
  4200. "src/relations/has-many-resolver.js"() {
  4201. "use strict";
  4202. init_src2();
  4203. init_utils2();
  4204. init_definition();
  4205. init_errors2();
  4206. init_repository2();
  4207. init_definition();
  4208. _HasManyResolver = class _HasManyResolver extends Service {
  4209. /**
  4210. * Include to.
  4211. *
  4212. * @param {object[]} entities
  4213. * @param {string} sourceName
  4214. * @param {string} targetName
  4215. * @param {string} relationName
  4216. * @param {string} foreignKey
  4217. * @param {object|undefined} scope
  4218. * @returns {Promise<void>}
  4219. */
  4220. async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
  4221. if (!entities || !Array.isArray(entities))
  4222. throw new InvalidArgumentError2(
  4223. 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v given.',
  4224. entities
  4225. );
  4226. if (!sourceName || typeof sourceName !== "string")
  4227. throw new InvalidArgumentError2(
  4228. 'The parameter "sourceName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
  4229. sourceName
  4230. );
  4231. if (!targetName || typeof targetName !== "string")
  4232. throw new InvalidArgumentError2(
  4233. 'The parameter "targetName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
  4234. targetName
  4235. );
  4236. if (!relationName || typeof relationName !== "string")
  4237. throw new InvalidArgumentError2(
  4238. 'The parameter "relationName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
  4239. relationName
  4240. );
  4241. if (!foreignKey || typeof foreignKey !== "string")
  4242. throw new InvalidArgumentError2(
  4243. 'The parameter "foreignKey" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
  4244. foreignKey
  4245. );
  4246. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4247. throw new InvalidArgumentError2(
  4248. 'The provided parameter "scope" of HasManyResolver.includeTo should be an Object, but %v given.',
  4249. scope
  4250. );
  4251. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4252. sourceName
  4253. );
  4254. const sourceIds = [];
  4255. entities.forEach((entity) => {
  4256. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4257. throw new InvalidArgumentError2(
  4258. 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v given.',
  4259. entity
  4260. );
  4261. const sourceId = entity[sourcePkPropName];
  4262. if (sourceIds.includes(sourceId)) return;
  4263. sourceIds.push(sourceId);
  4264. });
  4265. const promises = [];
  4266. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4267. scope = scope ? cloneDeep(scope) : {};
  4268. const targetsBySourceId = /* @__PURE__ */ new Map();
  4269. sourceIds.forEach((sourceId) => {
  4270. const filter = cloneDeep(scope);
  4271. filter.where = {
  4272. and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
  4273. };
  4274. promises.push(
  4275. targetRepository.find(filter).then((result) => {
  4276. var _a;
  4277. if (result.length) {
  4278. let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4279. targets = [...targets, ...result];
  4280. targetsBySourceId.set(sourceId, targets);
  4281. }
  4282. })
  4283. );
  4284. });
  4285. await Promise.all(promises);
  4286. entities.forEach((entity) => {
  4287. var _a;
  4288. const sourceId = entity[sourcePkPropName];
  4289. entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4290. });
  4291. }
  4292. /**
  4293. * Include polymorphic to.
  4294. *
  4295. * @param {object[]} entities
  4296. * @param {string} sourceName
  4297. * @param {string} targetName
  4298. * @param {string} relationName
  4299. * @param {string} foreignKey
  4300. * @param {string} discriminator
  4301. * @param {object|undefined} scope
  4302. * @returns {Promise<void>}
  4303. */
  4304. async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
  4305. if (!entities || !Array.isArray(entities))
  4306. throw new InvalidArgumentError2(
  4307. 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v given.',
  4308. entities
  4309. );
  4310. if (!sourceName || typeof sourceName !== "string")
  4311. throw new InvalidArgumentError2(
  4312. 'The parameter "sourceName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4313. sourceName
  4314. );
  4315. if (!targetName || typeof targetName !== "string")
  4316. throw new InvalidArgumentError2(
  4317. 'The parameter "targetName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4318. targetName
  4319. );
  4320. if (!relationName || typeof relationName !== "string")
  4321. throw new InvalidArgumentError2(
  4322. 'The parameter "relationName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4323. relationName
  4324. );
  4325. if (!foreignKey || typeof foreignKey !== "string")
  4326. throw new InvalidArgumentError2(
  4327. 'The parameter "foreignKey" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4328. foreignKey
  4329. );
  4330. if (!discriminator || typeof discriminator !== "string")
  4331. throw new InvalidArgumentError2(
  4332. 'The parameter "discriminator" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4333. discriminator
  4334. );
  4335. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4336. throw new InvalidArgumentError2(
  4337. 'The provided parameter "scope" of HasManyResolver.includePolymorphicTo should be an Object, but %v given.',
  4338. scope
  4339. );
  4340. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4341. sourceName
  4342. );
  4343. const sourceIds = [];
  4344. entities.forEach((entity) => {
  4345. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4346. throw new InvalidArgumentError2(
  4347. 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v given.',
  4348. entity
  4349. );
  4350. const sourceId = entity[sourcePkPropName];
  4351. if (sourceIds.includes(sourceId)) return;
  4352. sourceIds.push(sourceId);
  4353. });
  4354. const promises = [];
  4355. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4356. scope = scope ? cloneDeep(scope) : {};
  4357. const targetsBySourceId = /* @__PURE__ */ new Map();
  4358. sourceIds.forEach((sourceId) => {
  4359. const filter = cloneDeep(scope);
  4360. filter.where = {
  4361. and: [
  4362. { [foreignKey]: sourceId, [discriminator]: sourceName },
  4363. ...scope.where ? [scope.where] : []
  4364. ]
  4365. };
  4366. promises.push(
  4367. targetRepository.find(filter).then((result) => {
  4368. var _a;
  4369. if (result.length) {
  4370. let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4371. targets = [...targets, ...result];
  4372. targetsBySourceId.set(sourceId, targets);
  4373. }
  4374. })
  4375. );
  4376. });
  4377. await Promise.all(promises);
  4378. entities.forEach((entity) => {
  4379. var _a;
  4380. const sourceId = entity[sourcePkPropName];
  4381. entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4382. });
  4383. }
  4384. /**
  4385. * Include polymorphic by relation name.
  4386. *
  4387. * @param {object[]} entities
  4388. * @param {string} sourceName
  4389. * @param {string} targetName
  4390. * @param {string} relationName
  4391. * @param {string} targetRelationName
  4392. * @param {object|undefined} scope
  4393. * @returns {Promise<void>}
  4394. */
  4395. async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
  4396. if (!entities || !Array.isArray(entities))
  4397. throw new InvalidArgumentError2(
  4398. 'The parameter "entities" of HasManyResolver.includePolymorphicByRelationName requires an Array of Object, but %v given.',
  4399. entities
  4400. );
  4401. if (!sourceName || typeof sourceName !== "string")
  4402. throw new InvalidArgumentError2(
  4403. 'The parameter "sourceName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4404. sourceName
  4405. );
  4406. if (!targetName || typeof targetName !== "string")
  4407. throw new InvalidArgumentError2(
  4408. 'The parameter "targetName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4409. targetName
  4410. );
  4411. if (!relationName || typeof relationName !== "string")
  4412. throw new InvalidArgumentError2(
  4413. 'The parameter "relationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4414. relationName
  4415. );
  4416. if (!targetRelationName || typeof targetRelationName !== "string")
  4417. throw new InvalidArgumentError2(
  4418. 'The parameter "targetRelationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
  4419. targetRelationName
  4420. );
  4421. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4422. throw new InvalidArgumentError2(
  4423. 'The provided parameter "scope" of HasManyResolver.includePolymorphicByRelationName should be an Object, but %v given.',
  4424. scope
  4425. );
  4426. const targetRelationDef = this.getService(
  4427. ModelDefinitionUtils
  4428. ).getRelationDefinitionByName(targetName, targetRelationName);
  4429. if (targetRelationDef.type !== RelationType.BELONGS_TO)
  4430. throw new InvalidArgumentError2(
  4431. 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic "belongsTo", but %v type given.',
  4432. relationName,
  4433. sourceName,
  4434. targetRelationName,
  4435. targetRelationDef.type
  4436. );
  4437. if (!targetRelationDef.polymorphic)
  4438. throw new InvalidArgumentError2(
  4439. 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic too.',
  4440. relationName,
  4441. sourceName,
  4442. targetRelationName
  4443. );
  4444. const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
  4445. const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
  4446. return this.includePolymorphicTo(
  4447. entities,
  4448. sourceName,
  4449. targetName,
  4450. relationName,
  4451. foreignKey,
  4452. discriminator,
  4453. scope
  4454. );
  4455. }
  4456. };
  4457. __name(_HasManyResolver, "HasManyResolver");
  4458. HasManyResolver = _HasManyResolver;
  4459. }
  4460. });
  4461. // src/relations/belongs-to-resolver.js
  4462. var _BelongsToResolver, BelongsToResolver;
  4463. var init_belongs_to_resolver = __esm({
  4464. "src/relations/belongs-to-resolver.js"() {
  4465. "use strict";
  4466. init_src2();
  4467. init_utils2();
  4468. init_utils2();
  4469. init_errors2();
  4470. init_repository2();
  4471. init_definition();
  4472. _BelongsToResolver = class _BelongsToResolver extends Service {
  4473. /**
  4474. * Include to.
  4475. *
  4476. * @param {object[]} entities
  4477. * @param {string} sourceName
  4478. * @param {string} targetName
  4479. * @param {string} relationName
  4480. * @param {string|undefined} foreignKey
  4481. * @param {object|undefined} scope
  4482. * @returns {Promise<void>}
  4483. */
  4484. async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
  4485. if (!entities || !Array.isArray(entities))
  4486. throw new InvalidArgumentError2(
  4487. 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v given.',
  4488. entities
  4489. );
  4490. if (!sourceName || typeof sourceName !== "string")
  4491. throw new InvalidArgumentError2(
  4492. 'The parameter "sourceName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
  4493. sourceName
  4494. );
  4495. if (!targetName || typeof targetName !== "string")
  4496. throw new InvalidArgumentError2(
  4497. 'The parameter "targetName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
  4498. targetName
  4499. );
  4500. if (!relationName || typeof relationName !== "string")
  4501. throw new InvalidArgumentError2(
  4502. 'The parameter "relationName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
  4503. relationName
  4504. );
  4505. if (foreignKey && typeof foreignKey !== "string")
  4506. throw new InvalidArgumentError2(
  4507. 'The provided parameter "foreignKey" of BelongsToResolver.includeTo should be a String, but %v given.',
  4508. foreignKey
  4509. );
  4510. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4511. throw new InvalidArgumentError2(
  4512. 'The provided parameter "scope" of BelongsToResolver.includeTo should be an Object, but %v given.',
  4513. scope
  4514. );
  4515. if (foreignKey == null) foreignKey = `${relationName}Id`;
  4516. const targetIds = entities.reduce((acc, entity) => {
  4517. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4518. throw new InvalidArgumentError2(
  4519. 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v given.',
  4520. entity
  4521. );
  4522. const targetId = entity[foreignKey];
  4523. return targetId != null ? [...acc, targetId] : acc;
  4524. }, []);
  4525. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4526. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4527. targetName
  4528. );
  4529. scope = scope ? cloneDeep(scope) : {};
  4530. const filter = cloneDeep(scope);
  4531. filter.where = {
  4532. and: [
  4533. { [targetPkPropName]: { inq: targetIds } },
  4534. ...scope.where ? [scope.where] : []
  4535. ]
  4536. };
  4537. const targets = await targetRepository.find(filter);
  4538. entities.forEach((entity) => {
  4539. const target = targets.find(
  4540. (e) => e[targetPkPropName] === entity[foreignKey]
  4541. );
  4542. if (target) entity[relationName] = target;
  4543. });
  4544. }
  4545. /**
  4546. * Include polymorphic to.
  4547. *
  4548. * @param {object[]} entities
  4549. * @param {string} sourceName
  4550. * @param {string} relationName
  4551. * @param {string|undefined} foreignKey
  4552. * @param {string|undefined} discriminator
  4553. * @param {object|undefined} scope
  4554. * @returns {Promise<void>}
  4555. */
  4556. async includePolymorphicTo(entities, sourceName, relationName, foreignKey = void 0, discriminator = void 0, scope = void 0) {
  4557. if (!entities || !Array.isArray(entities))
  4558. throw new InvalidArgumentError2(
  4559. 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v given.',
  4560. entities
  4561. );
  4562. if (!sourceName || typeof sourceName !== "string")
  4563. throw new InvalidArgumentError2(
  4564. 'The parameter "sourceName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4565. sourceName
  4566. );
  4567. if (!relationName || typeof relationName !== "string")
  4568. throw new InvalidArgumentError2(
  4569. 'The parameter "relationName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v given.',
  4570. relationName
  4571. );
  4572. if (foreignKey && typeof foreignKey !== "string")
  4573. throw new InvalidArgumentError2(
  4574. 'The provided parameter "foreignKey" of BelongsToResolver.includePolymorphicTo should be a String, but %v given.',
  4575. foreignKey
  4576. );
  4577. if (discriminator && typeof discriminator !== "string")
  4578. throw new InvalidArgumentError2(
  4579. 'The provided parameter "discriminator" of BelongsToResolver.includePolymorphicTo should be a String, but %v given.',
  4580. discriminator
  4581. );
  4582. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4583. throw new InvalidArgumentError2(
  4584. 'The provided parameter "scope" of BelongsToResolver.includePolymorphicTo should be an Object, but %v given.',
  4585. scope
  4586. );
  4587. if (foreignKey == null) {
  4588. const singularRelationName = singularize(relationName);
  4589. foreignKey = `${singularRelationName}Id`;
  4590. }
  4591. if (discriminator == null) {
  4592. const singularRelationName = singularize(relationName);
  4593. discriminator = `${singularRelationName}Type`;
  4594. }
  4595. const targetIdsByTargetName = {};
  4596. entities.forEach((entity) => {
  4597. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4598. throw new InvalidArgumentError2(
  4599. 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v given.',
  4600. entity
  4601. );
  4602. const targetId = entity[foreignKey];
  4603. const targetName = entity[discriminator];
  4604. if (targetId == null || targetName == null) return;
  4605. if (targetIdsByTargetName[targetName] == null)
  4606. targetIdsByTargetName[targetName] = [];
  4607. if (!targetIdsByTargetName[targetName].includes(targetId))
  4608. targetIdsByTargetName[targetName].push(targetId);
  4609. });
  4610. const promises = [];
  4611. const targetNames = Object.keys(targetIdsByTargetName);
  4612. scope = scope ? cloneDeep(scope) : {};
  4613. const targetEntitiesByTargetNames = {};
  4614. targetNames.forEach((targetName) => {
  4615. let targetRepository;
  4616. try {
  4617. targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4618. } catch (error) {
  4619. if (error instanceof InvalidArgumentError2) {
  4620. if (error.message === `The model "${targetName}" is not defined.` || error.message === `The model "${targetName}" does not have a specified datasource.`) {
  4621. return;
  4622. }
  4623. } else {
  4624. throw error;
  4625. }
  4626. }
  4627. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4628. targetName
  4629. );
  4630. const targetFilter = cloneDeep(scope);
  4631. const targetIds = targetIdsByTargetName[targetName];
  4632. targetFilter.where = {
  4633. and: [
  4634. { [targetPkPropName]: { inq: targetIds } },
  4635. ...scope.where ? [scope.where] : []
  4636. ]
  4637. };
  4638. const promise = targetRepository.find(targetFilter).then((result) => {
  4639. var _a;
  4640. targetEntitiesByTargetNames[targetName] = [
  4641. ...(_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [],
  4642. ...result
  4643. ];
  4644. });
  4645. promises.push(promise);
  4646. });
  4647. await Promise.all(promises);
  4648. entities.forEach((entity) => {
  4649. var _a;
  4650. const targetId = entity[foreignKey];
  4651. const targetName = entity[discriminator];
  4652. if (targetId == null || targetName == null || targetEntitiesByTargetNames[targetName] == null) {
  4653. return;
  4654. }
  4655. const targetEntities = (_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [];
  4656. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4657. targetName
  4658. );
  4659. const target = targetEntities.find((e) => e[targetPkPropName] === targetId);
  4660. if (target) entity[relationName] = target;
  4661. });
  4662. }
  4663. };
  4664. __name(_BelongsToResolver, "BelongsToResolver");
  4665. BelongsToResolver = _BelongsToResolver;
  4666. }
  4667. });
  4668. // src/relations/references-many-resolver.js
  4669. var _ReferencesManyResolver, ReferencesManyResolver;
  4670. var init_references_many_resolver = __esm({
  4671. "src/relations/references-many-resolver.js"() {
  4672. "use strict";
  4673. init_src2();
  4674. init_utils2();
  4675. init_utils2();
  4676. init_errors2();
  4677. init_repository2();
  4678. init_definition();
  4679. _ReferencesManyResolver = class _ReferencesManyResolver extends Service {
  4680. /**
  4681. * Include to.
  4682. *
  4683. * @param {object[]} entities
  4684. * @param {string} sourceName
  4685. * @param {string} targetName
  4686. * @param {string} relationName
  4687. * @param {string|undefined} foreignKey
  4688. * @param {object|undefined} scope
  4689. * @returns {Promise<void>}
  4690. */
  4691. async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
  4692. if (!entities || !Array.isArray(entities))
  4693. throw new InvalidArgumentError2(
  4694. 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v given.',
  4695. entities
  4696. );
  4697. if (!sourceName || typeof sourceName !== "string")
  4698. throw new InvalidArgumentError2(
  4699. 'The parameter "sourceName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
  4700. sourceName
  4701. );
  4702. if (!targetName || typeof targetName !== "string")
  4703. throw new InvalidArgumentError2(
  4704. 'The parameter "targetName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
  4705. targetName
  4706. );
  4707. if (!relationName || typeof relationName !== "string")
  4708. throw new InvalidArgumentError2(
  4709. 'The parameter "relationName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
  4710. relationName
  4711. );
  4712. if (foreignKey && typeof foreignKey !== "string")
  4713. throw new InvalidArgumentError2(
  4714. 'The provided parameter "foreignKey" of ReferencesManyResolver.includeTo should be a String, but %v given.',
  4715. foreignKey
  4716. );
  4717. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4718. throw new InvalidArgumentError2(
  4719. 'The provided parameter "scope" of ReferencesManyResolver.includeTo should be an Object, but %v given.',
  4720. scope
  4721. );
  4722. if (foreignKey == null) {
  4723. const singularRelationName = singularize(relationName);
  4724. foreignKey = `${singularRelationName}Ids`;
  4725. }
  4726. const targetIds = entities.reduce((acc, entity) => {
  4727. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4728. throw new InvalidArgumentError2(
  4729. 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v given.',
  4730. entity
  4731. );
  4732. const ids = entity[foreignKey];
  4733. if (Array.isArray(ids))
  4734. ids.forEach((id) => {
  4735. if (id == null || acc.includes(id)) return;
  4736. acc.push(id);
  4737. });
  4738. return acc;
  4739. }, []);
  4740. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4741. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4742. targetName
  4743. );
  4744. scope = scope ? cloneDeep(scope) : {};
  4745. const filter = cloneDeep(scope);
  4746. filter.where = {
  4747. and: [
  4748. { [targetPkPropName]: { inq: targetIds } },
  4749. ...scope.where ? [scope.where] : []
  4750. ]
  4751. };
  4752. const targets = await targetRepository.find(filter);
  4753. entities.forEach((entity) => {
  4754. const ids = entity[foreignKey];
  4755. entity[relationName] = [];
  4756. if (Array.isArray(ids))
  4757. targets.forEach((target) => {
  4758. const targetId = target[targetPkPropName];
  4759. if (ids.includes(targetId)) entity[relationName].push(target);
  4760. });
  4761. });
  4762. }
  4763. };
  4764. __name(_ReferencesManyResolver, "ReferencesManyResolver");
  4765. ReferencesManyResolver = _ReferencesManyResolver;
  4766. }
  4767. });
  4768. // src/relations/index.js
  4769. var init_relations2 = __esm({
  4770. "src/relations/index.js"() {
  4771. "use strict";
  4772. init_has_one_resolver();
  4773. init_has_many_resolver();
  4774. init_belongs_to_resolver();
  4775. init_references_many_resolver();
  4776. }
  4777. });
  4778. // src/filter/include-clause-tool.js
  4779. var _IncludeClauseTool, IncludeClauseTool;
  4780. var init_include_clause_tool = __esm({
  4781. "src/filter/include-clause-tool.js"() {
  4782. "use strict";
  4783. init_src2();
  4784. init_definition();
  4785. init_relations2();
  4786. init_relations2();
  4787. init_where_clause_tool();
  4788. init_order_clause_tool();
  4789. init_slice_clause_tool();
  4790. init_errors2();
  4791. init_relations2();
  4792. init_fields_clause_tool();
  4793. init_definition();
  4794. init_relations2();
  4795. _IncludeClauseTool = class _IncludeClauseTool extends Service {
  4796. /**
  4797. * Include to.
  4798. *
  4799. * @param {object[]} entities
  4800. * @param {string} modelName
  4801. * @param {IncludeClause|undefined} clause
  4802. * @returns {Promise<void>}
  4803. */
  4804. async includeTo(entities, modelName, clause) {
  4805. clause = _IncludeClauseTool.normalizeIncludeClause(clause);
  4806. const promises = [];
  4807. clause.forEach((inclusion) => {
  4808. const relDef = this.getService(
  4809. ModelDefinitionUtils
  4810. ).getRelationDefinitionByName(modelName, inclusion.relation);
  4811. switch (relDef.type) {
  4812. // BELONGS_TO
  4813. case RelationType.BELONGS_TO:
  4814. if (relDef.polymorphic) {
  4815. promises.push(
  4816. this.getService(BelongsToResolver).includePolymorphicTo(
  4817. entities,
  4818. modelName,
  4819. inclusion.relation,
  4820. relDef.foreignKey,
  4821. relDef.discriminator,
  4822. inclusion.scope
  4823. )
  4824. );
  4825. } else {
  4826. promises.push(
  4827. this.getService(BelongsToResolver).includeTo(
  4828. entities,
  4829. modelName,
  4830. relDef.model,
  4831. inclusion.relation,
  4832. relDef.foreignKey,
  4833. inclusion.scope
  4834. )
  4835. );
  4836. }
  4837. break;
  4838. // HAS_ONE
  4839. case RelationType.HAS_ONE:
  4840. if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
  4841. promises.push(
  4842. this.getService(HasOneResolver).includePolymorphicByRelationName(
  4843. entities,
  4844. modelName,
  4845. relDef.model,
  4846. inclusion.relation,
  4847. relDef.polymorphic,
  4848. inclusion.scope
  4849. )
  4850. );
  4851. } else if (relDef.polymorphic) {
  4852. promises.push(
  4853. this.getService(HasOneResolver).includePolymorphicTo(
  4854. entities,
  4855. modelName,
  4856. relDef.model,
  4857. inclusion.relation,
  4858. relDef.foreignKey,
  4859. relDef.discriminator,
  4860. inclusion.scope
  4861. )
  4862. );
  4863. } else {
  4864. promises.push(
  4865. this.getService(HasOneResolver).includeTo(
  4866. entities,
  4867. modelName,
  4868. relDef.model,
  4869. inclusion.relation,
  4870. relDef.foreignKey,
  4871. inclusion.scope
  4872. )
  4873. );
  4874. }
  4875. break;
  4876. // HAS_MANY
  4877. case RelationType.HAS_MANY:
  4878. if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
  4879. promises.push(
  4880. this.getService(HasManyResolver).includePolymorphicByRelationName(
  4881. entities,
  4882. modelName,
  4883. relDef.model,
  4884. inclusion.relation,
  4885. relDef.polymorphic,
  4886. inclusion.scope
  4887. )
  4888. );
  4889. } else if (relDef.polymorphic) {
  4890. promises.push(
  4891. this.getService(HasManyResolver).includePolymorphicTo(
  4892. entities,
  4893. modelName,
  4894. relDef.model,
  4895. inclusion.relation,
  4896. relDef.foreignKey,
  4897. relDef.discriminator,
  4898. inclusion.scope
  4899. )
  4900. );
  4901. } else {
  4902. promises.push(
  4903. this.getService(HasManyResolver).includeTo(
  4904. entities,
  4905. modelName,
  4906. relDef.model,
  4907. inclusion.relation,
  4908. relDef.foreignKey,
  4909. inclusion.scope
  4910. )
  4911. );
  4912. }
  4913. break;
  4914. case RelationType.REFERENCES_MANY:
  4915. promises.push(
  4916. this.getService(ReferencesManyResolver).includeTo(
  4917. entities,
  4918. modelName,
  4919. relDef.model,
  4920. inclusion.relation,
  4921. relDef.foreignKey,
  4922. inclusion.scope
  4923. )
  4924. );
  4925. break;
  4926. default:
  4927. throw new InvalidArgumentError2(
  4928. "The relation type %v does not have an inclusion resolver.",
  4929. relDef.type
  4930. );
  4931. }
  4932. });
  4933. await Promise.all(promises);
  4934. }
  4935. /**
  4936. * Validate include clause.
  4937. *
  4938. * @param {IncludeClause|undefined} clause
  4939. */
  4940. static validateIncludeClause(clause) {
  4941. if (clause == null) {
  4942. } else if (clause && typeof clause === "string") {
  4943. } else if (Array.isArray(clause)) {
  4944. const relNames = [];
  4945. clause.flat(Infinity).forEach((el) => {
  4946. this.validateIncludeClause(el);
  4947. if (typeof el === "string") {
  4948. relNames.push(el);
  4949. } else if (typeof el === "object") {
  4950. Object.keys(el).forEach((key) => {
  4951. if (Object.prototype.hasOwnProperty.call(el, key))
  4952. relNames.push(key);
  4953. });
  4954. }
  4955. });
  4956. const duplicateNames = relNames.filter(
  4957. (name, i) => relNames.indexOf(name) !== i
  4958. );
  4959. if (duplicateNames.length)
  4960. throw new InvalidArgumentError2(
  4961. 'The provided option "include" has duplicates of %v.',
  4962. duplicateNames[0]
  4963. );
  4964. } else if (typeof clause === "object") {
  4965. if ("relation" in clause) {
  4966. if (!clause.relation || typeof clause.relation !== "string")
  4967. throw new InvalidArgumentError2(
  4968. 'The provided option "relation" should be a non-empty String, but %v given.',
  4969. clause.relation
  4970. );
  4971. if ("scope" in clause && clause) this.validateScopeClause(clause.scope);
  4972. } else {
  4973. Object.keys(clause).forEach((key) => {
  4974. if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
  4975. this.validateIncludeClause(key);
  4976. this.validateIncludeClause(clause[key]);
  4977. });
  4978. }
  4979. } else {
  4980. throw new InvalidArgumentError2(
  4981. 'The provided option "include" should have a non-empty String, an Object or an Array, but %v given.',
  4982. clause
  4983. );
  4984. }
  4985. }
  4986. /**
  4987. * Validate scope clause.
  4988. *
  4989. * @param {object|undefined} clause
  4990. */
  4991. static validateScopeClause(clause) {
  4992. if (clause == null) return;
  4993. if (typeof clause !== "object" || Array.isArray(clause))
  4994. throw new InvalidArgumentError2(
  4995. 'The provided option "scope" should be an Object, but %v given.',
  4996. clause
  4997. );
  4998. if (clause.where != null) {
  4999. WhereClauseTool.validateWhereClause(clause.where);
  5000. }
  5001. if (clause.order != null) {
  5002. OrderClauseTool.validateOrderClause(clause.order);
  5003. }
  5004. if (clause.skip != null) {
  5005. SliceClauseTool.validateSkipClause(clause.skip);
  5006. }
  5007. if (clause.limit != null) {
  5008. SliceClauseTool.validateLimitClause(clause.limit);
  5009. }
  5010. if (clause.fields != null) {
  5011. FieldsClauseTool.validateFieldsClause(clause.fields);
  5012. }
  5013. if (clause.include != null) {
  5014. _IncludeClauseTool.validateIncludeClause(clause.include);
  5015. }
  5016. }
  5017. /**
  5018. * Normalize include clause.
  5019. *
  5020. * @param {IncludeClause|undefined} clause
  5021. * @returns {object[]}
  5022. */
  5023. static normalizeIncludeClause(clause) {
  5024. let result = [];
  5025. if (clause == null) {
  5026. return result;
  5027. } else if (clause && typeof clause === "string") {
  5028. result.push({ relation: clause });
  5029. } else if (Array.isArray(clause)) {
  5030. clause.flat(Infinity).forEach((el) => {
  5031. el = this.normalizeIncludeClause(el);
  5032. result = [...result, ...el];
  5033. });
  5034. const relNames = result.map((v) => v.relation);
  5035. const duplicateNames = relNames.filter(
  5036. (name, i) => relNames.indexOf(name) !== i
  5037. );
  5038. if (duplicateNames.length)
  5039. throw new InvalidArgumentError2(
  5040. 'The provided option "include" has duplicates of %v.',
  5041. duplicateNames[0]
  5042. );
  5043. } else if (typeof clause === "object") {
  5044. if ("relation" in clause) {
  5045. if (!clause.relation || typeof clause.relation !== "string")
  5046. throw new InvalidArgumentError2(
  5047. 'The provided option "relation" should be a non-empty String, but %v given.',
  5048. clause.relation
  5049. );
  5050. const normalized = { relation: clause.relation };
  5051. const scope = this.normalizeScopeClause(clause.scope);
  5052. if (scope) normalized.scope = scope;
  5053. result.push(normalized);
  5054. } else {
  5055. Object.keys(clause).forEach((key) => {
  5056. if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
  5057. this.validateIncludeClause(key);
  5058. const normalized = { relation: key };
  5059. const include = this.normalizeIncludeClause(clause[key]);
  5060. if (include.length) normalized.scope = { include };
  5061. result.push(normalized);
  5062. });
  5063. }
  5064. } else {
  5065. throw new InvalidArgumentError2(
  5066. 'The provided option "include" should have a non-empty String, an Object or an Array, but %v given.',
  5067. clause
  5068. );
  5069. }
  5070. return result;
  5071. }
  5072. /**
  5073. * Normalize scope clause.
  5074. *
  5075. * @param {object|undefined} clause
  5076. * @returns {object|undefined}
  5077. */
  5078. static normalizeScopeClause(clause) {
  5079. if (clause == null) return;
  5080. if (typeof clause !== "object" || Array.isArray(clause))
  5081. throw new InvalidArgumentError2(
  5082. 'The provided option "scope" should be an Object, but %v given.',
  5083. clause
  5084. );
  5085. const result = {};
  5086. if (clause.where != null) {
  5087. WhereClauseTool.validateWhereClause(clause.where);
  5088. result.where = clause.where;
  5089. }
  5090. if (clause.order != null) {
  5091. OrderClauseTool.validateOrderClause(clause.order);
  5092. result.order = clause.order;
  5093. }
  5094. if (clause.skip != null) {
  5095. SliceClauseTool.validateSkipClause(clause.skip);
  5096. result.skip = clause.skip;
  5097. }
  5098. if (clause.limit != null) {
  5099. SliceClauseTool.validateLimitClause(clause.limit);
  5100. result.limit = clause.limit;
  5101. }
  5102. if (clause.fields != null) {
  5103. FieldsClauseTool.validateFieldsClause(clause.fields);
  5104. result.fields = clause.fields;
  5105. }
  5106. if (clause.include != null) {
  5107. result.include = this.normalizeIncludeClause(clause.include);
  5108. }
  5109. if (Object.keys(result).length) return result;
  5110. return void 0;
  5111. }
  5112. };
  5113. __name(_IncludeClauseTool, "IncludeClauseTool");
  5114. IncludeClauseTool = _IncludeClauseTool;
  5115. }
  5116. });
  5117. // src/filter/index.js
  5118. var init_filter = __esm({
  5119. "src/filter/index.js"() {
  5120. "use strict";
  5121. init_slice_clause_tool();
  5122. init_order_clause_tool();
  5123. init_where_clause_tool();
  5124. init_fields_clause_tool();
  5125. init_include_clause_tool();
  5126. init_operator_clause_tool();
  5127. }
  5128. });
  5129. // src/adapter/decorator/inclusion-decorator.js
  5130. var _InclusionDecorator, InclusionDecorator;
  5131. var init_inclusion_decorator = __esm({
  5132. "src/adapter/decorator/inclusion-decorator.js"() {
  5133. "use strict";
  5134. init_adapter();
  5135. init_src2();
  5136. init_filter();
  5137. init_errors2();
  5138. _InclusionDecorator = class _InclusionDecorator extends Service {
  5139. /**
  5140. * Decorate.
  5141. *
  5142. * @param {Adapter} adapter
  5143. */
  5144. decorate(adapter) {
  5145. if (!adapter || !(adapter instanceof Adapter))
  5146. throw new InvalidArgumentError2(
  5147. "The first argument of InclusionDecorator.decorate should be an Adapter instance, but %v given.",
  5148. adapter
  5149. );
  5150. const tool = adapter.getService(IncludeClauseTool);
  5151. const includeTo = /* @__PURE__ */ __name((...args) => tool.includeTo(...args), "includeTo");
  5152. const create = adapter.create;
  5153. adapter.create = async function(modelName, modelData, filter) {
  5154. const retvalData = await create.call(this, modelName, modelData, filter);
  5155. if (filter && typeof filter === "object" && filter.include)
  5156. await includeTo([retvalData], modelName, filter.include);
  5157. return retvalData;
  5158. };
  5159. const replaceById = adapter.replaceById;
  5160. adapter.replaceById = async function(modelName, id, modelData, filter) {
  5161. const retvalData = await replaceById.call(
  5162. this,
  5163. modelName,
  5164. id,
  5165. modelData,
  5166. filter
  5167. );
  5168. if (filter && typeof filter === "object" && filter.include)
  5169. await includeTo([retvalData], modelName, filter.include);
  5170. return retvalData;
  5171. };
  5172. const replaceOrCreate = adapter.replaceOrCreate;
  5173. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  5174. const retvalData = await replaceOrCreate.call(
  5175. this,
  5176. modelName,
  5177. modelData,
  5178. filter
  5179. );
  5180. if (filter && typeof filter === "object" && filter.include)
  5181. await includeTo([retvalData], modelName, filter.include);
  5182. return retvalData;
  5183. };
  5184. const patchById = adapter.patchById;
  5185. adapter.patchById = async function(modelName, id, modelData, filter) {
  5186. const retvalData = await patchById.call(
  5187. this,
  5188. modelName,
  5189. id,
  5190. modelData,
  5191. filter
  5192. );
  5193. if (filter && typeof filter === "object" && filter.include)
  5194. await includeTo([retvalData], modelName, filter.include);
  5195. return retvalData;
  5196. };
  5197. const find = adapter.find;
  5198. adapter.find = async function(modelName, filter) {
  5199. const modelItems = await find.call(this, modelName, filter);
  5200. if (filter && typeof filter === "object" && filter.include)
  5201. await includeTo(modelItems, modelName, filter.include);
  5202. return modelItems;
  5203. };
  5204. const findById = adapter.findById;
  5205. adapter.findById = async function(modelName, id, filter) {
  5206. const retvalData = await findById.call(this, modelName, id, filter);
  5207. if (filter && typeof filter === "object" && filter.include)
  5208. await includeTo([retvalData], modelName, filter.include);
  5209. return retvalData;
  5210. };
  5211. }
  5212. };
  5213. __name(_InclusionDecorator, "InclusionDecorator");
  5214. InclusionDecorator = _InclusionDecorator;
  5215. }
  5216. });
  5217. // src/adapter/decorator/default-values-decorator.js
  5218. var _DefaultValuesDecorator, DefaultValuesDecorator;
  5219. var init_default_values_decorator = __esm({
  5220. "src/adapter/decorator/default-values-decorator.js"() {
  5221. "use strict";
  5222. init_adapter();
  5223. init_src2();
  5224. init_errors2();
  5225. init_definition();
  5226. _DefaultValuesDecorator = class _DefaultValuesDecorator extends Service {
  5227. /**
  5228. * Decorate.
  5229. *
  5230. * @param {Adapter} adapter
  5231. */
  5232. decorate(adapter) {
  5233. if (!adapter || !(adapter instanceof Adapter))
  5234. throw new InvalidArgumentError2(
  5235. "The first argument of DefaultValuesDecorator.decorate should be an Adapter instance, but %v given.",
  5236. adapter
  5237. );
  5238. const utils = adapter.getService(ModelDefinitionUtils);
  5239. const setDefaults = /* @__PURE__ */ __name((...args) => utils.setDefaultValuesToEmptyProperties(...args), "setDefaults");
  5240. const create = adapter.create;
  5241. adapter.create = function(modelName, modelData, filter) {
  5242. modelData = setDefaults(modelName, modelData);
  5243. return create.call(this, modelName, modelData, filter);
  5244. };
  5245. const replaceById = adapter.replaceById;
  5246. adapter.replaceById = function(modelName, id, modelData, filter) {
  5247. modelData = setDefaults(modelName, modelData);
  5248. return replaceById.call(this, modelName, id, modelData, filter);
  5249. };
  5250. const replaceOrCreate = adapter.replaceOrCreate;
  5251. adapter.replaceOrCreate = function(modelName, modelData, filter) {
  5252. modelData = setDefaults(modelName, modelData);
  5253. return replaceOrCreate.call(this, modelName, modelData, filter);
  5254. };
  5255. const patch = adapter.patch;
  5256. adapter.patch = function(modelName, modelData, where) {
  5257. modelData = setDefaults(modelName, modelData, true);
  5258. return patch.call(this, modelName, modelData, where);
  5259. };
  5260. const patchById = adapter.patchById;
  5261. adapter.patchById = function(modelName, id, modelData, filter) {
  5262. modelData = setDefaults(modelName, modelData, true);
  5263. return patchById.call(this, modelName, id, modelData, filter);
  5264. };
  5265. const find = adapter.find;
  5266. adapter.find = async function(modelName, filter) {
  5267. const modelItems = await find.call(this, modelName, filter);
  5268. return modelItems.map((modelItem) => setDefaults(modelName, modelItem));
  5269. };
  5270. const findById = adapter.findById;
  5271. adapter.findById = async function(modelName, id, filter) {
  5272. const retvalData = await findById.call(this, modelName, id, filter);
  5273. return setDefaults(modelName, retvalData);
  5274. };
  5275. }
  5276. };
  5277. __name(_DefaultValuesDecorator, "DefaultValuesDecorator");
  5278. DefaultValuesDecorator = _DefaultValuesDecorator;
  5279. }
  5280. });
  5281. // src/adapter/decorator/data-sanitizing-decorator.js
  5282. var _DataSanitizingDecorator, DataSanitizingDecorator;
  5283. var init_data_sanitizing_decorator = __esm({
  5284. "src/adapter/decorator/data-sanitizing-decorator.js"() {
  5285. "use strict";
  5286. init_adapter();
  5287. init_src2();
  5288. init_errors2();
  5289. init_definition();
  5290. _DataSanitizingDecorator = class _DataSanitizingDecorator extends Service {
  5291. /**
  5292. * Decorate.
  5293. *
  5294. * @param {Adapter} adapter
  5295. */
  5296. decorate(adapter) {
  5297. if (!adapter || !(adapter instanceof Adapter))
  5298. throw new InvalidArgumentError2(
  5299. "The first argument of DataSanitizingDecorator.decorate should be an Adapter instance, but %v given.",
  5300. adapter
  5301. );
  5302. const sanitizer = adapter.getService(ModelDataSanitizer);
  5303. const sanitize = /* @__PURE__ */ __name((...args) => sanitizer.sanitize(...args), "sanitize");
  5304. const create = adapter.create;
  5305. adapter.create = async function(modelName, modelData, filter) {
  5306. modelData = sanitize(modelName, modelData);
  5307. return create.call(this, modelName, modelData, filter);
  5308. };
  5309. const replaceById = adapter.replaceById;
  5310. adapter.replaceById = async function(modelName, id, modelData, filter) {
  5311. modelData = sanitize(modelName, modelData);
  5312. return replaceById.call(this, modelName, id, modelData, filter);
  5313. };
  5314. const replaceOrCreate = adapter.replaceOrCreate;
  5315. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  5316. modelData = sanitize(modelName, modelData);
  5317. return replaceOrCreate.call(this, modelName, modelData, filter);
  5318. };
  5319. const patch = adapter.patch;
  5320. adapter.patch = async function(modelName, modelData, where) {
  5321. modelData = sanitize(modelName, modelData);
  5322. return patch.call(this, modelName, modelData, where);
  5323. };
  5324. const patchById = adapter.patchById;
  5325. adapter.patchById = async function(modelName, id, modelData, filter) {
  5326. modelData = sanitize(modelName, modelData);
  5327. return patchById.call(this, modelName, id, modelData, filter);
  5328. };
  5329. }
  5330. };
  5331. __name(_DataSanitizingDecorator, "DataSanitizingDecorator");
  5332. DataSanitizingDecorator = _DataSanitizingDecorator;
  5333. }
  5334. });
  5335. // src/adapter/decorator/data-validation-decorator.js
  5336. var _DataValidationDecorator, DataValidationDecorator;
  5337. var init_data_validation_decorator = __esm({
  5338. "src/adapter/decorator/data-validation-decorator.js"() {
  5339. "use strict";
  5340. init_adapter();
  5341. init_src2();
  5342. init_errors2();
  5343. init_definition();
  5344. _DataValidationDecorator = class _DataValidationDecorator extends Service {
  5345. /**
  5346. * Decorate.
  5347. *
  5348. * @param {Adapter} adapter
  5349. */
  5350. decorate(adapter) {
  5351. if (!adapter || !(adapter instanceof Adapter))
  5352. throw new InvalidArgumentError2(
  5353. "The first argument of DataValidationDecorator.decorate should be an Adapter instance, but %v given.",
  5354. adapter
  5355. );
  5356. const validator = this.getService(ModelDataValidator);
  5357. const create = adapter.create;
  5358. adapter.create = function(modelName, modelData, filter) {
  5359. validator.validate(modelName, modelData);
  5360. return create.call(this, modelName, modelData, filter);
  5361. };
  5362. const replaceById = adapter.replaceById;
  5363. adapter.replaceById = function(modelName, id, modelData, filter) {
  5364. validator.validate(modelName, modelData);
  5365. return replaceById.call(this, modelName, id, modelData, filter);
  5366. };
  5367. const replaceOrCreate = adapter.replaceOrCreate;
  5368. adapter.replaceOrCreate = function(modelName, modelData, filter) {
  5369. validator.validate(modelName, modelData);
  5370. return replaceOrCreate.call(this, modelName, modelData, filter);
  5371. };
  5372. const patch = adapter.patch;
  5373. adapter.patch = function(modelName, modelData, where) {
  5374. validator.validate(modelName, modelData, true);
  5375. return patch.call(this, modelName, modelData, where);
  5376. };
  5377. const patchById = adapter.patchById;
  5378. adapter.patchById = function(modelName, id, modelData, filter) {
  5379. validator.validate(modelName, modelData, true);
  5380. return patchById.call(this, modelName, id, modelData, filter);
  5381. };
  5382. }
  5383. };
  5384. __name(_DataValidationDecorator, "DataValidationDecorator");
  5385. DataValidationDecorator = _DataValidationDecorator;
  5386. }
  5387. });
  5388. // src/adapter/decorator/fields-filtering-decorator.js
  5389. var _FieldsFilteringDecorator, FieldsFilteringDecorator;
  5390. var init_fields_filtering_decorator = __esm({
  5391. "src/adapter/decorator/fields-filtering-decorator.js"() {
  5392. "use strict";
  5393. init_adapter();
  5394. init_src2();
  5395. init_filter();
  5396. init_errors2();
  5397. _FieldsFilteringDecorator = class _FieldsFilteringDecorator extends Service {
  5398. /**
  5399. * Decorate.
  5400. *
  5401. * @param {Adapter} adapter
  5402. */
  5403. decorate(adapter) {
  5404. if (!adapter || !(adapter instanceof Adapter))
  5405. throw new InvalidArgumentError2(
  5406. "The first argument of FieldsFilteringDecorator.decorate should be an Adapter instance, but %v given.",
  5407. adapter
  5408. );
  5409. const tool = adapter.getService(FieldsClauseTool);
  5410. const selectFields = /* @__PURE__ */ __name((...args) => tool.filter(...args), "selectFields");
  5411. const create = adapter.create;
  5412. adapter.create = async function(modelName, modelData, filter) {
  5413. let result = await create.call(this, modelName, modelData, filter);
  5414. if (filter && typeof filter === "object" && filter.fields)
  5415. result = selectFields(result, modelName, filter.fields);
  5416. return result;
  5417. };
  5418. const replaceById = adapter.replaceById;
  5419. adapter.replaceById = async function(modelName, id, modelData, filter) {
  5420. let result = await replaceById.call(
  5421. this,
  5422. modelName,
  5423. id,
  5424. modelData,
  5425. filter
  5426. );
  5427. if (filter && typeof filter === "object" && filter.fields)
  5428. result = selectFields(result, modelName, filter.fields);
  5429. return result;
  5430. };
  5431. const replaceOrCreate = adapter.replaceOrCreate;
  5432. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  5433. let result = await replaceOrCreate.call(
  5434. this,
  5435. modelName,
  5436. modelData,
  5437. filter
  5438. );
  5439. if (filter && typeof filter === "object" && filter.fields)
  5440. result = selectFields(result, modelName, filter.fields);
  5441. return result;
  5442. };
  5443. const patchById = adapter.patchById;
  5444. adapter.patchById = async function(modelName, id, modelData, filter) {
  5445. let result = await patchById.call(this, modelName, id, modelData, filter);
  5446. if (filter && typeof filter === "object" && filter.fields)
  5447. result = selectFields(result, modelName, filter.fields);
  5448. return result;
  5449. };
  5450. const find = adapter.find;
  5451. adapter.find = async function(modelName, filter) {
  5452. let result = await find.call(this, modelName, filter);
  5453. if (filter && typeof filter === "object" && filter.fields)
  5454. result = selectFields(result, modelName, filter.fields);
  5455. return result;
  5456. };
  5457. const findById = adapter.findById;
  5458. adapter.findById = async function(modelName, id, filter) {
  5459. let result = await findById.call(this, modelName, id, filter);
  5460. if (filter && typeof filter === "object" && filter.fields)
  5461. result = selectFields(result, modelName, filter.fields);
  5462. return result;
  5463. };
  5464. }
  5465. };
  5466. __name(_FieldsFilteringDecorator, "FieldsFilteringDecorator");
  5467. FieldsFilteringDecorator = _FieldsFilteringDecorator;
  5468. }
  5469. });
  5470. // src/adapter/decorator/data-transformation-decorator.js
  5471. var _DataTransformationDecorator, DataTransformationDecorator;
  5472. var init_data_transformation_decorator = __esm({
  5473. "src/adapter/decorator/data-transformation-decorator.js"() {
  5474. "use strict";
  5475. init_adapter();
  5476. init_src2();
  5477. init_errors2();
  5478. init_definition();
  5479. _DataTransformationDecorator = class _DataTransformationDecorator extends Service {
  5480. /**
  5481. * Decorate.
  5482. *
  5483. * @param {Adapter} adapter
  5484. */
  5485. decorate(adapter) {
  5486. if (!adapter || !(adapter instanceof Adapter))
  5487. throw new InvalidArgumentError2(
  5488. "The first argument of DataTransformerDecorator.decorate should be an Adapter instance, but %v given.",
  5489. adapter
  5490. );
  5491. const transformer = this.getService(ModelDataTransformer);
  5492. const create = adapter.create;
  5493. adapter.create = async function(modelName, modelData, filter) {
  5494. modelData = await transformer.transform(modelName, modelData);
  5495. return create.call(this, modelName, modelData, filter);
  5496. };
  5497. const replaceById = adapter.replaceById;
  5498. adapter.replaceById = async function(modelName, id, modelData, filter) {
  5499. modelData = await transformer.transform(modelName, modelData);
  5500. return replaceById.call(this, modelName, id, modelData, filter);
  5501. };
  5502. const replaceOrCreate = adapter.replaceOrCreate;
  5503. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  5504. modelData = await transformer.transform(modelName, modelData);
  5505. return replaceOrCreate.call(this, modelName, modelData, filter);
  5506. };
  5507. const patch = adapter.patch;
  5508. adapter.patch = async function(modelName, modelData, where) {
  5509. modelData = await transformer.transform(modelName, modelData, true);
  5510. return patch.call(this, modelName, modelData, where);
  5511. };
  5512. const patchById = adapter.patchById;
  5513. adapter.patchById = async function(modelName, id, modelData, filter) {
  5514. modelData = await transformer.transform(modelName, modelData, true);
  5515. return patchById.call(this, modelName, id, modelData, filter);
  5516. };
  5517. }
  5518. };
  5519. __name(_DataTransformationDecorator, "DataTransformationDecorator");
  5520. DataTransformationDecorator = _DataTransformationDecorator;
  5521. }
  5522. });
  5523. // src/adapter/decorator/property-uniqueness-decorator.js
  5524. var _PropertyUniquenessDecorator, PropertyUniquenessDecorator;
  5525. var init_property_uniqueness_decorator = __esm({
  5526. "src/adapter/decorator/property-uniqueness-decorator.js"() {
  5527. "use strict";
  5528. init_adapter();
  5529. init_src2();
  5530. init_errors2();
  5531. init_definition();
  5532. _PropertyUniquenessDecorator = class _PropertyUniquenessDecorator extends Service {
  5533. /**
  5534. * Decorate.
  5535. *
  5536. * @param {Adapter} adapter
  5537. */
  5538. decorate(adapter) {
  5539. if (!adapter || !(adapter instanceof Adapter))
  5540. throw new InvalidArgumentError2(
  5541. "The first argument of PropertyUniquenessDecorator.decorate should be an Adapter instance, but %v given.",
  5542. adapter
  5543. );
  5544. const validator = this.getService(PropertyUniquenessValidator);
  5545. const create = adapter.create;
  5546. adapter.create = async function(modelName, modelData, filter) {
  5547. const countMethod = adapter.count.bind(adapter, modelName);
  5548. await validator.validate(countMethod, "create", modelName, modelData);
  5549. return create.call(this, modelName, modelData, filter);
  5550. };
  5551. const replaceById = adapter.replaceById;
  5552. adapter.replaceById = async function(modelName, id, modelData, filter) {
  5553. const countMethod = adapter.count.bind(adapter, modelName);
  5554. await validator.validate(
  5555. countMethod,
  5556. "replaceById",
  5557. modelName,
  5558. modelData,
  5559. id
  5560. );
  5561. return replaceById.call(this, modelName, id, modelData, filter);
  5562. };
  5563. const replaceOrCreate = adapter.replaceOrCreate;
  5564. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  5565. const countMethod = adapter.count.bind(adapter, modelName);
  5566. await validator.validate(
  5567. countMethod,
  5568. "replaceOrCreate",
  5569. modelName,
  5570. modelData
  5571. );
  5572. return replaceOrCreate.call(this, modelName, modelData, filter);
  5573. };
  5574. const patch = adapter.patch;
  5575. adapter.patch = async function(modelName, modelData, where) {
  5576. const countMethod = adapter.count.bind(adapter, modelName);
  5577. await validator.validate(countMethod, "patch", modelName, modelData);
  5578. return patch.call(this, modelName, modelData, where);
  5579. };
  5580. const patchById = adapter.patchById;
  5581. adapter.patchById = async function(modelName, id, modelData, filter) {
  5582. const countMethod = adapter.count.bind(adapter, modelName);
  5583. await validator.validate(
  5584. countMethod,
  5585. "patchById",
  5586. modelName,
  5587. modelData,
  5588. id
  5589. );
  5590. return patchById.call(this, modelName, id, modelData, filter);
  5591. };
  5592. }
  5593. };
  5594. __name(_PropertyUniquenessDecorator, "PropertyUniquenessDecorator");
  5595. PropertyUniquenessDecorator = _PropertyUniquenessDecorator;
  5596. }
  5597. });
  5598. // src/adapter/decorator/index.js
  5599. var init_decorator = __esm({
  5600. "src/adapter/decorator/index.js"() {
  5601. "use strict";
  5602. init_inclusion_decorator();
  5603. init_default_values_decorator();
  5604. init_data_sanitizing_decorator();
  5605. init_data_validation_decorator();
  5606. init_fields_filtering_decorator();
  5607. init_data_transformation_decorator();
  5608. init_property_uniqueness_decorator();
  5609. }
  5610. });
  5611. // src/adapter/adapter.js
  5612. var _Adapter, Adapter;
  5613. var init_adapter = __esm({
  5614. "src/adapter/adapter.js"() {
  5615. "use strict";
  5616. init_src2();
  5617. init_errors2();
  5618. init_decorator();
  5619. init_decorator();
  5620. init_decorator();
  5621. init_decorator();
  5622. init_decorator();
  5623. init_decorator();
  5624. init_decorator();
  5625. _Adapter = class _Adapter extends Service {
  5626. /**
  5627. * Class name.
  5628. *
  5629. * @type {string}
  5630. */
  5631. get className() {
  5632. return _Adapter.name;
  5633. }
  5634. /**
  5635. * Settings.
  5636. *
  5637. * @type {object|undefined}
  5638. */
  5639. _settings;
  5640. /**
  5641. * Settings.
  5642. *
  5643. * @returns {object|undefined}
  5644. */
  5645. get settings() {
  5646. return this._settings;
  5647. }
  5648. /**
  5649. * Constructor.
  5650. *
  5651. * @param {object|undefined} container
  5652. * @param {object|undefined} settings
  5653. */
  5654. constructor(container = void 0, settings = void 0) {
  5655. super(container);
  5656. this._settings = settings;
  5657. if (this.constructor !== _Adapter) {
  5658. this.getService(DataSanitizingDecorator).decorate(this);
  5659. this.getService(DefaultValuesDecorator).decorate(this);
  5660. this.getService(DataTransformationDecorator).decorate(this);
  5661. this.getService(DataValidationDecorator).decorate(this);
  5662. this.getService(PropertyUniquenessDecorator).decorate(this);
  5663. this.getService(FieldsFilteringDecorator).decorate(this);
  5664. this.getService(InclusionDecorator).decorate(this);
  5665. }
  5666. }
  5667. /**
  5668. * Create.
  5669. *
  5670. * @param {string} modelName
  5671. * @param {object} modelData
  5672. * @param {object|undefined} filter
  5673. * @returns {Promise<object>}
  5674. */
  5675. create(modelName, modelData, filter = void 0) {
  5676. throw new NotImplementedError(
  5677. "%s.create is not implemented.",
  5678. this.constructor.name
  5679. );
  5680. }
  5681. /**
  5682. * Replace by id.
  5683. *
  5684. * @param {string} modelName
  5685. * @param {number|string} id
  5686. * @param {object} modelData
  5687. * @param {object|undefined} filter
  5688. * @returns {Promise<object>}
  5689. */
  5690. replaceById(modelName, id, modelData, filter = void 0) {
  5691. throw new NotImplementedError(
  5692. "%s.replaceById is not implemented.",
  5693. this.constructor.name
  5694. );
  5695. }
  5696. /**
  5697. * Replace or create.
  5698. *
  5699. * @param {string} modelName
  5700. * @param {object} modelData
  5701. * @param {object|undefined} filter
  5702. * @returns {Promise<object>}
  5703. */
  5704. replaceOrCreate(modelName, modelData, filter = void 0) {
  5705. throw new NotImplementedError(
  5706. "%s.replaceOrCreate is not implemented.",
  5707. this.constructor.name
  5708. );
  5709. }
  5710. /**
  5711. * Patch.
  5712. *
  5713. * @param {string} modelName
  5714. * @param {object} modelData
  5715. * @param {object|undefined} where
  5716. * @returns {Promise<number>}
  5717. */
  5718. patch(modelName, modelData, where = void 0) {
  5719. throw new NotImplementedError(
  5720. "%s.patch is not implemented.",
  5721. this.constructor.name
  5722. );
  5723. }
  5724. /**
  5725. * Patch by id.
  5726. *
  5727. * @param {string} modelName
  5728. * @param {number|string} id
  5729. * @param {object} modelData
  5730. * @param {object|undefined} filter
  5731. * @returns {Promise<object>}
  5732. */
  5733. patchById(modelName, id, modelData, filter = void 0) {
  5734. throw new NotImplementedError(
  5735. "%s.patchById is not implemented.",
  5736. this.constructor.name
  5737. );
  5738. }
  5739. /**
  5740. * Find.
  5741. *
  5742. * @param {string} modelName
  5743. * @param {object|undefined} filter
  5744. * @returns {Promise<object[]>}
  5745. */
  5746. find(modelName, filter = void 0) {
  5747. throw new NotImplementedError(
  5748. "%s.find is not implemented.",
  5749. this.constructor.name
  5750. );
  5751. }
  5752. /**
  5753. * Find by id.
  5754. *
  5755. * @param {string} modelName
  5756. * @param {number|string} id
  5757. * @param {object|undefined} filter
  5758. * @returns {Promise<object>}
  5759. */
  5760. findById(modelName, id, filter = void 0) {
  5761. throw new NotImplementedError(
  5762. "%s.findById is not implemented.",
  5763. this.constructor.name
  5764. );
  5765. }
  5766. /**
  5767. * Delete.
  5768. *
  5769. * @param {string} modelName
  5770. * @param {object|undefined} where
  5771. * @returns {Promise<number>}
  5772. */
  5773. delete(modelName, where = void 0) {
  5774. throw new NotImplementedError(
  5775. "%s.delete is not implemented.",
  5776. this.constructor.name
  5777. );
  5778. }
  5779. /**
  5780. * Delete by id.
  5781. *
  5782. * @param {string} modelName
  5783. * @param {number|string} id
  5784. * @returns {Promise<boolean>}
  5785. */
  5786. deleteById(modelName, id) {
  5787. throw new NotImplementedError(
  5788. "%s.deleteById is not implemented.",
  5789. this.constructor.name
  5790. );
  5791. }
  5792. /**
  5793. * Exists.
  5794. *
  5795. * @param {string} modelName
  5796. * @param {number|string} id
  5797. * @returns {Promise<boolean>}
  5798. */
  5799. exists(modelName, id) {
  5800. throw new NotImplementedError(
  5801. "%s.exists is not implemented.",
  5802. this.constructor.name
  5803. );
  5804. }
  5805. /**
  5806. * Count.
  5807. *
  5808. * @param {string} modelName
  5809. * @param {object|undefined} where
  5810. * @returns {Promise<number>}
  5811. */
  5812. count(modelName, where = void 0) {
  5813. throw new NotImplementedError(
  5814. "%s.count is not implemented.",
  5815. this.constructor.name
  5816. );
  5817. }
  5818. };
  5819. __name(_Adapter, "Adapter");
  5820. Adapter = _Adapter;
  5821. }
  5822. });
  5823. // src/adapter/builtin/memory-adapter.js
  5824. var memory_adapter_exports = {};
  5825. __export(memory_adapter_exports, {
  5826. MemoryAdapter: () => MemoryAdapter
  5827. });
  5828. var _MemoryAdapter, MemoryAdapter;
  5829. var init_memory_adapter = __esm({
  5830. "src/adapter/builtin/memory-adapter.js"() {
  5831. "use strict";
  5832. init_adapter();
  5833. init_utils2();
  5834. init_utils2();
  5835. init_definition();
  5836. init_filter();
  5837. init_filter();
  5838. init_filter();
  5839. init_errors2();
  5840. init_definition();
  5841. _MemoryAdapter = class _MemoryAdapter extends Adapter {
  5842. /**
  5843. * Tables.
  5844. *
  5845. * @type {Map<string, Map<number, Record<string, any>>>}
  5846. */
  5847. _tables = /* @__PURE__ */ new Map();
  5848. /**
  5849. * Last ids.
  5850. *
  5851. * @type {Map<string, number>}
  5852. */
  5853. _lastIds = /* @__PURE__ */ new Map();
  5854. /**
  5855. * Get table or create.
  5856. *
  5857. * @param {string} modelName
  5858. * @returns {Map<number, object>}
  5859. */
  5860. _getTableOrCreate(modelName) {
  5861. const tableName = this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
  5862. let table = this._tables.get(tableName);
  5863. if (table) return table;
  5864. table = /* @__PURE__ */ new Map();
  5865. this._tables.set(tableName, table);
  5866. return table;
  5867. }
  5868. /**
  5869. * Gen next id value.
  5870. *
  5871. * @param {string} modelName
  5872. * @param {string} propName
  5873. * @returns {number}
  5874. */
  5875. _genNextIdValue(modelName, propName) {
  5876. var _a;
  5877. const propType = this.getService(
  5878. ModelDefinitionUtils
  5879. ).getDataTypeByPropertyName(modelName, propName);
  5880. if (propType !== DataType.ANY && propType !== DataType.NUMBER)
  5881. throw new InvalidArgumentError2(
  5882. "The memory adapter able to generate only Number identifiers, but the primary key %v of the model %v is defined as %s. Do provide your own value for the %v property, or change the type in the primary key definition to a Number that will be generated automatically.",
  5883. propName,
  5884. modelName,
  5885. capitalize(propType),
  5886. propName
  5887. );
  5888. const tableName = this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
  5889. const lastId = (_a = this._lastIds.get(tableName)) != null ? _a : 0;
  5890. const nextId = lastId + 1;
  5891. this._lastIds.set(tableName, nextId);
  5892. const table = this._getTableOrCreate(modelName);
  5893. const existedIds = Array.from(table.keys());
  5894. if (existedIds.includes(nextId))
  5895. return this._genNextIdValue(modelName, propName);
  5896. return nextId;
  5897. }
  5898. /**
  5899. * Create
  5900. *
  5901. * @param {string} modelName
  5902. * @param {object} modelData
  5903. * @param {object|undefined} filter
  5904. * @returns {Promise<object>}
  5905. */
  5906. // eslint-disable-next-line no-unused-vars
  5907. async create(modelName, modelData, filter = void 0) {
  5908. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  5909. modelName
  5910. );
  5911. let idValue = modelData[pkPropName];
  5912. if (idValue == null || idValue === "" || idValue === 0) {
  5913. idValue = this._genNextIdValue(modelName, pkPropName);
  5914. }
  5915. const table = this._getTableOrCreate(modelName);
  5916. if (table.has(idValue))
  5917. throw new InvalidArgumentError2(
  5918. "The value %v of the primary key %v already exists in the model %v.",
  5919. idValue,
  5920. pkPropName,
  5921. modelName
  5922. );
  5923. modelData = cloneDeep(modelData);
  5924. modelData[pkPropName] = idValue;
  5925. const tableData = this.getService(
  5926. ModelDefinitionUtils
  5927. ).convertPropertyNamesToColumnNames(modelName, modelData);
  5928. table.set(idValue, tableData);
  5929. return this.getService(
  5930. ModelDefinitionUtils
  5931. ).convertColumnNamesToPropertyNames(modelName, tableData);
  5932. }
  5933. /**
  5934. * Replace by id.
  5935. *
  5936. * @param {string} modelName
  5937. * @param {string|number} id
  5938. * @param {object} modelData
  5939. * @param {object|undefined} filter
  5940. * @returns {Promise<object>}
  5941. */
  5942. // eslint-disable-next-line no-unused-vars
  5943. async replaceById(modelName, id, modelData, filter = void 0) {
  5944. const table = this._getTableOrCreate(modelName);
  5945. const isExists = table.has(id);
  5946. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  5947. modelName
  5948. );
  5949. if (!isExists)
  5950. throw new InvalidArgumentError2(
  5951. "The value %v of the primary key %v does not exist in the model %v.",
  5952. id,
  5953. pkPropName,
  5954. modelName
  5955. );
  5956. modelData = cloneDeep(modelData);
  5957. modelData[pkPropName] = id;
  5958. const tableData = this.getService(
  5959. ModelDefinitionUtils
  5960. ).convertPropertyNamesToColumnNames(modelName, modelData);
  5961. table.set(id, tableData);
  5962. return this.getService(
  5963. ModelDefinitionUtils
  5964. ).convertColumnNamesToPropertyNames(modelName, tableData);
  5965. }
  5966. /**
  5967. * Replace or create.
  5968. *
  5969. * @param {string} modelName
  5970. * @param {object} modelData
  5971. * @param {object|undefined} filter
  5972. * @returns {Promise<object>}
  5973. */
  5974. // eslint-disable-next-line no-unused-vars
  5975. async replaceOrCreate(modelName, modelData, filter = void 0) {
  5976. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  5977. modelName
  5978. );
  5979. let idValue = modelData[pkPropName];
  5980. if (idValue == null || idValue === "" || idValue === 0) {
  5981. idValue = this._genNextIdValue(modelName, pkPropName);
  5982. }
  5983. const table = this._getTableOrCreate(modelName);
  5984. modelData = cloneDeep(modelData);
  5985. modelData[pkPropName] = idValue;
  5986. const tableData = this.getService(
  5987. ModelDefinitionUtils
  5988. ).convertPropertyNamesToColumnNames(modelName, modelData);
  5989. table.set(idValue, tableData);
  5990. return this.getService(
  5991. ModelDefinitionUtils
  5992. ).convertColumnNamesToPropertyNames(modelName, tableData);
  5993. }
  5994. /**
  5995. * Patch.
  5996. *
  5997. * @param {string} modelName
  5998. * @param {object} modelData
  5999. * @param {object|undefined} where
  6000. * @returns {Promise<number>}
  6001. */
  6002. async patch(modelName, modelData, where = void 0) {
  6003. const table = this._getTableOrCreate(modelName);
  6004. const tableItems = Array.from(table.values());
  6005. if (!tableItems.length) return 0;
  6006. let modelItems = tableItems.map(
  6007. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  6008. modelName,
  6009. tableItem
  6010. )
  6011. );
  6012. if (where && typeof where === "object")
  6013. modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
  6014. const size = modelItems.length;
  6015. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  6016. modelName
  6017. );
  6018. modelData = cloneDeep(modelData);
  6019. delete modelData[pkPropName];
  6020. modelItems.forEach((existingModelData) => {
  6021. const mergedModelData = Object.assign({}, existingModelData, modelData);
  6022. const mergedTableData = this.getService(
  6023. ModelDefinitionUtils
  6024. ).convertPropertyNamesToColumnNames(modelName, mergedModelData);
  6025. const idValue = existingModelData[pkPropName];
  6026. table.set(idValue, mergedTableData);
  6027. });
  6028. return size;
  6029. }
  6030. /**
  6031. * Patch by id.
  6032. *
  6033. * @param {string} modelName
  6034. * @param {string|number} id
  6035. * @param {object} modelData
  6036. * @param {object|undefined} filter
  6037. * @returns {Promise<object>}
  6038. */
  6039. // eslint-disable-next-line no-unused-vars
  6040. async patchById(modelName, id, modelData, filter = void 0) {
  6041. const table = this._getTableOrCreate(modelName);
  6042. const existingTableData = table.get(id);
  6043. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  6044. modelName
  6045. );
  6046. if (existingTableData == null)
  6047. throw new InvalidArgumentError2(
  6048. "The value %v of the primary key %v does not exist in the model %v.",
  6049. id,
  6050. pkPropName,
  6051. modelName
  6052. );
  6053. modelData = cloneDeep(modelData);
  6054. delete modelData[pkPropName];
  6055. const existingModelData = this.getService(
  6056. ModelDefinitionUtils
  6057. ).convertColumnNamesToPropertyNames(modelName, existingTableData);
  6058. const mergedModelData = Object.assign({}, existingModelData, modelData);
  6059. const mergedTableData = this.getService(
  6060. ModelDefinitionUtils
  6061. ).convertPropertyNamesToColumnNames(modelName, mergedModelData);
  6062. table.set(id, mergedTableData);
  6063. return this.getService(
  6064. ModelDefinitionUtils
  6065. ).convertColumnNamesToPropertyNames(modelName, mergedTableData);
  6066. }
  6067. /**
  6068. * Find.
  6069. *
  6070. * @param {string} modelName
  6071. * @param {object|undefined} filter
  6072. * @returns {Promise<object[]>}
  6073. */
  6074. async find(modelName, filter = void 0) {
  6075. const table = this._getTableOrCreate(modelName);
  6076. const tableItems = Array.from(table.values());
  6077. let modelItems = tableItems.map(
  6078. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  6079. modelName,
  6080. tableItem
  6081. )
  6082. );
  6083. if (filter && typeof filter === "object") {
  6084. if (filter.where)
  6085. modelItems = this.getService(WhereClauseTool).filter(
  6086. modelItems,
  6087. filter.where
  6088. );
  6089. if (filter.skip || filter.limit)
  6090. modelItems = this.getService(SliceClauseTool).slice(
  6091. modelItems,
  6092. filter.skip,
  6093. filter.limit
  6094. );
  6095. if (filter.order)
  6096. this.getService(OrderClauseTool).sort(modelItems, filter.order);
  6097. }
  6098. return modelItems;
  6099. }
  6100. /**
  6101. * Find by id.
  6102. *
  6103. * @param {string} modelName
  6104. * @param {string|number} id
  6105. * @param {object|undefined} filter
  6106. * @returns {Promise<object>}
  6107. */
  6108. // eslint-disable-next-line no-unused-vars
  6109. async findById(modelName, id, filter = void 0) {
  6110. const table = this._getTableOrCreate(modelName);
  6111. const tableData = table.get(id);
  6112. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  6113. modelName
  6114. );
  6115. if (!tableData)
  6116. throw new InvalidArgumentError2(
  6117. "The value %v of the primary key %v does not exist in the model %v.",
  6118. id,
  6119. pkPropName,
  6120. modelName
  6121. );
  6122. return this.getService(
  6123. ModelDefinitionUtils
  6124. ).convertColumnNamesToPropertyNames(modelName, tableData);
  6125. }
  6126. /**
  6127. * Delete.
  6128. *
  6129. * @param {string} modelName
  6130. * @param {object|undefined} where
  6131. * @returns {Promise<number>}
  6132. */
  6133. async delete(modelName, where = void 0) {
  6134. const table = this._getTableOrCreate(modelName);
  6135. const tableItems = Array.from(table.values());
  6136. if (!tableItems.length) return 0;
  6137. let modelItems = tableItems.map(
  6138. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  6139. modelName,
  6140. tableItem
  6141. )
  6142. );
  6143. if (where && typeof where === "object")
  6144. modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
  6145. const size = modelItems.length;
  6146. const idPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  6147. modelName
  6148. );
  6149. modelItems.forEach((modelData) => {
  6150. const idValue = modelData[idPropName];
  6151. table.delete(idValue);
  6152. });
  6153. return size;
  6154. }
  6155. /**
  6156. * Delete by id.
  6157. *
  6158. * @param {string} modelName
  6159. * @param {string|number} id
  6160. * @returns {Promise<boolean>}
  6161. */
  6162. async deleteById(modelName, id) {
  6163. const table = this._getTableOrCreate(modelName);
  6164. const isExists = table.has(id);
  6165. table.delete(id);
  6166. return isExists;
  6167. }
  6168. /**
  6169. * Exists.
  6170. *
  6171. * @param {string} modelName
  6172. * @param {string|number} id
  6173. * @returns {Promise<boolean>}
  6174. */
  6175. async exists(modelName, id) {
  6176. const table = this._getTableOrCreate(modelName);
  6177. return table.has(id);
  6178. }
  6179. /**
  6180. * Count.
  6181. *
  6182. * @param {string} modelName
  6183. * @param {object|undefined} where
  6184. * @returns {Promise<number>}
  6185. */
  6186. async count(modelName, where = void 0) {
  6187. const table = this._getTableOrCreate(modelName);
  6188. const tableItems = Array.from(table.values());
  6189. let modelItems = tableItems.map(
  6190. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  6191. modelName,
  6192. tableItem
  6193. )
  6194. );
  6195. if (where && typeof where === "object")
  6196. modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
  6197. return modelItems.length;
  6198. }
  6199. };
  6200. __name(_MemoryAdapter, "MemoryAdapter");
  6201. MemoryAdapter = _MemoryAdapter;
  6202. }
  6203. });
  6204. // import("./builtin/**/*-adapter.js") in src/adapter/adapter-loader.js
  6205. var globImport_builtin_adapter_js;
  6206. var init_ = __esm({
  6207. 'import("./builtin/**/*-adapter.js") in src/adapter/adapter-loader.js'() {
  6208. globImport_builtin_adapter_js = __glob({
  6209. "./builtin/memory-adapter.js": () => Promise.resolve().then(() => (init_memory_adapter(), memory_adapter_exports))
  6210. });
  6211. }
  6212. });
  6213. // src/adapter/adapter-loader.js
  6214. function findAdapterCtorInModule(module2) {
  6215. let adapterCtor;
  6216. if (!module2 || typeof module2 !== "object" || Array.isArray(module2)) return;
  6217. for (const ctor of Object.values(module2)) {
  6218. console.log(ctor);
  6219. if (typeof ctor === "function" && ctor.prototype && typeof ctor.prototype === "object" && ctor.prototype.className === Adapter.name) {
  6220. adapterCtor = ctor;
  6221. break;
  6222. }
  6223. }
  6224. return adapterCtor;
  6225. }
  6226. var _AdapterLoader, AdapterLoader;
  6227. var init_adapter_loader = __esm({
  6228. "src/adapter/adapter-loader.js"() {
  6229. "use strict";
  6230. init_adapter();
  6231. init_src2();
  6232. init_errors2();
  6233. init_();
  6234. _AdapterLoader = class _AdapterLoader extends Service {
  6235. /**
  6236. * Load by name.
  6237. *
  6238. * @param {string} adapterName
  6239. * @param {object|undefined} settings
  6240. * @returns {Promise<Adapter>}
  6241. */
  6242. async loadByName(adapterName, settings = void 0) {
  6243. if (!adapterName || typeof adapterName !== "string")
  6244. throw new InvalidArgumentError2(
  6245. "The adapter name should be a non-empty String, but %v given.",
  6246. adapterName
  6247. );
  6248. let adapterCtor;
  6249. try {
  6250. const module2 = await globImport_builtin_adapter_js(`./builtin/${adapterName}-adapter.js`);
  6251. adapterCtor = findAdapterCtorInModule(module2);
  6252. } catch (e) {
  6253. }
  6254. if (!adapterCtor)
  6255. try {
  6256. const module2 = await Promise.resolve().then(() => __toESM(require(`@e22m4u/js-repository-${adapterName}-adapter`)));
  6257. adapterCtor = findAdapterCtorInModule(module2);
  6258. } catch (e) {
  6259. }
  6260. if (!adapterCtor)
  6261. throw new InvalidArgumentError2(
  6262. "The adapter %v is not found.",
  6263. adapterName
  6264. );
  6265. return new adapterCtor(this.container, settings);
  6266. }
  6267. };
  6268. __name(_AdapterLoader, "AdapterLoader");
  6269. AdapterLoader = _AdapterLoader;
  6270. __name(findAdapterCtorInModule, "findAdapterCtorInModule");
  6271. }
  6272. });
  6273. // src/adapter/adapter-registry.js
  6274. var _AdapterRegistry, AdapterRegistry;
  6275. var init_adapter_registry = __esm({
  6276. "src/adapter/adapter-registry.js"() {
  6277. "use strict";
  6278. init_adapter();
  6279. init_src2();
  6280. init_adapter_loader();
  6281. init_definition();
  6282. _AdapterRegistry = class _AdapterRegistry extends Service {
  6283. /**
  6284. * Adapters.
  6285. *
  6286. * @type {object}
  6287. */
  6288. _adapters = {};
  6289. /**
  6290. * Get adapter.
  6291. *
  6292. * @param {string} datasourceName
  6293. * @returns {Promise<Adapter>}
  6294. */
  6295. async getAdapter(datasourceName) {
  6296. let adapter = this._adapters[datasourceName];
  6297. if (adapter) return adapter;
  6298. const datasource = this.getService(DefinitionRegistry).getDatasource(datasourceName);
  6299. const adapterName = datasource.adapter;
  6300. adapter = await this.getService(AdapterLoader).loadByName(
  6301. adapterName,
  6302. datasource
  6303. );
  6304. this._adapters[datasourceName] = adapter;
  6305. return adapter;
  6306. }
  6307. };
  6308. __name(_AdapterRegistry, "AdapterRegistry");
  6309. AdapterRegistry = _AdapterRegistry;
  6310. }
  6311. });
  6312. // src/adapter/index.js
  6313. var init_adapter2 = __esm({
  6314. "src/adapter/index.js"() {
  6315. "use strict";
  6316. init_adapter();
  6317. init_adapter_loader();
  6318. init_adapter_registry();
  6319. }
  6320. });
  6321. // src/repository/repository.js
  6322. var _Repository, Repository;
  6323. var init_repository = __esm({
  6324. "src/repository/repository.js"() {
  6325. "use strict";
  6326. init_src2();
  6327. init_adapter2();
  6328. init_adapter2();
  6329. init_errors2();
  6330. init_definition();
  6331. _Repository = class _Repository extends Service {
  6332. /**
  6333. * Model name.
  6334. *
  6335. * @type {string}
  6336. */
  6337. _modelName;
  6338. /**
  6339. * Model name.
  6340. *
  6341. * @returns {string}
  6342. */
  6343. get modelName() {
  6344. return this._modelName;
  6345. }
  6346. /**
  6347. * Datasource name.
  6348. *
  6349. * @type {string}
  6350. */
  6351. _datasourceName;
  6352. /**
  6353. * Datasource name.
  6354. *
  6355. * @returns {string}
  6356. */
  6357. get datasourceName() {
  6358. return this._datasourceName;
  6359. }
  6360. /**
  6361. * Constructor.
  6362. *
  6363. * @typedef {import('@e22m4u/js-service').ServiceContainer} ServiceContainer
  6364. * @param {ServiceContainer} container
  6365. * @param {string} modelName
  6366. */
  6367. constructor(container, modelName) {
  6368. super(container);
  6369. this._modelName = modelName;
  6370. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  6371. const datasourceName = modelDef.datasource;
  6372. if (!datasourceName)
  6373. throw new InvalidArgumentError2(
  6374. "The model %v does not have a specified datasource.",
  6375. modelName
  6376. );
  6377. this._datasourceName = datasourceName;
  6378. }
  6379. /**
  6380. * Get adapter.
  6381. *
  6382. * @returns {Adapter}
  6383. */
  6384. async getAdapter() {
  6385. return this.getService(AdapterRegistry).getAdapter(this.datasourceName);
  6386. }
  6387. /**
  6388. * Create.
  6389. *
  6390. * @param {object} data
  6391. * @param {object|undefined} filter
  6392. * @returns {Promise<object>}
  6393. */
  6394. async create(data, filter = void 0) {
  6395. const adapter = await this.getAdapter();
  6396. return adapter.create(this.modelName, data, filter);
  6397. }
  6398. /**
  6399. * Replace by id.
  6400. *
  6401. * @param {number|string} id
  6402. * @param {object} data
  6403. * @param {object|undefined} filter
  6404. * @returns {Promise<object>}
  6405. */
  6406. async replaceById(id, data, filter = void 0) {
  6407. const adapter = await this.getAdapter();
  6408. return adapter.replaceById(this.modelName, id, data, filter);
  6409. }
  6410. /**
  6411. * Replace or create.
  6412. *
  6413. * @param {object} data
  6414. * @param {object|undefined} filter
  6415. * @returns {Promise<object>}
  6416. */
  6417. async replaceOrCreate(data, filter = void 0) {
  6418. const adapter = await this.getAdapter();
  6419. return adapter.replaceOrCreate(this.modelName, data, filter);
  6420. }
  6421. /**
  6422. * Patch.
  6423. *
  6424. * @param {object} data
  6425. * @param {object|undefined} where
  6426. * @returns {Promise<number>}
  6427. */
  6428. async patch(data, where = void 0) {
  6429. const adapter = await this.getAdapter();
  6430. return adapter.patch(this.modelName, data, where);
  6431. }
  6432. /**
  6433. * Patch by id.
  6434. *
  6435. * @param {number|string} id
  6436. * @param {object} data
  6437. * @param {object|undefined} filter
  6438. * @returns {Promise<object>}
  6439. */
  6440. async patchById(id, data, filter = void 0) {
  6441. const adapter = await this.getAdapter();
  6442. return adapter.patchById(this.modelName, id, data, filter);
  6443. }
  6444. /**
  6445. * Find.
  6446. *
  6447. * @param {object|undefined} filter
  6448. * @returns {Promise<object[]>}
  6449. */
  6450. async find(filter = void 0) {
  6451. const adapter = await this.getAdapter();
  6452. return adapter.find(this.modelName, filter);
  6453. }
  6454. /**
  6455. * Find one.
  6456. *
  6457. * @param {object|undefined} filter
  6458. * @returns {Promise<object|undefined>}
  6459. */
  6460. async findOne(filter = void 0) {
  6461. const adapter = await this.getAdapter();
  6462. filter = filter != null ? filter : {};
  6463. filter.limit = 1;
  6464. const result = await adapter.find(this.modelName, filter);
  6465. return result.length ? result[0] : void 0;
  6466. }
  6467. /**
  6468. * Find by id.
  6469. *
  6470. * @param {number|string} id
  6471. * @param {object|undefined} filter
  6472. * @returns {Promise<object>}
  6473. */
  6474. async findById(id, filter = void 0) {
  6475. const adapter = await this.getAdapter();
  6476. return adapter.findById(this.modelName, id, filter);
  6477. }
  6478. /**
  6479. * Delete.
  6480. *
  6481. * @param {object|undefined} where
  6482. * @returns {Promise<number>}
  6483. */
  6484. async delete(where = void 0) {
  6485. const adapter = await this.getAdapter();
  6486. return adapter.delete(this.modelName, where);
  6487. }
  6488. /**
  6489. * Delete by id.
  6490. *
  6491. * @param {number|string} id
  6492. * @returns {Promise<boolean>}
  6493. */
  6494. async deleteById(id) {
  6495. const adapter = await this.getAdapter();
  6496. return adapter.deleteById(this.modelName, id);
  6497. }
  6498. /**
  6499. * Exists.
  6500. *
  6501. * @param {number|string} id
  6502. * @returns {Promise<boolean>}
  6503. */
  6504. async exists(id) {
  6505. const adapter = await this.getAdapter();
  6506. return adapter.exists(this.modelName, id);
  6507. }
  6508. /**
  6509. * Count.
  6510. *
  6511. * @param {object|undefined} where
  6512. * @returns {Promise<number>}
  6513. */
  6514. async count(where = void 0) {
  6515. const adapter = await this.getAdapter();
  6516. return adapter.count(this.modelName, where);
  6517. }
  6518. };
  6519. __name(_Repository, "Repository");
  6520. Repository = _Repository;
  6521. }
  6522. });
  6523. // src/repository/repository-registry.js
  6524. var _RepositoryRegistry, RepositoryRegistry;
  6525. var init_repository_registry = __esm({
  6526. "src/repository/repository-registry.js"() {
  6527. "use strict";
  6528. init_src2();
  6529. init_repository();
  6530. init_errors2();
  6531. _RepositoryRegistry = class _RepositoryRegistry extends Service {
  6532. /**
  6533. * Repositories.
  6534. *
  6535. * @type {object}
  6536. */
  6537. _repositories = {};
  6538. /**
  6539. * Repository ctor.
  6540. *
  6541. * @type {typeof Repository}
  6542. * @private
  6543. */
  6544. _repositoryCtor = Repository;
  6545. /**
  6546. * Set repository ctor.
  6547. *
  6548. * @param {typeof Repository} ctor
  6549. */
  6550. setRepositoryCtor(ctor) {
  6551. if (!ctor || typeof ctor !== "function" || !(ctor.prototype instanceof Repository)) {
  6552. throw new InvalidArgumentError2(
  6553. "The first argument of RepositoryRegistry.setRepositoryCtor must inherit from Repository class, but %v given.",
  6554. ctor
  6555. );
  6556. }
  6557. this._repositoryCtor = ctor;
  6558. }
  6559. /**
  6560. * Get repository.
  6561. *
  6562. * @param {string} modelName
  6563. * @returns {Repository}
  6564. */
  6565. getRepository(modelName) {
  6566. let repository = this._repositories[modelName];
  6567. if (repository) return repository;
  6568. repository = new this._repositoryCtor(this.container, modelName);
  6569. this._repositories[modelName] = repository;
  6570. return repository;
  6571. }
  6572. };
  6573. __name(_RepositoryRegistry, "RepositoryRegistry");
  6574. RepositoryRegistry = _RepositoryRegistry;
  6575. }
  6576. });
  6577. // src/repository/index.js
  6578. var init_repository2 = __esm({
  6579. "src/repository/index.js"() {
  6580. "use strict";
  6581. init_repository();
  6582. init_repository_registry();
  6583. }
  6584. });
  6585. // src/index.js
  6586. var src_exports = {};
  6587. __export(src_exports, {
  6588. Adapter: () => Adapter,
  6589. AdapterLoader: () => AdapterLoader,
  6590. AdapterRegistry: () => AdapterRegistry,
  6591. BelongsToResolver: () => BelongsToResolver,
  6592. DEFAULT_PRIMARY_KEY_PROPERTY_NAME: () => DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  6593. DataType: () => DataType,
  6594. DatasourceDefinitionValidator: () => DatasourceDefinitionValidator,
  6595. DecoratorTargetType: () => DecoratorTargetType,
  6596. DefinitionRegistry: () => DefinitionRegistry,
  6597. EmptyValuesDefiner: () => EmptyValuesDefiner,
  6598. FieldsClauseTool: () => FieldsClauseTool,
  6599. HasManyResolver: () => HasManyResolver,
  6600. HasOneResolver: () => HasOneResolver,
  6601. IncludeClauseTool: () => IncludeClauseTool,
  6602. InvalidArgumentError: () => InvalidArgumentError2,
  6603. InvalidOperatorValueError: () => InvalidOperatorValueError,
  6604. ModelDataSanitizer: () => ModelDataSanitizer,
  6605. ModelDataTransformer: () => ModelDataTransformer,
  6606. ModelDataValidator: () => ModelDataValidator,
  6607. ModelDefinitionUtils: () => ModelDefinitionUtils,
  6608. ModelDefinitionValidator: () => ModelDefinitionValidator,
  6609. NotImplementedError: () => NotImplementedError,
  6610. OperatorClauseTool: () => OperatorClauseTool,
  6611. OrderClauseTool: () => OrderClauseTool,
  6612. PrimaryKeysDefinitionValidator: () => PrimaryKeysDefinitionValidator,
  6613. PropertiesDefinitionValidator: () => PropertiesDefinitionValidator,
  6614. PropertyTransformerRegistry: () => PropertyTransformerRegistry,
  6615. PropertyUniqueness: () => PropertyUniqueness,
  6616. PropertyUniquenessValidator: () => PropertyUniquenessValidator,
  6617. PropertyValidatorRegistry: () => PropertyValidatorRegistry,
  6618. ReferencesManyResolver: () => ReferencesManyResolver,
  6619. RelationType: () => RelationType,
  6620. RelationsDefinitionValidator: () => RelationsDefinitionValidator,
  6621. Repository: () => Repository,
  6622. RepositoryRegistry: () => RepositoryRegistry,
  6623. Schema: () => Schema,
  6624. SliceClauseTool: () => SliceClauseTool,
  6625. WhereClauseTool: () => WhereClauseTool,
  6626. capitalize: () => capitalize,
  6627. cloneDeep: () => cloneDeep,
  6628. excludeObjectKeys: () => excludeObjectKeys,
  6629. getCtorName: () => getCtorName,
  6630. getDecoratorTargetType: () => getDecoratorTargetType,
  6631. getValueByPath: () => getValueByPath,
  6632. isCtor: () => isCtor,
  6633. isDeepEqual: () => isDeepEqual,
  6634. isPromise: () => isPromise,
  6635. isPureObject: () => isPureObject,
  6636. selectObjectKeys: () => selectObjectKeys,
  6637. singularize: () => singularize,
  6638. stringToRegexp: () => stringToRegexp,
  6639. transformPromise: () => transformPromise
  6640. });
  6641. module.exports = __toCommonJS(src_exports);
  6642. // src/schema.js
  6643. init_src2();
  6644. init_repository2();
  6645. init_definition();
  6646. init_repository2();
  6647. var _Schema = class _Schema extends Service {
  6648. /**
  6649. * Define datasource.
  6650. *
  6651. * @param {object} datasourceDef
  6652. * @returns {this}
  6653. */
  6654. defineDatasource(datasourceDef) {
  6655. this.getService(DefinitionRegistry).addDatasource(datasourceDef);
  6656. return this;
  6657. }
  6658. /**
  6659. * Define model.
  6660. *
  6661. * @param {object} modelDef
  6662. * @returns {this}
  6663. */
  6664. defineModel(modelDef) {
  6665. this.getService(DefinitionRegistry).addModel(modelDef);
  6666. return this;
  6667. }
  6668. /**
  6669. * Get repository.
  6670. *
  6671. * @param {string} modelName
  6672. * @returns {Repository}
  6673. */
  6674. getRepository(modelName) {
  6675. return this.getService(RepositoryRegistry).getRepository(modelName);
  6676. }
  6677. };
  6678. __name(_Schema, "Schema");
  6679. var Schema = _Schema;
  6680. // src/index.js
  6681. init_utils2();
  6682. init_errors2();
  6683. init_filter();
  6684. init_adapter2();
  6685. init_relations2();
  6686. init_definition();
  6687. init_repository2();
  6688. // Annotate the CommonJS export names for ESM import in node:
  6689. 0 && (module.exports = {
  6690. Adapter,
  6691. AdapterLoader,
  6692. AdapterRegistry,
  6693. BelongsToResolver,
  6694. DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  6695. DataType,
  6696. DatasourceDefinitionValidator,
  6697. DecoratorTargetType,
  6698. DefinitionRegistry,
  6699. EmptyValuesDefiner,
  6700. FieldsClauseTool,
  6701. HasManyResolver,
  6702. HasOneResolver,
  6703. IncludeClauseTool,
  6704. InvalidArgumentError,
  6705. InvalidOperatorValueError,
  6706. ModelDataSanitizer,
  6707. ModelDataTransformer,
  6708. ModelDataValidator,
  6709. ModelDefinitionUtils,
  6710. ModelDefinitionValidator,
  6711. NotImplementedError,
  6712. OperatorClauseTool,
  6713. OrderClauseTool,
  6714. PrimaryKeysDefinitionValidator,
  6715. PropertiesDefinitionValidator,
  6716. PropertyTransformerRegistry,
  6717. PropertyUniqueness,
  6718. PropertyUniquenessValidator,
  6719. PropertyValidatorRegistry,
  6720. ReferencesManyResolver,
  6721. RelationType,
  6722. RelationsDefinitionValidator,
  6723. Repository,
  6724. RepositoryRegistry,
  6725. Schema,
  6726. SliceClauseTool,
  6727. WhereClauseTool,
  6728. capitalize,
  6729. cloneDeep,
  6730. excludeObjectKeys,
  6731. getCtorName,
  6732. getDecoratorTargetType,
  6733. getValueByPath,
  6734. isCtor,
  6735. isDeepEqual,
  6736. isPromise,
  6737. isPureObject,
  6738. selectObjectKeys,
  6739. singularize,
  6740. stringToRegexp,
  6741. transformPromise
  6742. });