2

Given the following (working) code in Sequelize 1.7.9:

database.User.find({ 
    where: { signupKey: signupKey }, 
    include: [{ model: database.Company, as: "Company" }] 
}).then(function( user ) {

My user object spits out something like this:

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

Under normal sequelize without eager loading, I can log user.values and get a more normal JS object, except with eager loading I get this when I log 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 } }

I want to be able to run user.values and get a flat user object, like is returned, and have user.company be a flat object as well, as a child of user. Does sequalize provide a method that you can run, even with eager loading, that allows you to get a flat object organized like I described?

Kyle Hotchkiss
  • 10,754
  • 20
  • 56
  • 82

2 Answers2

1

In v3.9 you can use 'get' method to convert ORM object to plain object:

user.get({plain: true})
Robo Burned
  • 542
  • 4
  • 13
0

What you need is raw: true in sequelize query

Found via:
https://stackoverflow.com/a/30796714/8839237

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

Peter
  • 323
  • 4
  • 15