I am new to Node.js/Sequelize.js. I have following piece of code for query:
我是Node.js / Sequelize.js的新手。我有以下代码用于查询:
var agent_list = models.agent.findAll({
subQuery: false,
where: qry_filter,
attributes: select_attributes,
include:include_models,
group: ['agent_id'],
order: agent_data.sort || appConfig.DEFAULT_AGENT_SORT,
limit: agent_data.num_results || appConfig.DEFAULT_RESPONSE_SIZE
})
.then(function(agent_list){
console.log(agent_list);
});
The statement "console.log(agent_list)" prints the data retrieved from db plus the meta information like options:{...} , modelOptions: {...} etc. dataValues object contains data that i want. The resultset is nested js objects, each has the same structure so it would be very difficult to loop through the resultset and get only the dataValues.
语句“console.log(agent_list)”打印从db检索的数据加上元信息,如选项:{...},modelOptions:{...}等.dataValues对象包含我想要的数据。结果集是嵌套的js对象,每个对象具有相同的结构,因此很难遍历结果集并只获取dataValues。
I have experience working with PHP where something like this $db -> Execute("$qry") would return resultset with meta and to get rows $db -> Execute("$qry")->getRows() can be used. How to achieve this in sequelize?
我有使用PHP的经验,其中类似这样的$ db - > Execute(“$ qry”)将返回带有meta的结果集并获取行$ db - >执行(“$ qry”) - > getRows()可以使用。如何在续集中实现这一目标?
2 个解决方案
#1
0
There is a npm package called sequelize-values which you can use.
你可以使用一个名为sequelize-values的npm包。
So in your case, your code would be
所以在你的情况下,你的代码就是
models.agent.findAll({
subQuery: false,
where: qry_filter,
attributes: select_attributes,
include: include_models,
group: ['agent_id'],
order: agent_data.sort || appConfig.DEFAULT_AGENT_SORT,
limit: agent_data.num_results || appConfig.DEFAULT_RESPONSE_SIZE
}).then(function(agent_list) {
return agent_list.map(function(agent) {
return agent.getValues();
});
}).then(function(agent_list) {
console.log(agent_list);
});
#2
0
Sequelize does not use concepts like resultsets or rows. It's an ORM, so rows (including nested associations) are treated as objects with nested objects as appropriate. It also applies an "Active Record" pattern, so each returned object has additional method added to it, like "save", "update", "delete" and more.
Sequelize不使用结果集或行等概念。它是一个ORM,因此行(包括嵌套关联)被视为具有嵌套对象的对象。它还应用“活动记录”模式,因此每个返回的对象都添加了其他方法,如“保存”,“更新”,“删除”等。
When Sequelize instances are serialized to JSON, they strip all the Sequelize "metadata" properties and just return simple objects, as you would expect.
当Sequelize实例被序列化为JSON时,它们会删除所有Sequelize“元数据”属性,只返回简单对象,如您所料。
Also, Sequelize instances make use of property getters and setters to transparently behave like simple JS objects. This means you can do something like agent_list[0].myProperty = 1; console.log(agent_list[0].myProperty);
and it will behave like you expect. The reason it does this is so it can keep track of updated values, so later "update" calls will only update the columns that have changed.
此外,Sequelize实例使用属性getter和setter来透明地表现得像简单的JS对象。这意味着你可以做一些像agent_list [0] .myProperty = 1;的console.log(agent_list [0] .myProperty);它会表现得像你期望的那样。它这样做的原因是它可以跟踪更新的值,因此以后“更新”调用只会更新已更改的列。
You should have no need to manually get the "rows" from the query result.
您不需要手动从查询结果中获取“行”。
#1
0
There is a npm package called sequelize-values which you can use.
你可以使用一个名为sequelize-values的npm包。
So in your case, your code would be
所以在你的情况下,你的代码就是
models.agent.findAll({
subQuery: false,
where: qry_filter,
attributes: select_attributes,
include: include_models,
group: ['agent_id'],
order: agent_data.sort || appConfig.DEFAULT_AGENT_SORT,
limit: agent_data.num_results || appConfig.DEFAULT_RESPONSE_SIZE
}).then(function(agent_list) {
return agent_list.map(function(agent) {
return agent.getValues();
});
}).then(function(agent_list) {
console.log(agent_list);
});
#2
0
Sequelize does not use concepts like resultsets or rows. It's an ORM, so rows (including nested associations) are treated as objects with nested objects as appropriate. It also applies an "Active Record" pattern, so each returned object has additional method added to it, like "save", "update", "delete" and more.
Sequelize不使用结果集或行等概念。它是一个ORM,因此行(包括嵌套关联)被视为具有嵌套对象的对象。它还应用“活动记录”模式,因此每个返回的对象都添加了其他方法,如“保存”,“更新”,“删除”等。
When Sequelize instances are serialized to JSON, they strip all the Sequelize "metadata" properties and just return simple objects, as you would expect.
当Sequelize实例被序列化为JSON时,它们会删除所有Sequelize“元数据”属性,只返回简单对象,如您所料。
Also, Sequelize instances make use of property getters and setters to transparently behave like simple JS objects. This means you can do something like agent_list[0].myProperty = 1; console.log(agent_list[0].myProperty);
and it will behave like you expect. The reason it does this is so it can keep track of updated values, so later "update" calls will only update the columns that have changed.
此外,Sequelize实例使用属性getter和setter来透明地表现得像简单的JS对象。这意味着你可以做一些像agent_list [0] .myProperty = 1;的console.log(agent_list [0] .myProperty);它会表现得像你期望的那样。它这样做的原因是它可以跟踪更新的值,因此以后“更新”调用只会更新已更改的列。
You should have no need to manually get the "rows" from the query result.
您不需要手动从查询结果中获取“行”。