GraphQLMogooseCursorPagiatiowithElasticsearchSupport
AddssupportforRelay-likecursorpagiatiowithMogoosemodels/documets.Thislibraryalsoprovidestype-ahead(autocomplete)fuctioality(withpagiatio)usigMogoDBregularexpressioqueries.Iadditio,youcaoptioallypagiatehydratedMogoosemodelsfromElasticsearch.
Istallyaradd@limit0/mogoose-graphql-pagiatio
UsagePagiatio,type-ahead,adElastic+MogoosepagiatiosupportareavailableviathePagiatio,TypeAheadadSearchPagiatioclasses,respectively.Allclassesshouldbecosidered,"sigleuse,"adshouldbeistatiatedoceperqueryorrequest.
PagiatioReturspagiatedresultsfromMogoDBfortheprovidedMogoosemodel.
Tobegiusig,requiretheclass
cost{Pagiatio}=require('@limit0/mogoose-graphql-pagiatio');Usetheclasscostructortocofigurethesettigsforthepagiatedquery.
costructor(Model,{criteria={},pagiatio={},sort={},projectio},optios={})Model:TheMogoosemodelistacetoquery.Required.
criteria:Aquerycriteriaobjecttoapplytothepagiatedquery.CabeayMogoDBquery.Forexample:{deleted:false}or{age:{$gt:30}}.Thecriteriawillbedeeplymerged,butwilligore"special"objectssuchasObjectIdad,bydefault,willolymerge"plaiobjects."Optioal.
pagiatio:Thepagiatioparametersobject.Caacceptafirstad/orafterproperty.Thefirstvaluespecifiesthelimit/pagesize.Theaftervaluespecifiesthecursortostartatwhepagiatig.Forexample:{first:50,after:'some-cursor-value'}wouldreturthefirst50edgesaftertheprovidedcursor.Bydefaulttheresultswillbelimitedto10edges.Optioal.
sort:Specifiesthesortoptios.Thefieldpropertyspecifiesthefieldtosortby,adtheorderdefiesthedirectio.Forexample:{field:'ame',order:-1}wouldsorttheedgesbyame,descedig.BydefaulttheedgesaresortedbyID,ascedig.Optioal.
projectio:Specifiesthefieldstoreturfromthedatabase.Forexample:{field:1}or{field:0}wouldicludeorexcludethespecifiedfield,respectively.Ifleftudefied,orasaemptyobject,allfieldswillberetured(whichisthedefaultbehavior).Optioal.
optios:Specifiesadditioalcofiguratiooptios,suchasdefaultlimit,maxlimit,sortcollatio,objectmergeoptios,adsortcreatedfield.
Completeexample:
cost{Pagiatio}=require('@limit0/mogoose-graphql-pagiatio');costYourModel=require('./your-model');costpagiated=ewPagiatio(YourModel,{criteria:{deleted:false},pagiatio:{first:25},sort:{field:'ame',order:-1},});//Retrievetheedges...costedges=pagiated.getEdges();Ocetheistaceiscreated,usethemethodslistedbelowtoaccessthepagiatedifo.
getTotalCout()Retursthetotalumberofdocumetsthatmatchthequerycriteria,regardlessofpagesize(limit).
getEdges()Returstheedges(Mogoosedocumetodes)thatmatchthequerycriteria.Theresultswillbelimitedbythefirstvalue.
getEdCursor()Retursthecursorvalue(o-obfuscated)ofthelastedgethatmatchesthecurretcriteria.ThisvaluewillresolvetotheMogoosedocumetID,adcabecoverteditoaobfuscatedIDwhereturedbytheGraphQLserver.Willreturulliforesultswerefoud.
hasNextPage()Determiesifaotherpageofedgesisavailable,basedothecurretcriteria.
Type-Ahead(AutoComplete)TypeaheadsupportisimplemetedusigMogoDBregularexpressioqueries.Tobegiusig,requiretheclass:
cost{TypeAhead}=require('@limit0/mogoose-graphql-pagiatio');Usetheclasscostructortocofigurethesettigsforthetype-aheadquery.
costructor(field,term,criteria={},optios={})field:Thefieldtoquery/searchagaist.Forexample:ame.Required.
term:Thesearchterm.Ca/shouldbeapartialphrase.Required.
criteria:AdditioalMogoDBquerycriteriatoapplywhequeryig.Forexample{deleted:false}.Thecriteriawillbedeeplymerged,butwilligore"special"objectssuchasObjectIdad,bydefault,willolymerge"plaiobjects."Optioal.
optios:Thetype-aheadcofiguratiooptiosobject.Hasthreetype-aheadrelatedproperties:
{//Determieshowtheregexiscostructed.//Caeitherbe`starts-with`,`eds-with`,`exact-match`or`cotais`.//Defaultsto`cotais`.positio:'cotais',//Whethertoescaperegexspecialcharacters.//Defaultsto`true`.escape:true,//Whethertotreattheregexqueryascasesesitive.//Defaultsto`false`.caseSesitive:false,}Iadditio,theoptioswillalsoaccepttheoptioalmergeOptiosobjectpropertyhtatcabeusedtooverridethedefaultmergerules.
pagiate(Model,pagiatio={},optios={})CreatesaPagiatioistaceusigthecostructedTypeAheadoptios.
buildCriteria()ReturstheMogoDBcriteriathatcabeusedforqueryigthedatabase.Geerally,thiswillotbeusedad,istead,shouldbeidirectlyaccessedviathepagiatemethod.
Completeexample:
cost{TypeAhead}=require('@limit0/mogoose-graphql-pagiatio');costYourModel=require('./your-model');costtypeahead=ewTypeAhead('ame','foo',{deleted:false,},{positio:'starts-with',});costpagiated=typeahead.pagiate(YourModel,{first:25});//Retrievetheedges...costedges=pagiated.getEdges();SearchPagiatioReturspagiatedadhydratedMogoose/MogoDBresultsfromElasticsearch.Thisassumesthatyouare,isomefashio,savigsomedataforaspecificMogoosemodelwithiElasticsearch,asthiswillattempttocoverttheElasticresultsbackitoMogoosedocumetsfromMogoDB.Thisalsoassumesthatthe_idvalueoftheidexeddocumetiElasticsearchmatchesthe_idvaluethatisstorediMogoDB.
Tobegiusig,requiretheclass
cost{SearchPagiatio}=require('@limit0/mogoose-graphql-pagiatio');Usetheclasscostructortocofigurethesettigsforthepagiatedquery.
costructor(Model,cliet,{params={},pagiatio={}})Model:TheMogoosemodelistacetouseforre-queryigMogoDBtohydratetheElasticsearchresults.Required.
cliet:TheElasticsearchclietistacecreatedfromtheelasticsearchNode/JSAPIlibrary.Required.
params:Theelasticsearchparametersthatwillbesettocliet.search.Forexample:{idex:'some-idex',type:'some-type',body:{query:{match_all:{}}}}Required.
pagiatio:Thepagiatioparametersobject.Caacceptafirstad/orafterproperty.Thefirstvaluespecifiesthelimit/pagesize.Theaftervaluespecifiesthecursortostartatwhepagiatig.Forexample:{first:50,after:'some-cursor-value'}wouldreturthefirst50edgesaftertheprovidedcursor.Bydefaulttheresultswillbelimitedto10edges.Optioal.
Completeexample:
cost{SearchPagiatio}=require('@limit0/mogoose-graphql-pagiatio');costelasticCliet=require('./your-elasticsearch-cliet');costYourModel=require('./your-model');costparams={idex:'your-idex',type:'your-type',body:{query:{match_all:{}},},};costpagiatio={first:25};costpagiated=ewSearchPagiatio(YourModel,elasticCliet,{params,pagiatio});//Retrievetheedges...costedges=pagiated.getEdges();Ocetheistaceiscreated,usethemethodslistedbelowtoaccessthepagiatedifo.
getTotalCout()Retursthetotalumberofdocumetsthatmatchthesearchcriteria,regardlessofpagesize(limit).
getEdges()Returstheedges(hydratedMogoosedocumetodesfromElasticsearchhits)thatmatchthesearchcriteria.Theresultswillbelimitedbythefirstvalue.
getEdCursor()Retursthecursorvalue(o-obfuscated)ofthelastedgethatmatchesthecurretcriteria.ThisvaluewillresolvetoaJSONstrigifiedversiooftheElasticsearchsortvalue,adcabecoverteditoaobfuscatedIDwhereturedbytheGraphQLserver.Willreturulliforesultswerefoud.
hasNextPage()Determiesifaotherpageofedgesisavailable,basedothecurretcriteria.
PagiatioResolversFially,thepagiatioResolversprovidehelperfuctiosforusigPagiatiowithaGraphQLserversuchasApollo.
Forexample,ifyouhadaGraphQLdefiitiosimilartothis:
scalarCursortypeQuery{allCotacts(pagiatio:PagiatioIput={},sort:CotactSortIput={}):CotactCoectio!searchCotacts(pagiatio:PagiatioIput={},phrase:Strig!):CotactCoectio!}typeCotactCoectio{totalCout:It!edges:[CotactEdge]!pageIfo:PageIfo!}typeCotactEdge{ode:Cotact!cursor:Cursor!}typeCotact{id:Strig!ame:Strig!email:Strig!}typePageIfo{hasNextPage:Boolea!edCursor:Cursor}iputPagiatioIput{first:It!=25after:Cursor}iputCotactSortIput{field:CotactSortField!=createdAtorder:It!=-1}eumCotactSortField{amecreatedAtupdatedAt}Thefollowigresolverscouldbeapplied:
cost{CursorType}=require('@limit0/graphql-custom-types');cost{Pagiatio,pagiatioResolvers,SearchPagiatio}=require('@limit0/mogoose-graphql-pagiatio');costelasticCliet=require('./path-to/elasticsearch-cliet');costCotact=require('./path-to/cotact-model');module.exports={//Thecursortype.//WillobfuscatetheMogoIDfor`Pagiatio`adtheElasticsortvaluefor`SearchPagiatio`Cursor:CursorType,//Applythepagiatioresolversforthecoectio.CotactCoectio:pagiatioResolvers.coectio,Query:{/***Usepagiatioothequery.*WillqueryMogoDBviaMogoosefortheprovidedmodel.*/allCotacts:(root,{pagiatio,sort})=>ewPagiatio(Cotact,{pagiatio,sort},{//Iformsthesortthatthe`createdAt`fieldspecifiesacreateddate.//WillisteadusethedocumetIDwhesortig.sort:{createdField:'createdAt'},}),/***Usesearchpagiatioothequery.*WillqueryElasticsearchadthehydratetheresultsfrom*MogoDB(viaMogoose)fortheprovidedmodel.*/searchCotacts:(root,{pagiatio,phrase})=>{//Settheparametersfortheelasticcliet`search`method.//@seehttps://www.elastic.co/guide/e/elasticsearch/cliet/javascript-api/curret/api-referece.html#api-searchcostparams={idex:'idex-where-cotacts-exist',type:'type-where-cotacts-exist',body:{query:{match:{ame:phrase}}},};returewSearchPagiatio(Cotact,elasticCliet,{params,pagiatio});}),},};






评论