tandy icon indicating copy to clipboard operation
tandy copied to clipboard

Make sort dialect-unspecific, allow multiple orders

Open mkg20001 opened this issue 2 years ago • 0 comments

Currently sort is dialect-specific (as it's being passed into the query as a raw string, therefore subject to dialects quoting rules) and being limited to one key

One idea that would work is to convert sort to accepting an array [[key, 'ASC/DESC']] and then iterate over that like this


const schema = Joi.array().items(Joi.array().ordered(Joi.string().valid('id', 'etc').required(), Joi.string().valid('asc', 'desc').default('asc')).required())
const { error, validated } = schema.validate(sortParam)

if (error) throw error

for (const sort of validated) {
  query = query.orderBy(sort[0], sort[1])
}

For backwards compatibility the new behaviour could be a flag or switched with sortParam.startsWith('[') (where the old schema is converted to the new one internally)

mkg20001 avatar Feb 25 '23 16:02 mkg20001