@skedulo/pulse-solution-services 0.0.22 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))(function(s,
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))(function(s,o){function a(e){try{h(i.next(e))}catch(e){o(e)}}function n(e){try{h(i.throw(e))}catch(e){o(e)}}function h(e){var t;e.done?s(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(a,n)}h((i=i.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLQueryBuilder=void 0;class GraphQLQueryBuilder{constructor(e,t=!1){this.fields=new Set(["UID"]),this.filters=[],this.parentQueries={},this.childQueries={},this.graphqlService=null,this.queryParams=e,this.objectName=e.objectName.charAt(0).toLowerCase()+e.objectName.slice(1),this.operationName=e.operationName,this.isParent=t}setGraphqlService(e){this.graphqlService=e}withFields(e){return e.forEach(e=>this.fields.add(e)),this}withFilter(e){if(this.isParent)throw new Error("Cannot apply filters to a parent query.");return this.filters.push(e),this}withLimit(e){if(this.isParent)throw new Error("Cannot apply limit to a parent query.");return this.limit=e,this}withFirst(e){if(this.isParent)throw new Error("Cannot apply limit to a parent query.");return this.first=e,this}withOffset(e){if(this.isParent)throw new Error("Cannot apply offset to a parent query.");return this.offset=e,this}withOrderBy(e){if(this.isParent)throw new Error("Cannot apply orderBy to a parent query.");return this.orderBy=e,this}withCursor(e){if(this.isParent)throw new Error("Cannot apply cursor to a parent query.");return this.after=e,this}withParentQuery(e){const t=new GraphQLQueryBuilder({objectName:e,operationName:this.operationName},!0);return this.parentQueries[e]=t,t}withChildQuery(e){const t=new GraphQLQueryBuilder({objectName:e,operationName:this.operationName});return this.childQueries[e]=t,t}formatQueryFields(){const e=Object.entries(this.parentQueries).map(([e,t])=>`${e} { ${t.formatQueryFields()} }`).join(", "),t=Object.entries(this.childQueries).map(([e,t])=>{const r=t.build(),i=[];r.filter&&i.push(`filter: "${r.filter}"`),r.orderBy&&i.push(`orderBy: "${r.orderBy}"`),void 0!==r.limit&&void 0!==this.limit&&i.push(`limit: ${r.limit}`);return`${e} ${i.length>0?`(${i.join(", ")})`:""} { ${r.fields||"UID"} }`}).join(", ");return[...this.fields,e,t].filter(Boolean).join(", ")}build(){return{objectName:this.objectName,operationName:this.operationName,fields:this.formatQueryFields(),filter:this.filters.length>0?this.filters.join(" AND "):void 0,first:this.first,limit:this.limit,offset:this.offset,orderBy:this.orderBy,after:this.after,readOnly:this.queryParams.readOnly}}execute(){return __awaiter(this,void 0,void 0,function*(){if(!this.graphqlService)return Promise.reject(new Error("No GraphQL service set for query execution."));const e=this.build();return e.readOnly=this.queryParams.readOnly,yield this.graphqlService.query(e)})}executeAll(){return __awaiter(this,arguments,void 0,function*(e=1,t=20){if(!this.graphqlService)throw new Error("No GraphQL service set for query execution.");if(e<1)throw new Error("fromPage must be at least 1.");if(t<e)throw new Error("toPage must be greater than or equal to fromPage.");const r=Math.min(t,20),i=this.build(),s=yield this.graphqlService.query(i),o=s.totalCount;if(0===o)return{records:[],totalCount:0,pageInfo:{hasNextPage:!1},endCursor:"",endOffset:0};const a=this.limit||this.first||200,n=Math.ceil(o/a),h=Math.min(r,n);if(0===Math.max(0,h-e+1))return{records:[],totalCount:o,pageInfo:{hasNextPage:h<n},endCursor:"",endOffset:(e-1)*a};const u=[];for(let t=e-1;t<h;t++){const e=new GraphQLQueryBuilder({objectName:this.objectName,operationName:this.operationName,readOnly:this.queryParams.readOnly});e.setGraphqlService(this.graphqlService),e.fields=new Set(this.fields),e.filters=[...this.filters],void 0!==this.limit?e.limit=a:e.first=a,e.offset=t*a,e.orderBy=this.orderBy,e.after=this.after,u.push(e)}const l=yield this.graphqlService.queryBatch(u),f=l.flatMap(e=>e.records),d=l[l.length-1];return{records:f,totalCount:s.totalCount,pageInfo:d.pageInfo,endCursor:d.endCursor,endOffset:d.endOffset}})}toString(){return`{ ${this.objectName} { ${this.formatQueryFields()} } }`}}exports.GraphQLQueryBuilder=GraphQLQueryBuilder;
|
package/package.json
CHANGED