如何在 FeathersJs 中设置默认排序顺序 [英] How Can I set the default Sort order in FeathersJs
问题描述
我对 Feathersjs 有问题,与 sequalize 集成.如果我像下面这样设置默认分页,并且没有指定排序,它会因为生成的 SQL 语句无效而产生错误.
I have an issue with Feathersjs , integrating with sequalize. If I set the default pagination like below, and there is no sort specified it will generate an error because the SQL statement generated is invalid.
使用默认值 5 创建的服务:
Service Created with default of 5:
app.use('/manifests', service({
paginate: {
default: 5,
max: 25
}
}));
SQL 语句生成(设置限制为 20)
SQL Statement Generated ( setting a limit of 20 )
SELECT [id], ...etc
FROM [Manifest] AS [Manifest] OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
设置默认订单是有意义的,但我不知道如何在服务中做到这一点.
It Makes sense to set a default order , but I am not sure how to do that in the service.
我想在这种情况下实现的SQL语句是
The SQL Statement I want to achieve in this case is
SELECT [id], ...etc
FROM [Manifest] AS [Manifest] ORDER BY Date desc OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
我想以某种方式为此设置默认值..?
I would like to somehow set the default for this..?
app.use('/manifests', service({
paginate: {
default: 5,
max: 25
}
sort:{
default: date -1 ( or something )
}
}));
推荐答案
Feathers hooks 允许您通过添加 $sort
常用查询参数 如果未设置:
Feathers hooks allow you to modify the query to what you need by adding the $sort
common query parameter if it is not set:
app.service('/manifests').hooks({
before(context) {
const { query = {} } = context.params;
if(!query.$sort) {
query.$sort = {
date: -1
}
}
context.params.query = query;
}
});
这篇关于如何在 FeathersJs 中设置默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!