-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
275c554
612e1a6
cf9c7fa
83a3310
fe99ba6
8663f07
1b56469
34727c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
} | ||||||
|
||||||
return await this.build(values, { | ||||||
isNewRecord: true, | ||||||
|
@@ -1759,6 +1763,18 @@ ${associationOwner._getAssociationDebugList()}`); | |||||
}).save(options); | ||||||
} | ||||||
|
||||||
static _searchInclude(values) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New functions like this one should go in I think |
||||||
const association = []; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const associationKeys = Object.keys(this.associations); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for (const value in values) { | ||||||
if (associationKeys.includes(value)) { | ||||||
association.push({ association: value }); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I think you can just push |
||||||
} | ||||||
} | ||||||
|
||||||
return association; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Find an entity that matches the query, or build (but don't save) the entity if none is found. | ||||||
* The successful result of the promise will be the tuple [instance, initialized]. | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1508,6 +1508,36 @@ describe(Support.getTestDialectTeaser('Model'), () => { | |||||
}); | ||||||
}); | ||||||
}); | ||||||
it('should create associated objects without include option', async function () { | ||||||
const Player = this.customSequelize.define('player', { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
name: { | ||||||
type: DataTypes.STRING, | ||||||
}, | ||||||
teamId: { | ||||||
type: DataTypes.INTEGER, | ||||||
}, | ||||||
}); | ||||||
const Team = this.customSequelize.define('team', { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
id: { | ||||||
type: DataTypes.INTEGER, | ||||||
primaryKey: true, | ||||||
}, | ||||||
name: { | ||||||
type: DataTypes.STRING, | ||||||
}, | ||||||
}); | ||||||
Player.team = Player.belongsTo(Team, { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
as: 'team', | ||||||
foreignKey: 'teamId', | ||||||
targetKey: 'id', | ||||||
}); | ||||||
await this.customSequelize.sync({ force: true }); | ||||||
const createdPlayer = await Player.create({ | ||||||
name: 'Player 1', team: { id: 1, name: 'My new team' }, | ||||||
}); | ||||||
expect(createdPlayer.team instanceof Team).to.be.ok; | ||||||
expect(createdPlayer.teamId).to.equal(1); | ||||||
}); | ||||||
}); | ||||||
|
||||||
it('should return autoIncrement primary key (create)', async function () { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only call
_searchInclude
ifoptions.include
is not set, to avoid computing it if not necessaryThe
association.length > 0
check should not be necessary and can be removedassociation
should be plural, but entire code can be simplified to this: