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: add missing createRoles association method for HasMany relationship #17163

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/associations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export type MultiAssociationAccessors = {
addMultiple: string;
add: string;
create: string;
createMultiple: string;
remove: string;
removeMultiple: string;
hasSingle: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/associations/belongs-to-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ export class BelongsToManyAssociation<
addMultiple: `add${plural}`,
add: `add${singular}`,
create: `create${singular}`,
createMultiple: `create${plural}`,
// TODO: add createMultiple association method for BelongsTo relationship
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
// TODO: add createMultiple association method for BelongsTo relationship
// TODO: add createMultiple association method for BelongsToMany relationship

Ideally this should be done in the same PR, as it now declares it as being available

remove: `remove${singular}`,
removeMultiple: `remove${plural}`,
hasSingle: `has${singular}`,
Expand Down
47 changes: 47 additions & 0 deletions packages/core/src/associations/has-many.ts
Copy link
Member

Choose a reason for hiding this comment

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

Don't forget to add the HasManyCreateAssociationsMixin type

Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class HasManyAssociation<
addMultiple: `add${plural}`,
add: `add${singular}`,
create: `create${singular}`,
createMultiple: `create${plural}`,
remove: `remove${singular}`,
removeMultiple: `remove${plural}`,
hasSingle: `has${singular}`,
Expand Down Expand Up @@ -228,12 +229,14 @@ export class HasManyAssociation<
'remove',
'removeMultiple',
'create',
'createMultiple',
],
{
hasSingle: 'has',
hasAll: 'has',
addMultiple: 'add',
removeMultiple: 'remove',
createMultiple: 'createMultiple',
},
);
}
Expand Down Expand Up @@ -605,6 +608,50 @@ export class HasManyAssociation<
options,
);
}

/**
* Create a new instances of the associated model and associate it with this.
*
* @param sourceInstance source instance
* @param values values for target model instance
* @param options Options passed to `target.bulkCreate`
*/
async createMultiple(
sourceInstance: S,
values: AllowIterable<CreationAttributes<T>>,
options:
| HasManyCreateAssociationMixinOptions<T>
| HasManyCreateAssociationMixinOptions<T>['fields'] = {},
): Promise<T[]> {
Copy link
Member

Choose a reason for hiding this comment

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

instead of copying the code, create should call createMultiple

if (Array.isArray(options)) {
options = {
fields: options,
};
}

if (this.scope) {
for (const attribute of Object.keys(this.scope)) {
// @ts-expect-error -- TODO: fix the typing of {@link AssociationScope}
for (const value of values) {
value[attribute] = this.scope[attribute];
}

if (options.fields) {
options.fields.push(attribute);
}
}
}

if (options.fields) {
options.fields.push(this.foreignKey);
}

for (const value of values) {
value[this.foreignKey] = sourceInstance.get(this.sourceKey);
}

return this.target.bulkCreate(values as ReadonlyArray<CreationAttributes<T>>, options);
}
}

// workaround https://github.com/evanw/esbuild/issues/1260
Expand Down