From 33283a4aa01824352f95aec9de922b595a30dfeb Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 25 Apr 2021 23:21:04 +0530 Subject: [PATCH] refactor: remove checkExisting param from manyToMany create calls Create methods always insert a fresh row to the database and hence checkExisting has no meaning --- adonis-typings/relations.ts | 6 +-- src/Orm/Relations/ManyToMany/QueryClient.ts | 46 ++++++++++++++------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/adonis-typings/relations.ts b/adonis-typings/relations.ts index d17b243e..2d557d51 100644 --- a/adonis-typings/relations.ts +++ b/adonis-typings/relations.ts @@ -711,16 +711,14 @@ declare module '@ioc:Adonis/Lucid/Relations' { * Create related model instance. Sets up FK automatically */ create( - values: Partial>>, - checkExisting?: boolean + values: Partial>> ): Promise> /** * Create many of related model instances. Sets up FK automatically */ createMany( - values: Partial>>[], - checkExisting?: boolean + values: Partial>>[] ): Promise[]> /** diff --git a/src/Orm/Relations/ManyToMany/QueryClient.ts b/src/Orm/Relations/ManyToMany/QueryClient.ts index c66e2614..fa5debf2 100644 --- a/src/Orm/Relations/ManyToMany/QueryClient.ts +++ b/src/Orm/Relations/ManyToMany/QueryClient.ts @@ -17,6 +17,28 @@ import { ManyToManyQueryBuilder } from './QueryBuilder' import { ManyToManySubQueryBuilder } from './SubQueryBuilder' import { managedTransaction, syncDiff } from '../../../utils' +/** + * ------------------------------------------------------------ + * NO_PIVOT_ATTRS + * ------------------------------------------------------------ + * + * We do not define pivot attributes during a save/create calls. Coz, one can + * attach the related instance with multiple parent instance. + * + * For example: + * + * user.related('skills').save(skill) + * user1.related('skills').save(skill) + * + * As per the above example, the `skill.$extras.pivot_user_id` will have + * which user id? + * + * Same is true with a create call + * + * const skill = user.related('skills').create({ name: 'Programming' }) + * user1.related('skills').save(skill) + */ + /** * Query client for executing queries in scope to the defined * relationship @@ -99,6 +121,7 @@ export class ManyToManyQueryClient implements ManyToManyClientContract { @@ -129,6 +152,7 @@ export class ManyToManyQueryClient implements ManyToManyClientContract { @@ -164,8 +188,9 @@ export class ManyToManyQueryClient implements ManyToManyClientContract { + public async create(values: ModelObject): Promise { return managedTransaction(this.parent.$trx || this.client, async (trx) => { this.parent.$trx = trx await this.parent.save() @@ -179,12 +204,8 @@ export class ManyToManyQueryClient implements ManyToManyClientContract { + public async createMany(values: ModelObject[]): Promise { return managedTransaction(this.parent.$trx || this.client, async (trx) => { this.parent.$trx = trx await this.parent.save() @@ -212,12 +231,7 @@ export class ManyToManyQueryClient implements ManyToManyClientContract this.relation.getPivotRelatedPair(one)[1] ) - if (checkExisting) { - await this.sync(relatedForeignKeyValues, false, trx) - } else { - await this.attach(relatedForeignKeyValues, trx) - } - + await this.attach(relatedForeignKeyValues, trx) return related }) }