filter Loopback API 只获取最新版本的行
filter Loopback API to get only rows with latest version
我是环回的新手,所以这是我的问题:
我有一个名为 "config-tables" 的模型,具有以下测试数据:
[
{
ID: 0,
CONFIG_NAME: "MainColor",
DATA_TYPE: "color",
CONFIG_VALUE: "#FF0000",
VERSION: 1
},
{
ID: 1,
CONFIG_NAME: "MainColor",
DATA_TYPE: "color",
CONFIG_VALUE: "#FF00FF",
VERSION: 2
}
]
我想创建两个自定义方法并将它们公开给 API:
第一,获取最新版本的方法。
第二种,获取最新版本所有数据的方法,本例version=2
我已经阅读了很多关于环回过滤器的内容,但不知道如何在 SQL 中使用类似于 MAX
或 TOP
的内容。
也许有更好的办法 - 但我只是在 /common/models/config-tables.js 中创建两个远程方法,以便获取 [=14] 的最后一个元素=]config-table 实体按 VERSION 属性排序:
'use strict';
module.exports = function (configTable) {
//first
configTable.getLastVersion = function (cb) {
// find only one configTable ordered by version
configTable.findOne({order: 'VERSION DESC'}, function (err, config) {
if(config && config.VERSION) {
cb(err, config.VERSION);
} else {
// or throw a new error, depending on what you want
cb(err, 0);
}
});
};
configTable.remoteMethod('getLastVersion', {
returns: {arg: 'VERSION', type: 'number'},
http: {verb: 'get'}
});
//second (depending on the first method)
configTable.getLatests = function (cb) {
configTable.getLastVersion(function (err, last_version) {
if(err || !last_version || last_version === 0) {
// maybe throw error here
return cb(new Error("No config found actually..."));
} else {
// find all configTables where VERSION = last version
configTable.find({where: {VERSION: last_version}}, function (err, configs) {
cb(err, configs);
});
}
});
};
configTable.remoteMethod('getLatests', {
returns: {arg: 'configTables', type: 'object'},
http: {verb: 'get'}
});
};
我是环回的新手,所以这是我的问题:
我有一个名为 "config-tables" 的模型,具有以下测试数据:
[
{
ID: 0,
CONFIG_NAME: "MainColor",
DATA_TYPE: "color",
CONFIG_VALUE: "#FF0000",
VERSION: 1
},
{
ID: 1,
CONFIG_NAME: "MainColor",
DATA_TYPE: "color",
CONFIG_VALUE: "#FF00FF",
VERSION: 2
}
]
我想创建两个自定义方法并将它们公开给 API:
第一,获取最新版本的方法。
第二种,获取最新版本所有数据的方法,本例version=2
我已经阅读了很多关于环回过滤器的内容,但不知道如何在 SQL 中使用类似于 MAX
或 TOP
的内容。
也许有更好的办法 - 但我只是在 /common/models/config-tables.js 中创建两个远程方法,以便获取 [=14] 的最后一个元素=]config-table 实体按 VERSION 属性排序:
'use strict';
module.exports = function (configTable) {
//first
configTable.getLastVersion = function (cb) {
// find only one configTable ordered by version
configTable.findOne({order: 'VERSION DESC'}, function (err, config) {
if(config && config.VERSION) {
cb(err, config.VERSION);
} else {
// or throw a new error, depending on what you want
cb(err, 0);
}
});
};
configTable.remoteMethod('getLastVersion', {
returns: {arg: 'VERSION', type: 'number'},
http: {verb: 'get'}
});
//second (depending on the first method)
configTable.getLatests = function (cb) {
configTable.getLastVersion(function (err, last_version) {
if(err || !last_version || last_version === 0) {
// maybe throw error here
return cb(new Error("No config found actually..."));
} else {
// find all configTables where VERSION = last version
configTable.find({where: {VERSION: last_version}}, function (err, configs) {
cb(err, configs);
});
}
});
};
configTable.remoteMethod('getLatests', {
returns: {arg: 'configTables', type: 'object'},
http: {verb: 'get'}
});
};