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

feat: make the include option of Model.create default to all provided associations #16922

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

sakupan102
Copy link

@sakupan102 sakupan102 commented Jan 1, 2024

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description Of Change

Its a feature request opened by #15233

Todos

  • Add tests for Models.create

Copy link
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

@@ -1759,6 +1763,18 @@ ${associationOwner._getAssociationDebugList()}`);
}).save(options);
}

static _searchInclude(values) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New functions like this one should go in model-internals.ts instead

I think getDefaultCreateInclude may be a more explicit name

@@ -1759,6 +1763,18 @@ ${associationOwner._getAssociationDebugList()}`);
}).save(options);
}

static _searchInclude(values) {
const association = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const association = [];
const associations = [];

@@ -1749,6 +1749,10 @@ ${associationOwner._getAssociationDebugList()}`);
*/
static async create(values, options) {
options = cloneDeep(options) ?? {};
const association = this._searchInclude(values);
if (association.length > 0 && !options.include) {
options.include = association;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only call _searchInclude if options.include is not set, to avoid computing it if not necessary

The association.length > 0 check should not be necessary and can be removed

association should be plural, but entire code can be simplified to this:

options.include ??= getDefaultCreateInclude(this, values);

@@ -1759,6 +1763,18 @@ ${associationOwner._getAssociationDebugList()}`);
}).save(options);
}

static _searchInclude(values) {
const association = [];
const associationKeys = Object.keys(this.associations);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const associationKeys = Object.keys(this.associations);
const associationNames = Object.keys(this.associations);

const associationKeys = Object.keys(this.associations);
for (const value in values) {
if (associationKeys.includes(value)) {
association.push({ association: value });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think you can just push value and that the { association } wrapper is redundant

@sakupan102 sakupan102 requested a review from a team as a code owner February 22, 2024 11:26
@ephys ephys self-requested a review February 22, 2024 15:48
@sakupan102
Copy link
Author

@ephys I made several corrections. This is ready to review.

@sakupan102 sakupan102 requested a review from a team as a code owner March 13, 2024 11:27
@sakupan102 sakupan102 requested a review from WikiRik March 13, 2024 11:27
@WikiRik
Copy link
Member

WikiRik commented Mar 13, 2024

Can you fix the prettier issue? We like to review PRs once CI has passed.

@sakupan102
Copy link
Author

@WikiRik @ephys fixed prettier issue. Could you please review this?

@ephys ephys changed the title feat: Remove the include option from Model.create feat: make the include option of Model.create default to all provided associations Mar 16, 2024
Copy link
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work on this

@@ -1916,6 +1917,7 @@ ${associationOwner._getAssociationDebugList()}`);
*/
static async create(values, options) {
options = cloneDeep(options) ?? {};
options.include ??= getDefaultCreateInclude(this, values);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think belongs inside of save when isNewRecord is true, because calling build then save should work too

export function getDefaultCreateInclude(model: ModelStatic, values: Object) {
const associations = [];
const associationNames = Object.keys(model.associations);
if (values) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the typing, this condition is always true

@@ -273,3 +273,17 @@ Either add a primary key to this model, or use one of the following alternatives
);
}
}

export function getDefaultCreateInclude(model: ModelStatic, values: Object) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function getDefaultCreateInclude(model: ModelStatic, values: Object) {
export function getDefaultCreateInclude(model: ModelStatic, values: UnknownRecord) {

@@ -1593,6 +1593,37 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
it('should create associated objects without include option', async function () {
const Player = this.customSequelize.define('player', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const Player = this.customSequelize.define('player', {
const Player = this.customSequelize.define('Player', {

type: DataTypes.INTEGER,
},
});
const Team = this.customSequelize.define('team', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const Team = this.customSequelize.define('team', {
const Team = this.customSequelize.define('Team', {

type: DataTypes.STRING,
},
});
Player.team = Player.belongsTo(Team, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Player.team = Player.belongsTo(Team, {
Player.belongsTo(Team, {

@sequelize-bot sequelize-bot bot added the conflicted This PR has merge conflicts and will not be present in the list of PRs to review label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicted This PR has merge conflicts and will not be present in the list of PRs to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants