Skip to content

Commit

Permalink
fix(database): add database.transaction method
Browse files Browse the repository at this point in the history
Closes #184
  • Loading branch information
thetutlage committed Oct 21, 2017
1 parent d99ec4f commit 56695d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ class Database {
})
}

/**
* Run a callback inside a transaction
*
* @param {Function} callback
*
* @method transaction
*
* @returns Object
*/
transaction (callback) {
return this.knex.transaction(callback)
}

/**
* Starts a global transaction, where all query builder
* methods will be part of transaction automatically.
Expand Down
9 changes: 9 additions & 0 deletions test/unit/database.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ test.group('Database | QueryBuilder', (group) => {
const fn = () => this.database.foo()
assert.throw(fn, 'Database.foo is not a function')
})

test('database.transaction should work', async (assert) => {
await this.database.transaction(function (trx) {
return trx.table('users').insert({ username: 'virk' })
})
const firstUser = await this.database.table('users').first()
assert.equal(firstUser.username, 'virk')
await this.database.truncate('users')
})
})

test.group('Database | Manager', () => {
Expand Down

0 comments on commit 56695d7

Please sign in to comment.