@@ -35,10 +35,9 @@ configManager.on('configChanged', onConfigChanged);
3535
3636export default {
3737 getList : async function getList ( resource , { pagination, sort, filter, meta } ) {
38-
3938 const query = findQueryWithId ( resource ) ;
40- query . limit = pagination . perPage ;
41- query . offset = ( pagination . page - 1 ) * pagination . perPage ;
39+ const limit = pagination . perPage ;
40+ const offset = ( pagination . page - 1 ) * pagination . perPage ;
4241 query . sort = sort ;
4342
4443 handleComunicaContextCreation ( query ) ;
@@ -53,6 +52,8 @@ export default {
5352 }
5453
5554 let results = await executeQuery ( query ) ;
55+ let totalItems = results . length ;
56+ results = results . slice ( offset , offset + limit ) ;
5657
5758 if ( Object . keys ( filter ) . length > 0 ) {
5859 results = results . filter ( ( result ) => {
@@ -64,7 +65,7 @@ export default {
6465
6566 return {
6667 data : results ,
67- total : query . totalItems
68+ total : totalItems
6869 } ;
6970 } ,
7071 getOne : async function getOne ( ) {
@@ -125,16 +126,6 @@ async function fetchQuery(query) {
125126 if ( ! query . variableOntology ) {
126127 query . variableOntology = findPredicates ( parsedQuery ) ;
127128 }
128- if ( parsedQuery . limit !== undefined && query . offset + query . limit > parsedQuery . limit ) {
129- parsedQuery . limit = parsedQuery . limit - query . offset ;
130- } else {
131- parsedQuery . limit = query . limit ;
132- }
133- if ( parsedQuery . offset ) {
134- parsedQuery . offset += query . offset ;
135- } else {
136- parsedQuery . offset = query . offset ;
137- }
138129 if ( ! parsedQuery . order && query . sort && query . sort . field !== "id" ) {
139130 const { field, order } = query . sort ;
140131 parsedQuery . order = [
@@ -290,7 +281,6 @@ async function handleQueryExecution(execution, query) {
290281 const resultType = execution . resultType ;
291282 if ( execution . resultType !== "boolean" ) {
292283 const metadata = await execution . metadata ( ) ;
293- query . totalItems = await countQueryResults ( query ) ;
294284 variables = metadata . variables . map ( ( val ) => {
295285 return val . value ;
296286 } ) ;
@@ -301,51 +291,6 @@ async function handleQueryExecution(execution, query) {
301291 }
302292}
303293
304- /**
305- * Predict the total number of elements in the result of a query
306- * @param {object } query - the query element from the configuration
307- * @returns {number } the actual number of results in the query, if it were executed
308- */
309- async function countQueryResults ( query ) {
310- const parser = new Parser ( ) ;
311- const parsedQuery = parser . parse ( query . rawText ) ;
312- const distinctInitial = parsedQuery . distinct ;
313- const offsetInitial = parsedQuery . offset ;
314- const limitInitial = parsedQuery . limit ;
315- parsedQuery . queryType = "SELECT" ;
316- parsedQuery . distinct = false ;
317- parsedQuery . offset = 0 ;
318- if ( parsedQuery . limit ) {
319- delete parsedQuery . limit ;
320- }
321- parsedQuery . variables = [
322- {
323- expression : {
324- type : "aggregate" ,
325- aggregation : "count" ,
326- expression : { termType : "Wildcard" , value : "*" } ,
327- distinct : distinctInitial
328- } ,
329- variable : { termType : "Variable" , value : "totalItems" } ,
330- } ,
331- ] ;
332- const generator = new Generator ( ) ;
333- const countQuery = generator . stringify ( parsedQuery ) ;
334- const bindings = await myEngine . queryBindings ( countQuery , {
335- sources : query . comunicaContext . sources ,
336- fetch : fetch ,
337- httpProxyHandler : proxyHandler ,
338- } ) ;
339- let totalItems = parseInt ( ( await bindings . toArray ( ) ) [ 0 ] . get ( "totalItems" ) . value ) ;
340- if ( offsetInitial ) {
341- totalItems -= offsetInitial ;
342- }
343- if ( limitInitial && totalItems > limitInitial ) {
344- totalItems = limitInitial ;
345- }
346- return totalItems ;
347- }
348-
349294const queryTypeHandlers = {
350295 bindings : configureBindingStream ,
351296 quads : configureQuadStream ,
0 commit comments