Sequelize Eager Loading - 返回更平坦的数据对象

时间:2014-11-21 20:21:54

标签: node.js sequelize.js

在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 } }

在没有预先加载的正常sequelize下,我可以记录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的子对象1}}。 sequalize是否提供了一种可以运行的方法,即使是在预先加载的情况下,也可以让你像我描述的那样组织一个扁平的对象?

2 个答案:

答案 0 :(得分:0)

在v3.9中,您可以使用' get'将ORM对象转换为普通对象的方法:

user.get({plain: true})

答案 1 :(得分:0)

您需要的是raw: true进行后续查询

发现于:
https://stackoverflow.com/a/30796714/8839237

文档:
http://docs.sequelizejs.com/manual/raw-queries.html