Skip to content

Commit

Permalink
feat: allow defining custom connection and client in factories
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 21, 2020
1 parent 4d9ab93 commit fd64466
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
12 changes: 11 additions & 1 deletion adonis-typings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
declare module '@ioc:Adonis/Lucid/Factory' {
import faker from 'faker'
import { OneOrMany } from '@ioc:Adonis/Lucid/DatabaseQueryBuilder'
import { TransactionClientContract } from '@ioc:Adonis/Lucid/Database'
import { LucidRow, LucidModel, ModelAttributes } from '@ioc:Adonis/Lucid/Model'
import { TransactionClientContract, QueryClientContract } from '@ioc:Adonis/Lucid/Database'
import { ExtractModelRelations, RelationshipsContract } from '@ioc:Adonis/Lucid/Relations'

/**
Expand Down Expand Up @@ -162,6 +162,16 @@ declare module '@ioc:Adonis/Lucid/Factory' {
* instances of lucid models
*/
export interface FactoryBuilderContract<FactoryModel extends FactoryModelContract<LucidModel>> {
/**
* Define custom database connection
*/
connection (connection: string): this

/**
* Define custom query client
*/
client (client: QueryClientContract): this

/**
* Apply pre-defined state
*/
Expand Down
28 changes: 26 additions & 2 deletions src/Factory/FactoryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* file that was distributed with this source code.
*/

import { LucidRow, LucidModel } from '@ioc:Adonis/Lucid/Model'
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'
import { LucidRow, LucidModel, ModelAdapterOptions } from '@ioc:Adonis/Lucid/Model'
import {
FactoryModelContract,
FactoryContextContract,
Expand Down Expand Up @@ -65,6 +66,11 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
*/
private ctx?: FactoryContextContract

/**
* A custom set of model adapter options
*/
private options?: ModelAdapterOptions

/**
* Instead of relying on the `FactoryModelContract`, we rely on the
* `FactoryModel`, since it exposes certain API's required for
Expand All @@ -82,7 +88,7 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
return new FactoryContext(isStubbed, undefined)
}

const client = this.model.model.$adapter.modelConstructorClient(this.model.model)
const client = this.model.model.$adapter.modelConstructorClient(this.model.model, this.options)
const trx = await client.transaction()
return new FactoryContext(isStubbed, trx)
}
Expand Down Expand Up @@ -174,6 +180,24 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
}
}

/**
* Define custom database connection
*/
public connection (connection: string): this {
this.options = this.options || {}
this.options.connection = connection
return this
}

/**
* Define custom query client
*/
public client (client: QueryClientContract): this {
this.options = this.options || {}
this.options.client = client
return this
}

/**
* Define custom context. Usually called by the relationships
* to share the parent context with relationship factory
Expand Down
6 changes: 6 additions & 0 deletions src/Factory/FactoryModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ export class FactoryModel<Model extends LucidModel> implements FactoryModelContr
query () {
return new FactoryBuilder(this.model)
},
client (...args: any[]) {
return this.query().client(...args)
},
connection (...args: any[]) {
return this.query().connection(...args)
},
apply (...args: any[]) {
return this.query().apply(...args)
},
Expand Down
64 changes: 64 additions & 0 deletions test/factory/factory-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,70 @@ test.group('Factory | Factory Builder | create', (group) => {
assert.isTrue(user.$isPersisted)
assert.deepEqual(stack, ['afterMake', 'beforeCreate', 'afterCreate'])
})

test('define custom connection', async (assert) => {
assert.plan(3)

class User extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public username: string

@column()
public points: number
}

const factory = new FactoryModel(User, () => {
return {}
}, factoryManager)
.state('withPoints', (user) => user.points = 10)
.build()

const user = await factory
.connection('secondary')
.apply('withPoints')
.create((_, { $trx }) => {
assert.equal($trx?.connectionName, 'secondary')
})

assert.equal(user.points, 10)
assert.isTrue(user.$isPersisted)
})

test('define custom query client', async (assert) => {
assert.plan(3)

class User extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public username: string

@column()
public points: number
}

const factory = new FactoryModel(User, () => {
return {}
}, factoryManager)
.state('withPoints', (user) => user.points = 10)
.build()

const client = db.connection('secondary')

const user = await factory
.client(client)
.apply('withPoints')
.create((_, { $trx }) => {
assert.equal($trx?.connectionName, 'secondary')
})

assert.equal(user.points, 10)
assert.isTrue(user.$isPersisted)
})
})

test.group('Factory | Factory Builder | createMany', (group) => {
Expand Down

0 comments on commit fd64466

Please sign in to comment.