15

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

Mon
  • 1,010
  • 1
  • 11
  • 19
  • Use `model.toObject` function: [http://mongoosejs.com/docs/api.html#document_Document-toObject](http://mongoosejs.com/docs/api.html#document_Document-toObject) – alexmac Aug 08 '17 at 09:10
  • thank you alexmac. but that's for mongoose, but I found a similar method for sequelize `model.get`, which I posted as answer. thnx – Mon Aug 09 '17 at 02:43

6 Answers6

33

Thank you very much for your help. I found a solution, though this isn't exactly what I'm looking for, but it works, and also still good.

The sequelize entity has a .get() method to return the plain object version. So it goes something like this:

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

Coming from this thread: Sequelize, convert entity to plain object. Look for CharlesA's answer.

Haven't tried with arrays but check its comments and the answer next to it if you're having problems with array of results. But since .create() only returns an object, it works for me. Anyway if you are using .findAll(), you should use {raw: true} option instead of this solution because it works in that method.

P.S. If anyone still has a solution where Sequelize itself will not return that large resultEntity object, but just the plain data object, just like {raw: true} option (because I think that's still lighter?), we're open.

Thank you very much.

Mon
  • 1,010
  • 1
  • 11
  • 19
6
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 :)

  • 1
    Hi :) You don't need to repeat other answers. Your alternative option is good enough to be written as an answer on its own. – SomoKRoceS Jul 10 '20 at 20:50
2

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

Kevin N.
  • 163
  • 5
0

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!

1020rpz
  • 914
  • 10
  • 21
0

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();
Hardik Shah
  • 111
  • 7
0
.then(response => (response.get({plain: true}))
Uğurcan
  • 91
  • 6
  • 2
    Please don't post code-only answers. Future readers will be grateful to see explained *why* this answers the question instead of having to infer it from the code. Also, since this is an old question, please explain how it complements the other answers. TBH this only looks like a repetition of some of them. – Gert Arnold Jun 19 '22 at 17:59