Setting all queries to raw = true sequelize
Asked Answered
M

2

16

I really like using sequelize as my ORM for my node application, but right now, I am kind of irritated when they are passing DAO objects by default when you query. How can I set the raw option to true all the time?

Muse answered 7/10, 2014 at 4:21 Comment(1)
your title says : set raw = true, and your question asks how to set raw= false...Freberg
F
26

According to the doc :

If you do not provide other arguments than the SQL, raw will be assumed to the true, and sequelize will not try to do any formatting to the results of the query.

That being said :

The Sequelize object has a [options.query={}] optional parameter to set default options for sequelize.query. Source

You should be able to use :

var sequelize = new Sequelize('database', 'username', 'password', {query:{raw:true}})
Freberg answered 7/10, 2014 at 4:27 Comment(6)
Wow, that was very helpful, I have one problem though, when I use create, there are still DAO objects, the query raw: true helped when I used find but it won't affect when I use create. Sample code: Model.create({name: 'test'}, {options: raw: true})Muse
try : Modelgroup.create(test, {raw: true})Freberg
github.com/sequelize/sequelize/wiki/…Freberg
You edited your comment... Model.create({name: 'test'}, {raw: true}) should workFreberg
well, the question : "Setting all queries to raw = true" is answered, maybe ask a specific question for the Model.create(). are you sure Model.create({name: 'test'}, {raw: true}) doesn't work?Freberg
Just be careful to note that this bypasses sequelize modal transformations. E.g TINYINT will be returned as 0/1 with raw as opposed to true/falseHalfcocked
A
3

For create you can use this:

Model.create(modelObject)
.then((resultEntity) => {
    const dataObj = resultEntity.get({plain:true})
}

Check this out: Set raw = true on Sequelize Model.create

Advertisement answered 9/8, 2017 at 3:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.