Skip to content

Commit

Permalink
feat(relations): add support for withTimestamps in belongsToMany
Browse files Browse the repository at this point in the history
now withTimestamps will respect timestamps in pivot table

Closes #84
  • Loading branch information
thetutlage committed Jul 16, 2017
1 parent 7bda1b8 commit d120ae6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unit/fixtures/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module.exports = {
table.integer('course_id')
table.boolean('is_enrolled')
table.integer('lessons_done')
table.timestamps()
}),
knex.schema.createTable('authors', function (table) {
table.increments()
Expand Down
70 changes: 70 additions & 0 deletions test/unit/lucid.relations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Database = require('../../src/Database')
const chai = require('chai')
const Ioc = require('adonis-fold').Ioc
const expect = chai.expect
const moment = require('moment')
const filesFixtures = require('./fixtures/files')
const relationFixtures = require('./fixtures/relations')
const config = require('./helpers/config')
Expand Down Expand Up @@ -2958,6 +2959,75 @@ describe('Relations', function () {
yield relationFixtures.truncate(Database, 'courses')
yield relationFixtures.truncate(Database, 'course_student')
})

it('should be return timestamps from the pivot table model withTimestamps method is used', function * () {
const savedStudent = yield relationFixtures.createRecords(Database, 'students', {name: 'ricky', id: 29})
const savedCourse = yield relationFixtures.createRecords(Database, 'courses', {title: 'geometry', id: 12})
class Course extends Model {
}
class Student extends Model {
courses () {
return this.belongsToMany(Course).withTimestamps()
}
}

Course.bootIfNotBooted()
const student = yield Student.find(savedStudent[0])
expect(student instanceof Student).to.equal(true)
expect(student.id).to.equal(savedStudent[0])
yield student.courses().attach(savedCourse, {is_enrolled: 1})
const courses = yield student.courses().fetch()
expect(courses.size()).to.equal(1)
expect(courses.isArray()).to.equal(true)
expect(courses.first()._pivot_created_at).to.equal(null)
expect(courses.first()._pivot_updated_at).to.equal(null)
yield relationFixtures.truncate(Database, 'students')
yield relationFixtures.truncate(Database, 'courses')
yield relationFixtures.truncate(Database, 'course_student')
})

it('should set timestamps on the pivot table when withTimestamps is set to true', function * () {
const savedStudent = yield relationFixtures.createRecords(Database, 'students', {name: 'ricky', id: 29})
class Course extends Model {
}
class Student extends Model {
courses () {
return this.belongsToMany(Course).withTimestamps()
}
}

Course.bootIfNotBooted()
const student = yield Student.find(savedStudent[0])
yield student.courses().create({title: 'geometry'})
const courses = yield student.courses().fetch()
expect(moment(courses.first()._pivot_created_at).isValid()).to.equal(true)
expect(moment(courses.first()._pivot_updated_at).isValid()).to.equal(true)
yield relationFixtures.truncate(Database, 'students')
yield relationFixtures.truncate(Database, 'courses')
yield relationFixtures.truncate(Database, 'course_student')
})

it('should work fine when withTimestamps and withPivot is used together', function * () {
const savedStudent = yield relationFixtures.createRecords(Database, 'students', {name: 'ricky', id: 29})
class Course extends Model {
}
class Student extends Model {
courses () {
return this.belongsToMany(Course).withTimestamps().withPivot('is_enrolled')
}
}

Course.bootIfNotBooted()
const student = yield Student.find(savedStudent[0])
yield student.courses().create({title: 'geometry'})
const courses = yield student.courses().fetch()
expect(moment(courses.first()._pivot_created_at).isValid()).to.equal(true)
expect(moment(courses.first()._pivot_updated_at).isValid()).to.equal(true)
expect(moment(courses.first()._pivot_course_id)).to.be.ok
yield relationFixtures.truncate(Database, 'students')
yield relationFixtures.truncate(Database, 'courses')
yield relationFixtures.truncate(Database, 'course_student')
})
})

context('HasManyThrough', function () {
Expand Down

0 comments on commit d120ae6

Please sign in to comment.