Set raw = true in Sequelize Model.create

I want to get a simple raw object after calling Model.create in Sequelize, the object itself that was created, no metadata or any other things. Just like the {raw: true} option in Model.find .

I already saw this answer: Setting all queries to raw = true sequelize , and no, Model.create({name: 'test'}, {raw: true}) does not work.

thanks

0
source share
2 answers

Many thanks for your help. I found a solution, although this is not quite what I am looking for, but it works, and is also still good.

The sequelize object has a .get() method to return the version of a simple object. So it looks something like this:

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

From this topic: Sequelize, convert an object to a simple object . Look at CharlesA's answer.

Did not try to use arrays, but check its comments and the answer next to it if you have problems with the array of results. But since .create() only returns an object, it works for me. In any case, if you use .findAll() , you should use the {raw: true} option instead, because it works in this method.

PS If someone else has a solution in which Sequelize itself will not return this large resultEntity object, but simply a simple data object, like the {raw: true} option (because I think it's still easier?), We are open.

Many thanks.

+2
source

You can also use .toJSON () in the model instance that was returned from the request. http://docs.sequelizejs.com/class/lib/model.js~Model.html#instance-method-toJSON

0
source

Source: https://habr.com/ru/post/1204151/