Setting all queries to raw = true sequelize

泄露秘密 提交于 2019-12-30 03:19:22

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/26228499/setting-all-queries-to-raw-true-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!