mongoose-graphql-pagination开源项目

我要开发同款
匿名用户2021年11月23日
130阅读

技术信息

开源地址
https://gitee.com/MTrun/react-big-screen
授权协议
MIT License

作品详情

GraphQLMogooseCursorPagiatiowithElasticsearchSupport

AddssupportforRelay-likecursorpagiatiowithMogoosemodels/documets.Thislibraryalsoprovidestype-ahead(autocomplete)fuctioality(withpagiatio)usigMogoDBregularexpressioqueries.Iadditio,youcaoptioallypagiatehydratedMogoosemodelsfromElasticsearch.

Istall

yaradd@limit0/mogoose-graphql-pagiatio

Usage

Pagiatio,type-ahead,adElastic+MogoosepagiatiosupportareavailableviathePagiatio,TypeAheadadSearchPagiatioclasses,respectively.Allclassesshouldbecosidered,"sigleuse,"adshouldbeistatiatedoceperqueryorrequest.

Pagiatio

ReturspagiatedresultsfromMogoDBfortheprovidedMogoosemodel.

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();SearchPagiatio

ReturspagiatedadhydratedMogoose/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.

PagiatioResolvers

Fially,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});}),},};

功能介绍

GraphQL Mongoose Cursor Pagination with Elasticsearch Support Adds support for Relay-like cursor ...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论