feat(queries): fixed keyset non-sequential scan pagination

This commit is contained in:
Dimitrie Stefanescu
2020-05-10 17:35:45 +01:00
parent 34490a5a1b
commit 755f9e63db
2 changed files with 11 additions and 8 deletions
+9 -6
View File
@@ -252,7 +252,7 @@ module.exports = {
if ( orderBy.field === 'id' ) {
cteInnerQuery.orderBy( 'id', direction )
} else {
cteInnerQuery.orderByRaw( knex.raw( `jsonb_path_query_first( data, ? ) ${direction}`, [ '$.' + orderBy.field ] ) )
cteInnerQuery.orderByRaw( knex.raw( `jsonb_path_query_first( data, ? ) ${direction}, id asc`, [ '$.' + orderBy.field ] ) )
}
} )
@@ -266,6 +266,9 @@ module.exports = {
if ( typeof cursor.value === 'boolean' ) castType = 'boolean'
if ( typeof cursor.value === 'number' ) castType = 'numeric'
if( castType === 'text')
cursor.value = `"${cursor.value}"`
if ( operatorsWhitelist.indexOf( cursor.operator ) == -1 )
throw new Error( 'Invalid operator for cursor' )
@@ -273,21 +276,21 @@ module.exports = {
if ( cursor.field === 'id' ) {
mainQuery.where( knex.raw( `id ${cursor.operator} ? `, [ cursor.value ] ) )
} else {
mainQuery.where( knex.raw( `jsonb_path_query_first( data, ? )::${castType} ${cursor.operator} ? `, [ '$.' + cursor.field, castType === 'text' ? `"${cursor.value}"` : cursor.value ] ) )
mainQuery.where( knex.raw( `jsonb_path_query_first( data, ? )::${castType} ${cursor.operator}= ? `, [ '$.' + cursor.field, cursor.value ] ) )
}
} else {
mainQuery.where( knex.raw( `??::${castType} ${cursor.operator}= ? `, [ select.indexOf( cursor.field ).toString( ), castType === 'text' ? `"${cursor.value}"` : cursor.value ] ) )
mainQuery.where( knex.raw( `??::${castType} ${cursor.operator}= ? `, [ select.indexOf( cursor.field ).toString( ), cursor.value ] ) )
}
if ( cursor.lastSeenId ) {
console.log(cursor)
mainQuery.andWhere( qb => {
qb.where( 'id', '>=', cursor.lastSeenId )
qb.where( 'id', '>', cursor.lastSeenId )
// qb.andWhere( 'id', '!=', cursor.lastSeenId )
if ( unwrapData )
qb.orWhere( knex.raw( `jsonb_path_query_first( data, ? )::${castType} ${cursor.operator} ? `, [ '$.' + cursor.field, castType === 'text' ? `"${cursor.value}"` : cursor.value ] ) )
qb.orWhere( knex.raw( `jsonb_path_query_first( data, ? )::${castType} ${cursor.operator} ? `, [ '$.' + cursor.field, cursor.value ] ) )
else
qb.orWhere( knex.raw( `??::${castType} ${cursor.operator} ? `, [ select.indexOf( cursor.field ).toString( ), castType === 'text' ? `"${cursor.value}"` : cursor.value ] ) )
qb.orWhere( knex.raw( `??::${castType} ${cursor.operator} ? `, [ select.indexOf( cursor.field ).toString( ), cursor.value ] ) )
} )
}
}
+2 -2
View File
@@ -246,7 +246,7 @@ describe( 'Objects', ( ) => {
select: [ 'similar', 'id' ],
query: [ { field: 'similar', operator: '>=', value: 0 }, { field: 'similar', operator: '<', value: 100 } ],
orderBy: { field: 'similar', direction: 'asc' },
limit: 9
limit: 2
} )
let test4 = await getObjectChildrenQuery( {
@@ -255,7 +255,7 @@ describe( 'Objects', ( ) => {
query: [ { field: 'similar', operator: '>=', value: 0 }, { field: 'similar', operator: '<', value: 100 } ],
orderBy: { field: 'similar', direction: 'asc' },
cursor: test3.cursor,
limit: 9
limit: 2
} )
console.log( test3.objects )