Skip to content

Commit

Permalink
feat(lucid): add Model.truncate() function (#240)
Browse files Browse the repository at this point in the history
* feat(lucid): add Model.reset() function

* fix(lucid): returns the truncate call

* chore(lucid): rename reset to truncate
  • Loading branch information
RomainLanz authored and thetutlage committed Nov 17, 2017
1 parent d3cc53a commit 9be15a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ class Model extends BaseModel {
return Promise.all(payloadArray.map((payload) => this.create(payload, trx)))
}

/**
* Deletes all rows of this model (truncate table).
*
* @method truncate
*
* @return {Promise<void>}
*/
static truncate () {
const query = this.query()
return query.truncate()
}

/**
* Returns an object of values dirty after persisting to
* database or after fetching from database.
Expand Down
15 changes: 15 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,21 @@ test.group('Model', (group) => {
assert.isFalse(user.isNew)
})

test('reset a table in the database', async (assert) => {
class User extends Model {
}

User._bootIfNotBooted()
const user = await User.create({ username: 'virk' })
assert.isTrue(user.$persisted)
assert.isFalse(user.isNew)

await User.truncate()

const users = await User.all()
assert.equal(users.rows.length, 0)
})

test('further changes to instance returned by create should update the model', async (assert) => {
class User extends Model {
}
Expand Down

0 comments on commit 9be15a9

Please sign in to comment.