-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from cyjake/docs-group-queries
docs: enhance aggregation query types & fix raw query result type
- Loading branch information
Showing
2 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,29 @@ import { Bone, connect } from '../..' | |
describe('=> Querying (TypeScript)', function() { | ||
class Post extends Bone { | ||
static table = 'articles' | ||
title: string; | ||
} | ||
|
||
class User extends Bone { | ||
static initialize() { | ||
this.hasMany('posts', { foreignKey: 'authorId' }); | ||
} | ||
id: number; | ||
posts?: Post[]; | ||
} | ||
|
||
before(async function() { | ||
Bone.driver = null; | ||
await connect({ | ||
dialect: 'sqlite', | ||
database: '/tmp/leoric.sqlite3', | ||
models: [ Post ], | ||
models: [ Post, User ], | ||
}); | ||
}); | ||
|
||
beforeEach(async function() { | ||
await Post.truncate(); | ||
await User.truncate(); | ||
}); | ||
|
||
describe('=> Driver', function() { | ||
|
@@ -33,4 +43,40 @@ describe('=> Querying (TypeScript)', function() { | |
assert.ok(insertId); | ||
}); | ||
}); | ||
|
||
describe('=> Associations', function() { | ||
it('Bone.findOne().with()', async function() { | ||
const author = await User.create({ | ||
email: '[email protected]', | ||
nickname: 'Hey', | ||
status: 0, | ||
}); | ||
await Post.bulkCreate([ | ||
{ title: 'Leah' }, | ||
{ title: 'Stranger', authorId: author.id } | ||
]) | ||
const user = await User.findOne({}).with({ posts: { select: 'title' } }); | ||
assert.equal(user.id, author.id); | ||
assert.ok(Array.isArray(user.posts)); | ||
assert.equal(user.posts.length, 1); | ||
assert.equal(user.posts[0].title, 'Stranger'); | ||
}); | ||
}); | ||
|
||
describe('=> Aggregations', function() { | ||
it('Bone.count()', async function() { | ||
const count = await Post.count(); | ||
assert.equal(count, 0); | ||
}); | ||
|
||
it('Bone.group().count()', async function() { | ||
const result = await Post.group('title').count(); | ||
assert.ok(Array.isArray(result)); | ||
}); | ||
|
||
it('Bone.where().count()', async function() { | ||
const count = await Post.where({ title: 'Leah' }).count(); | ||
assert.equal(count, 0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters