Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it is possible to sequelize save items only after all models update? #12187

Closed
ssdk86 opened this issue Apr 28, 2020 · 1 comment
Closed

it is possible to sequelize save items only after all models update? #12187

ssdk86 opened this issue Apr 28, 2020 · 1 comment

Comments

@ssdk86
Copy link

ssdk86 commented Apr 28, 2020

In my models I have One-To-Many relationships, and want to update and save all in one try..catch function to define if error there.

Team.hasMany(Player);
Player.belongsTo(Team);

const MyTeam = await Team.findOne({
  where: {
    name: "chikago"
  },
  include: Playes
});

result in json like this

MyTeam": {
    "id" : 5,
    "name" : "chikago",
    "title"  : "lets'go Chikago!!!"

    "Players": [
        {
            "id": 1,
            "name": "bob"
            "years": 18
        },
        {
            "id": 2,
            "name": "tom"
            "years": 20
        },
    ]
};

so in MyTeam I have result with team and players related to it.
now i want to edit some values in MyTeam entity and in players, and create new player, then save all this with try...catch handling error;

for exapmle:

MyTeam.title = "here are we go";
try {
    await MyTeam.save();
} catch (error) {
    console.log(error);
}
let newPlayer = new Player({...});
try {
    await newPlayer.save();
} catch (error) {
    console.log(error);
};

// here many other code ...


try {
    MyTeam.Players.map(async player => {
        player.years = player.years + 1;
        await player.save();
    });
} catch (error) {
    console.log(error);
}

but there is a way if i need to save all my editing some like

let entityManager = new Sequeilize('entityManager');
MyTeam.title = "here are we go";
entityManager.add(MyTeam);

let newPlayer = new Player({...});
entityManager.add(newPlayer);

MyTeam.Players.map(player => {
    player.years = player.years + 1;
    entityManager.add(player);
});

try {
    await entityManager.save();
} catch (error) {
    console.log(error);
};

I an trying to do something like this, as described in #5471 (comment)
but model saving and updated as they push to array

let savingEntities = [];
MyTeam.title = "here are we go";
savingEntities.push(MyTeam);

let newPlayer = new Player({...});
savingEntities.push(newPlayer);

MyTeam.Players.map(player => {
    player.years = player.years + 1;
    savingEntities.push(player);
});

try {
    await Promise.all(savingEntities);
} catch (error) {
    console.log(error);
};
@sushantdhiman
Copy link
Contributor

Please use Github Issue Tracker only for reporting bugs, requesting new features or discussions. Ask questions on Stackoverflow sequelize.js tag or Slack. While I would like to help answer any questions but it still take too much time.

If you are reporting a bug please isolate it as SSCCE, present it nicely and follow issue template. This will help maintainers and contributors understand your issue quickly.

If you are requesting a feature spend some time to properly present your use case with some examples, current and expected outcome.

( What is SSCCE ? You can find template for Sequelize here )

A few good example of SSCCE

  1. ResourceRequest timed out - connection not reestablished after db restarted #8014 (comment)
  2. removeAttribute('id') results in undefined: null data value #7318 (comment)
  3. toJSON converting booleans to "t" (true) or "f" (false) #8749 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants