index.cjs 197 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  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. var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  39. // src/utils/is-promise.js
  40. function isPromise(value) {
  41. if (!value) return false;
  42. if (typeof value !== "object") return false;
  43. return typeof value.then === "function";
  44. }
  45. var init_is_promise = __esm({
  46. "src/utils/is-promise.js"() {
  47. __name(isPromise, "isPromise");
  48. }
  49. });
  50. // src/utils/capitalize.js
  51. function capitalize(string) {
  52. if (!string || typeof string !== "string") return string;
  53. return string.charAt(0).toUpperCase() + string.slice(1);
  54. }
  55. var init_capitalize = __esm({
  56. "src/utils/capitalize.js"() {
  57. __name(capitalize, "capitalize");
  58. }
  59. });
  60. // src/utils/clone-deep.js
  61. function cloneDeep(value) {
  62. if (!value) return value;
  63. const types = [Number, String, Boolean];
  64. let result;
  65. types.forEach((type) => {
  66. if (value instanceof type) result = type(value);
  67. });
  68. if (result === void 0) {
  69. if (Array.isArray(value)) {
  70. result = [];
  71. value.forEach((child, index) => {
  72. result[index] = cloneDeep(child);
  73. });
  74. } else if (typeof value === "object") {
  75. if ("nodeType" in value && value.nodeType && "cloneNode" in value && typeof value.cloneNode === "function") {
  76. result = value.cloneNode(true);
  77. } else if (!("prototype" in value) || !value.prototype) {
  78. if (value instanceof Date) {
  79. result = new Date(value);
  80. } else if (value.constructor && value.constructor.name === "Object") {
  81. result = {};
  82. for (const key in value) {
  83. result[key] = cloneDeep(value[key]);
  84. }
  85. } else {
  86. result = value;
  87. }
  88. } else {
  89. result = value;
  90. }
  91. } else {
  92. result = value;
  93. }
  94. }
  95. return result;
  96. }
  97. var init_clone_deep = __esm({
  98. "src/utils/clone-deep.js"() {
  99. __name(cloneDeep, "cloneDeep");
  100. }
  101. });
  102. // src/utils/singularize.js
  103. function singularize(noun) {
  104. if (!noun || typeof noun !== "string") return noun;
  105. const endings = {
  106. ves: "fe",
  107. ies: "y",
  108. i: "us",
  109. zes: "ze",
  110. ses: "s",
  111. es: "e",
  112. s: ""
  113. };
  114. return noun.replace(
  115. new RegExp(`(${Object.keys(endings).join("|")})$`),
  116. (r) => endings[r]
  117. );
  118. }
  119. var init_singularize = __esm({
  120. "src/utils/singularize.js"() {
  121. __name(singularize, "singularize");
  122. }
  123. });
  124. // src/utils/is-deep-equal.js
  125. function isDeepEqual(firstValue, secondValue) {
  126. const cached = /* @__PURE__ */ new WeakMap();
  127. const compare = /* @__PURE__ */ __name((a, b) => {
  128. if (a === null || b === null) return a === b;
  129. if (typeof a !== "object" || typeof b !== "object") return a === b;
  130. const dataTypeA = Array.isArray(a) ? "array" : "object";
  131. const dataTypeB = Array.isArray(b) ? "array" : "object";
  132. if (dataTypeA !== dataTypeB) return false;
  133. const keysA = Object.keys(a);
  134. const keysB = Object.keys(b);
  135. if (keysA.length !== keysB.length) return false;
  136. const symbolsA = Object.getOwnPropertySymbols(a);
  137. const symbolsB = Object.getOwnPropertySymbols(b);
  138. if (symbolsA.length !== symbolsB.length) return false;
  139. let setForA = cached.get(a);
  140. if (setForA == null) {
  141. setForA = /* @__PURE__ */ new Set();
  142. cached.set(a, setForA);
  143. } else if (setForA.has(b)) {
  144. return true;
  145. }
  146. setForA.add(b);
  147. let setForB = cached.get(b);
  148. if (setForB == null) {
  149. setForB = /* @__PURE__ */ new Set();
  150. cached.set(b, setForB);
  151. } else if (setForB.has(a)) {
  152. return true;
  153. }
  154. setForB.add(a);
  155. const propertyNamesA = [...keysA, ...symbolsA];
  156. for (const propertyNameA of propertyNamesA) {
  157. if (!Object.prototype.hasOwnProperty.call(b, propertyNameA)) return false;
  158. const propertyValueA = a[propertyNameA];
  159. const propertyValueB = b[propertyNameA];
  160. if (!compare(propertyValueA, propertyValueB)) return false;
  161. }
  162. return true;
  163. }, "compare");
  164. return compare(firstValue, secondValue);
  165. }
  166. var init_is_deep_equal = __esm({
  167. "src/utils/is-deep-equal.js"() {
  168. __name(isDeepEqual, "isDeepEqual");
  169. }
  170. });
  171. // src/errors/not-implemented-error.js
  172. var import_js_format, _NotImplementedError, NotImplementedError;
  173. var init_not_implemented_error = __esm({
  174. "src/errors/not-implemented-error.js"() {
  175. import_js_format = require("@e22m4u/js-format");
  176. _NotImplementedError = class _NotImplementedError extends import_js_format.Errorf {
  177. };
  178. __name(_NotImplementedError, "NotImplementedError");
  179. NotImplementedError = _NotImplementedError;
  180. }
  181. });
  182. // src/errors/invalid-argument-error.js
  183. var import_js_format2, _InvalidArgumentError, InvalidArgumentError;
  184. var init_invalid_argument_error = __esm({
  185. "src/errors/invalid-argument-error.js"() {
  186. import_js_format2 = require("@e22m4u/js-format");
  187. _InvalidArgumentError = class _InvalidArgumentError extends import_js_format2.Errorf {
  188. };
  189. __name(_InvalidArgumentError, "InvalidArgumentError");
  190. InvalidArgumentError = _InvalidArgumentError;
  191. }
  192. });
  193. // src/errors/invalid-operator-value-error.js
  194. var import_js_format3, _InvalidOperatorValueError, InvalidOperatorValueError;
  195. var init_invalid_operator_value_error = __esm({
  196. "src/errors/invalid-operator-value-error.js"() {
  197. import_js_format3 = require("@e22m4u/js-format");
  198. _InvalidOperatorValueError = class _InvalidOperatorValueError extends Error {
  199. /**
  200. * Constructor.
  201. *
  202. * @param {string} operator
  203. * @param {string} expected
  204. * @param {*} value
  205. */
  206. constructor(operator, expected, value) {
  207. super(
  208. (0, import_js_format3.format)(
  209. "Condition of {%s: ...} should have %s, but %v was given.",
  210. operator,
  211. expected,
  212. value
  213. )
  214. );
  215. }
  216. };
  217. __name(_InvalidOperatorValueError, "InvalidOperatorValueError");
  218. InvalidOperatorValueError = _InvalidOperatorValueError;
  219. }
  220. });
  221. // src/errors/index.js
  222. var init_errors = __esm({
  223. "src/errors/index.js"() {
  224. init_not_implemented_error();
  225. init_invalid_argument_error();
  226. init_invalid_operator_value_error();
  227. }
  228. });
  229. // src/utils/like-to-regexp.js
  230. function likeToRegexp(pattern, isCaseInsensitive = false) {
  231. if (typeof pattern !== "string") {
  232. throw new InvalidArgumentError(
  233. "The first argument of `likeToRegexp` should be a String, but %v was given.",
  234. pattern
  235. );
  236. }
  237. const regexSpecials = "-[]{}()*+?.\\^$|";
  238. let regexString = "";
  239. let isEscaping = false;
  240. for (const char of pattern) {
  241. if (isEscaping) {
  242. regexString += regexSpecials.includes(char) ? `\\${char}` : char;
  243. isEscaping = false;
  244. } else if (char === "\\") {
  245. isEscaping = true;
  246. } else if (char === "%") {
  247. regexString += ".*";
  248. } else if (char === "_") {
  249. regexString += ".";
  250. } else if (regexSpecials.includes(char)) {
  251. regexString += `\\${char}`;
  252. } else {
  253. regexString += char;
  254. }
  255. }
  256. if (isEscaping) {
  257. regexString += "\\\\";
  258. }
  259. const flags = isCaseInsensitive ? "i" : "";
  260. return new RegExp(`^${regexString}$`, flags);
  261. }
  262. var init_like_to_regexp = __esm({
  263. "src/utils/like-to-regexp.js"() {
  264. init_errors();
  265. __name(likeToRegexp, "likeToRegexp");
  266. }
  267. });
  268. // src/utils/is-plain-object.js
  269. function isPlainObject(value) {
  270. return Boolean(
  271. typeof value === "object" && value && !Array.isArray(value) && (!value.constructor || value.constructor && value.constructor.name === "Object")
  272. );
  273. }
  274. var init_is_plain_object = __esm({
  275. "src/utils/is-plain-object.js"() {
  276. __name(isPlainObject, "isPlainObject");
  277. }
  278. });
  279. // src/utils/string-to-regexp.js
  280. function stringToRegexp(pattern, flags = void 0) {
  281. if (pattern instanceof RegExp) {
  282. return new RegExp(pattern, flags);
  283. }
  284. return new RegExp(pattern, flags);
  285. }
  286. var init_string_to_regexp = __esm({
  287. "src/utils/string-to-regexp.js"() {
  288. __name(stringToRegexp, "stringToRegexp");
  289. }
  290. });
  291. // src/utils/get-value-by-path.js
  292. function getValueByPath(obj, path, orElse = void 0) {
  293. if (!obj || typeof obj !== "object") return orElse;
  294. if (!path || typeof path !== "string") return orElse;
  295. const keys = path.split(".");
  296. let value = obj;
  297. for (const key of keys) {
  298. if (typeof value === "object" && value !== null && key in value) {
  299. value = value[key];
  300. } else {
  301. value = orElse;
  302. break;
  303. }
  304. }
  305. return value;
  306. }
  307. var init_get_value_by_path = __esm({
  308. "src/utils/get-value-by-path.js"() {
  309. __name(getValueByPath, "getValueByPath");
  310. }
  311. });
  312. // src/utils/select-object-keys.js
  313. function selectObjectKeys(obj, keys) {
  314. if (!obj || typeof obj !== "object" || Array.isArray(obj))
  315. throw new InvalidArgumentError(
  316. "The first argument of selectObjectKeys should be an Object, but %v was given.",
  317. obj
  318. );
  319. if (!Array.isArray(keys))
  320. throw new InvalidArgumentError(
  321. "The second argument of selectObjectKeys should be an Array of String, but %v was given.",
  322. keys
  323. );
  324. keys.forEach((key) => {
  325. if (typeof key !== "string")
  326. throw new InvalidArgumentError(
  327. "The second argument of selectObjectKeys should be an Array of String, but %v was given.",
  328. key
  329. );
  330. });
  331. const result = {};
  332. const allKeys = Object.keys(obj);
  333. allKeys.forEach((key) => {
  334. if (keys.includes(key)) result[key] = obj[key];
  335. });
  336. return result;
  337. }
  338. var init_select_object_keys = __esm({
  339. "src/utils/select-object-keys.js"() {
  340. init_errors();
  341. __name(selectObjectKeys, "selectObjectKeys");
  342. }
  343. });
  344. // src/utils/exclude-object-keys.js
  345. function excludeObjectKeys(obj, keys) {
  346. if (typeof obj !== "object" || !obj || Array.isArray(obj))
  347. throw new InvalidArgumentError(
  348. "Cannot exclude keys from a non-Object value, %v was given.",
  349. obj
  350. );
  351. const result = { ...obj };
  352. keys = Array.isArray(keys) ? keys : [keys];
  353. keys.forEach((key) => delete result[key]);
  354. return result;
  355. }
  356. var init_exclude_object_keys = __esm({
  357. "src/utils/exclude-object-keys.js"() {
  358. init_errors();
  359. __name(excludeObjectKeys, "excludeObjectKeys");
  360. }
  361. });
  362. // src/utils/model-name-to-model-key.js
  363. function modelNameToModelKey(modelName) {
  364. if (!modelName || typeof modelName !== "string" || /\s/.test(modelName))
  365. throw new InvalidArgumentError(
  366. "The model name should be a non-empty String without spaces, but %v was given.",
  367. modelName
  368. );
  369. return modelName.toLowerCase().replace(/[-_]/g, "");
  370. }
  371. var init_model_name_to_model_key = __esm({
  372. "src/utils/model-name-to-model-key.js"() {
  373. init_errors();
  374. __name(modelNameToModelKey, "modelNameToModelKey");
  375. }
  376. });
  377. // src/utils/index.js
  378. var init_utils = __esm({
  379. "src/utils/index.js"() {
  380. init_is_promise();
  381. init_capitalize();
  382. init_clone_deep();
  383. init_singularize();
  384. init_is_deep_equal();
  385. init_like_to_regexp();
  386. init_is_plain_object();
  387. init_string_to_regexp();
  388. init_get_value_by_path();
  389. init_select_object_keys();
  390. init_exclude_object_keys();
  391. init_model_name_to_model_key();
  392. }
  393. });
  394. // src/filter/slice-clause-tool.js
  395. var import_js_service, _SliceClauseTool, SliceClauseTool;
  396. var init_slice_clause_tool = __esm({
  397. "src/filter/slice-clause-tool.js"() {
  398. import_js_service = require("@e22m4u/js-service");
  399. init_errors();
  400. _SliceClauseTool = class _SliceClauseTool extends import_js_service.Service {
  401. /**
  402. * Slice.
  403. *
  404. * @param {object[]} entities
  405. * @param {number|undefined} skip
  406. * @param {number|undefined} limit
  407. * @returns {object[]}
  408. */
  409. slice(entities, skip = void 0, limit = void 0) {
  410. if (!Array.isArray(entities))
  411. throw new InvalidArgumentError(
  412. "The first argument of SliceClauseTool.slice should be an Array, but %v was given.",
  413. entities
  414. );
  415. if (skip != null && typeof skip !== "number")
  416. throw new InvalidArgumentError(
  417. 'The provided option "skip" should be a Number, but %v was given.',
  418. skip
  419. );
  420. if (limit != null && typeof limit !== "number")
  421. throw new InvalidArgumentError(
  422. 'The provided option "limit" should be a Number, but %v was given.',
  423. limit
  424. );
  425. skip = skip || 0;
  426. limit = limit || entities.length;
  427. return entities.slice(skip, skip + limit);
  428. }
  429. /**
  430. * Validate skip clause.
  431. *
  432. * @param {number|undefined} skip
  433. */
  434. static validateSkipClause(skip) {
  435. if (skip == null) return;
  436. if (typeof skip !== "number")
  437. throw new InvalidArgumentError(
  438. 'The provided option "skip" should be a Number, but %v was given.',
  439. skip
  440. );
  441. }
  442. /**
  443. * Validate limit clause.
  444. *
  445. * @param {number|undefined} limit
  446. */
  447. static validateLimitClause(limit) {
  448. if (limit == null) return;
  449. if (typeof limit !== "number")
  450. throw new InvalidArgumentError(
  451. 'The provided option "limit" should be a Number, but %v was given.',
  452. limit
  453. );
  454. }
  455. };
  456. __name(_SliceClauseTool, "SliceClauseTool");
  457. SliceClauseTool = _SliceClauseTool;
  458. }
  459. });
  460. // src/filter/order-clause-tool.js
  461. function compareFn(a, b) {
  462. let undefinedA, undefinedB;
  463. for (let i = 0, l = this.length; i < l; i++) {
  464. const aVal = getValueByPath(a, this[i].key);
  465. const bVal = getValueByPath(b, this[i].key);
  466. undefinedB = bVal === void 0 && aVal !== void 0;
  467. undefinedA = aVal === void 0 && bVal !== void 0;
  468. if (undefinedB || aVal > bVal) {
  469. return this[i].reverse;
  470. } else if (undefinedA || aVal < bVal) {
  471. return -1 * this[i].reverse;
  472. }
  473. }
  474. return 0;
  475. }
  476. var import_js_service2, _OrderClauseTool, OrderClauseTool;
  477. var init_order_clause_tool = __esm({
  478. "src/filter/order-clause-tool.js"() {
  479. import_js_service2 = require("@e22m4u/js-service");
  480. init_utils();
  481. init_errors();
  482. _OrderClauseTool = class _OrderClauseTool extends import_js_service2.Service {
  483. /**
  484. * Sort.
  485. *
  486. * @param {object[]} entities
  487. * @param {string|string[]|undefined} clause
  488. */
  489. sort(entities, clause) {
  490. if (clause == null) return;
  491. if (Array.isArray(clause) === false) clause = [clause];
  492. if (!clause.length) return;
  493. const mapping = [];
  494. clause.forEach((key, index) => {
  495. if (!key || typeof key !== "string")
  496. throw new InvalidArgumentError(
  497. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v was given.',
  498. key
  499. );
  500. let reverse = 1;
  501. const matches = key.match(/\s+(A|DE)SC$/i);
  502. if (matches) {
  503. key = key.replace(/\s+(A|DE)SC/i, "");
  504. if (matches[1].toLowerCase() === "de") reverse = -1;
  505. }
  506. mapping[index] = { key, reverse };
  507. });
  508. entities.sort(compareFn.bind(mapping));
  509. }
  510. /**
  511. * Validate order clause.
  512. *
  513. * @param {string|string[]|undefined} clause
  514. */
  515. static validateOrderClause(clause) {
  516. if (clause == null) return;
  517. if (Array.isArray(clause) === false) clause = [clause];
  518. if (!clause.length) return;
  519. clause.forEach((field) => {
  520. if (!field || typeof field !== "string")
  521. throw new InvalidArgumentError(
  522. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v was given.',
  523. field
  524. );
  525. });
  526. }
  527. /**
  528. * Normalize order clause.
  529. *
  530. * @param {string|string[]|undefined} clause
  531. * @returns {string[]|undefined}
  532. */
  533. static normalizeOrderClause(clause) {
  534. if (clause == null) return;
  535. if (Array.isArray(clause) === false) clause = [clause];
  536. if (!clause.length) return;
  537. clause.forEach((field) => {
  538. if (!field || typeof field !== "string")
  539. throw new InvalidArgumentError(
  540. 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v was given.',
  541. field
  542. );
  543. });
  544. return clause;
  545. }
  546. };
  547. __name(_OrderClauseTool, "OrderClauseTool");
  548. OrderClauseTool = _OrderClauseTool;
  549. __name(compareFn, "compareFn");
  550. }
  551. });
  552. // src/filter/operator-clause-tool.js
  553. var import_js_service3, _OperatorClauseTool, OperatorClauseTool;
  554. var init_operator_clause_tool = __esm({
  555. "src/filter/operator-clause-tool.js"() {
  556. import_js_service3 = require("@e22m4u/js-service");
  557. init_utils();
  558. init_errors();
  559. _OperatorClauseTool = class _OperatorClauseTool extends import_js_service3.Service {
  560. /**
  561. * Compare.
  562. *
  563. * @param {*} val1 The 1st value
  564. * @param {*} val2 The 2nd value
  565. * @param {*} noTypeConversion
  566. * @returns {number} 0: =, positive: >, negative <
  567. */
  568. compare(val1, val2, noTypeConversion = false) {
  569. if (val1 === val2) {
  570. return 0;
  571. }
  572. if (val1 == null || val2 == null) {
  573. return val1 == val2 ? 0 : NaN;
  574. }
  575. const type1 = typeof val1;
  576. const type2 = typeof val2;
  577. if (type1 === "object" || type2 === "object") {
  578. return isDeepEqual(val1, val2) ? 0 : NaN;
  579. }
  580. if ((type1 === "number" || type1 === "string" || type1 === "boolean") && (type2 === "number" || type2 === "string" || type2 === "boolean")) {
  581. if (noTypeConversion && type1 !== type2) {
  582. return NaN;
  583. }
  584. const num1 = Number(val1);
  585. const num2 = Number(val2);
  586. if (!isNaN(num1) && !isNaN(num2)) {
  587. return num1 - num2;
  588. }
  589. }
  590. if (type1 === "string" && type2 === "string") {
  591. if (val1 > val2) return 1;
  592. if (val1 < val2) return -1;
  593. return 0;
  594. }
  595. return NaN;
  596. }
  597. /**
  598. * Test all operators.
  599. *
  600. * @param {object} clause
  601. * @param {*} value
  602. * @returns {boolean|undefined}
  603. */
  604. testAll(clause, value) {
  605. if (!clause || typeof clause !== "object" || Array.isArray(clause))
  606. throw new InvalidArgumentError(
  607. "The first argument of OperatorUtils.testAll should be an Object, but %v was given.",
  608. clause
  609. );
  610. const operatorMap = {
  611. eq: this.testEqNeq,
  612. neq: this.testEqNeq,
  613. gt: this.testGtLt,
  614. gte: this.testGtLt,
  615. lt: this.testGtLt,
  616. lte: this.testGtLt,
  617. inq: this.testInq,
  618. nin: this.testNin,
  619. between: this.testBetween,
  620. exists: this.testExists,
  621. like: this.testLike,
  622. nlike: this.testNlike,
  623. ilike: this.testIlike,
  624. nilike: this.testNilike,
  625. regexp: this.testRegexp
  626. };
  627. const clauseKeys = Object.keys(clause);
  628. const knownOperators = clauseKeys.filter((key) => operatorMap[key]);
  629. if (knownOperators.length === 0) {
  630. return void 0;
  631. }
  632. return knownOperators.every((op) => {
  633. const singleOpClause = { [op]: clause[op] };
  634. if (op === "regexp" && "flags" in clause) {
  635. singleOpClause.flags = clause.flags;
  636. }
  637. const testFn = operatorMap[op];
  638. const result = testFn.call(this, singleOpClause, value);
  639. return result;
  640. });
  641. }
  642. /**
  643. * Test eq/neq operator.
  644. *
  645. * @example
  646. * ```ts
  647. * {
  648. * eq: 'foo',
  649. * }
  650. * ```
  651. *
  652. * @example
  653. * ```ts
  654. * {
  655. * neq: 'foo',
  656. * }
  657. * ```
  658. *
  659. * @param {object} clause
  660. * @param {*} value
  661. * @returns {boolean|undefined}
  662. */
  663. testEqNeq(clause, value) {
  664. if (!clause || typeof clause !== "object")
  665. throw new InvalidArgumentError(
  666. "The first argument of OperatorUtils.testEqNeq should be an Object, but %v was given.",
  667. clause
  668. );
  669. if ("eq" in clause) return this.compare(clause.eq, value, true) === 0;
  670. if ("neq" in clause) return this.compare(clause.neq, value, true) !== 0;
  671. }
  672. /**
  673. * Test lt/gt/lte/gte operator.
  674. *
  675. * @example
  676. * ```ts
  677. * {
  678. * lt: 10,
  679. * }
  680. * ```
  681. *
  682. * @example
  683. * ```ts
  684. * {
  685. * lte: 10,
  686. * }
  687. * ```
  688. *
  689. * @example
  690. * ```ts
  691. * {
  692. * gt: 10,
  693. * }
  694. * ```
  695. *
  696. * @example
  697. * ```ts
  698. * {
  699. * gte: 10,
  700. * }
  701. * ```
  702. *
  703. * @param {object} clause
  704. * @param {*} value
  705. * @returns {boolean|undefined}
  706. */
  707. testGtLt(clause, value) {
  708. if (!clause || typeof clause !== "object")
  709. throw new InvalidArgumentError(
  710. "The first argument of OperatorUtils.testGtLt should be an Object, but %v was given.",
  711. clause
  712. );
  713. if ("gt" in clause) return this.compare(value, clause.gt) > 0;
  714. if ("gte" in clause) return this.compare(value, clause.gte) >= 0;
  715. if ("lt" in clause) return this.compare(value, clause.lt) < 0;
  716. if ("lte" in clause) return this.compare(value, clause.lte) <= 0;
  717. }
  718. /**
  719. * Test inc operator.
  720. *
  721. * @example
  722. * ```ts
  723. * {
  724. * inc: ['foo', 'bar'],
  725. * }
  726. * ```
  727. *
  728. * @param {object} clause
  729. * @param {*} value
  730. * @returns {boolean|undefined}
  731. */
  732. testInq(clause, value) {
  733. if (!clause || typeof clause !== "object")
  734. throw new InvalidArgumentError(
  735. "The first argument of OperatorUtils.testInq should be an Object, but %v was given.",
  736. clause
  737. );
  738. if ("inq" in clause && clause.inq !== void 0) {
  739. if (!clause.inq || !Array.isArray(clause.inq)) {
  740. throw new InvalidOperatorValueError(
  741. "inq",
  742. "an Array of possible values",
  743. clause.inq
  744. );
  745. }
  746. for (let i = 0; i < clause.inq.length; i++) {
  747. if (this.compare(clause.inq[i], value, true) === 0) {
  748. return true;
  749. }
  750. }
  751. return false;
  752. }
  753. }
  754. /**
  755. * Test nin operator.
  756. *
  757. * @example
  758. * ```ts
  759. * {
  760. * nin: ['foo', 'bar'],
  761. * }
  762. * ```
  763. *
  764. * @param {object} clause
  765. * @param {*} value
  766. * @returns {boolean|undefined}
  767. */
  768. testNin(clause, value) {
  769. if (!clause || typeof clause !== "object")
  770. throw new InvalidArgumentError(
  771. "The first argument of OperatorUtils.testNin should be an Object, but %v was given.",
  772. clause
  773. );
  774. if ("nin" in clause && clause.nin !== void 0) {
  775. if (!clause.nin || !Array.isArray(clause.nin)) {
  776. throw new InvalidOperatorValueError(
  777. "nin",
  778. "an Array of possible values",
  779. clause.nin
  780. );
  781. }
  782. return clause.nin.every((element) => {
  783. return this.compare(element, value, true) !== 0;
  784. });
  785. }
  786. }
  787. /**
  788. * Test between operator.
  789. *
  790. * @example
  791. * ```ts
  792. * {
  793. * between: [10, 20],
  794. * }
  795. * ```
  796. *
  797. * @param {object} clause
  798. * @param {*} value
  799. * @returns {boolean|undefined}
  800. */
  801. testBetween(clause, value) {
  802. if (!clause || typeof clause !== "object")
  803. throw new InvalidArgumentError(
  804. "The first argument of OperatorUtils.testBetween should be an Object, but %v was given.",
  805. clause
  806. );
  807. if ("between" in clause && clause.between !== void 0) {
  808. if (!Array.isArray(clause.between) || clause.between.length !== 2) {
  809. throw new InvalidOperatorValueError(
  810. "between",
  811. "an Array of 2 elements",
  812. clause.between
  813. );
  814. }
  815. return this.testGtLt({ gte: clause.between[0] }, value) && this.testGtLt({ lte: clause.between[1] }, value);
  816. }
  817. }
  818. /**
  819. * Test exists operator.
  820. *
  821. * @example
  822. * ```ts
  823. * {
  824. * exists: true,
  825. * }
  826. * ```
  827. *
  828. * @param {object} clause
  829. * @param {*} value
  830. * @returns {boolean|undefined}
  831. */
  832. testExists(clause, value) {
  833. if (!clause || typeof clause !== "object")
  834. throw new InvalidArgumentError(
  835. "The first argument of OperatorUtils.testExists should be an Object, but %v was given.",
  836. clause
  837. );
  838. if ("exists" in clause && clause.exists !== void 0) {
  839. if (typeof clause.exists !== "boolean") {
  840. throw new InvalidOperatorValueError(
  841. "exists",
  842. "a Boolean",
  843. clause.exists
  844. );
  845. }
  846. return clause.exists ? value !== void 0 : value === void 0;
  847. }
  848. }
  849. /**
  850. * Test like operator.
  851. *
  852. * @example
  853. * ```ts
  854. * {
  855. * like: 'foo',
  856. * }
  857. * ```
  858. *
  859. * @param {object} clause
  860. * @param {*} value
  861. * @returns {boolean|undefined}
  862. */
  863. testLike(clause, value) {
  864. if (!clause || typeof clause !== "object" || Array.isArray(clause))
  865. throw new InvalidArgumentError(
  866. "The first argument of OperatorUtils.testLike should be an Object, but %v was given.",
  867. clause
  868. );
  869. if ("like" in clause && clause.like !== void 0) {
  870. if (typeof clause.like !== "string")
  871. throw new InvalidOperatorValueError("like", "a String", clause.like);
  872. return likeToRegexp(clause.like).test(value);
  873. }
  874. }
  875. /**
  876. * Test nlike operator.
  877. *
  878. * @example
  879. * ```ts
  880. * {
  881. * nlike: 'foo',
  882. * }
  883. * ```
  884. *
  885. * @param {object} clause
  886. * @param {*} value
  887. * @returns {boolean|undefined}
  888. */
  889. testNlike(clause, value) {
  890. if (!clause || typeof clause !== "object" || Array.isArray(clause))
  891. throw new InvalidArgumentError(
  892. "The first argument of OperatorUtils.testNlike should be an Object, but %v was given.",
  893. clause
  894. );
  895. if ("nlike" in clause && clause.nlike !== void 0) {
  896. if (typeof clause.nlike !== "string") {
  897. throw new InvalidOperatorValueError("nlike", "a String", clause.nlike);
  898. }
  899. return !likeToRegexp(clause.nlike).test(value);
  900. }
  901. }
  902. /**
  903. * Test ilike operator.
  904. *
  905. * @example
  906. * ```ts
  907. * {
  908. * ilike: 'foo',
  909. * }
  910. * ```
  911. *
  912. * @param {object} clause
  913. * @param {*} value
  914. * @returns {boolean|undefined}
  915. */
  916. testIlike(clause, value) {
  917. if (!clause || typeof clause !== "object" || Array.isArray(clause))
  918. throw new InvalidArgumentError(
  919. "The first argument of OperatorUtils.testIlike should be an Object, but %v was given.",
  920. clause
  921. );
  922. if ("ilike" in clause && clause.ilike !== void 0) {
  923. if (typeof clause.ilike !== "string") {
  924. throw new InvalidOperatorValueError("ilike", "a String", clause.ilike);
  925. }
  926. return likeToRegexp(clause.ilike, true).test(value);
  927. }
  928. }
  929. /**
  930. * Test nilike operator.
  931. *
  932. * @example
  933. * ```ts
  934. * {
  935. * nilike: 'foo',
  936. * }
  937. * ```
  938. *
  939. * @param {object} clause
  940. * @param {*} value
  941. * @returns {boolean|undefined}
  942. */
  943. testNilike(clause, value) {
  944. if (!clause || typeof clause !== "object" || Array.isArray(clause))
  945. throw new InvalidArgumentError(
  946. "The first argument of OperatorUtils.testNilike should be an Object, but %v was given.",
  947. clause
  948. );
  949. if ("nilike" in clause && clause.nilike !== void 0) {
  950. if (typeof clause.nilike !== "string") {
  951. throw new InvalidOperatorValueError(
  952. "nilike",
  953. "a String",
  954. clause.nilike
  955. );
  956. }
  957. return !likeToRegexp(clause.nilike, true).test(value);
  958. }
  959. }
  960. /**
  961. * Test regexp.
  962. *
  963. * @example
  964. * ```ts
  965. * {
  966. * regexp: 'foo.*',
  967. * }
  968. * ```
  969. *
  970. * @example
  971. * ```ts
  972. * {
  973. * regexp: 'foo.*',
  974. * flags: 'i',
  975. * }
  976. * ```
  977. *
  978. * @param {object} clause
  979. * @param {*} value
  980. * @returns {boolean|undefined}
  981. */
  982. testRegexp(clause, value) {
  983. if (!clause || typeof clause !== "object")
  984. throw new InvalidArgumentError(
  985. "The first argument of OperatorUtils.testRegexp should be an Object, but %v was given.",
  986. clause
  987. );
  988. if ("regexp" in clause && clause.regexp !== void 0) {
  989. if (typeof clause.regexp !== "string" && !(clause.regexp instanceof RegExp)) {
  990. throw new InvalidOperatorValueError(
  991. "regexp",
  992. "a String",
  993. clause.regexp
  994. );
  995. }
  996. const flags = clause.flags || void 0;
  997. if (flags && typeof flags !== "string")
  998. throw new InvalidArgumentError(
  999. "RegExp flags should be a String, but %v was given.",
  1000. clause.flags
  1001. );
  1002. if (!value || typeof value !== "string") return false;
  1003. const regExp = stringToRegexp(clause.regexp, flags);
  1004. return !!value.match(regExp);
  1005. }
  1006. }
  1007. };
  1008. __name(_OperatorClauseTool, "OperatorClauseTool");
  1009. OperatorClauseTool = _OperatorClauseTool;
  1010. }
  1011. });
  1012. // src/filter/where-clause-tool.js
  1013. var import_js_service4, _WhereClauseTool, WhereClauseTool;
  1014. var init_where_clause_tool = __esm({
  1015. "src/filter/where-clause-tool.js"() {
  1016. import_js_service4 = require("@e22m4u/js-service");
  1017. init_errors();
  1018. init_operator_clause_tool();
  1019. init_utils();
  1020. _WhereClauseTool = class _WhereClauseTool extends import_js_service4.Service {
  1021. /**
  1022. * Filter by where clause.
  1023. *
  1024. * @example
  1025. * ```
  1026. * const entities = [
  1027. * {foo: 1, bar: 'a'},
  1028. * {foo: 2, bar: 'b'},
  1029. * {foo: 3, bar: 'b'},
  1030. * {foo: 4, bar: 'b'},
  1031. * ];
  1032. *
  1033. * const result = filterByWhereClause(entities, {
  1034. * foo: {gt: 2},
  1035. * bar: 'b',
  1036. * });
  1037. *
  1038. * console.log(result);
  1039. * // [
  1040. * // {foo: 3, bar: 'b'},
  1041. * // {foo: 4, bar: 'b'},
  1042. * // ];
  1043. *
  1044. * ```
  1045. *
  1046. * @param {object[]} entities
  1047. * @param {WhereClause|undefined} where
  1048. * @returns {object[]}
  1049. */
  1050. filter(entities, where = void 0) {
  1051. if (!Array.isArray(entities))
  1052. throw new InvalidArgumentError(
  1053. "The first argument of WhereClauseTool.filter should be an Array of Object, but %v was given.",
  1054. entities
  1055. );
  1056. if (where == null) return entities;
  1057. return entities.filter(this._createFilter(where));
  1058. }
  1059. /**
  1060. * Create where filter.
  1061. *
  1062. * @param {WhereClause} whereClause
  1063. * @returns {Function}
  1064. */
  1065. _createFilter(whereClause) {
  1066. if (typeof whereClause !== "object" || Array.isArray(whereClause))
  1067. throw new InvalidArgumentError(
  1068. 'The provided option "where" should be an Object, but %v was given.',
  1069. whereClause
  1070. );
  1071. const keys = Object.keys(whereClause);
  1072. return (data) => {
  1073. if (typeof data !== "object")
  1074. throw new InvalidArgumentError(
  1075. "The first argument of WhereClauseTool.filter should be an Array of Object, but %v was given.",
  1076. data
  1077. );
  1078. return keys.every((key) => {
  1079. if (key === "and" && key in whereClause) {
  1080. const andClause = whereClause[key];
  1081. if (Array.isArray(andClause))
  1082. return andClause.every((clause) => this._createFilter(clause)(data));
  1083. } else if (key === "or" && key in whereClause) {
  1084. const orClause = whereClause[key];
  1085. if (Array.isArray(orClause))
  1086. return orClause.some((clause) => this._createFilter(clause)(data));
  1087. }
  1088. const value = getValueByPath(data, key);
  1089. const matcher = whereClause[key];
  1090. if (this._test(matcher, value)) return true;
  1091. });
  1092. };
  1093. }
  1094. /**
  1095. * Value testing.
  1096. *
  1097. * @param {*} example
  1098. * @param {*} value
  1099. * @returns {boolean}
  1100. */
  1101. _test(example, value) {
  1102. if (example === value) {
  1103. return true;
  1104. }
  1105. if (example === null) {
  1106. return value === null;
  1107. }
  1108. if (example === void 0) {
  1109. return value === void 0;
  1110. }
  1111. if (example instanceof RegExp) {
  1112. if (typeof value === "string") {
  1113. return example.test(value);
  1114. }
  1115. if (Array.isArray(value)) {
  1116. return value.some((el) => typeof el === "string" && example.test(el));
  1117. }
  1118. return false;
  1119. }
  1120. if (isPlainObject(example)) {
  1121. const operatorsTest = this.getService(OperatorClauseTool).testAll(
  1122. example,
  1123. value
  1124. );
  1125. if (operatorsTest !== void 0) {
  1126. if ("neq" in example && Array.isArray(value)) {
  1127. return !value.some((el) => isDeepEqual(el, example.neq));
  1128. }
  1129. return operatorsTest;
  1130. }
  1131. }
  1132. if (Array.isArray(value)) {
  1133. const isElementMatched = value.some((el) => isDeepEqual(el, example));
  1134. if (isElementMatched) return true;
  1135. }
  1136. return isDeepEqual(example, value);
  1137. }
  1138. /**
  1139. * Validate where clause.
  1140. *
  1141. * @param {WhereClause|undefined} clause
  1142. */
  1143. static validateWhereClause(clause) {
  1144. if (clause == null || typeof clause === "function") return;
  1145. if (typeof clause !== "object" || Array.isArray(clause))
  1146. throw new InvalidArgumentError(
  1147. 'The provided option "where" should be an Object, but %v was given.',
  1148. clause
  1149. );
  1150. }
  1151. };
  1152. __name(_WhereClauseTool, "WhereClauseTool");
  1153. WhereClauseTool = _WhereClauseTool;
  1154. }
  1155. });
  1156. // src/definition/model/relations/relation-type.js
  1157. var RelationType;
  1158. var init_relation_type = __esm({
  1159. "src/definition/model/relations/relation-type.js"() {
  1160. RelationType = {
  1161. BELONGS_TO: "belongsTo",
  1162. HAS_ONE: "hasOne",
  1163. HAS_MANY: "hasMany",
  1164. REFERENCES_MANY: "referencesMany"
  1165. };
  1166. }
  1167. });
  1168. // src/definition/model/relations/relations-definition-validator.js
  1169. var import_js_service5, _RelationsDefinitionValidator, RelationsDefinitionValidator;
  1170. var init_relations_definition_validator = __esm({
  1171. "src/definition/model/relations/relations-definition-validator.js"() {
  1172. import_js_service5 = require("@e22m4u/js-service");
  1173. init_relation_type();
  1174. init_errors();
  1175. _RelationsDefinitionValidator = class _RelationsDefinitionValidator extends import_js_service5.Service {
  1176. /**
  1177. * Validate.
  1178. *
  1179. * @param {string} modelName
  1180. * @param {object} relDefs
  1181. */
  1182. validate(modelName, relDefs) {
  1183. if (!modelName || typeof modelName !== "string")
  1184. throw new InvalidArgumentError(
  1185. "The first argument of RelationsDefinitionValidator.validate should be a non-empty String, but %v was given.",
  1186. modelName
  1187. );
  1188. if (!relDefs || typeof relDefs !== "object" || Array.isArray(relDefs))
  1189. throw new InvalidArgumentError(
  1190. 'The provided option "relations" of the model %v should be an Object, but %v was given.',
  1191. modelName,
  1192. relDefs
  1193. );
  1194. const relNames = Object.keys(relDefs);
  1195. relNames.forEach((relName) => {
  1196. const relDef = relDefs[relName];
  1197. this._validateRelation(modelName, relName, relDef);
  1198. });
  1199. }
  1200. /**
  1201. * Validate relation.
  1202. *
  1203. * @param {string} modelName
  1204. * @param {string} relName
  1205. * @param {object} relDef
  1206. */
  1207. _validateRelation(modelName, relName, relDef) {
  1208. if (!modelName || typeof modelName !== "string")
  1209. throw new InvalidArgumentError(
  1210. "The first argument of RelationsDefinitionValidator._validateRelation should be a non-empty String, but %v was given.",
  1211. modelName
  1212. );
  1213. if (!relName || typeof relName !== "string")
  1214. throw new InvalidArgumentError(
  1215. "The relation name of the model %v should be a non-empty String, but %v was given.",
  1216. modelName,
  1217. relName
  1218. );
  1219. if (!relDef || typeof relDef !== "object" || Array.isArray(relDef))
  1220. throw new InvalidArgumentError(
  1221. "The relation %v of the model %v should be an Object, but %v was given.",
  1222. relName,
  1223. modelName,
  1224. relDef
  1225. );
  1226. if (!relDef.type || !Object.values(RelationType).includes(relDef.type))
  1227. throw new InvalidArgumentError(
  1228. 'The relation %v of the model %v requires the option "type" to have one of relation types: %l, but %v was given.',
  1229. relName,
  1230. modelName,
  1231. Object.values(RelationType),
  1232. relDef.type
  1233. );
  1234. this._validateBelongsTo(modelName, relName, relDef);
  1235. this._validateHasOne(modelName, relName, relDef);
  1236. this._validateHasMany(modelName, relName, relDef);
  1237. this._validateReferencesMany(modelName, relName, relDef);
  1238. }
  1239. /**
  1240. * Validate "belongsTo".
  1241. *
  1242. * @example The regular "belongsTo" relation.
  1243. * ```
  1244. * {
  1245. * type: RelationType.BELONGS_TO,
  1246. * model: 'model',
  1247. * foreignKey: 'modelId', // optional
  1248. * }
  1249. * ```
  1250. *
  1251. * @example The polymorphic "belongsTo" relation.
  1252. * ```
  1253. * {
  1254. * type: RelationType.BELONGS_TO,
  1255. * polymorphic: true,
  1256. * foreignKey: 'referenceId', // optional
  1257. * discriminator: 'referenceType', // optional
  1258. * }
  1259. * ```
  1260. *
  1261. * @param {string} modelName
  1262. * @param {string} relName
  1263. * @param {object} relDef
  1264. * @private
  1265. */
  1266. _validateBelongsTo(modelName, relName, relDef) {
  1267. if (relDef.type !== RelationType.BELONGS_TO) return;
  1268. if (relDef.polymorphic) {
  1269. if (typeof relDef.polymorphic !== "boolean")
  1270. throw new InvalidArgumentError(
  1271. 'The relation %v of the model %v has the type "belongsTo", so it expects the option "polymorphic" to be a Boolean, but %v was given.',
  1272. relName,
  1273. modelName,
  1274. relDef.polymorphic
  1275. );
  1276. if (relDef.foreignKey && typeof relDef.foreignKey !== "string")
  1277. throw new InvalidArgumentError(
  1278. '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 was given.',
  1279. relName,
  1280. modelName,
  1281. relDef.foreignKey
  1282. );
  1283. if (relDef.discriminator && typeof relDef.discriminator !== "string")
  1284. throw new InvalidArgumentError(
  1285. '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 was given.',
  1286. relName,
  1287. modelName,
  1288. relDef.discriminator
  1289. );
  1290. } else {
  1291. if (!relDef.model || typeof relDef.model !== "string")
  1292. throw new InvalidArgumentError(
  1293. '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 was given.',
  1294. relName,
  1295. modelName,
  1296. relDef.model
  1297. );
  1298. if (relDef.foreignKey && typeof relDef.foreignKey !== "string")
  1299. throw new InvalidArgumentError(
  1300. 'The relation %v of the model %v has the type "belongsTo", so it expects the provided option "foreignKey" to be a String, but %v was given.',
  1301. relName,
  1302. modelName,
  1303. relDef.foreignKey
  1304. );
  1305. if (relDef.discriminator)
  1306. throw new InvalidArgumentError(
  1307. 'The relation %v of the model %v is a non-polymorphic "belongsTo" relation, so it should not have the option "discriminator" to be provided.',
  1308. relName,
  1309. modelName
  1310. );
  1311. }
  1312. }
  1313. /**
  1314. * Validate "hasOne".
  1315. *
  1316. * @example The regular "hasOne" relation.
  1317. * ```
  1318. * {
  1319. * type: RelationType.HAS_ONE,
  1320. * model: 'model',
  1321. * foreignKey: 'modelId',
  1322. * }
  1323. * ```
  1324. *
  1325. * @example The polymorphic "hasOne" relation with a target relation name.
  1326. * ```
  1327. * {
  1328. * type: RelationType.HAS_ONE,
  1329. * model: 'model',
  1330. * polymorphic: 'reference',
  1331. * }
  1332. * ```
  1333. *
  1334. * @example The polymorphic "hasOne" relation with target relation keys.
  1335. * ```
  1336. * {
  1337. * type: RelationType.HAS_ONE,
  1338. * model: 'model',
  1339. * polymorphic: true,
  1340. * foreignKey: 'referenceId',
  1341. * discriminator: 'referenceType',
  1342. * }
  1343. * ```
  1344. *
  1345. * @param {string} modelName
  1346. * @param {string} relName
  1347. * @param {object} relDef
  1348. * @private
  1349. */
  1350. _validateHasOne(modelName, relName, relDef) {
  1351. if (relDef.type !== RelationType.HAS_ONE) return;
  1352. if (!relDef.model || typeof relDef.model !== "string")
  1353. throw new InvalidArgumentError(
  1354. '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 was given.',
  1355. relName,
  1356. modelName,
  1357. relDef.model
  1358. );
  1359. if (relDef.polymorphic) {
  1360. if (typeof relDef.polymorphic === "string") {
  1361. if (relDef.foreignKey)
  1362. throw new InvalidArgumentError(
  1363. '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.',
  1364. relName,
  1365. modelName
  1366. );
  1367. if (relDef.discriminator)
  1368. throw new InvalidArgumentError(
  1369. '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.',
  1370. relName,
  1371. modelName
  1372. );
  1373. } else if (typeof relDef.polymorphic === "boolean") {
  1374. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1375. throw new InvalidArgumentError(
  1376. '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 was given.',
  1377. relName,
  1378. modelName,
  1379. relDef.foreignKey
  1380. );
  1381. if (!relDef.discriminator || typeof relDef.discriminator !== "string")
  1382. throw new InvalidArgumentError(
  1383. '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 was given.',
  1384. relName,
  1385. modelName,
  1386. relDef.discriminator
  1387. );
  1388. } else {
  1389. throw new InvalidArgumentError(
  1390. '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 was given.',
  1391. relName,
  1392. modelName,
  1393. relDef.polymorphic
  1394. );
  1395. }
  1396. } else {
  1397. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1398. throw new InvalidArgumentError(
  1399. '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 was given.',
  1400. relName,
  1401. modelName,
  1402. relDef.foreignKey
  1403. );
  1404. if (relDef.discriminator)
  1405. throw new InvalidArgumentError(
  1406. 'The relation %v of the model %v is a non-polymorphic "hasOne" relation, so it should not have the option "discriminator" to be provided.',
  1407. relName,
  1408. modelName
  1409. );
  1410. }
  1411. }
  1412. /**
  1413. * Validate "hasMany".
  1414. *
  1415. * @example The regular "hasMany" relation.
  1416. * ```
  1417. * {
  1418. * type: RelationType.HAS_MANY,
  1419. * model: 'model',
  1420. * foreignKey: 'modelId',
  1421. * }
  1422. * ```
  1423. *
  1424. * @example The polymorphic "hasMany" relation with a target relation name.
  1425. * ```
  1426. * {
  1427. * type: RelationType.HAS_MANY,
  1428. * model: 'model',
  1429. * polymorphic: 'reference',
  1430. * }
  1431. * ```
  1432. *
  1433. * @example The polymorphic "hasMany" relation with target relation keys.
  1434. * ```
  1435. * {
  1436. * type: RelationType.HAS_MANY,
  1437. * model: 'model',
  1438. * polymorphic: true,
  1439. * foreignKey: 'referenceId',
  1440. * discriminator: 'referenceType',
  1441. * }
  1442. * ```
  1443. *
  1444. * @param {string} modelName
  1445. * @param {string} relName
  1446. * @param {object} relDef
  1447. * @private
  1448. */
  1449. _validateHasMany(modelName, relName, relDef) {
  1450. if (relDef.type !== RelationType.HAS_MANY) return;
  1451. if (!relDef.model || typeof relDef.model !== "string")
  1452. throw new InvalidArgumentError(
  1453. '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 was given.',
  1454. relName,
  1455. modelName,
  1456. relDef.model
  1457. );
  1458. if (relDef.polymorphic) {
  1459. if (typeof relDef.polymorphic === "string") {
  1460. if (relDef.foreignKey)
  1461. throw new InvalidArgumentError(
  1462. '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.',
  1463. relName,
  1464. modelName
  1465. );
  1466. if (relDef.discriminator)
  1467. throw new InvalidArgumentError(
  1468. '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.',
  1469. relName,
  1470. modelName
  1471. );
  1472. } else if (typeof relDef.polymorphic === "boolean") {
  1473. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1474. throw new InvalidArgumentError(
  1475. '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 was given.',
  1476. relName,
  1477. modelName,
  1478. relDef.foreignKey
  1479. );
  1480. if (!relDef.discriminator || typeof relDef.discriminator !== "string")
  1481. throw new InvalidArgumentError(
  1482. '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 was given.',
  1483. relName,
  1484. modelName,
  1485. relDef.discriminator
  1486. );
  1487. } else {
  1488. throw new InvalidArgumentError(
  1489. '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 was given.',
  1490. relName,
  1491. modelName,
  1492. relDef.polymorphic
  1493. );
  1494. }
  1495. } else {
  1496. if (!relDef.foreignKey || typeof relDef.foreignKey !== "string")
  1497. throw new InvalidArgumentError(
  1498. '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 was given.',
  1499. relName,
  1500. modelName,
  1501. relDef.foreignKey
  1502. );
  1503. if (relDef.discriminator)
  1504. throw new InvalidArgumentError(
  1505. 'The relation %v of the model %v is a non-polymorphic "hasMany" relation, so it should not have the option "discriminator" to be provided.',
  1506. relName,
  1507. modelName
  1508. );
  1509. }
  1510. }
  1511. /**
  1512. * Validate "referencesMany".
  1513. *
  1514. * @example
  1515. * ```
  1516. * {
  1517. * type: RelationType.REFERENCES_MANY,
  1518. * model: 'model',
  1519. * foreignKey: 'modelIds', // optional
  1520. * }
  1521. * ```
  1522. *
  1523. * @param {string} modelName
  1524. * @param {string} relName
  1525. * @param {object} relDef
  1526. * @private
  1527. */
  1528. _validateReferencesMany(modelName, relName, relDef) {
  1529. if (relDef.type !== RelationType.REFERENCES_MANY) return;
  1530. if (!relDef.model || typeof relDef.model !== "string")
  1531. throw new InvalidArgumentError(
  1532. '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 was given.',
  1533. relName,
  1534. modelName,
  1535. relDef.model
  1536. );
  1537. if (relDef.foreignKey && typeof relDef.foreignKey !== "string")
  1538. throw new InvalidArgumentError(
  1539. 'The relation %v of the model %v has the type "referencesMany", so it expects the provided option "foreignKey" to be a String, but %v was given.',
  1540. relName,
  1541. modelName,
  1542. relDef.foreignKey
  1543. );
  1544. if (relDef.discriminator)
  1545. throw new InvalidArgumentError(
  1546. 'The relation %v of the model %v has the type "referencesMany", so it should not have the option "discriminator" to be provided.',
  1547. relName,
  1548. modelName
  1549. );
  1550. }
  1551. };
  1552. __name(_RelationsDefinitionValidator, "RelationsDefinitionValidator");
  1553. RelationsDefinitionValidator = _RelationsDefinitionValidator;
  1554. }
  1555. });
  1556. // src/definition/model/relations/index.js
  1557. var init_relations = __esm({
  1558. "src/definition/model/relations/index.js"() {
  1559. init_relation_type();
  1560. init_relations_definition_validator();
  1561. }
  1562. });
  1563. // src/definition/model/properties/data-type.js
  1564. var DataType;
  1565. var init_data_type = __esm({
  1566. "src/definition/model/properties/data-type.js"() {
  1567. DataType = {
  1568. ANY: "any",
  1569. STRING: "string",
  1570. NUMBER: "number",
  1571. BOOLEAN: "boolean",
  1572. ARRAY: "array",
  1573. OBJECT: "object"
  1574. };
  1575. }
  1576. });
  1577. // src/definition/model/properties/property-uniqueness.js
  1578. var PropertyUniqueness;
  1579. var init_property_uniqueness = __esm({
  1580. "src/definition/model/properties/property-uniqueness.js"() {
  1581. PropertyUniqueness = {
  1582. STRICT: "strict",
  1583. SPARSE: "sparse",
  1584. NON_UNIQUE: "nonUnique"
  1585. };
  1586. }
  1587. });
  1588. // src/definition/definition-registry.js
  1589. var import_js_service6, _DefinitionRegistry, DefinitionRegistry;
  1590. var init_definition_registry = __esm({
  1591. "src/definition/definition-registry.js"() {
  1592. import_js_service6 = require("@e22m4u/js-service");
  1593. init_utils();
  1594. init_errors();
  1595. init_model();
  1596. init_definition();
  1597. _DefinitionRegistry = class _DefinitionRegistry extends import_js_service6.Service {
  1598. /**
  1599. * Datasources.
  1600. *
  1601. * @type {object}
  1602. */
  1603. _datasources = {};
  1604. /**
  1605. * Models.
  1606. *
  1607. * @type {object}
  1608. */
  1609. _models = {};
  1610. /**
  1611. * Add datasource.
  1612. *
  1613. * @param {object} datasourceDef
  1614. */
  1615. addDatasource(datasourceDef) {
  1616. this.getService(DatasourceDefinitionValidator).validate(datasourceDef);
  1617. const name = datasourceDef.name;
  1618. if (name in this._datasources)
  1619. throw new InvalidArgumentError(
  1620. "The datasource %v is already defined.",
  1621. name
  1622. );
  1623. this._datasources[name] = datasourceDef;
  1624. }
  1625. /**
  1626. * Has datasource.
  1627. *
  1628. * @param {string} name
  1629. * @returns {boolean}
  1630. */
  1631. hasDatasource(name) {
  1632. return Boolean(this._datasources[name]);
  1633. }
  1634. /**
  1635. * Get datasource.
  1636. *
  1637. * @param {string} name
  1638. * @returns {object}
  1639. */
  1640. getDatasource(name) {
  1641. const datasourceDef = this._datasources[name];
  1642. if (!datasourceDef)
  1643. throw new InvalidArgumentError("The datasource %v is not defined.", name);
  1644. return datasourceDef;
  1645. }
  1646. /**
  1647. * Add model.
  1648. *
  1649. * @param {object} modelDef
  1650. */
  1651. addModel(modelDef) {
  1652. this.getService(ModelDefinitionValidator).validate(modelDef);
  1653. const modelKey = modelNameToModelKey(modelDef.name);
  1654. if (modelKey in this._models)
  1655. throw new InvalidArgumentError(
  1656. "The model %v is already defined.",
  1657. modelDef.name
  1658. );
  1659. this._models[modelKey] = modelDef;
  1660. }
  1661. /**
  1662. * Has model.
  1663. *
  1664. * @param {string} name
  1665. * @returns {boolean}
  1666. */
  1667. hasModel(name) {
  1668. const modelKey = modelNameToModelKey(name);
  1669. return Boolean(this._models[modelKey]);
  1670. }
  1671. /**
  1672. * Get model.
  1673. *
  1674. * @param {string} name
  1675. * @returns {object}
  1676. */
  1677. getModel(name) {
  1678. const modelKey = modelNameToModelKey(name);
  1679. const modelDef = this._models[modelKey];
  1680. if (!modelDef)
  1681. throw new InvalidArgumentError("The model %v is not defined.", name);
  1682. return modelDef;
  1683. }
  1684. };
  1685. __name(_DefinitionRegistry, "DefinitionRegistry");
  1686. DefinitionRegistry = _DefinitionRegistry;
  1687. }
  1688. });
  1689. // src/definition/model/model-definition-utils.js
  1690. var import_js_service7, import_js_empty_values, DEFAULT_PRIMARY_KEY_PROPERTY_NAME, _ModelDefinitionUtils, ModelDefinitionUtils;
  1691. var init_model_definition_utils = __esm({
  1692. "src/definition/model/model-definition-utils.js"() {
  1693. import_js_service7 = require("@e22m4u/js-service");
  1694. init_properties();
  1695. import_js_empty_values = require("@e22m4u/js-empty-values");
  1696. init_errors();
  1697. init_definition_registry();
  1698. init_utils();
  1699. DEFAULT_PRIMARY_KEY_PROPERTY_NAME = "id";
  1700. _ModelDefinitionUtils = class _ModelDefinitionUtils extends import_js_service7.Service {
  1701. /**
  1702. * Get primary key as property name.
  1703. *
  1704. * @param {string} modelName
  1705. * @returns {string}
  1706. */
  1707. getPrimaryKeyAsPropertyName(modelName) {
  1708. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1709. const propNames = Object.keys(propDefs).filter((propName) => {
  1710. const propDef = propDefs[propName];
  1711. return propDef && typeof propDef === "object" && propDef.primaryKey;
  1712. });
  1713. if (propNames.length < 1) {
  1714. const isDefaultPrimaryKeyAlreadyInUse = Object.keys(propDefs).includes(
  1715. DEFAULT_PRIMARY_KEY_PROPERTY_NAME
  1716. );
  1717. if (isDefaultPrimaryKeyAlreadyInUse)
  1718. throw new InvalidArgumentError(
  1719. '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.',
  1720. DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  1721. modelName
  1722. );
  1723. return DEFAULT_PRIMARY_KEY_PROPERTY_NAME;
  1724. }
  1725. return propNames[0];
  1726. }
  1727. /**
  1728. * Get primary key as column name.
  1729. *
  1730. * @param {string} modelName
  1731. * @returns {string}
  1732. */
  1733. getPrimaryKeyAsColumnName(modelName) {
  1734. const pkPropName = this.getPrimaryKeyAsPropertyName(modelName);
  1735. let pkColName;
  1736. try {
  1737. pkColName = this.getColumnNameByPropertyName(modelName, pkPropName);
  1738. } catch (error) {
  1739. if (!(error instanceof InvalidArgumentError)) throw error;
  1740. }
  1741. if (pkColName === void 0) return pkPropName;
  1742. return pkColName;
  1743. }
  1744. /**
  1745. * Get table name by model name.
  1746. *
  1747. * @param {string} modelName
  1748. * @returns {string}
  1749. */
  1750. getTableNameByModelName(modelName) {
  1751. var _a;
  1752. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  1753. return (_a = modelDef.tableName) != null ? _a : modelName;
  1754. }
  1755. /**
  1756. * Get column name by property name.
  1757. *
  1758. * @param {string} modelName
  1759. * @param {string} propertyName
  1760. * @returns {string}
  1761. */
  1762. getColumnNameByPropertyName(modelName, propertyName) {
  1763. var _a;
  1764. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1765. const propDef = propDefs[propertyName];
  1766. if (!propDef)
  1767. throw new InvalidArgumentError(
  1768. "The model %v does not have the property %v.",
  1769. modelName,
  1770. propertyName
  1771. );
  1772. if (propDef && typeof propDef === "object")
  1773. return (_a = propDef.columnName) != null ? _a : propertyName;
  1774. return propertyName;
  1775. }
  1776. /**
  1777. * Get default property value.
  1778. *
  1779. * @param {string} modelName
  1780. * @param {string} propertyName
  1781. * @returns {*}
  1782. */
  1783. getDefaultPropertyValue(modelName, propertyName) {
  1784. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1785. const propDef = propDefs[propertyName];
  1786. if (!propDef)
  1787. throw new InvalidArgumentError(
  1788. "The model %v does not have the property %v.",
  1789. modelName,
  1790. propertyName
  1791. );
  1792. if (propDef && typeof propDef === "object")
  1793. return propDef.default instanceof Function ? propDef.default() : propDef.default;
  1794. }
  1795. /**
  1796. * Set default values for empty properties.
  1797. *
  1798. * @param {string} modelName
  1799. * @param {object} modelData
  1800. * @param {boolean|undefined} onlyProvidedProperties
  1801. * @returns {object}
  1802. */
  1803. setDefaultValuesToEmptyProperties(modelName, modelData, onlyProvidedProperties = false) {
  1804. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1805. const propNames = onlyProvidedProperties ? Object.keys(modelData) : Object.keys(propDefs);
  1806. const extendedData = cloneDeep(modelData);
  1807. const emptyValuesService = this.getService(import_js_empty_values.EmptyValuesService);
  1808. propNames.forEach((propName) => {
  1809. const propDef = propDefs[propName];
  1810. const propValue = extendedData[propName];
  1811. const propType = propDef != null ? this.getDataTypeFromPropertyDefinition(propDef) : DataType.ANY;
  1812. const isEmpty = emptyValuesService.isEmptyByType(propType, propValue);
  1813. if (!isEmpty) return;
  1814. if (propDef && typeof propDef === "object" && propDef.default !== void 0) {
  1815. extendedData[propName] = this.getDefaultPropertyValue(
  1816. modelName,
  1817. propName
  1818. );
  1819. }
  1820. });
  1821. return extendedData;
  1822. }
  1823. /**
  1824. * Convert property names to column names.
  1825. *
  1826. * @param {string} modelName
  1827. * @param {object} modelData
  1828. * @returns {object}
  1829. */
  1830. convertPropertyNamesToColumnNames(modelName, modelData) {
  1831. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1832. const propNames = Object.keys(propDefs);
  1833. const convertedData = cloneDeep(modelData);
  1834. propNames.forEach((propName) => {
  1835. if (!(propName in convertedData)) return;
  1836. const colName = this.getColumnNameByPropertyName(modelName, propName);
  1837. let propValue = convertedData[propName];
  1838. const propDef = propDefs[propName];
  1839. if (propValue !== null && typeof propValue === "object" && !Array.isArray(propValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.OBJECT && propDef.model) {
  1840. propValue = this.convertPropertyNamesToColumnNames(
  1841. propDef.model,
  1842. propValue
  1843. );
  1844. }
  1845. if (Array.isArray(propValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.ARRAY && propDef.itemModel) {
  1846. propValue = propValue.map((el) => {
  1847. return el !== null && typeof el === "object" && !Array.isArray(el) ? this.convertPropertyNamesToColumnNames(propDef.itemModel, el) : el;
  1848. });
  1849. }
  1850. delete convertedData[propName];
  1851. convertedData[colName] = propValue;
  1852. });
  1853. return convertedData;
  1854. }
  1855. /**
  1856. * Convert column names to property names.
  1857. *
  1858. * @param {string} modelName
  1859. * @param {object} tableData
  1860. * @returns {object}
  1861. */
  1862. convertColumnNamesToPropertyNames(modelName, tableData) {
  1863. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1864. const propNames = Object.keys(propDefs);
  1865. const convertedData = cloneDeep(tableData);
  1866. propNames.forEach((propName) => {
  1867. const colName = this.getColumnNameByPropertyName(modelName, propName);
  1868. if (!(colName in convertedData)) return;
  1869. let colValue = convertedData[colName];
  1870. const propDef = propDefs[propName];
  1871. if (colValue !== null && typeof colValue === "object" && !Array.isArray(colValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.OBJECT && propDef.model) {
  1872. colValue = this.convertColumnNamesToPropertyNames(
  1873. propDef.model,
  1874. colValue
  1875. );
  1876. }
  1877. if (Array.isArray(colValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.ARRAY && propDef.itemModel) {
  1878. colValue = colValue.map((el) => {
  1879. return el !== null && typeof el === "object" && !Array.isArray(el) ? this.convertColumnNamesToPropertyNames(propDef.itemModel, el) : el;
  1880. });
  1881. }
  1882. delete convertedData[colName];
  1883. convertedData[propName] = colValue;
  1884. });
  1885. return convertedData;
  1886. }
  1887. /**
  1888. * Get data type by property name.
  1889. *
  1890. * @param {string} modelName
  1891. * @param {string} propertyName
  1892. * @returns {string}
  1893. */
  1894. getDataTypeByPropertyName(modelName, propertyName) {
  1895. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  1896. const propDef = propDefs[propertyName];
  1897. if (!propDef) {
  1898. const pkPropName = this.getPrimaryKeyAsPropertyName(modelName);
  1899. if (pkPropName === propertyName) return DataType.ANY;
  1900. throw new InvalidArgumentError(
  1901. "The model %v does not have the property %v.",
  1902. modelName,
  1903. propertyName
  1904. );
  1905. }
  1906. if (typeof propDef === "string") return propDef;
  1907. return propDef.type;
  1908. }
  1909. /**
  1910. * Get data type from property definition.
  1911. *
  1912. * @param {object} propDef
  1913. * @returns {string}
  1914. */
  1915. getDataTypeFromPropertyDefinition(propDef) {
  1916. if ((!propDef || typeof propDef !== "object") && !Object.values(DataType).includes(propDef)) {
  1917. throw new InvalidArgumentError(
  1918. 'The argument "propDef" of the ModelDefinitionUtils.getDataTypeFromPropertyDefinition should be an Object or the DataType enum, but %v was given.',
  1919. propDef
  1920. );
  1921. }
  1922. if (typeof propDef === "string") return propDef;
  1923. const dataType = propDef.type;
  1924. if (!Object.values(DataType).includes(dataType))
  1925. throw new InvalidArgumentError(
  1926. 'The given Object to the ModelDefinitionUtils.getDataTypeFromPropertyDefinition should have the "type" property with one of values: %l, but %v was given.',
  1927. Object.values(DataType),
  1928. propDef.type
  1929. );
  1930. return dataType;
  1931. }
  1932. /**
  1933. * Get own properties definition of primary keys.
  1934. *
  1935. * @param {string} modelName
  1936. * @returns {object}
  1937. */
  1938. getOwnPropertiesDefinitionOfPrimaryKeys(modelName) {
  1939. var _a;
  1940. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  1941. const propDefs = (_a = modelDef.properties) != null ? _a : {};
  1942. const pkPropNames = Object.keys(propDefs).filter((propName) => {
  1943. const propDef = propDefs[propName];
  1944. return typeof propDef === "object" && propDef.primaryKey;
  1945. });
  1946. return pkPropNames.reduce((a, k) => ({ ...a, [k]: propDefs[k] }), {});
  1947. }
  1948. /**
  1949. * Get own properties definition without primary keys.
  1950. *
  1951. * @param {string} modelName
  1952. * @returns {object}
  1953. */
  1954. getOwnPropertiesDefinitionWithoutPrimaryKeys(modelName) {
  1955. var _a;
  1956. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  1957. const propDefs = (_a = modelDef.properties) != null ? _a : {};
  1958. return Object.keys(propDefs).reduce((result, propName) => {
  1959. const propDef = propDefs[propName];
  1960. if (typeof propDef === "object" && propDef.primaryKey) return result;
  1961. return { ...result, [propName]: propDef };
  1962. }, {});
  1963. }
  1964. /**
  1965. * Get properties definition in base model hierarchy.
  1966. *
  1967. * @param {string} modelName
  1968. * @returns {object}
  1969. */
  1970. getPropertiesDefinitionInBaseModelHierarchy(modelName) {
  1971. let pkPropDefs = {};
  1972. let regularPropDefs = {};
  1973. const recursion = /* @__PURE__ */ __name((currModelName, prevModelName = void 0) => {
  1974. if (currModelName === prevModelName)
  1975. throw new InvalidArgumentError(
  1976. "The model %v has a circular inheritance.",
  1977. currModelName
  1978. );
  1979. if (Object.keys(pkPropDefs).length === 0)
  1980. pkPropDefs = this.getOwnPropertiesDefinitionOfPrimaryKeys(currModelName);
  1981. regularPropDefs = {
  1982. ...this.getOwnPropertiesDefinitionWithoutPrimaryKeys(currModelName),
  1983. ...regularPropDefs
  1984. };
  1985. const modelDef = this.getService(DefinitionRegistry).getModel(currModelName);
  1986. if (modelDef.base) recursion(modelDef.base, currModelName);
  1987. }, "recursion");
  1988. recursion(modelName);
  1989. return { ...pkPropDefs, ...regularPropDefs };
  1990. }
  1991. /**
  1992. * Get own relations definition.
  1993. *
  1994. * @param {string} modelName
  1995. * @returns {object}
  1996. */
  1997. getOwnRelationsDefinition(modelName) {
  1998. var _a;
  1999. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  2000. return (_a = modelDef.relations) != null ? _a : {};
  2001. }
  2002. /**
  2003. * Get relations definition in base model hierarchy.
  2004. *
  2005. * @param {string} modelName
  2006. * @returns {object}
  2007. */
  2008. getRelationsDefinitionInBaseModelHierarchy(modelName) {
  2009. let result = {};
  2010. const recursion = /* @__PURE__ */ __name((currModelName, prevModelName = void 0) => {
  2011. var _a;
  2012. if (currModelName === prevModelName)
  2013. throw new InvalidArgumentError(
  2014. "The model %v has a circular inheritance.",
  2015. currModelName
  2016. );
  2017. const modelDef = this.getService(DefinitionRegistry).getModel(currModelName);
  2018. const ownRelDefs = (_a = modelDef.relations) != null ? _a : {};
  2019. result = { ...ownRelDefs, ...result };
  2020. if (modelDef.base) recursion(modelDef.base, currModelName);
  2021. }, "recursion");
  2022. recursion(modelName);
  2023. return result;
  2024. }
  2025. /**
  2026. * Get relation definition by name.
  2027. *
  2028. * @param {string} modelName
  2029. * @param {string} relationName
  2030. * @returns {object}
  2031. */
  2032. getRelationDefinitionByName(modelName, relationName) {
  2033. const relDefs = this.getRelationsDefinitionInBaseModelHierarchy(modelName);
  2034. const relNames = Object.keys(relDefs);
  2035. let foundDef;
  2036. for (const relName of relNames) {
  2037. if (relName === relationName) {
  2038. foundDef = relDefs[relName];
  2039. break;
  2040. }
  2041. }
  2042. if (!foundDef)
  2043. throw new InvalidArgumentError(
  2044. "The model %v does not have relation name %v.",
  2045. modelName,
  2046. relationName
  2047. );
  2048. return foundDef;
  2049. }
  2050. /**
  2051. * Exclude object keys by relation names.
  2052. *
  2053. * @param {string} modelName
  2054. * @param {object} modelData
  2055. * @returns {object}
  2056. */
  2057. excludeObjectKeysByRelationNames(modelName, modelData) {
  2058. if (!modelData || typeof modelData !== "object" || Array.isArray(modelData))
  2059. throw new InvalidArgumentError(
  2060. "The second argument of ModelDefinitionUtils.excludeObjectKeysByRelationNames should be an Object, but %v was given.",
  2061. modelData
  2062. );
  2063. const relDefs = this.getRelationsDefinitionInBaseModelHierarchy(modelName);
  2064. const relNames = Object.keys(relDefs);
  2065. return excludeObjectKeys(modelData, relNames);
  2066. }
  2067. /**
  2068. * Get model name of property value if defined.
  2069. *
  2070. * @param {string} modelName
  2071. * @param {string} propertyName
  2072. * @returns {string|undefined}
  2073. */
  2074. getModelNameOfPropertyValueIfDefined(modelName, propertyName) {
  2075. if (!modelName || typeof modelName !== "string")
  2076. throw new InvalidArgumentError(
  2077. 'Parameter "modelName" of ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined requires a non-empty String, but %v was given.',
  2078. modelName
  2079. );
  2080. if (!propertyName || typeof propertyName !== "string")
  2081. throw new InvalidArgumentError(
  2082. 'Parameter "propertyName" of ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined requires a non-empty String, but %v was given.',
  2083. propertyName
  2084. );
  2085. const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2086. const propDef = propDefs[propertyName];
  2087. if (!propDef) return void 0;
  2088. if (propDef && typeof propDef === "object") {
  2089. if (propDef.type === DataType.OBJECT) return propDef.model || void 0;
  2090. if (propDef.type === DataType.ARRAY)
  2091. return propDef.itemModel || void 0;
  2092. }
  2093. return void 0;
  2094. }
  2095. };
  2096. __name(_ModelDefinitionUtils, "ModelDefinitionUtils");
  2097. ModelDefinitionUtils = _ModelDefinitionUtils;
  2098. }
  2099. });
  2100. // src/definition/model/properties/property-uniqueness-validator.js
  2101. var import_js_service8, import_js_empty_values2, _PropertyUniquenessValidator, PropertyUniquenessValidator;
  2102. var init_property_uniqueness_validator = __esm({
  2103. "src/definition/model/properties/property-uniqueness-validator.js"() {
  2104. init_data_type();
  2105. import_js_service8 = require("@e22m4u/js-service");
  2106. init_utils();
  2107. import_js_empty_values2 = require("@e22m4u/js-empty-values");
  2108. init_property_uniqueness();
  2109. init_errors();
  2110. init_model_definition_utils();
  2111. _PropertyUniquenessValidator = class _PropertyUniquenessValidator extends import_js_service8.Service {
  2112. /**
  2113. * Validate.
  2114. *
  2115. * @param {Function} countMethod
  2116. * @param {string} methodName
  2117. * @param {string} modelName
  2118. * @param {object} modelData
  2119. * @param {*} modelId
  2120. * @returns {Promise<undefined>}
  2121. */
  2122. async validate(countMethod, methodName, modelName, modelData, modelId = void 0) {
  2123. if (typeof countMethod !== "function")
  2124. throw new InvalidArgumentError(
  2125. 'The parameter "countMethod" of the PropertyUniquenessValidator must be a Function, but %v was given.',
  2126. countMethod
  2127. );
  2128. if (!methodName || typeof methodName !== "string")
  2129. throw new InvalidArgumentError(
  2130. 'The parameter "methodName" of the PropertyUniquenessValidator must be a non-empty String, but %v was given.',
  2131. methodName
  2132. );
  2133. if (!modelName || typeof modelName !== "string")
  2134. throw new InvalidArgumentError(
  2135. 'The parameter "modelName" of the PropertyUniquenessValidator must be a non-empty String, but %v was given.',
  2136. modelName
  2137. );
  2138. if (!isPlainObject(modelData))
  2139. throw new InvalidArgumentError(
  2140. "The data of the model %v should be an Object, but %v was given.",
  2141. modelName,
  2142. modelData
  2143. );
  2144. const propDefs = this.getService(
  2145. ModelDefinitionUtils
  2146. ).getPropertiesDefinitionInBaseModelHierarchy(modelName);
  2147. const isPartial = methodName === "patch" || methodName === "patchById";
  2148. const propNames = Object.keys(isPartial ? modelData : propDefs);
  2149. const idProp = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  2150. modelName
  2151. );
  2152. const createError = /* @__PURE__ */ __name((propName, propValue) => new InvalidArgumentError(
  2153. "An existing document of the model %v already has the property %v with the value %v and should be unique.",
  2154. modelName,
  2155. propName,
  2156. propValue
  2157. ), "createError");
  2158. let willBeReplaced = void 0;
  2159. const emptyValuesService = this.getService(import_js_empty_values2.EmptyValuesService);
  2160. for (const propName of propNames) {
  2161. const propDef = propDefs[propName];
  2162. if (!propDef || typeof propDef === "string" || !propDef.unique || propDef.unique === PropertyUniqueness.NON_UNIQUE) {
  2163. continue;
  2164. }
  2165. const propValue = modelData[propName];
  2166. if (propDef.unique === PropertyUniqueness.SPARSE) {
  2167. const propType = propDef.type || DataType.ANY;
  2168. const isEmpty = emptyValuesService.isEmptyByType(propType, propValue);
  2169. if (isEmpty) continue;
  2170. }
  2171. if (methodName === "create") {
  2172. const count = await countMethod({ [propName]: propValue });
  2173. if (count > 0) throw createError(propName, propValue);
  2174. } else if (methodName === "replaceById") {
  2175. const count = await countMethod({
  2176. [idProp]: { neq: modelId },
  2177. [propName]: propValue
  2178. });
  2179. if (count > 0) throw createError(propName, propValue);
  2180. } else if (methodName === "replaceOrCreate") {
  2181. const idFromData = modelData[idProp];
  2182. if (willBeReplaced == null && idFromData != null) {
  2183. const count = await countMethod({ [idProp]: idFromData });
  2184. willBeReplaced = count > 0;
  2185. }
  2186. if (willBeReplaced) {
  2187. const count = await countMethod({
  2188. [idProp]: { neq: idFromData },
  2189. [propName]: propValue
  2190. });
  2191. if (count > 0) throw createError(propName, propValue);
  2192. } else {
  2193. const count = await countMethod({ [propName]: propValue });
  2194. if (count > 0) throw createError(propName, propValue);
  2195. }
  2196. } else if (methodName === "patch") {
  2197. const count = await countMethod({ [propName]: propValue });
  2198. if (count > 0) throw createError(propName, propValue);
  2199. } else if (methodName === "patchById") {
  2200. const count = await countMethod({
  2201. [idProp]: { neq: modelId },
  2202. [propName]: propValue
  2203. });
  2204. if (count > 0) throw createError(propName, propValue);
  2205. } else {
  2206. throw new InvalidArgumentError(
  2207. "The PropertyUniquenessValidator does not support the adapter method %v.",
  2208. methodName
  2209. );
  2210. }
  2211. }
  2212. }
  2213. };
  2214. __name(_PropertyUniquenessValidator, "PropertyUniquenessValidator");
  2215. PropertyUniquenessValidator = _PropertyUniquenessValidator;
  2216. }
  2217. });
  2218. // src/definition/model/properties/primary-keys-definition-validator.js
  2219. var import_js_service9, _PrimaryKeysDefinitionValidator, PrimaryKeysDefinitionValidator;
  2220. var init_primary_keys_definition_validator = __esm({
  2221. "src/definition/model/properties/primary-keys-definition-validator.js"() {
  2222. import_js_service9 = require("@e22m4u/js-service");
  2223. init_errors();
  2224. init_model_definition_utils();
  2225. _PrimaryKeysDefinitionValidator = class _PrimaryKeysDefinitionValidator extends import_js_service9.Service {
  2226. /**
  2227. * Validate.
  2228. *
  2229. * @param {string} modelName
  2230. * @param {object} propDefs
  2231. */
  2232. validate(modelName, propDefs) {
  2233. const propNames = Object.keys(propDefs).filter((propName) => {
  2234. const propDef = propDefs[propName];
  2235. return propDef && typeof propDef === "object" && propDef.primaryKey;
  2236. });
  2237. if (propNames.length < 1) {
  2238. const isDefaultPrimaryKeyAlreadyInUse = Object.keys(propDefs).includes(DEFAULT_PRIMARY_KEY_PROPERTY_NAME);
  2239. if (isDefaultPrimaryKeyAlreadyInUse)
  2240. throw new InvalidArgumentError(
  2241. '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.',
  2242. DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  2243. modelName
  2244. );
  2245. return;
  2246. }
  2247. if (propNames.length > 1)
  2248. throw new InvalidArgumentError(
  2249. "The model definition %v should not have multiple primary keys, but %v keys given.",
  2250. modelName,
  2251. propNames.length
  2252. );
  2253. const pkPropName = propNames[0];
  2254. const pkPropDef = propDefs[pkPropName];
  2255. if (pkPropDef && typeof pkPropDef === "object" && pkPropDef.default !== void 0) {
  2256. throw new InvalidArgumentError(
  2257. "Do not specify a default value for the primary key %v of the model %v.",
  2258. pkPropName,
  2259. modelName
  2260. );
  2261. }
  2262. }
  2263. };
  2264. __name(_PrimaryKeysDefinitionValidator, "PrimaryKeysDefinitionValidator");
  2265. PrimaryKeysDefinitionValidator = _PrimaryKeysDefinitionValidator;
  2266. }
  2267. });
  2268. // src/definition/model/properties/properties-definition-validator.js
  2269. var import_js_service10, _PropertiesDefinitionValidator, PropertiesDefinitionValidator;
  2270. var init_properties_definition_validator = __esm({
  2271. "src/definition/model/properties/properties-definition-validator.js"() {
  2272. import_js_service10 = require("@e22m4u/js-service");
  2273. init_data_type();
  2274. init_utils();
  2275. init_property_uniqueness();
  2276. init_errors();
  2277. init_primary_keys_definition_validator();
  2278. _PropertiesDefinitionValidator = class _PropertiesDefinitionValidator extends import_js_service10.Service {
  2279. /**
  2280. * Validate.
  2281. *
  2282. * @param {string} modelName
  2283. * @param {object} propDefs
  2284. */
  2285. validate(modelName, propDefs) {
  2286. if (!modelName || typeof modelName !== "string")
  2287. throw new InvalidArgumentError(
  2288. "The first argument of PropertiesDefinitionValidator.validate should be a non-empty String, but %v was given.",
  2289. modelName
  2290. );
  2291. if (!propDefs || typeof propDefs !== "object" || Array.isArray(propDefs)) {
  2292. throw new InvalidArgumentError(
  2293. 'The provided option "properties" of the model %v should be an Object, but %v was given.',
  2294. modelName,
  2295. propDefs
  2296. );
  2297. }
  2298. const propNames = Object.keys(propDefs);
  2299. propNames.forEach((propName) => {
  2300. const propDef = propDefs[propName];
  2301. this._validateProperty(modelName, propName, propDef);
  2302. });
  2303. this.getService(PrimaryKeysDefinitionValidator).validate(
  2304. modelName,
  2305. propDefs
  2306. );
  2307. }
  2308. /**
  2309. * Validate property.
  2310. *
  2311. * @param {string} modelName
  2312. * @param {string} propName
  2313. * @param {object} propDef
  2314. */
  2315. _validateProperty(modelName, propName, propDef) {
  2316. if (!modelName || typeof modelName !== "string")
  2317. throw new InvalidArgumentError(
  2318. "The first argument of PropertiesDefinitionValidator._validateProperty should be a non-empty String, but %v was given.",
  2319. modelName
  2320. );
  2321. if (!propName || typeof propName !== "string")
  2322. throw new InvalidArgumentError(
  2323. "The property name of the model %v should be a non-empty String, but %v was given.",
  2324. modelName,
  2325. propName
  2326. );
  2327. if (!propDef)
  2328. throw new InvalidArgumentError(
  2329. "The property %v of the model %v should have a property definition, but %v was given.",
  2330. propName,
  2331. modelName,
  2332. propDef
  2333. );
  2334. if (typeof propDef === "string") {
  2335. if (!Object.values(DataType).includes(propDef))
  2336. throw new InvalidArgumentError(
  2337. "In case of a short property definition, the property %v of the model %v should have one of data types: %l, but %v was given.",
  2338. propName,
  2339. modelName,
  2340. Object.values(DataType),
  2341. propDef
  2342. );
  2343. return;
  2344. }
  2345. if (!propDef || typeof propDef !== "object" || Array.isArray(propDef)) {
  2346. throw new InvalidArgumentError(
  2347. "In case of a full property definition, the property %v of the model %v should be an Object, but %v was given.",
  2348. propName,
  2349. modelName,
  2350. propDef
  2351. );
  2352. }
  2353. if (!propDef.type || !Object.values(DataType).includes(propDef.type))
  2354. throw new InvalidArgumentError(
  2355. 'The property %v of the model %v requires the option "type" to have one of data types: %l, but %v was given.',
  2356. propName,
  2357. modelName,
  2358. Object.values(DataType),
  2359. propDef.type
  2360. );
  2361. if (propDef.itemType && !Object.values(DataType).includes(propDef.itemType)) {
  2362. throw new InvalidArgumentError(
  2363. 'The provided option "itemType" of the property %v in the model %v should have one of data types: %l, but %v was given.',
  2364. propName,
  2365. modelName,
  2366. Object.values(DataType),
  2367. propDef.itemType
  2368. );
  2369. }
  2370. if (propDef.itemModel && typeof propDef.itemModel !== "string") {
  2371. throw new InvalidArgumentError(
  2372. 'The provided option "itemModel" of the property %v in the model %v should be a String, but %v was given.',
  2373. propName,
  2374. modelName,
  2375. propDef.itemModel
  2376. );
  2377. }
  2378. if (propDef.model && typeof propDef.model !== "string")
  2379. throw new InvalidArgumentError(
  2380. 'The provided option "model" of the property %v in the model %v should be a String, but %v was given.',
  2381. propName,
  2382. modelName,
  2383. propDef.model
  2384. );
  2385. if (propDef.primaryKey && typeof propDef.primaryKey !== "boolean")
  2386. throw new InvalidArgumentError(
  2387. 'The provided option "primaryKey" of the property %v in the model %v should be a Boolean, but %v was given.',
  2388. propName,
  2389. modelName,
  2390. propDef.primaryKey
  2391. );
  2392. if (propDef.columnName && typeof propDef.columnName !== "string")
  2393. throw new InvalidArgumentError(
  2394. 'The provided option "columnName" of the property %v in the model %v should be a String, but %v was given.',
  2395. propName,
  2396. modelName,
  2397. propDef.columnName
  2398. );
  2399. if (propDef.columnType && typeof propDef.columnType !== "string")
  2400. throw new InvalidArgumentError(
  2401. 'The provided option "columnType" of the property %v in the model %v should be a String, but %v was given.',
  2402. propName,
  2403. modelName,
  2404. propDef.columnType
  2405. );
  2406. if (propDef.required && typeof propDef.required !== "boolean")
  2407. throw new InvalidArgumentError(
  2408. 'The provided option "required" of the property %v in the model %v should be a Boolean, but %v was given.',
  2409. propName,
  2410. modelName,
  2411. propDef.required
  2412. );
  2413. if (propDef.required && propDef.default !== void 0)
  2414. throw new InvalidArgumentError(
  2415. 'The property %v of the model %v is a required property, so it should not have the option "default" to be provided.',
  2416. propName,
  2417. modelName
  2418. );
  2419. if (propDef.primaryKey && propDef.required)
  2420. throw new InvalidArgumentError(
  2421. 'The property %v of the model %v is a primary key, so it should not have the option "required" to be provided.',
  2422. propName,
  2423. modelName
  2424. );
  2425. if (propDef.primaryKey && propDef.default !== void 0)
  2426. throw new InvalidArgumentError(
  2427. 'The property %v of the model %v is a primary key, so it should not have the option "default" to be provided.',
  2428. propName,
  2429. modelName
  2430. );
  2431. if (propDef.itemType && propDef.type !== DataType.ARRAY)
  2432. throw new InvalidArgumentError(
  2433. 'The property %v of the model %v has a non-array type, so it should not have the option "itemType" to be provided.',
  2434. propName,
  2435. modelName,
  2436. propDef.type
  2437. );
  2438. if (propDef.itemModel && propDef.type !== DataType.ARRAY)
  2439. throw new InvalidArgumentError(
  2440. 'The option "itemModel" is not supported for %s property type, so the property %v of the model %v should not have the option "itemModel" to be provided.',
  2441. capitalize(propDef.type),
  2442. propName,
  2443. modelName
  2444. );
  2445. if (propDef.itemModel && propDef.itemType !== DataType.OBJECT) {
  2446. if (propDef.itemType) {
  2447. throw new InvalidArgumentError(
  2448. 'The provided option "itemModel" requires the option "itemType" to be explicitly set to Object, but the property %v of the model %v has specified item type as %s.',
  2449. propName,
  2450. modelName,
  2451. capitalize(propDef.itemType)
  2452. );
  2453. } else {
  2454. throw new InvalidArgumentError(
  2455. 'The provided option "itemModel" requires the option "itemType" to be explicitly set to Object, but the property %v of the model %v does not have specified item type.',
  2456. propName,
  2457. modelName
  2458. );
  2459. }
  2460. }
  2461. if (propDef.model && propDef.type !== DataType.OBJECT)
  2462. throw new InvalidArgumentError(
  2463. '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.',
  2464. capitalize(propDef.type),
  2465. propName,
  2466. modelName
  2467. );
  2468. if (propDef.unique) {
  2469. if (typeof propDef.unique !== "boolean" && !Object.values(PropertyUniqueness).includes(propDef.unique)) {
  2470. throw new InvalidArgumentError(
  2471. 'The provided option "unique" of the property %v in the model %v should be a Boolean or one of values: %l, but %v was given.',
  2472. propName,
  2473. modelName,
  2474. Object.values(PropertyUniqueness),
  2475. propDef.unique
  2476. );
  2477. }
  2478. }
  2479. if (propDef.unique && propDef.primaryKey)
  2480. throw new InvalidArgumentError(
  2481. 'The property %v of the model %v is a primary key, so it should not have the option "unique" to be provided.',
  2482. propName,
  2483. modelName
  2484. );
  2485. }
  2486. };
  2487. __name(_PropertiesDefinitionValidator, "PropertiesDefinitionValidator");
  2488. PropertiesDefinitionValidator = _PropertiesDefinitionValidator;
  2489. }
  2490. });
  2491. // src/definition/model/properties/index.js
  2492. var init_properties = __esm({
  2493. "src/definition/model/properties/index.js"() {
  2494. init_data_type();
  2495. init_property_uniqueness();
  2496. init_property_uniqueness_validator();
  2497. init_properties_definition_validator();
  2498. init_primary_keys_definition_validator();
  2499. }
  2500. });
  2501. // src/definition/model/model-data-sanitizer.js
  2502. var import_js_service11, _ModelDataSanitizer, ModelDataSanitizer;
  2503. var init_model_data_sanitizer = __esm({
  2504. "src/definition/model/model-data-sanitizer.js"() {
  2505. import_js_service11 = require("@e22m4u/js-service");
  2506. init_errors();
  2507. init_model_definition_utils();
  2508. _ModelDataSanitizer = class _ModelDataSanitizer extends import_js_service11.Service {
  2509. /**
  2510. * Validate.
  2511. *
  2512. * @param {string} modelName
  2513. * @param {object} modelData
  2514. * @returns {object}
  2515. */
  2516. sanitize(modelName, modelData) {
  2517. if (!modelName || typeof modelName !== "string")
  2518. throw new InvalidArgumentError(
  2519. "The first argument of ModelDataSanitizer.sanitize should be a string, but %v was given.",
  2520. modelName
  2521. );
  2522. if (!modelData || typeof modelData !== "object")
  2523. throw new InvalidArgumentError(
  2524. "The second argument of ModelDataSanitizer.sanitize should be an Object, but %v was given.",
  2525. modelData
  2526. );
  2527. return this.getService(
  2528. ModelDefinitionUtils
  2529. ).excludeObjectKeysByRelationNames(modelName, modelData);
  2530. }
  2531. };
  2532. __name(_ModelDataSanitizer, "ModelDataSanitizer");
  2533. ModelDataSanitizer = _ModelDataSanitizer;
  2534. }
  2535. });
  2536. // src/definition/model/model-definition-validator.js
  2537. var import_js_service12, _ModelDefinitionValidator, ModelDefinitionValidator;
  2538. var init_model_definition_validator = __esm({
  2539. "src/definition/model/model-definition-validator.js"() {
  2540. import_js_service12 = require("@e22m4u/js-service");
  2541. init_errors();
  2542. init_relations();
  2543. init_properties();
  2544. _ModelDefinitionValidator = class _ModelDefinitionValidator extends import_js_service12.Service {
  2545. /**
  2546. * Validate.
  2547. *
  2548. * @param {object} modelDef
  2549. */
  2550. validate(modelDef) {
  2551. if (!modelDef || typeof modelDef !== "object" || Array.isArray(modelDef))
  2552. throw new InvalidArgumentError(
  2553. "The model definition should be an Object, but %v was given.",
  2554. modelDef
  2555. );
  2556. if (!modelDef.name || typeof modelDef.name !== "string")
  2557. throw new InvalidArgumentError(
  2558. 'The model definition requires the option "name" as a non-empty String, but %v was given.',
  2559. modelDef.name
  2560. );
  2561. if (modelDef.datasource && typeof modelDef.datasource !== "string")
  2562. throw new InvalidArgumentError(
  2563. 'The provided option "datasource" of the model %v should be a String, but %v was given.',
  2564. modelDef.name,
  2565. modelDef.datasource
  2566. );
  2567. if (modelDef.base && typeof modelDef.base !== "string")
  2568. throw new InvalidArgumentError(
  2569. 'The provided option "base" of the model %v should be a String, but %v was given.',
  2570. modelDef.name,
  2571. modelDef.base
  2572. );
  2573. if (modelDef.tableName && typeof modelDef.tableName !== "string")
  2574. throw new InvalidArgumentError(
  2575. 'The provided option "tableName" of the model %v should be a String, but %v was given.',
  2576. modelDef.name,
  2577. modelDef.tableName
  2578. );
  2579. if (modelDef.properties) {
  2580. if (typeof modelDef.properties !== "object" || Array.isArray(modelDef.properties)) {
  2581. throw new InvalidArgumentError(
  2582. 'The provided option "properties" of the model %v should be an Object, but %v was given.',
  2583. modelDef.name,
  2584. modelDef.properties
  2585. );
  2586. }
  2587. this.getService(PropertiesDefinitionValidator).validate(
  2588. modelDef.name,
  2589. modelDef.properties
  2590. );
  2591. }
  2592. if (modelDef.relations) {
  2593. if (typeof modelDef.relations !== "object" || Array.isArray(modelDef.relations)) {
  2594. throw new InvalidArgumentError(
  2595. 'The provided option "relations" of the model %v should be an Object, but %v was given.',
  2596. modelDef.name,
  2597. modelDef.relations
  2598. );
  2599. }
  2600. this.getService(RelationsDefinitionValidator).validate(
  2601. modelDef.name,
  2602. modelDef.relations
  2603. );
  2604. }
  2605. }
  2606. };
  2607. __name(_ModelDefinitionValidator, "ModelDefinitionValidator");
  2608. ModelDefinitionValidator = _ModelDefinitionValidator;
  2609. }
  2610. });
  2611. // src/definition/model/index.js
  2612. var init_model = __esm({
  2613. "src/definition/model/index.js"() {
  2614. init_relations();
  2615. init_properties();
  2616. init_model_data_sanitizer();
  2617. init_model_definition_utils();
  2618. init_model_definition_validator();
  2619. }
  2620. });
  2621. // src/definition/datasource/datasource-definition-validator.js
  2622. var import_js_service13, _DatasourceDefinitionValidator, DatasourceDefinitionValidator;
  2623. var init_datasource_definition_validator = __esm({
  2624. "src/definition/datasource/datasource-definition-validator.js"() {
  2625. import_js_service13 = require("@e22m4u/js-service");
  2626. init_errors();
  2627. _DatasourceDefinitionValidator = class _DatasourceDefinitionValidator extends import_js_service13.Service {
  2628. /**
  2629. * Validate.
  2630. *
  2631. * @param {object} datasourceDef
  2632. */
  2633. validate(datasourceDef) {
  2634. if (!datasourceDef || typeof datasourceDef !== "object")
  2635. throw new InvalidArgumentError(
  2636. "The datasource definition should be an Object, but %v was given.",
  2637. datasourceDef
  2638. );
  2639. if (!datasourceDef.name || typeof datasourceDef.name !== "string")
  2640. throw new InvalidArgumentError(
  2641. 'The datasource definition requires the option "name" as a non-empty String, but %v was given.',
  2642. datasourceDef.name
  2643. );
  2644. if (!datasourceDef.adapter || typeof datasourceDef.adapter !== "string")
  2645. throw new InvalidArgumentError(
  2646. 'The datasource %v requires the option "adapter" as a non-empty String, but %v was given.',
  2647. datasourceDef.name,
  2648. datasourceDef.adapter
  2649. );
  2650. }
  2651. };
  2652. __name(_DatasourceDefinitionValidator, "DatasourceDefinitionValidator");
  2653. DatasourceDefinitionValidator = _DatasourceDefinitionValidator;
  2654. }
  2655. });
  2656. // src/definition/datasource/index.js
  2657. var init_datasource = __esm({
  2658. "src/definition/datasource/index.js"() {
  2659. init_datasource_definition_validator();
  2660. }
  2661. });
  2662. // src/definition/index.js
  2663. var init_definition = __esm({
  2664. "src/definition/index.js"() {
  2665. init_model();
  2666. init_datasource();
  2667. init_definition_registry();
  2668. }
  2669. });
  2670. // src/filter/fields-clause-tool.js
  2671. var import_js_service14, _FieldsClauseTool, FieldsClauseTool;
  2672. var init_fields_clause_tool = __esm({
  2673. "src/filter/fields-clause-tool.js"() {
  2674. import_js_service14 = require("@e22m4u/js-service");
  2675. init_utils();
  2676. init_errors();
  2677. init_definition();
  2678. _FieldsClauseTool = class _FieldsClauseTool extends import_js_service14.Service {
  2679. /**
  2680. * Filter.
  2681. *
  2682. * @param {object|object[]} input
  2683. * @param {string} modelName
  2684. * @param {string|string[]|undefined} clause
  2685. * @returns {object|object[]}
  2686. */
  2687. filter(input, modelName, clause) {
  2688. const isArray = Array.isArray(input);
  2689. let entities = isArray ? input : [input];
  2690. entities.forEach((entity) => {
  2691. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  2692. throw new InvalidArgumentError(
  2693. "The first argument of FieldsClauseTool.filter should be an Object or an Array of Object, but %v was given.",
  2694. entity
  2695. );
  2696. });
  2697. if (!modelName || typeof modelName !== "string")
  2698. throw new InvalidArgumentError(
  2699. "The second argument of FieldsClauseTool.filter should be a non-empty String, but %v was given.",
  2700. modelName
  2701. );
  2702. if (clause == null) return input;
  2703. const fields = Array.isArray(clause) ? clause.slice() : [clause];
  2704. if (!fields.length) return input;
  2705. fields.forEach((field) => {
  2706. if (!field || typeof field !== "string")
  2707. throw new InvalidArgumentError(
  2708. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v was given.',
  2709. field
  2710. );
  2711. });
  2712. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  2713. modelName
  2714. );
  2715. if (fields.indexOf(pkPropName) === -1) fields.push(pkPropName);
  2716. entities = entities.map((entity) => selectObjectKeys(entity, fields));
  2717. return isArray ? entities : entities[0];
  2718. }
  2719. /**
  2720. * Validate fields clause.
  2721. *
  2722. * @param {string|string[]|undefined} clause
  2723. */
  2724. static validateFieldsClause(clause) {
  2725. if (clause == null) return;
  2726. const fields = Array.isArray(clause) ? clause : [clause];
  2727. if (!fields.length) return;
  2728. fields.forEach((field) => {
  2729. if (!field || typeof field !== "string")
  2730. throw new InvalidArgumentError(
  2731. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v was given.',
  2732. field
  2733. );
  2734. });
  2735. }
  2736. /**
  2737. * Normalize fields clause.
  2738. *
  2739. * @param {string|string[]|undefined} clause
  2740. * @returns {string[]|undefined}
  2741. */
  2742. static normalizeFieldsClause(clause) {
  2743. if (clause == null) return;
  2744. const fields = Array.isArray(clause) ? clause : [clause];
  2745. if (!fields.length) return;
  2746. fields.forEach((field) => {
  2747. if (!field || typeof field !== "string")
  2748. throw new InvalidArgumentError(
  2749. 'The provided option "fields" should be a non-empty String or an Array of non-empty String, but %v was given.',
  2750. field
  2751. );
  2752. });
  2753. return fields;
  2754. }
  2755. };
  2756. __name(_FieldsClauseTool, "FieldsClauseTool");
  2757. FieldsClauseTool = _FieldsClauseTool;
  2758. }
  2759. });
  2760. // src/adapter/decorator/inclusion-decorator.js
  2761. var import_js_service15, _InclusionDecorator, InclusionDecorator;
  2762. var init_inclusion_decorator = __esm({
  2763. "src/adapter/decorator/inclusion-decorator.js"() {
  2764. init_adapter();
  2765. import_js_service15 = require("@e22m4u/js-service");
  2766. init_filter();
  2767. init_errors();
  2768. _InclusionDecorator = class _InclusionDecorator extends import_js_service15.Service {
  2769. /**
  2770. * Decorate.
  2771. *
  2772. * @param {Adapter} adapter
  2773. */
  2774. decorate(adapter) {
  2775. if (!adapter || !(adapter instanceof Adapter))
  2776. throw new InvalidArgumentError(
  2777. "The first argument of InclusionDecorator.decorate should be an Adapter instance, but %v was given.",
  2778. adapter
  2779. );
  2780. const tool = adapter.getService(IncludeClauseTool);
  2781. const includeTo = /* @__PURE__ */ __name((...args) => tool.includeTo(...args), "includeTo");
  2782. const create = adapter.create;
  2783. adapter.create = async function(modelName, modelData, filter) {
  2784. const retvalData = await create.call(this, modelName, modelData, filter);
  2785. if (filter && typeof filter === "object" && filter.include)
  2786. await includeTo([retvalData], modelName, filter.include);
  2787. return retvalData;
  2788. };
  2789. const replaceById = adapter.replaceById;
  2790. adapter.replaceById = async function(modelName, id, modelData, filter) {
  2791. const retvalData = await replaceById.call(
  2792. this,
  2793. modelName,
  2794. id,
  2795. modelData,
  2796. filter
  2797. );
  2798. if (filter && typeof filter === "object" && filter.include)
  2799. await includeTo([retvalData], modelName, filter.include);
  2800. return retvalData;
  2801. };
  2802. const replaceOrCreate = adapter.replaceOrCreate;
  2803. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  2804. const retvalData = await replaceOrCreate.call(
  2805. this,
  2806. modelName,
  2807. modelData,
  2808. filter
  2809. );
  2810. if (filter && typeof filter === "object" && filter.include)
  2811. await includeTo([retvalData], modelName, filter.include);
  2812. return retvalData;
  2813. };
  2814. const patchById = adapter.patchById;
  2815. adapter.patchById = async function(modelName, id, modelData, filter) {
  2816. const retvalData = await patchById.call(
  2817. this,
  2818. modelName,
  2819. id,
  2820. modelData,
  2821. filter
  2822. );
  2823. if (filter && typeof filter === "object" && filter.include)
  2824. await includeTo([retvalData], modelName, filter.include);
  2825. return retvalData;
  2826. };
  2827. const find = adapter.find;
  2828. adapter.find = async function(modelName, filter) {
  2829. const modelItems = await find.call(this, modelName, filter);
  2830. if (filter && typeof filter === "object" && filter.include)
  2831. await includeTo(modelItems, modelName, filter.include);
  2832. return modelItems;
  2833. };
  2834. const findById = adapter.findById;
  2835. adapter.findById = async function(modelName, id, filter) {
  2836. const retvalData = await findById.call(this, modelName, id, filter);
  2837. if (filter && typeof filter === "object" && filter.include)
  2838. await includeTo([retvalData], modelName, filter.include);
  2839. return retvalData;
  2840. };
  2841. }
  2842. };
  2843. __name(_InclusionDecorator, "InclusionDecorator");
  2844. InclusionDecorator = _InclusionDecorator;
  2845. }
  2846. });
  2847. // src/adapter/decorator/default-values-decorator.js
  2848. var import_js_service16, _DefaultValuesDecorator, DefaultValuesDecorator;
  2849. var init_default_values_decorator = __esm({
  2850. "src/adapter/decorator/default-values-decorator.js"() {
  2851. init_adapter();
  2852. import_js_service16 = require("@e22m4u/js-service");
  2853. init_errors();
  2854. init_definition();
  2855. _DefaultValuesDecorator = class _DefaultValuesDecorator extends import_js_service16.Service {
  2856. /**
  2857. * Decorate.
  2858. *
  2859. * @param {Adapter} adapter
  2860. */
  2861. decorate(adapter) {
  2862. if (!adapter || !(adapter instanceof Adapter))
  2863. throw new InvalidArgumentError(
  2864. "The first argument of DefaultValuesDecorator.decorate should be an Adapter instance, but %v was given.",
  2865. adapter
  2866. );
  2867. const utils = adapter.getService(ModelDefinitionUtils);
  2868. const setDefaults = /* @__PURE__ */ __name((...args) => utils.setDefaultValuesToEmptyProperties(...args), "setDefaults");
  2869. const create = adapter.create;
  2870. adapter.create = function(modelName, modelData, filter) {
  2871. modelData = setDefaults(modelName, modelData);
  2872. return create.call(this, modelName, modelData, filter);
  2873. };
  2874. const replaceById = adapter.replaceById;
  2875. adapter.replaceById = function(modelName, id, modelData, filter) {
  2876. modelData = setDefaults(modelName, modelData);
  2877. return replaceById.call(this, modelName, id, modelData, filter);
  2878. };
  2879. const replaceOrCreate = adapter.replaceOrCreate;
  2880. adapter.replaceOrCreate = function(modelName, modelData, filter) {
  2881. modelData = setDefaults(modelName, modelData);
  2882. return replaceOrCreate.call(this, modelName, modelData, filter);
  2883. };
  2884. const patch = adapter.patch;
  2885. adapter.patch = function(modelName, modelData, where) {
  2886. modelData = setDefaults(modelName, modelData, true);
  2887. return patch.call(this, modelName, modelData, where);
  2888. };
  2889. const patchById = adapter.patchById;
  2890. adapter.patchById = function(modelName, id, modelData, filter) {
  2891. modelData = setDefaults(modelName, modelData, true);
  2892. return patchById.call(this, modelName, id, modelData, filter);
  2893. };
  2894. const find = adapter.find;
  2895. adapter.find = async function(modelName, filter) {
  2896. const modelItems = await find.call(this, modelName, filter);
  2897. return modelItems.map((modelItem) => setDefaults(modelName, modelItem));
  2898. };
  2899. const findById = adapter.findById;
  2900. adapter.findById = async function(modelName, id, filter) {
  2901. const retvalData = await findById.call(this, modelName, id, filter);
  2902. return setDefaults(modelName, retvalData);
  2903. };
  2904. }
  2905. };
  2906. __name(_DefaultValuesDecorator, "DefaultValuesDecorator");
  2907. DefaultValuesDecorator = _DefaultValuesDecorator;
  2908. }
  2909. });
  2910. // src/adapter/decorator/data-sanitizing-decorator.js
  2911. var import_js_service17, _DataSanitizingDecorator, DataSanitizingDecorator;
  2912. var init_data_sanitizing_decorator = __esm({
  2913. "src/adapter/decorator/data-sanitizing-decorator.js"() {
  2914. init_adapter();
  2915. import_js_service17 = require("@e22m4u/js-service");
  2916. init_errors();
  2917. init_definition();
  2918. _DataSanitizingDecorator = class _DataSanitizingDecorator extends import_js_service17.Service {
  2919. /**
  2920. * Decorate.
  2921. *
  2922. * @param {Adapter} adapter
  2923. */
  2924. decorate(adapter) {
  2925. if (!adapter || !(adapter instanceof Adapter))
  2926. throw new InvalidArgumentError(
  2927. "The first argument of DataSanitizingDecorator.decorate should be an Adapter instance, but %v was given.",
  2928. adapter
  2929. );
  2930. const sanitizer = adapter.getService(ModelDataSanitizer);
  2931. const sanitize = /* @__PURE__ */ __name((...args) => sanitizer.sanitize(...args), "sanitize");
  2932. const create = adapter.create;
  2933. adapter.create = async function(modelName, modelData, filter) {
  2934. modelData = sanitize(modelName, modelData);
  2935. return create.call(this, modelName, modelData, filter);
  2936. };
  2937. const replaceById = adapter.replaceById;
  2938. adapter.replaceById = async function(modelName, id, modelData, filter) {
  2939. modelData = sanitize(modelName, modelData);
  2940. return replaceById.call(this, modelName, id, modelData, filter);
  2941. };
  2942. const replaceOrCreate = adapter.replaceOrCreate;
  2943. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  2944. modelData = sanitize(modelName, modelData);
  2945. return replaceOrCreate.call(this, modelName, modelData, filter);
  2946. };
  2947. const patch = adapter.patch;
  2948. adapter.patch = async function(modelName, modelData, where) {
  2949. modelData = sanitize(modelName, modelData);
  2950. return patch.call(this, modelName, modelData, where);
  2951. };
  2952. const patchById = adapter.patchById;
  2953. adapter.patchById = async function(modelName, id, modelData, filter) {
  2954. modelData = sanitize(modelName, modelData);
  2955. return patchById.call(this, modelName, id, modelData, filter);
  2956. };
  2957. }
  2958. };
  2959. __name(_DataSanitizingDecorator, "DataSanitizingDecorator");
  2960. DataSanitizingDecorator = _DataSanitizingDecorator;
  2961. }
  2962. });
  2963. // src/adapter/decorator/fields-filtering-decorator.js
  2964. var import_js_service18, _FieldsFilteringDecorator, FieldsFilteringDecorator;
  2965. var init_fields_filtering_decorator = __esm({
  2966. "src/adapter/decorator/fields-filtering-decorator.js"() {
  2967. init_adapter();
  2968. import_js_service18 = require("@e22m4u/js-service");
  2969. init_filter();
  2970. init_errors();
  2971. _FieldsFilteringDecorator = class _FieldsFilteringDecorator extends import_js_service18.Service {
  2972. /**
  2973. * Decorate.
  2974. *
  2975. * @param {Adapter} adapter
  2976. */
  2977. decorate(adapter) {
  2978. if (!adapter || !(adapter instanceof Adapter))
  2979. throw new InvalidArgumentError(
  2980. "The first argument of FieldsFilteringDecorator.decorate should be an Adapter instance, but %v was given.",
  2981. adapter
  2982. );
  2983. const tool = adapter.getService(FieldsClauseTool);
  2984. const selectFields = /* @__PURE__ */ __name((...args) => tool.filter(...args), "selectFields");
  2985. const create = adapter.create;
  2986. adapter.create = async function(modelName, modelData, filter) {
  2987. let result = await create.call(this, modelName, modelData, filter);
  2988. if (filter && typeof filter === "object" && filter.fields)
  2989. result = selectFields(result, modelName, filter.fields);
  2990. return result;
  2991. };
  2992. const replaceById = adapter.replaceById;
  2993. adapter.replaceById = async function(modelName, id, modelData, filter) {
  2994. let result = await replaceById.call(
  2995. this,
  2996. modelName,
  2997. id,
  2998. modelData,
  2999. filter
  3000. );
  3001. if (filter && typeof filter === "object" && filter.fields)
  3002. result = selectFields(result, modelName, filter.fields);
  3003. return result;
  3004. };
  3005. const replaceOrCreate = adapter.replaceOrCreate;
  3006. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  3007. let result = await replaceOrCreate.call(
  3008. this,
  3009. modelName,
  3010. modelData,
  3011. filter
  3012. );
  3013. if (filter && typeof filter === "object" && filter.fields)
  3014. result = selectFields(result, modelName, filter.fields);
  3015. return result;
  3016. };
  3017. const patchById = adapter.patchById;
  3018. adapter.patchById = async function(modelName, id, modelData, filter) {
  3019. let result = await patchById.call(this, modelName, id, modelData, filter);
  3020. if (filter && typeof filter === "object" && filter.fields)
  3021. result = selectFields(result, modelName, filter.fields);
  3022. return result;
  3023. };
  3024. const find = adapter.find;
  3025. adapter.find = async function(modelName, filter) {
  3026. let result = await find.call(this, modelName, filter);
  3027. if (filter && typeof filter === "object" && filter.fields)
  3028. result = selectFields(result, modelName, filter.fields);
  3029. return result;
  3030. };
  3031. const findById = adapter.findById;
  3032. adapter.findById = async function(modelName, id, filter) {
  3033. let result = await findById.call(this, modelName, id, filter);
  3034. if (filter && typeof filter === "object" && filter.fields)
  3035. result = selectFields(result, modelName, filter.fields);
  3036. return result;
  3037. };
  3038. }
  3039. };
  3040. __name(_FieldsFilteringDecorator, "FieldsFilteringDecorator");
  3041. FieldsFilteringDecorator = _FieldsFilteringDecorator;
  3042. }
  3043. });
  3044. // src/adapter/decorator/property-uniqueness-decorator.js
  3045. var import_js_service19, _PropertyUniquenessDecorator, PropertyUniquenessDecorator;
  3046. var init_property_uniqueness_decorator = __esm({
  3047. "src/adapter/decorator/property-uniqueness-decorator.js"() {
  3048. init_adapter();
  3049. import_js_service19 = require("@e22m4u/js-service");
  3050. init_errors();
  3051. init_definition();
  3052. _PropertyUniquenessDecorator = class _PropertyUniquenessDecorator extends import_js_service19.Service {
  3053. /**
  3054. * Decorate.
  3055. *
  3056. * @param {Adapter} adapter
  3057. */
  3058. decorate(adapter) {
  3059. if (!adapter || !(adapter instanceof Adapter))
  3060. throw new InvalidArgumentError(
  3061. "The first argument of PropertyUniquenessDecorator.decorate should be an Adapter instance, but %v was given.",
  3062. adapter
  3063. );
  3064. const validator = this.getService(PropertyUniquenessValidator);
  3065. const create = adapter.create;
  3066. adapter.create = async function(modelName, modelData, filter) {
  3067. const countMethod = adapter.count.bind(adapter, modelName);
  3068. await validator.validate(countMethod, "create", modelName, modelData);
  3069. return create.call(this, modelName, modelData, filter);
  3070. };
  3071. const replaceById = adapter.replaceById;
  3072. adapter.replaceById = async function(modelName, id, modelData, filter) {
  3073. const countMethod = adapter.count.bind(adapter, modelName);
  3074. await validator.validate(
  3075. countMethod,
  3076. "replaceById",
  3077. modelName,
  3078. modelData,
  3079. id
  3080. );
  3081. return replaceById.call(this, modelName, id, modelData, filter);
  3082. };
  3083. const replaceOrCreate = adapter.replaceOrCreate;
  3084. adapter.replaceOrCreate = async function(modelName, modelData, filter) {
  3085. const countMethod = adapter.count.bind(adapter, modelName);
  3086. await validator.validate(
  3087. countMethod,
  3088. "replaceOrCreate",
  3089. modelName,
  3090. modelData
  3091. );
  3092. return replaceOrCreate.call(this, modelName, modelData, filter);
  3093. };
  3094. const patch = adapter.patch;
  3095. adapter.patch = async function(modelName, modelData, where) {
  3096. const countMethod = adapter.count.bind(adapter, modelName);
  3097. await validator.validate(countMethod, "patch", modelName, modelData);
  3098. return patch.call(this, modelName, modelData, where);
  3099. };
  3100. const patchById = adapter.patchById;
  3101. adapter.patchById = async function(modelName, id, modelData, filter) {
  3102. const countMethod = adapter.count.bind(adapter, modelName);
  3103. await validator.validate(
  3104. countMethod,
  3105. "patchById",
  3106. modelName,
  3107. modelData,
  3108. id
  3109. );
  3110. return patchById.call(this, modelName, id, modelData, filter);
  3111. };
  3112. }
  3113. };
  3114. __name(_PropertyUniquenessDecorator, "PropertyUniquenessDecorator");
  3115. PropertyUniquenessDecorator = _PropertyUniquenessDecorator;
  3116. }
  3117. });
  3118. // src/adapter/decorator/index.js
  3119. var init_decorator = __esm({
  3120. "src/adapter/decorator/index.js"() {
  3121. init_inclusion_decorator();
  3122. init_default_values_decorator();
  3123. init_data_sanitizing_decorator();
  3124. init_fields_filtering_decorator();
  3125. init_property_uniqueness_decorator();
  3126. }
  3127. });
  3128. // src/adapter/adapter.js
  3129. var import_js_service20, ADAPTER_CLASS_NAME, _Adapter, Adapter;
  3130. var init_adapter = __esm({
  3131. "src/adapter/adapter.js"() {
  3132. import_js_service20 = require("@e22m4u/js-service");
  3133. init_errors();
  3134. init_decorator();
  3135. ADAPTER_CLASS_NAME = "Adapter";
  3136. _Adapter = class _Adapter extends import_js_service20.Service {
  3137. /**
  3138. * Settings.
  3139. *
  3140. * @type {object|undefined}
  3141. */
  3142. _settings;
  3143. /**
  3144. * Settings.
  3145. *
  3146. * @returns {object|undefined}
  3147. */
  3148. get settings() {
  3149. return this._settings;
  3150. }
  3151. /**
  3152. * Constructor.
  3153. *
  3154. * @param {object|undefined} container
  3155. * @param {object|undefined} settings
  3156. */
  3157. constructor(container = void 0, settings = void 0) {
  3158. super(container);
  3159. this._settings = settings;
  3160. if (this.constructor !== _Adapter) {
  3161. this.getService(DataSanitizingDecorator).decorate(this);
  3162. this.getService(DefaultValuesDecorator).decorate(this);
  3163. this.getService(PropertyUniquenessDecorator).decorate(this);
  3164. this.getService(FieldsFilteringDecorator).decorate(this);
  3165. this.getService(InclusionDecorator).decorate(this);
  3166. }
  3167. }
  3168. /**
  3169. * Create.
  3170. *
  3171. * @param {string} modelName
  3172. * @param {object} modelData
  3173. * @param {object|undefined} filter
  3174. * @returns {Promise<object>}
  3175. */
  3176. create(modelName, modelData, filter = void 0) {
  3177. throw new NotImplementedError(
  3178. "%s.create is not implemented.",
  3179. this.constructor.name
  3180. );
  3181. }
  3182. /**
  3183. * Replace by id.
  3184. *
  3185. * @param {string} modelName
  3186. * @param {number|string} id
  3187. * @param {object} modelData
  3188. * @param {object|undefined} filter
  3189. * @returns {Promise<object>}
  3190. */
  3191. replaceById(modelName, id, modelData, filter = void 0) {
  3192. throw new NotImplementedError(
  3193. "%s.replaceById is not implemented.",
  3194. this.constructor.name
  3195. );
  3196. }
  3197. /**
  3198. * Replace or create.
  3199. *
  3200. * @param {string} modelName
  3201. * @param {object} modelData
  3202. * @param {object|undefined} filter
  3203. * @returns {Promise<object>}
  3204. */
  3205. replaceOrCreate(modelName, modelData, filter = void 0) {
  3206. throw new NotImplementedError(
  3207. "%s.replaceOrCreate is not implemented.",
  3208. this.constructor.name
  3209. );
  3210. }
  3211. /**
  3212. * Patch.
  3213. *
  3214. * @param {string} modelName
  3215. * @param {object} modelData
  3216. * @param {object|undefined} where
  3217. * @returns {Promise<number>}
  3218. */
  3219. patch(modelName, modelData, where = void 0) {
  3220. throw new NotImplementedError(
  3221. "%s.patch is not implemented.",
  3222. this.constructor.name
  3223. );
  3224. }
  3225. /**
  3226. * Patch by id.
  3227. *
  3228. * @param {string} modelName
  3229. * @param {number|string} id
  3230. * @param {object} modelData
  3231. * @param {object|undefined} filter
  3232. * @returns {Promise<object>}
  3233. */
  3234. patchById(modelName, id, modelData, filter = void 0) {
  3235. throw new NotImplementedError(
  3236. "%s.patchById is not implemented.",
  3237. this.constructor.name
  3238. );
  3239. }
  3240. /**
  3241. * Find.
  3242. *
  3243. * @param {string} modelName
  3244. * @param {object|undefined} filter
  3245. * @returns {Promise<object[]>}
  3246. */
  3247. find(modelName, filter = void 0) {
  3248. throw new NotImplementedError(
  3249. "%s.find is not implemented.",
  3250. this.constructor.name
  3251. );
  3252. }
  3253. /**
  3254. * Find by id.
  3255. *
  3256. * @param {string} modelName
  3257. * @param {number|string} id
  3258. * @param {object|undefined} filter
  3259. * @returns {Promise<object>}
  3260. */
  3261. findById(modelName, id, filter = void 0) {
  3262. throw new NotImplementedError(
  3263. "%s.findById is not implemented.",
  3264. this.constructor.name
  3265. );
  3266. }
  3267. /**
  3268. * Delete.
  3269. *
  3270. * @param {string} modelName
  3271. * @param {object|undefined} where
  3272. * @returns {Promise<number>}
  3273. */
  3274. delete(modelName, where = void 0) {
  3275. throw new NotImplementedError(
  3276. "%s.delete is not implemented.",
  3277. this.constructor.name
  3278. );
  3279. }
  3280. /**
  3281. * Delete by id.
  3282. *
  3283. * @param {string} modelName
  3284. * @param {number|string} id
  3285. * @returns {Promise<boolean>}
  3286. */
  3287. deleteById(modelName, id) {
  3288. throw new NotImplementedError(
  3289. "%s.deleteById is not implemented.",
  3290. this.constructor.name
  3291. );
  3292. }
  3293. /**
  3294. * Exists.
  3295. *
  3296. * @param {string} modelName
  3297. * @param {number|string} id
  3298. * @returns {Promise<boolean>}
  3299. */
  3300. exists(modelName, id) {
  3301. throw new NotImplementedError(
  3302. "%s.exists is not implemented.",
  3303. this.constructor.name
  3304. );
  3305. }
  3306. /**
  3307. * Count.
  3308. *
  3309. * @param {string} modelName
  3310. * @param {object|undefined} where
  3311. * @returns {Promise<number>}
  3312. */
  3313. count(modelName, where = void 0) {
  3314. throw new NotImplementedError(
  3315. "%s.count is not implemented.",
  3316. this.constructor.name
  3317. );
  3318. }
  3319. };
  3320. __name(_Adapter, "Adapter");
  3321. /**
  3322. * Kinds.
  3323. *
  3324. * @type {string[]}
  3325. */
  3326. __publicField(_Adapter, "kinds", [...import_js_service20.Service.kinds, ADAPTER_CLASS_NAME]);
  3327. Adapter = _Adapter;
  3328. }
  3329. });
  3330. // src/adapter/builtin/memory-adapter.js
  3331. var memory_adapter_exports = {};
  3332. __export(memory_adapter_exports, {
  3333. MemoryAdapter: () => MemoryAdapter
  3334. });
  3335. var _MemoryAdapter, MemoryAdapter;
  3336. var init_memory_adapter = __esm({
  3337. "src/adapter/builtin/memory-adapter.js"() {
  3338. init_adapter();
  3339. init_utils();
  3340. init_errors();
  3341. init_definition();
  3342. init_filter();
  3343. _MemoryAdapter = class _MemoryAdapter extends Adapter {
  3344. /**
  3345. * Tables.
  3346. *
  3347. * @type {Map<string, Map<number, Record<string, any>>>}
  3348. */
  3349. _tables = /* @__PURE__ */ new Map();
  3350. /**
  3351. * Last ids.
  3352. *
  3353. * @type {Map<string, number>}
  3354. */
  3355. _lastIds = /* @__PURE__ */ new Map();
  3356. /**
  3357. * Get table or create.
  3358. *
  3359. * @param {string} modelName
  3360. * @returns {Map<number, object>}
  3361. */
  3362. _getTableOrCreate(modelName) {
  3363. const tableName = this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
  3364. let table = this._tables.get(tableName);
  3365. if (table) return table;
  3366. table = /* @__PURE__ */ new Map();
  3367. this._tables.set(tableName, table);
  3368. return table;
  3369. }
  3370. /**
  3371. * Gen next id value.
  3372. *
  3373. * @param {string} modelName
  3374. * @param {string} propName
  3375. * @returns {number}
  3376. */
  3377. _genNextIdValue(modelName, propName) {
  3378. var _a;
  3379. const modelUtils = this.getService(ModelDefinitionUtils);
  3380. const propType = modelUtils.getDataTypeByPropertyName(modelName, propName);
  3381. if (propType !== DataType.ANY && propType !== DataType.NUMBER) {
  3382. throw new InvalidArgumentError(
  3383. "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.",
  3384. propName,
  3385. modelName,
  3386. capitalize(propType),
  3387. propName
  3388. );
  3389. }
  3390. const tableName = modelUtils.getTableNameByModelName(modelName);
  3391. const table = this._getTableOrCreate(modelName);
  3392. let nextId = (_a = this._lastIds.get(tableName)) != null ? _a : 0;
  3393. do {
  3394. nextId++;
  3395. } while (table.has(nextId));
  3396. this._lastIds.set(tableName, nextId);
  3397. return nextId;
  3398. }
  3399. /**
  3400. * Update last id value if needed.
  3401. *
  3402. * Если переданное значение последнего использованного
  3403. * идентификатора больше текущего, то текущее значение
  3404. * перезаписывается полученным.
  3405. *
  3406. * @param {string} modelName
  3407. * @param {number} idValue
  3408. */
  3409. _updateLastIdValueIfNeeded(modelName, idValue) {
  3410. var _a;
  3411. const tableName = this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
  3412. const currentLastId = (_a = this._lastIds.get(tableName)) != null ? _a : 0;
  3413. if (idValue > currentLastId) {
  3414. this._lastIds.set(tableName, idValue);
  3415. }
  3416. }
  3417. /**
  3418. * Create
  3419. *
  3420. * @param {string} modelName
  3421. * @param {object} modelData
  3422. * @param {object|undefined} filter
  3423. * @returns {Promise<object>}
  3424. */
  3425. // eslint-disable-next-line no-unused-vars
  3426. async create(modelName, modelData, filter = void 0) {
  3427. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3428. modelName
  3429. );
  3430. let idValue = modelData[pkPropName];
  3431. if (idValue == null || idValue === "" || idValue === 0) {
  3432. idValue = this._genNextIdValue(modelName, pkPropName);
  3433. } else if (typeof idValue === "number") {
  3434. this._updateLastIdValueIfNeeded(modelName, idValue);
  3435. }
  3436. const table = this._getTableOrCreate(modelName);
  3437. if (table.has(idValue))
  3438. throw new InvalidArgumentError(
  3439. "The value %v of the primary key %v already exists in the model %v.",
  3440. idValue,
  3441. pkPropName,
  3442. modelName
  3443. );
  3444. modelData = cloneDeep(modelData);
  3445. modelData[pkPropName] = idValue;
  3446. const tableData = this.getService(
  3447. ModelDefinitionUtils
  3448. ).convertPropertyNamesToColumnNames(modelName, modelData);
  3449. table.set(idValue, tableData);
  3450. return this.getService(
  3451. ModelDefinitionUtils
  3452. ).convertColumnNamesToPropertyNames(modelName, tableData);
  3453. }
  3454. /**
  3455. * Replace by id.
  3456. *
  3457. * @param {string} modelName
  3458. * @param {string|number} id
  3459. * @param {object} modelData
  3460. * @param {object|undefined} filter
  3461. * @returns {Promise<object>}
  3462. */
  3463. // eslint-disable-next-line no-unused-vars
  3464. async replaceById(modelName, id, modelData, filter = void 0) {
  3465. const table = this._getTableOrCreate(modelName);
  3466. const isExists = table.has(id);
  3467. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3468. modelName
  3469. );
  3470. if (!isExists)
  3471. throw new InvalidArgumentError(
  3472. "The value %v of the primary key %v does not exist in the model %v.",
  3473. id,
  3474. pkPropName,
  3475. modelName
  3476. );
  3477. modelData = cloneDeep(modelData);
  3478. modelData[pkPropName] = id;
  3479. const tableData = this.getService(
  3480. ModelDefinitionUtils
  3481. ).convertPropertyNamesToColumnNames(modelName, modelData);
  3482. table.set(id, tableData);
  3483. return this.getService(
  3484. ModelDefinitionUtils
  3485. ).convertColumnNamesToPropertyNames(modelName, tableData);
  3486. }
  3487. /**
  3488. * Replace or create.
  3489. *
  3490. * @param {string} modelName
  3491. * @param {object} modelData
  3492. * @param {object|undefined} filter
  3493. * @returns {Promise<object>}
  3494. */
  3495. // eslint-disable-next-line no-unused-vars
  3496. async replaceOrCreate(modelName, modelData, filter = void 0) {
  3497. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3498. modelName
  3499. );
  3500. let idValue = modelData[pkPropName];
  3501. if (idValue == null || idValue === "" || idValue === 0) {
  3502. idValue = this._genNextIdValue(modelName, pkPropName);
  3503. } else if (typeof idValue === "number") {
  3504. this._updateLastIdValueIfNeeded(modelName, idValue);
  3505. }
  3506. const table = this._getTableOrCreate(modelName);
  3507. modelData = cloneDeep(modelData);
  3508. modelData[pkPropName] = idValue;
  3509. const tableData = this.getService(
  3510. ModelDefinitionUtils
  3511. ).convertPropertyNamesToColumnNames(modelName, modelData);
  3512. table.set(idValue, tableData);
  3513. return this.getService(
  3514. ModelDefinitionUtils
  3515. ).convertColumnNamesToPropertyNames(modelName, tableData);
  3516. }
  3517. /**
  3518. * Patch.
  3519. *
  3520. * @param {string} modelName
  3521. * @param {object} modelData
  3522. * @param {object|undefined} where
  3523. * @returns {Promise<number>}
  3524. */
  3525. async patch(modelName, modelData, where = void 0) {
  3526. const table = this._getTableOrCreate(modelName);
  3527. const tableItems = Array.from(table.values());
  3528. if (!tableItems.length) return 0;
  3529. let modelItems = tableItems.map(
  3530. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  3531. modelName,
  3532. tableItem
  3533. )
  3534. );
  3535. if (where && typeof where === "object")
  3536. modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
  3537. const size = modelItems.length;
  3538. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3539. modelName
  3540. );
  3541. modelData = cloneDeep(modelData);
  3542. delete modelData[pkPropName];
  3543. modelItems.forEach((existingModelData) => {
  3544. const mergedModelData = Object.assign({}, existingModelData, modelData);
  3545. const mergedTableData = this.getService(
  3546. ModelDefinitionUtils
  3547. ).convertPropertyNamesToColumnNames(modelName, mergedModelData);
  3548. const idValue = existingModelData[pkPropName];
  3549. table.set(idValue, mergedTableData);
  3550. });
  3551. return size;
  3552. }
  3553. /**
  3554. * Patch by id.
  3555. *
  3556. * @param {string} modelName
  3557. * @param {string|number} id
  3558. * @param {object} modelData
  3559. * @param {object|undefined} filter
  3560. * @returns {Promise<object>}
  3561. */
  3562. // eslint-disable-next-line no-unused-vars
  3563. async patchById(modelName, id, modelData, filter = void 0) {
  3564. const table = this._getTableOrCreate(modelName);
  3565. const existingTableData = table.get(id);
  3566. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3567. modelName
  3568. );
  3569. if (existingTableData == null)
  3570. throw new InvalidArgumentError(
  3571. "The value %v of the primary key %v does not exist in the model %v.",
  3572. id,
  3573. pkPropName,
  3574. modelName
  3575. );
  3576. modelData = cloneDeep(modelData);
  3577. delete modelData[pkPropName];
  3578. const existingModelData = this.getService(
  3579. ModelDefinitionUtils
  3580. ).convertColumnNamesToPropertyNames(modelName, existingTableData);
  3581. const mergedModelData = Object.assign({}, existingModelData, modelData);
  3582. const mergedTableData = this.getService(
  3583. ModelDefinitionUtils
  3584. ).convertPropertyNamesToColumnNames(modelName, mergedModelData);
  3585. table.set(id, mergedTableData);
  3586. return this.getService(
  3587. ModelDefinitionUtils
  3588. ).convertColumnNamesToPropertyNames(modelName, mergedTableData);
  3589. }
  3590. /**
  3591. * Find.
  3592. *
  3593. * @param {string} modelName
  3594. * @param {object|undefined} filter
  3595. * @returns {Promise<object[]>}
  3596. */
  3597. async find(modelName, filter = void 0) {
  3598. const table = this._getTableOrCreate(modelName);
  3599. const tableItems = Array.from(table.values());
  3600. let modelItems = tableItems.map(
  3601. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  3602. modelName,
  3603. tableItem
  3604. )
  3605. );
  3606. if (filter && typeof filter === "object") {
  3607. if (filter.where)
  3608. modelItems = this.getService(WhereClauseTool).filter(
  3609. modelItems,
  3610. filter.where
  3611. );
  3612. if (filter.skip || filter.limit)
  3613. modelItems = this.getService(SliceClauseTool).slice(
  3614. modelItems,
  3615. filter.skip,
  3616. filter.limit
  3617. );
  3618. if (filter.order)
  3619. this.getService(OrderClauseTool).sort(modelItems, filter.order);
  3620. }
  3621. return modelItems;
  3622. }
  3623. /**
  3624. * Find by id.
  3625. *
  3626. * @param {string} modelName
  3627. * @param {string|number} id
  3628. * @param {object|undefined} filter
  3629. * @returns {Promise<object>}
  3630. */
  3631. // eslint-disable-next-line no-unused-vars
  3632. async findById(modelName, id, filter = void 0) {
  3633. const table = this._getTableOrCreate(modelName);
  3634. const tableData = table.get(id);
  3635. const pkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3636. modelName
  3637. );
  3638. if (!tableData)
  3639. throw new InvalidArgumentError(
  3640. "The value %v of the primary key %v does not exist in the model %v.",
  3641. id,
  3642. pkPropName,
  3643. modelName
  3644. );
  3645. return this.getService(
  3646. ModelDefinitionUtils
  3647. ).convertColumnNamesToPropertyNames(modelName, tableData);
  3648. }
  3649. /**
  3650. * Delete.
  3651. *
  3652. * @param {string} modelName
  3653. * @param {object|undefined} where
  3654. * @returns {Promise<number>}
  3655. */
  3656. async delete(modelName, where = void 0) {
  3657. const table = this._getTableOrCreate(modelName);
  3658. const tableItems = Array.from(table.values());
  3659. if (!tableItems.length) return 0;
  3660. let modelItems = tableItems.map(
  3661. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  3662. modelName,
  3663. tableItem
  3664. )
  3665. );
  3666. if (where && typeof where === "object")
  3667. modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
  3668. const size = modelItems.length;
  3669. const idPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  3670. modelName
  3671. );
  3672. modelItems.forEach((modelData) => {
  3673. const idValue = modelData[idPropName];
  3674. table.delete(idValue);
  3675. });
  3676. return size;
  3677. }
  3678. /**
  3679. * Delete by id.
  3680. *
  3681. * @param {string} modelName
  3682. * @param {string|number} id
  3683. * @returns {Promise<boolean>}
  3684. */
  3685. async deleteById(modelName, id) {
  3686. const table = this._getTableOrCreate(modelName);
  3687. const isExists = table.has(id);
  3688. table.delete(id);
  3689. return isExists;
  3690. }
  3691. /**
  3692. * Exists.
  3693. *
  3694. * @param {string} modelName
  3695. * @param {string|number} id
  3696. * @returns {Promise<boolean>}
  3697. */
  3698. async exists(modelName, id) {
  3699. const table = this._getTableOrCreate(modelName);
  3700. return table.has(id);
  3701. }
  3702. /**
  3703. * Count.
  3704. *
  3705. * @param {string} modelName
  3706. * @param {object|undefined} where
  3707. * @returns {Promise<number>}
  3708. */
  3709. async count(modelName, where = void 0) {
  3710. const table = this._getTableOrCreate(modelName);
  3711. const tableItems = Array.from(table.values());
  3712. let modelItems = tableItems.map(
  3713. (tableItem) => this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
  3714. modelName,
  3715. tableItem
  3716. )
  3717. );
  3718. if (where && typeof where === "object")
  3719. modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
  3720. return modelItems.length;
  3721. }
  3722. };
  3723. __name(_MemoryAdapter, "MemoryAdapter");
  3724. MemoryAdapter = _MemoryAdapter;
  3725. }
  3726. });
  3727. // import("./builtin/**/*-adapter.js") in src/adapter/adapter-loader.js
  3728. var globImport_builtin_adapter_js;
  3729. var init_ = __esm({
  3730. 'import("./builtin/**/*-adapter.js") in src/adapter/adapter-loader.js'() {
  3731. globImport_builtin_adapter_js = __glob({
  3732. "./builtin/memory-adapter.js": () => Promise.resolve().then(() => (init_memory_adapter(), memory_adapter_exports))
  3733. });
  3734. }
  3735. });
  3736. // src/adapter/adapter-loader.js
  3737. function findAdapterCtorInModule(module2) {
  3738. let adapterCtor;
  3739. if (!module2 || typeof module2 !== "object" || Array.isArray(module2)) return;
  3740. for (const ctor of Object.values(module2)) {
  3741. if (typeof ctor === "function" && Array.isArray(ctor.kinds) && Adapter.kinds.includes(ADAPTER_CLASS_NAME)) {
  3742. adapterCtor = ctor;
  3743. break;
  3744. }
  3745. }
  3746. return adapterCtor;
  3747. }
  3748. var import_js_service21, _AdapterLoader, AdapterLoader;
  3749. var init_adapter_loader = __esm({
  3750. "src/adapter/adapter-loader.js"() {
  3751. import_js_service21 = require("@e22m4u/js-service");
  3752. init_errors();
  3753. init_adapter();
  3754. init_();
  3755. _AdapterLoader = class _AdapterLoader extends import_js_service21.Service {
  3756. /**
  3757. * Load by name.
  3758. *
  3759. * @param {string} adapterName
  3760. * @param {object|undefined} settings
  3761. * @returns {Promise<Adapter>}
  3762. */
  3763. async loadByName(adapterName, settings = void 0) {
  3764. if (!adapterName || typeof adapterName !== "string")
  3765. throw new InvalidArgumentError(
  3766. "The adapter name should be a non-empty String, but %v was given.",
  3767. adapterName
  3768. );
  3769. let adapterCtor;
  3770. try {
  3771. const module2 = await globImport_builtin_adapter_js(`./builtin/${adapterName}-adapter.js`);
  3772. adapterCtor = findAdapterCtorInModule(module2);
  3773. } catch {
  3774. }
  3775. if (!adapterCtor)
  3776. try {
  3777. const module2 = await Promise.resolve().then(() => __toESM(require(`@e22m4u/js-repository-${adapterName}-adapter`)));
  3778. adapterCtor = findAdapterCtorInModule(module2);
  3779. } catch {
  3780. }
  3781. if (!adapterCtor)
  3782. throw new InvalidArgumentError(
  3783. "The adapter %v is not found.",
  3784. adapterName
  3785. );
  3786. return new adapterCtor(this.container, settings);
  3787. }
  3788. };
  3789. __name(_AdapterLoader, "AdapterLoader");
  3790. AdapterLoader = _AdapterLoader;
  3791. __name(findAdapterCtorInModule, "findAdapterCtorInModule");
  3792. }
  3793. });
  3794. // src/adapter/adapter-registry.js
  3795. var import_js_service22, _AdapterRegistry, AdapterRegistry;
  3796. var init_adapter_registry = __esm({
  3797. "src/adapter/adapter-registry.js"() {
  3798. init_adapter();
  3799. import_js_service22 = require("@e22m4u/js-service");
  3800. init_adapter_loader();
  3801. init_definition();
  3802. _AdapterRegistry = class _AdapterRegistry extends import_js_service22.Service {
  3803. /**
  3804. * Adapters.
  3805. *
  3806. * @type {object}
  3807. */
  3808. _adapters = {};
  3809. /**
  3810. * Get adapter.
  3811. *
  3812. * @param {string} datasourceName
  3813. * @returns {Promise<Adapter>}
  3814. */
  3815. async getAdapter(datasourceName) {
  3816. let adapter = this._adapters[datasourceName];
  3817. if (adapter) return adapter;
  3818. const datasource = this.getService(DefinitionRegistry).getDatasource(datasourceName);
  3819. const adapterName = datasource.adapter;
  3820. adapter = await this.getService(AdapterLoader).loadByName(
  3821. adapterName,
  3822. datasource
  3823. );
  3824. this._adapters[datasourceName] = adapter;
  3825. return adapter;
  3826. }
  3827. };
  3828. __name(_AdapterRegistry, "AdapterRegistry");
  3829. AdapterRegistry = _AdapterRegistry;
  3830. }
  3831. });
  3832. // src/adapter/index.js
  3833. var init_adapter2 = __esm({
  3834. "src/adapter/index.js"() {
  3835. init_adapter();
  3836. init_adapter_loader();
  3837. init_adapter_registry();
  3838. }
  3839. });
  3840. // src/repository/repository.js
  3841. var import_js_service23, _Repository, Repository;
  3842. var init_repository = __esm({
  3843. "src/repository/repository.js"() {
  3844. import_js_service23 = require("@e22m4u/js-service");
  3845. init_errors();
  3846. init_definition();
  3847. init_adapter2();
  3848. _Repository = class _Repository extends import_js_service23.Service {
  3849. /**
  3850. * Model name.
  3851. *
  3852. * @type {string}
  3853. */
  3854. _modelName;
  3855. /**
  3856. * Model name.
  3857. *
  3858. * @returns {string}
  3859. */
  3860. get modelName() {
  3861. return this._modelName;
  3862. }
  3863. /**
  3864. * Datasource name.
  3865. *
  3866. * @type {string}
  3867. */
  3868. _datasourceName;
  3869. /**
  3870. * Datasource name.
  3871. *
  3872. * @returns {string}
  3873. */
  3874. get datasourceName() {
  3875. return this._datasourceName;
  3876. }
  3877. /**
  3878. * Constructor.
  3879. *
  3880. * @typedef {import('@e22m4u/js-service').ServiceContainer} ServiceContainer
  3881. * @param {ServiceContainer} container
  3882. * @param {string} modelName
  3883. */
  3884. constructor(container, modelName) {
  3885. super(container);
  3886. this._modelName = modelName;
  3887. const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
  3888. const datasourceName = modelDef.datasource;
  3889. if (!datasourceName)
  3890. throw new InvalidArgumentError(
  3891. "The model %v does not have a specified datasource.",
  3892. modelName
  3893. );
  3894. this._datasourceName = datasourceName;
  3895. }
  3896. /**
  3897. * Get adapter.
  3898. *
  3899. * @returns {Adapter}
  3900. */
  3901. async getAdapter() {
  3902. return this.getService(AdapterRegistry).getAdapter(this.datasourceName);
  3903. }
  3904. /**
  3905. * Create.
  3906. *
  3907. * @param {object} data
  3908. * @param {object|undefined} filter
  3909. * @returns {Promise<object>}
  3910. */
  3911. async create(data, filter = void 0) {
  3912. const adapter = await this.getAdapter();
  3913. return adapter.create(this.modelName, data, filter);
  3914. }
  3915. /**
  3916. * Replace by id.
  3917. *
  3918. * @param {number|string} id
  3919. * @param {object} data
  3920. * @param {object|undefined} filter
  3921. * @returns {Promise<object>}
  3922. */
  3923. async replaceById(id, data, filter = void 0) {
  3924. const adapter = await this.getAdapter();
  3925. return adapter.replaceById(this.modelName, id, data, filter);
  3926. }
  3927. /**
  3928. * Replace or create.
  3929. *
  3930. * @param {object} data
  3931. * @param {object|undefined} filter
  3932. * @returns {Promise<object>}
  3933. */
  3934. async replaceOrCreate(data, filter = void 0) {
  3935. const adapter = await this.getAdapter();
  3936. return adapter.replaceOrCreate(this.modelName, data, filter);
  3937. }
  3938. /**
  3939. * Patch.
  3940. *
  3941. * @param {object} data
  3942. * @param {object|undefined} where
  3943. * @returns {Promise<number>}
  3944. */
  3945. async patch(data, where = void 0) {
  3946. const adapter = await this.getAdapter();
  3947. return adapter.patch(this.modelName, data, where);
  3948. }
  3949. /**
  3950. * Patch by id.
  3951. *
  3952. * @param {number|string} id
  3953. * @param {object} data
  3954. * @param {object|undefined} filter
  3955. * @returns {Promise<object>}
  3956. */
  3957. async patchById(id, data, filter = void 0) {
  3958. const adapter = await this.getAdapter();
  3959. return adapter.patchById(this.modelName, id, data, filter);
  3960. }
  3961. /**
  3962. * Find.
  3963. *
  3964. * @param {object|undefined} filter
  3965. * @returns {Promise<object[]>}
  3966. */
  3967. async find(filter = void 0) {
  3968. const adapter = await this.getAdapter();
  3969. return adapter.find(this.modelName, filter);
  3970. }
  3971. /**
  3972. * Find one.
  3973. *
  3974. * @param {object|undefined} filter
  3975. * @returns {Promise<object|undefined>}
  3976. */
  3977. async findOne(filter = void 0) {
  3978. const adapter = await this.getAdapter();
  3979. filter = filter != null ? filter : {};
  3980. filter.limit = 1;
  3981. const result = await adapter.find(this.modelName, filter);
  3982. return result.length ? result[0] : void 0;
  3983. }
  3984. /**
  3985. * Find by id.
  3986. *
  3987. * @param {number|string} id
  3988. * @param {object|undefined} filter
  3989. * @returns {Promise<object>}
  3990. */
  3991. async findById(id, filter = void 0) {
  3992. const adapter = await this.getAdapter();
  3993. return adapter.findById(this.modelName, id, filter);
  3994. }
  3995. /**
  3996. * Delete.
  3997. *
  3998. * @param {object|undefined} where
  3999. * @returns {Promise<number>}
  4000. */
  4001. async delete(where = void 0) {
  4002. const adapter = await this.getAdapter();
  4003. return adapter.delete(this.modelName, where);
  4004. }
  4005. /**
  4006. * Delete by id.
  4007. *
  4008. * @param {number|string} id
  4009. * @returns {Promise<boolean>}
  4010. */
  4011. async deleteById(id) {
  4012. const adapter = await this.getAdapter();
  4013. return adapter.deleteById(this.modelName, id);
  4014. }
  4015. /**
  4016. * Exists.
  4017. *
  4018. * @param {number|string} id
  4019. * @returns {Promise<boolean>}
  4020. */
  4021. async exists(id) {
  4022. const adapter = await this.getAdapter();
  4023. return adapter.exists(this.modelName, id);
  4024. }
  4025. /**
  4026. * Count.
  4027. *
  4028. * @param {object|undefined} where
  4029. * @returns {Promise<number>}
  4030. */
  4031. async count(where = void 0) {
  4032. const adapter = await this.getAdapter();
  4033. return adapter.count(this.modelName, where);
  4034. }
  4035. };
  4036. __name(_Repository, "Repository");
  4037. Repository = _Repository;
  4038. }
  4039. });
  4040. // src/repository/repository-registry.js
  4041. var import_js_service24, _RepositoryRegistry, RepositoryRegistry;
  4042. var init_repository_registry = __esm({
  4043. "src/repository/repository-registry.js"() {
  4044. import_js_service24 = require("@e22m4u/js-service");
  4045. init_repository();
  4046. init_utils();
  4047. init_errors();
  4048. _RepositoryRegistry = class _RepositoryRegistry extends import_js_service24.Service {
  4049. /**
  4050. * Repositories.
  4051. *
  4052. * @type {object}
  4053. */
  4054. _repositories = {};
  4055. /**
  4056. * Repository ctor.
  4057. *
  4058. * @type {typeof Repository}
  4059. * @private
  4060. */
  4061. _repositoryCtor = Repository;
  4062. /**
  4063. * Set repository ctor.
  4064. *
  4065. * @param {typeof Repository} ctor
  4066. */
  4067. setRepositoryCtor(ctor) {
  4068. if (!ctor || typeof ctor !== "function" || !(ctor.prototype instanceof Repository)) {
  4069. throw new InvalidArgumentError(
  4070. "The first argument of RepositoryRegistry.setRepositoryCtor must inherit from Repository class, but %v was given.",
  4071. ctor
  4072. );
  4073. }
  4074. this._repositoryCtor = ctor;
  4075. }
  4076. /**
  4077. * Get repository.
  4078. *
  4079. * @param {string} modelName
  4080. * @returns {Repository}
  4081. */
  4082. getRepository(modelName) {
  4083. const modelKey = modelNameToModelKey(modelName);
  4084. let repository = this._repositories[modelKey];
  4085. if (repository) return repository;
  4086. repository = new this._repositoryCtor(this.container, modelName);
  4087. this._repositories[modelKey] = repository;
  4088. return repository;
  4089. }
  4090. };
  4091. __name(_RepositoryRegistry, "RepositoryRegistry");
  4092. RepositoryRegistry = _RepositoryRegistry;
  4093. }
  4094. });
  4095. // src/repository/index.js
  4096. var init_repository2 = __esm({
  4097. "src/repository/index.js"() {
  4098. init_repository();
  4099. init_repository_registry();
  4100. }
  4101. });
  4102. // src/relations/has-one-resolver.js
  4103. var import_js_service25, _HasOneResolver, HasOneResolver;
  4104. var init_has_one_resolver = __esm({
  4105. "src/relations/has-one-resolver.js"() {
  4106. import_js_service25 = require("@e22m4u/js-service");
  4107. init_utils();
  4108. init_errors();
  4109. init_repository2();
  4110. init_definition();
  4111. _HasOneResolver = class _HasOneResolver extends import_js_service25.Service {
  4112. /**
  4113. * Include to.
  4114. *
  4115. * @param {object[]} entities
  4116. * @param {string} sourceName
  4117. * @param {string} targetName
  4118. * @param {string} relationName
  4119. * @param {string} foreignKey
  4120. * @param {object|undefined} scope
  4121. * @returns {Promise<void>}
  4122. */
  4123. async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
  4124. if (!entities || !Array.isArray(entities))
  4125. throw new InvalidArgumentError(
  4126. 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v was given.',
  4127. entities
  4128. );
  4129. if (!sourceName || typeof sourceName !== "string")
  4130. throw new InvalidArgumentError(
  4131. 'The parameter "sourceName" of HasOneResolver.includeTo requires a non-empty String, but %v was given.',
  4132. sourceName
  4133. );
  4134. if (!targetName || typeof targetName !== "string")
  4135. throw new InvalidArgumentError(
  4136. 'The parameter "targetName" of HasOneResolver.includeTo requires a non-empty String, but %v was given.',
  4137. targetName
  4138. );
  4139. if (!relationName || typeof relationName !== "string")
  4140. throw new InvalidArgumentError(
  4141. 'The parameter "relationName" of HasOneResolver.includeTo requires a non-empty String, but %v was given.',
  4142. relationName
  4143. );
  4144. if (!foreignKey || typeof foreignKey !== "string")
  4145. throw new InvalidArgumentError(
  4146. 'The parameter "foreignKey" of HasOneResolver.includeTo requires a non-empty String, but %v was given.',
  4147. foreignKey
  4148. );
  4149. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4150. throw new InvalidArgumentError(
  4151. 'The provided parameter "scope" of HasOneResolver.includeTo should be an Object, but %v was given.',
  4152. scope
  4153. );
  4154. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4155. sourceName
  4156. );
  4157. const sourceIds = [];
  4158. entities.forEach((entity) => {
  4159. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4160. throw new InvalidArgumentError(
  4161. 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v was given.',
  4162. entity
  4163. );
  4164. const sourceId = entity[sourcePkPropName];
  4165. if (sourceIds.includes(sourceId)) return;
  4166. sourceIds.push(sourceId);
  4167. });
  4168. const promises = [];
  4169. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4170. scope = scope ? cloneDeep(scope) : {};
  4171. const targetBySourceId = /* @__PURE__ */ new Map();
  4172. sourceIds.forEach((sourceId) => {
  4173. const filter = cloneDeep(scope);
  4174. filter.where = {
  4175. and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
  4176. };
  4177. filter.limit = 1;
  4178. promises.push(
  4179. targetRepository.find(filter).then((result) => {
  4180. if (result.length) targetBySourceId.set(sourceId, result[0]);
  4181. })
  4182. );
  4183. });
  4184. await Promise.all(promises);
  4185. Array.from(targetBySourceId.keys()).forEach((sourceId) => {
  4186. const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
  4187. sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
  4188. });
  4189. }
  4190. /**
  4191. * Include polymorphic to.
  4192. *
  4193. * @param {object[]} entities
  4194. * @param {string} sourceName
  4195. * @param {string} targetName
  4196. * @param {string} relationName
  4197. * @param {string} foreignKey
  4198. * @param {string} discriminator
  4199. * @param {object|undefined} scope
  4200. * @returns {Promise<void>}
  4201. */
  4202. async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
  4203. if (!entities || !Array.isArray(entities))
  4204. throw new InvalidArgumentError(
  4205. 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v was given.',
  4206. entities
  4207. );
  4208. if (!sourceName || typeof sourceName !== "string")
  4209. throw new InvalidArgumentError(
  4210. 'The parameter "sourceName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4211. sourceName
  4212. );
  4213. if (!targetName || typeof targetName !== "string")
  4214. throw new InvalidArgumentError(
  4215. 'The parameter "targetName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4216. targetName
  4217. );
  4218. if (!relationName || typeof relationName !== "string")
  4219. throw new InvalidArgumentError(
  4220. 'The parameter "relationName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4221. relationName
  4222. );
  4223. if (!foreignKey || typeof foreignKey !== "string")
  4224. throw new InvalidArgumentError(
  4225. 'The parameter "foreignKey" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4226. foreignKey
  4227. );
  4228. if (!discriminator || typeof discriminator !== "string")
  4229. throw new InvalidArgumentError(
  4230. 'The parameter "discriminator" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4231. discriminator
  4232. );
  4233. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4234. throw new InvalidArgumentError(
  4235. 'The provided parameter "scope" of HasOneResolver.includePolymorphicTo should be an Object, but %v was given.',
  4236. scope
  4237. );
  4238. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4239. sourceName
  4240. );
  4241. const sourceIds = [];
  4242. entities.forEach((entity) => {
  4243. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4244. throw new InvalidArgumentError(
  4245. 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v was given.',
  4246. entity
  4247. );
  4248. const sourceId = entity[sourcePkPropName];
  4249. if (sourceIds.includes(sourceId)) return;
  4250. sourceIds.push(sourceId);
  4251. });
  4252. const promises = [];
  4253. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4254. scope = scope ? cloneDeep(scope) : {};
  4255. const targetBySourceId = /* @__PURE__ */ new Map();
  4256. sourceIds.forEach((sourceId) => {
  4257. const filter = cloneDeep(scope);
  4258. filter.where = {
  4259. and: [
  4260. { [foreignKey]: sourceId, [discriminator]: sourceName },
  4261. ...scope.where ? [scope.where] : []
  4262. ]
  4263. };
  4264. filter.limit = 1;
  4265. promises.push(
  4266. targetRepository.find(filter).then((result) => {
  4267. if (result.length) targetBySourceId.set(sourceId, result[0]);
  4268. })
  4269. );
  4270. });
  4271. await Promise.all(promises);
  4272. Array.from(targetBySourceId.keys()).forEach((sourceId) => {
  4273. const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
  4274. sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
  4275. });
  4276. }
  4277. /**
  4278. * Include polymorphic by relation name.
  4279. *
  4280. * @param {object[]} entities
  4281. * @param {string} sourceName
  4282. * @param {string} targetName
  4283. * @param {string} relationName
  4284. * @param {string} targetRelationName
  4285. * @param {object|undefined} scope
  4286. * @returns {Promise<void>}
  4287. */
  4288. async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
  4289. if (!entities || !Array.isArray(entities))
  4290. throw new InvalidArgumentError(
  4291. 'The parameter "entities" of HasOneResolver.includePolymorphicByRelationName requires an Array of Object, but %v was given.',
  4292. entities
  4293. );
  4294. if (!sourceName || typeof sourceName !== "string")
  4295. throw new InvalidArgumentError(
  4296. 'The parameter "sourceName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4297. sourceName
  4298. );
  4299. if (!targetName || typeof targetName !== "string")
  4300. throw new InvalidArgumentError(
  4301. 'The parameter "targetName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4302. targetName
  4303. );
  4304. if (!relationName || typeof relationName !== "string")
  4305. throw new InvalidArgumentError(
  4306. 'The parameter "relationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4307. relationName
  4308. );
  4309. if (!targetRelationName || typeof targetRelationName !== "string")
  4310. throw new InvalidArgumentError(
  4311. 'The parameter "targetRelationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4312. targetRelationName
  4313. );
  4314. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4315. throw new InvalidArgumentError(
  4316. 'The provided parameter "scope" of HasOneResolver.includePolymorphicByRelationName should be an Object, but %v was given.',
  4317. scope
  4318. );
  4319. const targetRelationDef = this.getService(
  4320. ModelDefinitionUtils
  4321. ).getRelationDefinitionByName(targetName, targetRelationName);
  4322. if (targetRelationDef.type !== RelationType.BELONGS_TO)
  4323. throw new InvalidArgumentError(
  4324. '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 was given.',
  4325. relationName,
  4326. sourceName,
  4327. targetRelationName,
  4328. targetRelationDef.type
  4329. );
  4330. if (!targetRelationDef.polymorphic)
  4331. throw new InvalidArgumentError(
  4332. 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic too.',
  4333. relationName,
  4334. sourceName,
  4335. targetRelationName
  4336. );
  4337. const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
  4338. const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
  4339. return this.includePolymorphicTo(
  4340. entities,
  4341. sourceName,
  4342. targetName,
  4343. relationName,
  4344. foreignKey,
  4345. discriminator,
  4346. scope
  4347. );
  4348. }
  4349. };
  4350. __name(_HasOneResolver, "HasOneResolver");
  4351. HasOneResolver = _HasOneResolver;
  4352. }
  4353. });
  4354. // src/relations/has-many-resolver.js
  4355. var import_js_service26, _HasManyResolver, HasManyResolver;
  4356. var init_has_many_resolver = __esm({
  4357. "src/relations/has-many-resolver.js"() {
  4358. import_js_service26 = require("@e22m4u/js-service");
  4359. init_utils();
  4360. init_errors();
  4361. init_repository2();
  4362. init_definition();
  4363. _HasManyResolver = class _HasManyResolver extends import_js_service26.Service {
  4364. /**
  4365. * Include to.
  4366. *
  4367. * @param {object[]} entities
  4368. * @param {string} sourceName
  4369. * @param {string} targetName
  4370. * @param {string} relationName
  4371. * @param {string} foreignKey
  4372. * @param {object|undefined} scope
  4373. * @returns {Promise<void>}
  4374. */
  4375. async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
  4376. if (!entities || !Array.isArray(entities))
  4377. throw new InvalidArgumentError(
  4378. 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v was given.',
  4379. entities
  4380. );
  4381. if (!sourceName || typeof sourceName !== "string")
  4382. throw new InvalidArgumentError(
  4383. 'The parameter "sourceName" of HasManyResolver.includeTo requires a non-empty String, but %v was given.',
  4384. sourceName
  4385. );
  4386. if (!targetName || typeof targetName !== "string")
  4387. throw new InvalidArgumentError(
  4388. 'The parameter "targetName" of HasManyResolver.includeTo requires a non-empty String, but %v was given.',
  4389. targetName
  4390. );
  4391. if (!relationName || typeof relationName !== "string")
  4392. throw new InvalidArgumentError(
  4393. 'The parameter "relationName" of HasManyResolver.includeTo requires a non-empty String, but %v was given.',
  4394. relationName
  4395. );
  4396. if (!foreignKey || typeof foreignKey !== "string")
  4397. throw new InvalidArgumentError(
  4398. 'The parameter "foreignKey" of HasManyResolver.includeTo requires a non-empty String, but %v was given.',
  4399. foreignKey
  4400. );
  4401. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4402. throw new InvalidArgumentError(
  4403. 'The provided parameter "scope" of HasManyResolver.includeTo should be an Object, but %v was given.',
  4404. scope
  4405. );
  4406. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4407. sourceName
  4408. );
  4409. const sourceIds = [];
  4410. entities.forEach((entity) => {
  4411. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4412. throw new InvalidArgumentError(
  4413. 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v was given.',
  4414. entity
  4415. );
  4416. const sourceId = entity[sourcePkPropName];
  4417. if (sourceIds.includes(sourceId)) return;
  4418. sourceIds.push(sourceId);
  4419. });
  4420. const promises = [];
  4421. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4422. scope = scope ? cloneDeep(scope) : {};
  4423. const targetsBySourceId = /* @__PURE__ */ new Map();
  4424. sourceIds.forEach((sourceId) => {
  4425. const filter = cloneDeep(scope);
  4426. filter.where = {
  4427. and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
  4428. };
  4429. promises.push(
  4430. targetRepository.find(filter).then((result) => {
  4431. var _a;
  4432. if (result.length) {
  4433. let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4434. targets = [...targets, ...result];
  4435. targetsBySourceId.set(sourceId, targets);
  4436. }
  4437. })
  4438. );
  4439. });
  4440. await Promise.all(promises);
  4441. entities.forEach((entity) => {
  4442. var _a;
  4443. const sourceId = entity[sourcePkPropName];
  4444. entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4445. });
  4446. }
  4447. /**
  4448. * Include polymorphic to.
  4449. *
  4450. * @param {object[]} entities
  4451. * @param {string} sourceName
  4452. * @param {string} targetName
  4453. * @param {string} relationName
  4454. * @param {string} foreignKey
  4455. * @param {string} discriminator
  4456. * @param {object|undefined} scope
  4457. * @returns {Promise<void>}
  4458. */
  4459. async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
  4460. if (!entities || !Array.isArray(entities))
  4461. throw new InvalidArgumentError(
  4462. 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v was given.',
  4463. entities
  4464. );
  4465. if (!sourceName || typeof sourceName !== "string")
  4466. throw new InvalidArgumentError(
  4467. 'The parameter "sourceName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4468. sourceName
  4469. );
  4470. if (!targetName || typeof targetName !== "string")
  4471. throw new InvalidArgumentError(
  4472. 'The parameter "targetName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4473. targetName
  4474. );
  4475. if (!relationName || typeof relationName !== "string")
  4476. throw new InvalidArgumentError(
  4477. 'The parameter "relationName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4478. relationName
  4479. );
  4480. if (!foreignKey || typeof foreignKey !== "string")
  4481. throw new InvalidArgumentError(
  4482. 'The parameter "foreignKey" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4483. foreignKey
  4484. );
  4485. if (!discriminator || typeof discriminator !== "string")
  4486. throw new InvalidArgumentError(
  4487. 'The parameter "discriminator" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4488. discriminator
  4489. );
  4490. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4491. throw new InvalidArgumentError(
  4492. 'The provided parameter "scope" of HasManyResolver.includePolymorphicTo should be an Object, but %v was given.',
  4493. scope
  4494. );
  4495. const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4496. sourceName
  4497. );
  4498. const sourceIds = [];
  4499. entities.forEach((entity) => {
  4500. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4501. throw new InvalidArgumentError(
  4502. 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v was given.',
  4503. entity
  4504. );
  4505. const sourceId = entity[sourcePkPropName];
  4506. if (sourceIds.includes(sourceId)) return;
  4507. sourceIds.push(sourceId);
  4508. });
  4509. const promises = [];
  4510. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4511. scope = scope ? cloneDeep(scope) : {};
  4512. const targetsBySourceId = /* @__PURE__ */ new Map();
  4513. sourceIds.forEach((sourceId) => {
  4514. const filter = cloneDeep(scope);
  4515. filter.where = {
  4516. and: [
  4517. { [foreignKey]: sourceId, [discriminator]: sourceName },
  4518. ...scope.where ? [scope.where] : []
  4519. ]
  4520. };
  4521. promises.push(
  4522. targetRepository.find(filter).then((result) => {
  4523. var _a;
  4524. if (result.length) {
  4525. let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4526. targets = [...targets, ...result];
  4527. targetsBySourceId.set(sourceId, targets);
  4528. }
  4529. })
  4530. );
  4531. });
  4532. await Promise.all(promises);
  4533. entities.forEach((entity) => {
  4534. var _a;
  4535. const sourceId = entity[sourcePkPropName];
  4536. entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
  4537. });
  4538. }
  4539. /**
  4540. * Include polymorphic by relation name.
  4541. *
  4542. * @param {object[]} entities
  4543. * @param {string} sourceName
  4544. * @param {string} targetName
  4545. * @param {string} relationName
  4546. * @param {string} targetRelationName
  4547. * @param {object|undefined} scope
  4548. * @returns {Promise<void>}
  4549. */
  4550. async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
  4551. if (!entities || !Array.isArray(entities))
  4552. throw new InvalidArgumentError(
  4553. 'The parameter "entities" of HasManyResolver.includePolymorphicByRelationName requires an Array of Object, but %v was given.',
  4554. entities
  4555. );
  4556. if (!sourceName || typeof sourceName !== "string")
  4557. throw new InvalidArgumentError(
  4558. 'The parameter "sourceName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4559. sourceName
  4560. );
  4561. if (!targetName || typeof targetName !== "string")
  4562. throw new InvalidArgumentError(
  4563. 'The parameter "targetName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4564. targetName
  4565. );
  4566. if (!relationName || typeof relationName !== "string")
  4567. throw new InvalidArgumentError(
  4568. 'The parameter "relationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4569. relationName
  4570. );
  4571. if (!targetRelationName || typeof targetRelationName !== "string")
  4572. throw new InvalidArgumentError(
  4573. 'The parameter "targetRelationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v was given.',
  4574. targetRelationName
  4575. );
  4576. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4577. throw new InvalidArgumentError(
  4578. 'The provided parameter "scope" of HasManyResolver.includePolymorphicByRelationName should be an Object, but %v was given.',
  4579. scope
  4580. );
  4581. const targetRelationDef = this.getService(
  4582. ModelDefinitionUtils
  4583. ).getRelationDefinitionByName(targetName, targetRelationName);
  4584. if (targetRelationDef.type !== RelationType.BELONGS_TO)
  4585. throw new InvalidArgumentError(
  4586. '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 was given.',
  4587. relationName,
  4588. sourceName,
  4589. targetRelationName,
  4590. targetRelationDef.type
  4591. );
  4592. if (!targetRelationDef.polymorphic)
  4593. throw new InvalidArgumentError(
  4594. 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic too.',
  4595. relationName,
  4596. sourceName,
  4597. targetRelationName
  4598. );
  4599. const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
  4600. const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
  4601. return this.includePolymorphicTo(
  4602. entities,
  4603. sourceName,
  4604. targetName,
  4605. relationName,
  4606. foreignKey,
  4607. discriminator,
  4608. scope
  4609. );
  4610. }
  4611. };
  4612. __name(_HasManyResolver, "HasManyResolver");
  4613. HasManyResolver = _HasManyResolver;
  4614. }
  4615. });
  4616. // src/relations/belongs-to-resolver.js
  4617. var import_js_service27, _BelongsToResolver, BelongsToResolver;
  4618. var init_belongs_to_resolver = __esm({
  4619. "src/relations/belongs-to-resolver.js"() {
  4620. import_js_service27 = require("@e22m4u/js-service");
  4621. init_errors();
  4622. init_utils();
  4623. init_repository2();
  4624. init_definition();
  4625. _BelongsToResolver = class _BelongsToResolver extends import_js_service27.Service {
  4626. /**
  4627. * Include to.
  4628. *
  4629. * @param {object[]} entities
  4630. * @param {string} sourceName
  4631. * @param {string} targetName
  4632. * @param {string} relationName
  4633. * @param {string|undefined} foreignKey
  4634. * @param {object|undefined} scope
  4635. * @returns {Promise<void>}
  4636. */
  4637. async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
  4638. if (!entities || !Array.isArray(entities))
  4639. throw new InvalidArgumentError(
  4640. 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v was given.',
  4641. entities
  4642. );
  4643. if (!sourceName || typeof sourceName !== "string")
  4644. throw new InvalidArgumentError(
  4645. 'The parameter "sourceName" of BelongsToResolver.includeTo requires a non-empty String, but %v was given.',
  4646. sourceName
  4647. );
  4648. if (!targetName || typeof targetName !== "string")
  4649. throw new InvalidArgumentError(
  4650. 'The parameter "targetName" of BelongsToResolver.includeTo requires a non-empty String, but %v was given.',
  4651. targetName
  4652. );
  4653. if (!relationName || typeof relationName !== "string")
  4654. throw new InvalidArgumentError(
  4655. 'The parameter "relationName" of BelongsToResolver.includeTo requires a non-empty String, but %v was given.',
  4656. relationName
  4657. );
  4658. if (foreignKey && typeof foreignKey !== "string")
  4659. throw new InvalidArgumentError(
  4660. 'The provided parameter "foreignKey" of BelongsToResolver.includeTo should be a String, but %v was given.',
  4661. foreignKey
  4662. );
  4663. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4664. throw new InvalidArgumentError(
  4665. 'The provided parameter "scope" of BelongsToResolver.includeTo should be an Object, but %v was given.',
  4666. scope
  4667. );
  4668. if (foreignKey == null) foreignKey = `${relationName}Id`;
  4669. const targetIds = entities.reduce((acc, entity) => {
  4670. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4671. throw new InvalidArgumentError(
  4672. 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v was given.',
  4673. entity
  4674. );
  4675. const targetId = entity[foreignKey];
  4676. return targetId != null ? [...acc, targetId] : acc;
  4677. }, []);
  4678. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4679. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4680. targetName
  4681. );
  4682. scope = scope ? cloneDeep(scope) : {};
  4683. const filter = cloneDeep(scope);
  4684. filter.where = {
  4685. and: [
  4686. { [targetPkPropName]: { inq: targetIds } },
  4687. ...scope.where ? [scope.where] : []
  4688. ]
  4689. };
  4690. const targets = await targetRepository.find(filter);
  4691. entities.forEach((entity) => {
  4692. const target = targets.find(
  4693. (e) => e[targetPkPropName] === entity[foreignKey]
  4694. );
  4695. if (target) entity[relationName] = target;
  4696. });
  4697. }
  4698. /**
  4699. * Include polymorphic to.
  4700. *
  4701. * @param {object[]} entities
  4702. * @param {string} sourceName
  4703. * @param {string} relationName
  4704. * @param {string|undefined} foreignKey
  4705. * @param {string|undefined} discriminator
  4706. * @param {object|undefined} scope
  4707. * @returns {Promise<void>}
  4708. */
  4709. async includePolymorphicTo(entities, sourceName, relationName, foreignKey = void 0, discriminator = void 0, scope = void 0) {
  4710. if (!entities || !Array.isArray(entities))
  4711. throw new InvalidArgumentError(
  4712. 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v was given.',
  4713. entities
  4714. );
  4715. if (!sourceName || typeof sourceName !== "string")
  4716. throw new InvalidArgumentError(
  4717. 'The parameter "sourceName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4718. sourceName
  4719. );
  4720. if (!relationName || typeof relationName !== "string")
  4721. throw new InvalidArgumentError(
  4722. 'The parameter "relationName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v was given.',
  4723. relationName
  4724. );
  4725. if (foreignKey && typeof foreignKey !== "string")
  4726. throw new InvalidArgumentError(
  4727. 'The provided parameter "foreignKey" of BelongsToResolver.includePolymorphicTo should be a String, but %v was given.',
  4728. foreignKey
  4729. );
  4730. if (discriminator && typeof discriminator !== "string")
  4731. throw new InvalidArgumentError(
  4732. 'The provided parameter "discriminator" of BelongsToResolver.includePolymorphicTo should be a String, but %v was given.',
  4733. discriminator
  4734. );
  4735. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4736. throw new InvalidArgumentError(
  4737. 'The provided parameter "scope" of BelongsToResolver.includePolymorphicTo should be an Object, but %v was given.',
  4738. scope
  4739. );
  4740. if (foreignKey == null) {
  4741. const singularRelationName = singularize(relationName);
  4742. foreignKey = `${singularRelationName}Id`;
  4743. }
  4744. if (discriminator == null) {
  4745. const singularRelationName = singularize(relationName);
  4746. discriminator = `${singularRelationName}Type`;
  4747. }
  4748. const targetIdsByTargetName = {};
  4749. entities.forEach((entity) => {
  4750. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4751. throw new InvalidArgumentError(
  4752. 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v was given.',
  4753. entity
  4754. );
  4755. const targetId = entity[foreignKey];
  4756. const targetName = entity[discriminator];
  4757. if (targetId == null || targetName == null) return;
  4758. if (targetIdsByTargetName[targetName] == null)
  4759. targetIdsByTargetName[targetName] = [];
  4760. if (!targetIdsByTargetName[targetName].includes(targetId))
  4761. targetIdsByTargetName[targetName].push(targetId);
  4762. });
  4763. const promises = [];
  4764. const targetNames = Object.keys(targetIdsByTargetName);
  4765. scope = scope ? cloneDeep(scope) : {};
  4766. const targetEntitiesByTargetNames = {};
  4767. targetNames.forEach((targetName) => {
  4768. let targetRepository;
  4769. try {
  4770. targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4771. } catch (error) {
  4772. if (error instanceof InvalidArgumentError) {
  4773. if (error.message === `The model "${targetName}" is not defined.` || error.message === `The model "${targetName}" does not have a specified datasource.`) {
  4774. return;
  4775. }
  4776. } else {
  4777. throw error;
  4778. }
  4779. }
  4780. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4781. targetName
  4782. );
  4783. const targetFilter = cloneDeep(scope);
  4784. const targetIds = targetIdsByTargetName[targetName];
  4785. targetFilter.where = {
  4786. and: [
  4787. { [targetPkPropName]: { inq: targetIds } },
  4788. ...scope.where ? [scope.where] : []
  4789. ]
  4790. };
  4791. const promise = targetRepository.find(targetFilter).then((result) => {
  4792. var _a;
  4793. targetEntitiesByTargetNames[targetName] = [
  4794. ...(_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [],
  4795. ...result
  4796. ];
  4797. });
  4798. promises.push(promise);
  4799. });
  4800. await Promise.all(promises);
  4801. entities.forEach((entity) => {
  4802. var _a;
  4803. const targetId = entity[foreignKey];
  4804. const targetName = entity[discriminator];
  4805. if (targetId == null || targetName == null || targetEntitiesByTargetNames[targetName] == null) {
  4806. return;
  4807. }
  4808. const targetEntities = (_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [];
  4809. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4810. targetName
  4811. );
  4812. const target = targetEntities.find((e) => e[targetPkPropName] === targetId);
  4813. if (target) entity[relationName] = target;
  4814. });
  4815. }
  4816. };
  4817. __name(_BelongsToResolver, "BelongsToResolver");
  4818. BelongsToResolver = _BelongsToResolver;
  4819. }
  4820. });
  4821. // src/relations/references-many-resolver.js
  4822. var import_js_service28, _ReferencesManyResolver, ReferencesManyResolver;
  4823. var init_references_many_resolver = __esm({
  4824. "src/relations/references-many-resolver.js"() {
  4825. import_js_service28 = require("@e22m4u/js-service");
  4826. init_errors();
  4827. init_utils();
  4828. init_repository2();
  4829. init_definition();
  4830. _ReferencesManyResolver = class _ReferencesManyResolver extends import_js_service28.Service {
  4831. /**
  4832. * Include to.
  4833. *
  4834. * @param {object[]} entities
  4835. * @param {string} sourceName
  4836. * @param {string} targetName
  4837. * @param {string} relationName
  4838. * @param {string|undefined} foreignKey
  4839. * @param {object|undefined} scope
  4840. * @returns {Promise<void>}
  4841. */
  4842. async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
  4843. if (!entities || !Array.isArray(entities))
  4844. throw new InvalidArgumentError(
  4845. 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v was given.',
  4846. entities
  4847. );
  4848. if (!sourceName || typeof sourceName !== "string")
  4849. throw new InvalidArgumentError(
  4850. 'The parameter "sourceName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v was given.',
  4851. sourceName
  4852. );
  4853. if (!targetName || typeof targetName !== "string")
  4854. throw new InvalidArgumentError(
  4855. 'The parameter "targetName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v was given.',
  4856. targetName
  4857. );
  4858. if (!relationName || typeof relationName !== "string")
  4859. throw new InvalidArgumentError(
  4860. 'The parameter "relationName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v was given.',
  4861. relationName
  4862. );
  4863. if (foreignKey && typeof foreignKey !== "string")
  4864. throw new InvalidArgumentError(
  4865. 'The provided parameter "foreignKey" of ReferencesManyResolver.includeTo should be a String, but %v was given.',
  4866. foreignKey
  4867. );
  4868. if (scope && (typeof scope !== "object" || Array.isArray(scope)))
  4869. throw new InvalidArgumentError(
  4870. 'The provided parameter "scope" of ReferencesManyResolver.includeTo should be an Object, but %v was given.',
  4871. scope
  4872. );
  4873. if (foreignKey == null) {
  4874. const singularRelationName = singularize(relationName);
  4875. foreignKey = `${singularRelationName}Ids`;
  4876. }
  4877. const targetIds = entities.reduce((acc, entity) => {
  4878. if (!entity || typeof entity !== "object" || Array.isArray(entity))
  4879. throw new InvalidArgumentError(
  4880. 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v was given.',
  4881. entity
  4882. );
  4883. const ids = entity[foreignKey];
  4884. if (Array.isArray(ids))
  4885. ids.forEach((id) => {
  4886. if (id == null || acc.includes(id)) return;
  4887. acc.push(id);
  4888. });
  4889. return acc;
  4890. }, []);
  4891. const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
  4892. const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
  4893. targetName
  4894. );
  4895. scope = scope ? cloneDeep(scope) : {};
  4896. const filter = cloneDeep(scope);
  4897. filter.where = {
  4898. and: [
  4899. { [targetPkPropName]: { inq: targetIds } },
  4900. ...scope.where ? [scope.where] : []
  4901. ]
  4902. };
  4903. const targets = await targetRepository.find(filter);
  4904. entities.forEach((entity) => {
  4905. const ids = entity[foreignKey];
  4906. entity[relationName] = [];
  4907. if (Array.isArray(ids))
  4908. targets.forEach((target) => {
  4909. const targetId = target[targetPkPropName];
  4910. if (ids.includes(targetId)) entity[relationName].push(target);
  4911. });
  4912. });
  4913. }
  4914. };
  4915. __name(_ReferencesManyResolver, "ReferencesManyResolver");
  4916. ReferencesManyResolver = _ReferencesManyResolver;
  4917. }
  4918. });
  4919. // src/relations/index.js
  4920. var init_relations2 = __esm({
  4921. "src/relations/index.js"() {
  4922. init_has_one_resolver();
  4923. init_has_many_resolver();
  4924. init_belongs_to_resolver();
  4925. init_references_many_resolver();
  4926. }
  4927. });
  4928. // src/filter/include-clause-tool.js
  4929. var import_js_service29, _IncludeClauseTool, IncludeClauseTool;
  4930. var init_include_clause_tool = __esm({
  4931. "src/filter/include-clause-tool.js"() {
  4932. import_js_service29 = require("@e22m4u/js-service");
  4933. init_where_clause_tool();
  4934. init_order_clause_tool();
  4935. init_slice_clause_tool();
  4936. init_errors();
  4937. init_fields_clause_tool();
  4938. init_definition();
  4939. init_relations2();
  4940. _IncludeClauseTool = class _IncludeClauseTool extends import_js_service29.Service {
  4941. /**
  4942. * Include to.
  4943. *
  4944. * @param {object[]} entities
  4945. * @param {string} modelName
  4946. * @param {IncludeClause|undefined} clause
  4947. * @returns {Promise<void>}
  4948. */
  4949. async includeTo(entities, modelName, clause) {
  4950. clause = _IncludeClauseTool.normalizeIncludeClause(clause);
  4951. const promises = [];
  4952. clause.forEach((inclusion) => {
  4953. const relDef = this.getService(
  4954. ModelDefinitionUtils
  4955. ).getRelationDefinitionByName(modelName, inclusion.relation);
  4956. switch (relDef.type) {
  4957. // BELONGS_TO
  4958. case RelationType.BELONGS_TO:
  4959. if (relDef.polymorphic) {
  4960. promises.push(
  4961. this.getService(BelongsToResolver).includePolymorphicTo(
  4962. entities,
  4963. modelName,
  4964. inclusion.relation,
  4965. relDef.foreignKey,
  4966. relDef.discriminator,
  4967. inclusion.scope
  4968. )
  4969. );
  4970. } else {
  4971. promises.push(
  4972. this.getService(BelongsToResolver).includeTo(
  4973. entities,
  4974. modelName,
  4975. relDef.model,
  4976. inclusion.relation,
  4977. relDef.foreignKey,
  4978. inclusion.scope
  4979. )
  4980. );
  4981. }
  4982. break;
  4983. // HAS_ONE
  4984. case RelationType.HAS_ONE:
  4985. if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
  4986. promises.push(
  4987. this.getService(HasOneResolver).includePolymorphicByRelationName(
  4988. entities,
  4989. modelName,
  4990. relDef.model,
  4991. inclusion.relation,
  4992. relDef.polymorphic,
  4993. inclusion.scope
  4994. )
  4995. );
  4996. } else if (relDef.polymorphic) {
  4997. promises.push(
  4998. this.getService(HasOneResolver).includePolymorphicTo(
  4999. entities,
  5000. modelName,
  5001. relDef.model,
  5002. inclusion.relation,
  5003. relDef.foreignKey,
  5004. relDef.discriminator,
  5005. inclusion.scope
  5006. )
  5007. );
  5008. } else {
  5009. promises.push(
  5010. this.getService(HasOneResolver).includeTo(
  5011. entities,
  5012. modelName,
  5013. relDef.model,
  5014. inclusion.relation,
  5015. relDef.foreignKey,
  5016. inclusion.scope
  5017. )
  5018. );
  5019. }
  5020. break;
  5021. // HAS_MANY
  5022. case RelationType.HAS_MANY:
  5023. if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
  5024. promises.push(
  5025. this.getService(HasManyResolver).includePolymorphicByRelationName(
  5026. entities,
  5027. modelName,
  5028. relDef.model,
  5029. inclusion.relation,
  5030. relDef.polymorphic,
  5031. inclusion.scope
  5032. )
  5033. );
  5034. } else if (relDef.polymorphic) {
  5035. promises.push(
  5036. this.getService(HasManyResolver).includePolymorphicTo(
  5037. entities,
  5038. modelName,
  5039. relDef.model,
  5040. inclusion.relation,
  5041. relDef.foreignKey,
  5042. relDef.discriminator,
  5043. inclusion.scope
  5044. )
  5045. );
  5046. } else {
  5047. promises.push(
  5048. this.getService(HasManyResolver).includeTo(
  5049. entities,
  5050. modelName,
  5051. relDef.model,
  5052. inclusion.relation,
  5053. relDef.foreignKey,
  5054. inclusion.scope
  5055. )
  5056. );
  5057. }
  5058. break;
  5059. case RelationType.REFERENCES_MANY:
  5060. promises.push(
  5061. this.getService(ReferencesManyResolver).includeTo(
  5062. entities,
  5063. modelName,
  5064. relDef.model,
  5065. inclusion.relation,
  5066. relDef.foreignKey,
  5067. inclusion.scope
  5068. )
  5069. );
  5070. break;
  5071. default:
  5072. throw new InvalidArgumentError(
  5073. "The relation type %v does not have an inclusion resolver.",
  5074. relDef.type
  5075. );
  5076. }
  5077. });
  5078. await Promise.all(promises);
  5079. }
  5080. /**
  5081. * Validate include clause.
  5082. *
  5083. * @param {IncludeClause|undefined} clause
  5084. */
  5085. static validateIncludeClause(clause) {
  5086. if (clause == null) {
  5087. } else if (clause && typeof clause === "string") {
  5088. } else if (Array.isArray(clause)) {
  5089. const relNames = [];
  5090. clause.flat(Infinity).forEach((el) => {
  5091. this.validateIncludeClause(el);
  5092. if (typeof el === "string") {
  5093. relNames.push(el);
  5094. } else if (typeof el === "object") {
  5095. Object.keys(el).forEach((key) => {
  5096. if (Object.prototype.hasOwnProperty.call(el, key))
  5097. relNames.push(key);
  5098. });
  5099. }
  5100. });
  5101. const duplicateNames = relNames.filter(
  5102. (name, i) => relNames.indexOf(name) !== i
  5103. );
  5104. if (duplicateNames.length)
  5105. throw new InvalidArgumentError(
  5106. 'The provided option "include" has duplicates of %v.',
  5107. duplicateNames[0]
  5108. );
  5109. } else if (typeof clause === "object") {
  5110. if ("relation" in clause) {
  5111. if (!clause.relation || typeof clause.relation !== "string")
  5112. throw new InvalidArgumentError(
  5113. 'The provided option "relation" should be a non-empty String, but %v was given.',
  5114. clause.relation
  5115. );
  5116. if ("scope" in clause && clause) this.validateScopeClause(clause.scope);
  5117. } else {
  5118. Object.keys(clause).forEach((key) => {
  5119. if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
  5120. this.validateIncludeClause(key);
  5121. this.validateIncludeClause(clause[key]);
  5122. });
  5123. }
  5124. } else {
  5125. throw new InvalidArgumentError(
  5126. 'The provided option "include" should have a non-empty String, an Object or an Array, but %v was given.',
  5127. clause
  5128. );
  5129. }
  5130. }
  5131. /**
  5132. * Validate scope clause.
  5133. *
  5134. * @param {object|undefined} clause
  5135. */
  5136. static validateScopeClause(clause) {
  5137. if (clause == null) return;
  5138. if (typeof clause !== "object" || Array.isArray(clause))
  5139. throw new InvalidArgumentError(
  5140. 'The provided option "scope" should be an Object, but %v was given.',
  5141. clause
  5142. );
  5143. if (clause.where != null) {
  5144. WhereClauseTool.validateWhereClause(clause.where);
  5145. }
  5146. if (clause.order != null) {
  5147. OrderClauseTool.validateOrderClause(clause.order);
  5148. }
  5149. if (clause.skip != null) {
  5150. SliceClauseTool.validateSkipClause(clause.skip);
  5151. }
  5152. if (clause.limit != null) {
  5153. SliceClauseTool.validateLimitClause(clause.limit);
  5154. }
  5155. if (clause.fields != null) {
  5156. FieldsClauseTool.validateFieldsClause(clause.fields);
  5157. }
  5158. if (clause.include != null) {
  5159. _IncludeClauseTool.validateIncludeClause(clause.include);
  5160. }
  5161. }
  5162. /**
  5163. * Normalize include clause.
  5164. *
  5165. * @param {IncludeClause|undefined} clause
  5166. * @returns {object[]}
  5167. */
  5168. static normalizeIncludeClause(clause) {
  5169. let result = [];
  5170. if (clause == null) {
  5171. return result;
  5172. } else if (clause && typeof clause === "string") {
  5173. result.push({ relation: clause });
  5174. } else if (Array.isArray(clause)) {
  5175. clause.flat(Infinity).forEach((el) => {
  5176. el = this.normalizeIncludeClause(el);
  5177. result = [...result, ...el];
  5178. });
  5179. const relNames = result.map((v) => v.relation);
  5180. const duplicateNames = relNames.filter(
  5181. (name, i) => relNames.indexOf(name) !== i
  5182. );
  5183. if (duplicateNames.length)
  5184. throw new InvalidArgumentError(
  5185. 'The provided option "include" has duplicates of %v.',
  5186. duplicateNames[0]
  5187. );
  5188. } else if (typeof clause === "object") {
  5189. if ("relation" in clause) {
  5190. if (!clause.relation || typeof clause.relation !== "string")
  5191. throw new InvalidArgumentError(
  5192. 'The provided option "relation" should be a non-empty String, but %v was given.',
  5193. clause.relation
  5194. );
  5195. const normalized = { relation: clause.relation };
  5196. const scope = this.normalizeScopeClause(clause.scope);
  5197. if (scope) normalized.scope = scope;
  5198. result.push(normalized);
  5199. } else {
  5200. Object.keys(clause).forEach((key) => {
  5201. if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
  5202. this.validateIncludeClause(key);
  5203. const normalized = { relation: key };
  5204. const include = this.normalizeIncludeClause(clause[key]);
  5205. if (include.length) normalized.scope = { include };
  5206. result.push(normalized);
  5207. });
  5208. }
  5209. } else {
  5210. throw new InvalidArgumentError(
  5211. 'The provided option "include" should have a non-empty String, an Object or an Array, but %v was given.',
  5212. clause
  5213. );
  5214. }
  5215. return result;
  5216. }
  5217. /**
  5218. * Normalize scope clause.
  5219. *
  5220. * @param {object|undefined} clause
  5221. * @returns {object|undefined}
  5222. */
  5223. static normalizeScopeClause(clause) {
  5224. if (clause == null) return;
  5225. if (typeof clause !== "object" || Array.isArray(clause))
  5226. throw new InvalidArgumentError(
  5227. 'The provided option "scope" should be an Object, but %v was given.',
  5228. clause
  5229. );
  5230. const result = {};
  5231. if (clause.where != null) {
  5232. WhereClauseTool.validateWhereClause(clause.where);
  5233. result.where = clause.where;
  5234. }
  5235. if (clause.order != null) {
  5236. OrderClauseTool.validateOrderClause(clause.order);
  5237. result.order = clause.order;
  5238. }
  5239. if (clause.skip != null) {
  5240. SliceClauseTool.validateSkipClause(clause.skip);
  5241. result.skip = clause.skip;
  5242. }
  5243. if (clause.limit != null) {
  5244. SliceClauseTool.validateLimitClause(clause.limit);
  5245. result.limit = clause.limit;
  5246. }
  5247. if (clause.fields != null) {
  5248. FieldsClauseTool.validateFieldsClause(clause.fields);
  5249. result.fields = clause.fields;
  5250. }
  5251. if (clause.include != null) {
  5252. result.include = this.normalizeIncludeClause(clause.include);
  5253. }
  5254. if (Object.keys(result).length) return result;
  5255. return void 0;
  5256. }
  5257. };
  5258. __name(_IncludeClauseTool, "IncludeClauseTool");
  5259. IncludeClauseTool = _IncludeClauseTool;
  5260. }
  5261. });
  5262. // src/filter/index.js
  5263. var init_filter = __esm({
  5264. "src/filter/index.js"() {
  5265. init_slice_clause_tool();
  5266. init_order_clause_tool();
  5267. init_where_clause_tool();
  5268. init_fields_clause_tool();
  5269. init_include_clause_tool();
  5270. init_operator_clause_tool();
  5271. }
  5272. });
  5273. // src/index.js
  5274. var index_exports = {};
  5275. __export(index_exports, {
  5276. ADAPTER_CLASS_NAME: () => ADAPTER_CLASS_NAME,
  5277. Adapter: () => Adapter,
  5278. AdapterLoader: () => AdapterLoader,
  5279. AdapterRegistry: () => AdapterRegistry,
  5280. BelongsToResolver: () => BelongsToResolver,
  5281. DEFAULT_PRIMARY_KEY_PROPERTY_NAME: () => DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  5282. DataType: () => DataType,
  5283. DatabaseSchema: () => DatabaseSchema,
  5284. DatasourceDefinitionValidator: () => DatasourceDefinitionValidator,
  5285. DefinitionRegistry: () => DefinitionRegistry,
  5286. FieldsClauseTool: () => FieldsClauseTool,
  5287. HasManyResolver: () => HasManyResolver,
  5288. HasOneResolver: () => HasOneResolver,
  5289. IncludeClauseTool: () => IncludeClauseTool,
  5290. InvalidArgumentError: () => InvalidArgumentError,
  5291. InvalidOperatorValueError: () => InvalidOperatorValueError,
  5292. ModelDataSanitizer: () => ModelDataSanitizer,
  5293. ModelDefinitionUtils: () => ModelDefinitionUtils,
  5294. ModelDefinitionValidator: () => ModelDefinitionValidator,
  5295. NotImplementedError: () => NotImplementedError,
  5296. OperatorClauseTool: () => OperatorClauseTool,
  5297. OrderClauseTool: () => OrderClauseTool,
  5298. PrimaryKeysDefinitionValidator: () => PrimaryKeysDefinitionValidator,
  5299. PropertiesDefinitionValidator: () => PropertiesDefinitionValidator,
  5300. PropertyUniqueness: () => PropertyUniqueness,
  5301. PropertyUniquenessValidator: () => PropertyUniquenessValidator,
  5302. ReferencesManyResolver: () => ReferencesManyResolver,
  5303. RelationType: () => RelationType,
  5304. RelationsDefinitionValidator: () => RelationsDefinitionValidator,
  5305. Repository: () => Repository,
  5306. RepositoryRegistry: () => RepositoryRegistry,
  5307. SliceClauseTool: () => SliceClauseTool,
  5308. WhereClauseTool: () => WhereClauseTool,
  5309. capitalize: () => capitalize,
  5310. cloneDeep: () => cloneDeep,
  5311. excludeObjectKeys: () => excludeObjectKeys,
  5312. getValueByPath: () => getValueByPath,
  5313. isDeepEqual: () => isDeepEqual,
  5314. isPlainObject: () => isPlainObject,
  5315. isPromise: () => isPromise,
  5316. likeToRegexp: () => likeToRegexp,
  5317. modelNameToModelKey: () => modelNameToModelKey,
  5318. selectObjectKeys: () => selectObjectKeys,
  5319. singularize: () => singularize,
  5320. stringToRegexp: () => stringToRegexp
  5321. });
  5322. module.exports = __toCommonJS(index_exports);
  5323. init_utils();
  5324. init_errors();
  5325. init_filter();
  5326. init_adapter2();
  5327. // src/database-schema.js
  5328. var import_js_service30 = require("@e22m4u/js-service");
  5329. init_definition();
  5330. init_repository2();
  5331. var _DatabaseSchema = class _DatabaseSchema extends import_js_service30.Service {
  5332. /**
  5333. * Define datasource.
  5334. *
  5335. * @param {object} datasourceDef
  5336. * @returns {this}
  5337. */
  5338. defineDatasource(datasourceDef) {
  5339. this.getService(DefinitionRegistry).addDatasource(datasourceDef);
  5340. return this;
  5341. }
  5342. /**
  5343. * Define model.
  5344. *
  5345. * @param {object} modelDef
  5346. * @returns {this}
  5347. */
  5348. defineModel(modelDef) {
  5349. this.getService(DefinitionRegistry).addModel(modelDef);
  5350. return this;
  5351. }
  5352. /**
  5353. * Get repository.
  5354. *
  5355. * @param {string} modelName
  5356. * @returns {Repository}
  5357. */
  5358. getRepository(modelName) {
  5359. return this.getService(RepositoryRegistry).getRepository(modelName);
  5360. }
  5361. };
  5362. __name(_DatabaseSchema, "DatabaseSchema");
  5363. var DatabaseSchema = _DatabaseSchema;
  5364. // src/index.js
  5365. init_relations2();
  5366. init_definition();
  5367. init_repository2();
  5368. // Annotate the CommonJS export names for ESM import in node:
  5369. 0 && (module.exports = {
  5370. ADAPTER_CLASS_NAME,
  5371. Adapter,
  5372. AdapterLoader,
  5373. AdapterRegistry,
  5374. BelongsToResolver,
  5375. DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
  5376. DataType,
  5377. DatabaseSchema,
  5378. DatasourceDefinitionValidator,
  5379. DefinitionRegistry,
  5380. FieldsClauseTool,
  5381. HasManyResolver,
  5382. HasOneResolver,
  5383. IncludeClauseTool,
  5384. InvalidArgumentError,
  5385. InvalidOperatorValueError,
  5386. ModelDataSanitizer,
  5387. ModelDefinitionUtils,
  5388. ModelDefinitionValidator,
  5389. NotImplementedError,
  5390. OperatorClauseTool,
  5391. OrderClauseTool,
  5392. PrimaryKeysDefinitionValidator,
  5393. PropertiesDefinitionValidator,
  5394. PropertyUniqueness,
  5395. PropertyUniquenessValidator,
  5396. ReferencesManyResolver,
  5397. RelationType,
  5398. RelationsDefinitionValidator,
  5399. Repository,
  5400. RepositoryRegistry,
  5401. SliceClauseTool,
  5402. WhereClauseTool,
  5403. capitalize,
  5404. cloneDeep,
  5405. excludeObjectKeys,
  5406. getValueByPath,
  5407. isDeepEqual,
  5408. isPlainObject,
  5409. isPromise,
  5410. likeToRegexp,
  5411. modelNameToModelKey,
  5412. selectObjectKeys,
  5413. singularize,
  5414. stringToRegexp
  5415. });