Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch associations on create or save with Sequelize

Fairly simple problem, just cant find the good/clean way to do this without making a call to another find

I've got my node app rigged up with Angular-Resource, and I'm just making some round-trip like data calls on new or changed data.

So ngResource making the $save() call to my /api/users/:id and such. And Node reacts to this call by creating or finding the user, making the updates, and saving them.

Whether through create() or save(), it returns the created record, and for right now, I use res.json(user) to spill the created/returned record for my Angular to handle populating my view with the updated information

Now, I know with Sequelizes find() and findAll() methods, I can use findAll({ include: [{ all: true }]}) or specify my models individually.

What I want to know is, what is the best way to get my records associations on save/create

and unfortunately, this just doesn't work:

models.User.create(newuser, {include:[{ all: true }]}).then(function(user) {
        res.json(user);
    });

Do I really have to perform another find() just to get my managed models associations?

like image 774
RedactedProfile Avatar asked Nov 09 '22 17:11

RedactedProfile


1 Answers

To better illustrate the opted solution from RedactedProfile's comment, here's the code.

models.User
  .create(newuser, {include:[{ all: true }]})
  .then(user => {
      user.reload().then(user => { res.json(user); })
  });
like image 114
ernestchu Avatar answered Nov 14 '22 22:11

ernestchu