Set raw = true on Sequelize Model.create

Viewed 18588

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

I've already seen this answer: Setting all queries to raw = true sequelize, and no, Model.create({name: 'test'}, {raw: true}) doesn't work.

Thanks

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

Like mentioned before or if you want to keep with async/await syntax go for:

const myResultVar = (await Model.create(modelObject)).get({plain:true})

Basically the same thing just not leaving the async/await syntax behind :)

I'm soooo late, but maybe this will help someone else searching for this like I was.

In your query options instead of

{raw: true}

try

{plain: true}

works for me

I think this is also an option you can pass to your sequelize connection to the DB in order to have all you return in plain/raw:

something like that:

.... new Sequelize('dbUrl',{query:{raw:true, plain:true}, logging: false});

Cheers!

If nothing else works, explicitly casting the returned instance to json should definitely work. Sequelize provides a function on its instances which does just that. Follow the below example:

let newRecord = await models.movies.create({...data for new row});
newRecord = newRecord.toJSON();
.then(response => (response.get({plain: true}))
Related