Skip to content

Commit

Permalink
refactor: do not set $extras when creating many to many via factory
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The $extras will not have pivot and related foriegn keys
  • Loading branch information
thetutlage committed Apr 26, 2021
1 parent 36b7e4a commit 7a98761
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 66 deletions.
34 changes: 2 additions & 32 deletions src/Factory/Relations/ManyToMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { ManyToManyRelationContract } from '@ioc:Adonis/Lucid/Relations'
import { LucidModel, LucidRow, ModelObject } from '@ioc:Adonis/Lucid/Model'
import { LucidModel, LucidRow } from '@ioc:Adonis/Lucid/Model'
import {
RelationCallback,
FactoryModelContract,
Expand Down Expand Up @@ -36,18 +36,6 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract
public async make(parent: LucidRow, callback?: RelationCallback, count?: number) {
const factory = this.compile(callback)
const instances = await factory.makeStubbedMany(count || 1)

const [pivotKey, pivotValue] = this.relation.getPivotPair(parent)
instances.forEach((related) => {
const [pivotRelatedKey, pivotRelatedValue] = this.relation.getPivotRelatedPair(related)

/**
* Update model $extra properties
*/
related.$extras[pivotKey] = pivotValue
related.$extras[pivotRelatedKey] = pivotRelatedValue
})

parent.$setRelated(this.relation.relationName, instances)
}

Expand All @@ -58,28 +46,10 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract
const factory = this.compile(callback)
const instances = await factory.createMany(count || 1)

/**
* Loop over instances to build pivot attributes
*/
const pivotAttributes: ModelObject = {}
const [pivotKey, pivotValue] = this.relation.getPivotPair(parent)
instances.forEach((related) => {
const [pivotRelatedKey, pivotRelatedValue] = this.relation.getPivotRelatedPair(related)

/**
* Update model $extra properties
*/
related.$extras[pivotKey] = pivotValue
related.$extras[pivotRelatedKey] = pivotRelatedValue

// custom pivot attributes will come here
pivotAttributes[pivotRelatedValue] = {}
})

/**
* Make pivot insert query
*/
await this.relation.client(parent, this.ctx.$trx!).attach(pivotAttributes)
await this.relation.client(parent, this.ctx.$trx!).saveMany(instances)

/**
* Setup in-memory relationship
Expand Down
35 changes: 1 addition & 34 deletions test/factory/many-to-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ test.group('Factory | ManyToMany | make', (group) => {

assert.exists(user.skills[0].id)
assert.instanceOf(user.skills[0], Skill)
assert.deepEqual(user.skills[0].$extras, {
skill_id: user.skills[0].id,
user_id: user.id,
})
assert.deepEqual(user.skills[0].$extras, {})
assert.isFalse(user.skills[0].$isPersisted)
})

Expand Down Expand Up @@ -165,10 +162,6 @@ test.group('Factory | ManyToMany | make', (group) => {
assert.instanceOf(user.skills[0], Skill)
assert.isFalse(user.skills[0].$isPersisted)
assert.equal(user.skills[0].name, 'Dancing')
assert.deepEqual(user.skills[0].$extras, {
skill_id: user.skills[0].id,
user_id: user.id,
})
})

test('make many relationship', async (assert) => {
Expand Down Expand Up @@ -231,16 +224,6 @@ test.group('Factory | ManyToMany | make', (group) => {
assert.instanceOf(user.skills[1], Skill)
assert.isFalse(user.skills[1].$isPersisted)
assert.equal(user.skills[1].name, 'Dancing')

assert.deepEqual(user.skills[0].$extras, {
skill_id: user.skills[0].id,
user_id: user.id,
})

assert.deepEqual(user.skills[1].$extras, {
skill_id: user.skills[1].id,
user_id: user.id,
})
})
})

Expand Down Expand Up @@ -312,10 +295,6 @@ test.group('Factory | ManyToMany | create', (group) => {
assert.lengthOf(user.skills, 1)
assert.instanceOf(user.skills[0], Skill)
assert.isTrue(user.skills[0].$isPersisted)
assert.deepEqual(user.skills[0].$extras, {
user_id: user.id,
skill_id: user.skills[0].id,
})

const users = await db.from('users').select('*')
const skills = await db.from('skills').select('*')
Expand Down Expand Up @@ -380,10 +359,6 @@ test.group('Factory | ManyToMany | create', (group) => {
assert.instanceOf(user.skills[0], Skill)
assert.isTrue(user.skills[0].$isPersisted)
assert.equal(user.skills[0].name, 'Dancing')
assert.deepEqual(user.skills[0].$extras, {
user_id: user.id,
skill_id: user.skills[0].id,
})
})

test('create many relationships', async (assert) => {
Expand Down Expand Up @@ -439,18 +414,10 @@ test.group('Factory | ManyToMany | create', (group) => {
assert.instanceOf(user.skills[0], Skill)
assert.isTrue(user.skills[0].$isPersisted)
assert.equal(user.skills[0].name, 'Dancing')
assert.deepEqual(user.skills[0].$extras, {
user_id: user.id,
skill_id: user.skills[0].id,
})

assert.instanceOf(user.skills[1], Skill)
assert.isTrue(user.skills[1].$isPersisted)
assert.equal(user.skills[1].name, 'Programming')
assert.deepEqual(user.skills[1].$extras, {
user_id: user.id,
skill_id: user.skills[1].id,
})
})

test('rollback changes on error', async (assert) => {
Expand Down

0 comments on commit 7a98761

Please sign in to comment.