Sequelize Eager Loading - 返回更扁平的数据对象
Posted
技术标签:
【中文标题】Sequelize Eager Loading - 返回更扁平的数据对象【英文标题】:Sequelize Eager Loading - Return flatter data object 【发布时间】:2015-01-20 02:06:53 【问题描述】:鉴于 Sequelize 1.7.9 中的以下(工作)代码:
database.User.find(
where: signupKey: signupKey ,
include: [ model: database.Company, as: "Company" ]
).then(function( user )
我的user
对象吐出类似这样的内容:
dataValues:
id: 1,
email: 'email',
password: '',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: '24Pm9MZ22',
status: 'pending',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null,
company:
dataValues: [Object],
_previousDataValues: [Object],
__options: [Object],
options: [Object],
hasPrimaryKeys: true,
selectedValues: [Object],
__eagerlyLoadedAssociations: [],
isNewRecord: false ,
_previousDataValues:
id: 1,
email: 'email',
password: '',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: '24Pm9MZ22',
status: 'pending',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null,
company:
dataValues: [Object],
_previousDataValues: [Object],
__options: [Object],
options: [Object],
hasPrimaryKeys: true,
selectedValues: [Object],
__eagerlyLoadedAssociations: [],
isNewRecord: false ,
__options:
timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: ,
classMethods: associate: [Function] ,
validate: ,
freezeTableName: false,
freezeAssociations: false,
underscored: false,
syncOnAssociation: true,
paranoid: false,
whereCollection: signupKey: '24Pm9MZ22' ,
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
hooks: beforeCreate: [], afterCreate: [] ,
omitNull: false,
uniqueKeys: ,
hasPrimaryKeys: true ,
options:
isNewRecord: false,
isDirty: false,
include: [ [Object] ],
includeNames: [ 'Company', 'company' ],
includeMap: Company: [Object] ,
includeValidated: true,
raw: true ,
hasPrimaryKeys: true,
selectedValues:
id: 1,
email: 'email',
password: '',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: '24Pm9MZ22',
status: 'pending',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null ,
__eagerlyLoadedAssociations: [],
isNewRecord: false,
company:
dataValues:
id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST) ,
_previousDataValues:
id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST) ,
__options:
timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: ,
classMethods: [Object],
validate: ,
freezeTableName: false,
freezeAssociations: false,
underscored: false,
syncOnAssociation: true,
paranoid: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
hooks: [Object],
omitNull: false,
uniqueKeys: ,
hasPrimaryKeys: true ,
options:
isNewRecord: false,
isDirty: false,
include: undefined,
includeNames: undefined,
includeMap: undefined,
includeValidated: true,
raw: true ,
hasPrimaryKeys: true,
selectedValues:
id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST) ,
__eagerlyLoadedAssociations: [],
isNewRecord: false
在没有预加载的正常续集下,我可以记录 user.values
并获得更正常的 JS 对象,除了预加载我在记录 user.values
时得到这个:
id: 1,
email: 'email',
password: 'password',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: 'bN2dGdNbD',
status: 'pending',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null,
company:
dataValues:
id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST) ,
_previousDataValues:
id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST) ,
__options:
timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: ,
classMethods: [Object],
validate: ,
freezeTableName: false,
freezeAssociations: false,
underscored: false,
syncOnAssociation: true,
paranoid: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
hooks: [Object],
omitNull: false,
uniqueKeys: ,
hasPrimaryKeys: true ,
options:
isNewRecord: false,
isDirty: false,
include: undefined,
includeNames: undefined,
includeMap: undefined,
includeValidated: true,
raw: true ,
hasPrimaryKeys: true,
selectedValues:
id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST) ,
__eagerlyLoadedAssociations: [],
isNewRecord: false
我希望能够运行user.values
并获得一个平面user
对象,就像返回的一样,并且让user.company
也成为一个平面对象,作为user
的子对象。 sequalize 是否提供了一种您可以运行的方法,即使是在预先加载的情况下,也可以让您像我描述的那样组织一个平面对象?
【问题讨论】:
在 2.0toJSON
默认返回一个普通对象,但恐怕在 1.7 中没有办法得到它
@JanAagaardMeier 太棒了!我知道很多好事即将到来,很高兴听到 toJSON 就是其中之一
【参考方案1】:
在 v3.9 中,您可以使用 'get' 方法将 ORM 对象转换为普通对象:
user.get(plain: true)
【讨论】:
【参考方案2】:你需要的是raw: true
在sequelize查询
通过以下方式找到:https://***.com/a/30796714/8839237
文档:http://docs.sequelizejs.com/manual/raw-queries.html
【讨论】:
以上是关于Sequelize Eager Loading - 返回更扁平的数据对象的主要内容,如果未能解决你的问题,请参考以下文章
Laravel - Eager Loading BelongsToMany 关系
Laravel Eager Loading - 仅加载特定列