-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 all 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import NodeUtil from 'node:util'; | |
import type { IndexOptions } from './dialects/abstract/query-interface.js'; | ||
import type { WhereAttributeHash } from './dialects/abstract/where-sql-builder-types.js'; | ||
import { EagerLoadingError } from './errors'; | ||
import type { Attributes, Model, Transactionable } from './model'; | ||
import type { Attributes, Model, ModelStatic, Transactionable } from './model'; | ||
import type { ModelDefinition } from './model-definition.js'; | ||
import type { Sequelize } from './sequelize'; | ||
import { isModelStatic } from './utils/model-utils.js'; | ||
|
@@ -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) { | ||
const associations = []; | ||
const associationNames = Object.keys(model.associations); | ||
if (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. based on the typing, this condition is always true |
||
for (const value of Object.keys(values)) { | ||
if (associationNames.includes(value)) { | ||
associations.push(value); | ||
} | ||
} | ||
} | ||
|
||
return associations; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { BaseSqlExpression } from './expression-builders/base-sql-expression.js' | |
import { | ||
_validateIncludedElements, | ||
combineIncludes, | ||
getDefaultCreateInclude, | ||
getModelPkWhere, | ||
setTransactionFromCls, | ||
throwInvalidInclude, | ||
|
@@ -1916,6 +1917,7 @@ ${associationOwner._getAssociationDebugList()}`); | |
*/ | ||
static async create(values, options) { | ||
options = cloneDeep(options) ?? {}; | ||
options.include ??= getDefaultCreateInclude(this, 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. I think belongs inside of |
||
|
||
return await this.build(values, { | ||
isNewRecord: true, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1593,6 +1593,37 @@ 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.