Skip to content

Commit

Permalink
fix(relations) add paginate method
Browse files Browse the repository at this point in the history
  • Loading branch information
LookinGit authored and thetutlage committed Nov 7, 2017
1 parent dfc438c commit 2c6c56c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Lucid/Relations/BaseRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const methodsList = [
'count',
'truncate',
'ids',
'paginate',
'pair',
'pluckFirst',
'pluckId',
Expand Down
28 changes: 28 additions & 0 deletions test/unit/lucid-relations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test.group('Relations | HasOne', (group) => {
await ioc.use('Adonis/Src/Database').table('pictures').truncate()
await ioc.use('Adonis/Src/Database').table('identities').truncate()
await ioc.use('Adonis/Src/Database').table('cars').truncate()
await ioc.use('Adonis/Src/Database').table('posts').truncate()
})

group.after(async () => {
Expand Down Expand Up @@ -1078,6 +1079,33 @@ test.group('Relations | HasOne', (group) => {
assert.equal(userQuery.sql, helpers.formatQuery('select *, (select count(*) from "profiles" where users.id = profiles.user_id) as "profile_count" from "users" limit ?'))
})

test('return relation with paginate method', async (assert) => {
class Post extends Model {
users () {
return this.belongsToMany(User)
}
}
class User extends Model {
posts () {
return this.belongsToMany(Post)
}
}

User._bootIfNotBooted()
Post._bootIfNotBooted()

await ioc.use('Database').table('posts').insert([{ title: 'Post #1' }, { title: 'Post #2' }])
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
await ioc.use('Database').table('post_user').insert([{ user_id: 1, post_id: 2 }, { user_id: 2, post_id: 2 }])

const post = await Post.find(2)
const users = await post.users().paginate(1, 1)
assert.equal(users.pages.total, 2)
assert.equal(users.pages.page, 1)
assert.equal(users.pages.perPage, 1)
assert.equal(users.pages.lastPage, 2)
})

test('define count column for withCount', async (assert) => {
class Profile extends Model {
}
Expand Down

0 comments on commit 2c6c56c

Please sign in to comment.