Loopbackjs 如何获得订单';2'<';10';在环回中使用过滤器?
我的数据如Loopbackjs 如何获得订单';2'<';10';在环回中使用过滤器?,loopbackjs,loopback,Loopbackjs,Loopback,我的数据如['2','13','13A','14-1'],如何使用过滤器获得正确的顺序?谢谢大家。IIUC,您正在数据库中将数字(2,10等)存储为字符串('2','10'等) 环回依赖于数据库执行排序(排序) 以下是几件可以尝试的事情: 修改模型定义以将特性存储为number。环回是智能的,在将用户(RESTAPI客户端)提供的字符串值存储到数据库之前,它会将这些值强制为数字。这将是我首选的解决方案,因为它不需要应用程序中的任何复杂代码,并且可以保持性能 根据您使用的数据库,可以将其配置为将字
['2','13','13A','14-1']
,如何使用过滤器获得正确的顺序?谢谢大家。IIUC,您正在数据库中将数字(2
,10
等)存储为字符串('2'
,'10'
等)
环回依赖于数据库执行排序(排序)
以下是几件可以尝试的事情:
修改模型定义以将特性存储为number
。环回是智能的,在将用户(RESTAPI客户端)提供的字符串值存储到数据库之前,它会将这些值强制为数字。这将是我首选的解决方案,因为它不需要应用程序中的任何复杂代码,并且可以保持性能
根据您使用的数据库,可以将其配置为将字符串值作为数字进行排序。这不是环回专用的,我真的帮不了你
作为最后一种手段,您可以在内存中对记录进行排序,当数据库不支持基于位置的查询时,LoopBack已经在这样做了。其思想是告诉数据库返回符合筛选标准的所有记录,然后在Node.js进程中应用顺序
、限制
、跳过
和其他选项。请注意,这会严重影响性能,并且仅适用于大小合理的数据
至于第三个选项:实现方面,您需要覆盖模型类中的find
方法
// common/models/my-model.js
module.exports = function(MyModel) {
MyModel.on('modelRemoted', () => {
MyModel._findRaw = MyModel.find;
MyModel.find = findWithCustomSort;
});
}
function findWithCustomSort(filter, options, cb) {
if (!cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
} else if (!options && typeof filter === 'function') {
cb = filter;
filter = undefined;
}
}
const dbFilter = {
where: filter.where,
include: filter.include,
fields: filter.fields,
};
if (cb) {
this._findRaw(dbFilter, options, (err, found) => {
if (err) return cb(err);
else cb(null, sortResults(filter, found))
});
} else {
return this._findRaw(dbFilter, options)
.then(found => sortResults(filter, found));
}
}
function sortResults(filter, data) {
// implement your sorting rules, don't forget about "limit", "skip", etc.
}
更新
在自定义方法中是否有使用sql进行查询的方法
是的,您可以使用MyModel.dataSource.connector.execute
函数执行任何SQL,请参阅。不过有一个缺点-此方法基于回调,不能使用Promise API或async/await
const idValue = 1;
MyModel.dataSource.connector.execute(
'SELECT * FROM MyModel WHERE id=?',
[idValue]
(err, results) => {
if (err) console.error('query failed', err);
else console.log('found data', results);
});
IIUC,您正在数据库中将数字(2
,10
等)存储为字符串('2'
,'10'
等)
环回依赖于数据库执行排序(排序)
以下是几件可以尝试的事情:
修改模型定义以将特性存储为number
。环回是智能的,在将用户(RESTAPI客户端)提供的字符串值存储到数据库之前,它会将这些值强制为数字。这将是我首选的解决方案,因为它不需要应用程序中的任何复杂代码,并且可以保持性能
根据您使用的数据库,可以将其配置为将字符串值作为数字进行排序。这不是环回专用的,我真的帮不了你
作为最后一种手段,您可以在内存中对记录进行排序,当数据库不支持基于位置的查询时,LoopBack已经在这样做了。其思想是告诉数据库返回符合筛选标准的所有记录,然后在Node.js进程中应用顺序
、限制
、跳过
和其他选项。请注意,这会严重影响性能,并且仅适用于大小合理的数据
至于第三个选项:实现方面,您需要覆盖模型类中的find
方法
// common/models/my-model.js
module.exports = function(MyModel) {
MyModel.on('modelRemoted', () => {
MyModel._findRaw = MyModel.find;
MyModel.find = findWithCustomSort;
});
}
function findWithCustomSort(filter, options, cb) {
if (!cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
} else if (!options && typeof filter === 'function') {
cb = filter;
filter = undefined;
}
}
const dbFilter = {
where: filter.where,
include: filter.include,
fields: filter.fields,
};
if (cb) {
this._findRaw(dbFilter, options, (err, found) => {
if (err) return cb(err);
else cb(null, sortResults(filter, found))
});
} else {
return this._findRaw(dbFilter, options)
.then(found => sortResults(filter, found));
}
}
function sortResults(filter, data) {
// implement your sorting rules, don't forget about "limit", "skip", etc.
}
更新
在自定义方法中是否有使用sql进行查询的方法
是的,您可以使用MyModel.dataSource.connector.execute
函数执行任何SQL,请参阅。不过有一个缺点-此方法基于回调,不能使用Promise API或async/await
const idValue = 1;
MyModel.dataSource.connector.execute(
'SELECT * FROM MyModel WHERE id=?',
[idValue]
(err, results) => {
if (err) console.error('query failed', err);
else console.log('found data', results);
});
衷心感谢您的回答。我正在使用loopback 3.22.0和postgres。我需要正确的floorNumber顺序,有没有办法在自定义方法中使用sql进行查询?或者我应该使用前缀和后缀这两个字段来保存floorNumber,然后我可以使用order filter获得正确的订单?我更新了我的答案,以展示如何执行SQL查询。衷心感谢您的回答。我正在使用loopback 3.22.0和postgres。我需要正确的floorNumber顺序,有没有办法在自定义方法中使用sql进行查询?或者我应该使用两个字段(如前缀和后缀)来保存floorNumber,然后我可以使用order filter获取正确的订单?我更新了答案以显示如何执行SQL查询。