Skip to content

Commit

Permalink
feat: add useTransaction and useConnection methods
Browse files Browse the repository at this point in the history
The methods can be used through chainable API to use a custom connection
or transaction during the `model.save` or `model.delete` call
  • Loading branch information
thetutlage committed Apr 30, 2020
1 parent 7d40daf commit 789f038
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
3 changes: 3 additions & 0 deletions adonis-typings/model.ts
Expand Up @@ -421,6 +421,9 @@ declare module '@ioc:Adonis/Lucid/Model' {
$trx?: TransactionClientContract,
$setOptionsAndTrx (options?: ModelAdapterOptions): void

useTransaction (trx: TransactionClientContract): this
useConnection (connection: string): this

/**
* Gives an option to the end user to define constraints for update, insert
* and delete queries. Since the query builder for these queries aren't
Expand Down
17 changes: 17 additions & 0 deletions src/Orm/BaseModel/index.ts
Expand Up @@ -1068,6 +1068,7 @@ export class BaseModel implements LucidRow {
*/
public set $options (options: ModelOptions | undefined) {
if (!options) {
this.modelOptions = undefined
return
}

Expand Down Expand Up @@ -1095,6 +1096,22 @@ export class BaseModel implements LucidRow {
this.$options = options
}

/**
* A chainable method to set transaction on the model
*/
public useTransaction (trx: TransactionClientContract): this {
this.$trx = trx
return this
}

/**
* A chainable method to set transaction on the model
*/
public useConnection (connection: string): this {
this.$options = { connection }
return this
}

/**
* Set attribute
*/
Expand Down
44 changes: 41 additions & 3 deletions test/orm/base-model.spec.ts
Expand Up @@ -41,6 +41,7 @@ import {
ormAdapter,
resetTables,
FakeAdapter,
getProfiler,
getBaseModel,
} from '../../test-helpers'

Expand Down Expand Up @@ -186,6 +187,46 @@ test.group('Base model | boot', (group) => {
})
})

test.group('Base Model | options', (group) => {
group.before(async () => {
db = getDb()
BaseModel = getBaseModel(ormAdapter(db))
})

group.after(async () => {
await db.manager.closeAll()
})

test('set connection using useConnection method', (assert) => {
class User extends BaseModel {
@column()
public username: string
}

const user = new User()
user.username = 'virk'

user.useConnection('foo')
assert.deepEqual(user.$options, { connection: 'foo' })
})

test('set connection do not overwrite profiler from the options', (assert) => {
class User extends BaseModel {
@column()
public username: string
}

const user = new User()
user.username = 'virk'

const profiler = getProfiler()
user.$options = { profiler: profiler }

user.useConnection('foo')
assert.deepEqual(user.$options, { connection: 'foo', profiler: profiler })
})
})

test.group('Base Model | getter-setters', (group) => {
group.before(async () => {
db = getDb()
Expand Down Expand Up @@ -1723,7 +1764,6 @@ test.group('Base | apdater', (group) => {
User.$adapter = adapter
const user = new User()
user.username = 'virk'
user.$options = { connection: 'foo' }

await user.save()

Expand All @@ -1745,7 +1785,6 @@ test.group('Base | apdater', (group) => {
User.$adapter = adapter
const user = new User()
user.username = 'virk'
user.$options = { connection: 'foo' }

await user.save()

Expand Down Expand Up @@ -1777,7 +1816,6 @@ test.group('Base | apdater', (group) => {
User.$adapter = adapter
const user = new User()
user.username = 'virk'
user.$options = { connection: 'foo' }

await user.save()
await user.delete()
Expand Down

0 comments on commit 789f038

Please sign in to comment.