olavim / objection-cursor

Cursor based pagination plugin for Objection.js
MIT License
30 stars 8 forks source link

Issue when using modifiers to sort relations on withGraphFetched() #41

Open tony-oldport opened 1 year ago

tony-oldport commented 1 year ago

If I build a query using withGraphFetched() and a modifier to sort the relation it throws an error Cursor does not match ordering

Here's an example using objection v3.0.1, knex v0.95.15 and objection-cursor v1.2.6

const invoices = await InvoiceModel.query()
  .withGraphFetched('invoiceItems(invoiceItemsOrder)')
  .modifiers({
    invoiceItemsOrder (builder ) {
      builder.orderBy('position')
    }
  })

Throws the following error:

   stack: 'Error: Cursor does not match ordering\n' 
   at CursorPageOperation.onBefore (/usr/app/node_modules/objection-cursor/lib/operations/CursorPageOperation.js:29:10)\n'
   at /usr/app/node_modules/objection-cursor/lib/operations/utils.js:29:57\n'
   at Array.reduce (<anonymous>)\n'
   at Object.<anonymous> (/usr/app/node_modules/objection-cursor/lib/operations/utils.js:29:34)\n'
   at RunBeforeOperation.onBefore1 (/usr/app/node_modules/objection/lib/queryBuilder/operations/RunBeforeOperation.js:17:22)\n'
   at Object.callAsyncOperationMethod (/usr/app/node_modules/objection/lib/queryBuilder/QueryBuilderOperationSupport.js:387:39)\n'
   at /usr/app/node_modules/objection/lib/queryBuilder/QueryBuilder.js:1143:19\n'
   at processTicksAndRejections (node:internal/process/task_queues:96:5)'

I also tried it out with a reference builder, but same result: builder.orderBy(ref('invoice_items.position'))