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?
Setting all queries to raw = true sequelize
Asked Answered
your title says : set raw = true, and your question asks how to set raw= false... –
Freberg
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}})
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 work –
Freberg 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/false –
Halfcocked
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
© 2022 - 2024 — McMap. All rights reserved.