Skip to content

Commit

Permalink
refactor: remove checkExisting param from manyToMany create calls
Browse files Browse the repository at this point in the history
Create methods always insert a fresh row to the database and hence
checkExisting has no meaning
  • Loading branch information
thetutlage committed Apr 25, 2021
1 parent c917fec commit 33283a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
6 changes: 2 additions & 4 deletions adonis-typings/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,16 +711,14 @@ declare module '@ioc:Adonis/Lucid/Relations' {
* Create related model instance. Sets up FK automatically
*/
create(
values: Partial<ModelAttributes<InstanceType<RelatedModel>>>,
checkExisting?: boolean
values: Partial<ModelAttributes<InstanceType<RelatedModel>>>
): Promise<InstanceType<RelatedModel>>

/**
* Create many of related model instances. Sets up FK automatically
*/
createMany(
values: Partial<ModelAttributes<InstanceType<RelatedModel>>>[],
checkExisting?: boolean
values: Partial<ModelAttributes<InstanceType<RelatedModel>>>[]
): Promise<InstanceType<RelatedModel>[]>

/**
Expand Down
46 changes: 30 additions & 16 deletions src/Orm/Relations/ManyToMany/QueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,6 +121,7 @@ export class ManyToManyQueryClient implements ManyToManyClientContract<ManyToMan

/**
* Save related model instance.
* @note: Read the "NO_PIVOT_ATTRS" section at the top
*/
public async save(related: LucidRow, checkExisting: boolean = true) {
await managedTransaction(this.parent.$trx || this.client, async (trx) => {
Expand Down Expand Up @@ -129,6 +152,7 @@ export class ManyToManyQueryClient implements ManyToManyClientContract<ManyToMan

/**
* Save many of related model instances
* @note: Read the "NO_PIVOT_ATTRS" section at the top
*/
public async saveMany(related: LucidRow[], checkExisting: boolean = true) {
await managedTransaction(this.parent.$trx || this.client, async (trx) => {
Expand Down Expand Up @@ -164,8 +188,9 @@ export class ManyToManyQueryClient implements ManyToManyClientContract<ManyToMan
/**
* Create and persist an instance of related model. Also makes the pivot table
* entry to create the relationship
* @note: Read the "NO_PIVOT_ATTRS" section at the top
*/
public async create(values: ModelObject, checkExisting: boolean = true): Promise<LucidRow> {
public async create(values: ModelObject): Promise<LucidRow> {
return managedTransaction(this.parent.$trx || this.client, async (trx) => {
this.parent.$trx = trx
await this.parent.save()
Expand All @@ -179,24 +204,18 @@ export class ManyToManyQueryClient implements ManyToManyClientContract<ManyToMan
* Sync or attach a new one row
*/
const [, relatedForeignKeyValue] = this.relation.getPivotRelatedPair(related)
if (checkExisting) {
await this.sync([relatedForeignKeyValue], false, trx)
} else {
await this.attach([relatedForeignKeyValue], trx)
}

await this.attach([relatedForeignKeyValue], trx)
return related
})
}

/**
* Create and persist multiple of instances of related model. Also makes
* the pivot table entries to create the relationship.
* @note: Read the "NO_PIVOT_ATTRS" section at the top
*/
public async createMany(
values: ModelObject[],
checkExisting: boolean = true
): Promise<LucidRow[]> {
public async createMany(values: ModelObject[]): Promise<LucidRow[]> {
return managedTransaction(this.parent.$trx || this.client, async (trx) => {
this.parent.$trx = trx
await this.parent.save()
Expand All @@ -212,12 +231,7 @@ export class ManyToManyQueryClient implements ManyToManyClientContract<ManyToMan
const relatedForeignKeyValues = related.map(
(one) => 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
})
}
Expand Down

0 comments on commit 33283a4

Please sign in to comment.