Skip to content

Commit

Permalink
Merge pull request #208 from cyjake/docs-group-queries
Browse files Browse the repository at this point in the history
docs: enhance aggregation query types & fix raw query result type
  • Loading branch information
zimond authored Oct 26, 2021
2 parents 035ae0e + a874dea commit 1255902
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
48 changes: 47 additions & 1 deletion test/types/querying.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
});
});
});
26 changes: 13 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type OrderOptions = { [name: string]: 'desc' | 'asc' };
type SetOptions = { [key: string]: Literal };

type WithOptions = {
[qualifier: string]: { select: string[], throughRelation: string }
[qualifier: string]: { select: string | string[], throughRelation?: string }
}

declare class Spell<T extends typeof Bone, U = InstanceType<T> | Collection<InstanceType<T>> | ResultSet | number | null> extends Promise<U> {
Expand Down Expand Up @@ -91,11 +91,11 @@ declare class Spell<T extends typeof Bone, U = InstanceType<T> | Collection<Inst
offset(skip: number): Spell<T, U>;
limit(skip: number): Spell<T, U>;

count(name?: string): Spell<T, ResultSet>;
average(name?: string): Spell<T, ResultSet>;
minimum(name?: string): Spell<T, ResultSet>;
maximum(name?: string): Spell<T, ResultSet>;
sum(name?: string): Spell<T, ResultSet>;
count(name?: string): Spell<T, Extract<U, ResultSet | number>>;
average(name?: string): Spell<T, Extract<U, ResultSet | number>>;
minimum(name?: string): Spell<T, Extract<U, ResultSet | number>>;
maximum(name?: string): Spell<T, Extract<U, ResultSet | number>>;
sum(name?: string): Spell<T, Extract<U, ResultSet | number>>;

batch(size?: number): AsyncIterable<T>;

Expand Down Expand Up @@ -161,8 +161,8 @@ interface QueryOptions {
interface QueryResult {
insertId?: number;
affectedRows?: number;
rows?: Record<string, Literal>,
fields?: { table: string, name: string },
rows?: Array<Record<string, Literal>>,
fields?: Array<{ table: string, name: string }>,
}

interface Connection {
Expand Down Expand Up @@ -423,11 +423,11 @@ export class Bone {
static order<T extends typeof Bone>(this: T, name: string, order?: 'desc' | 'asc'): Spell<T>;
static order<T extends typeof Bone>(this: T, opts: OrderOptions): Spell<T>;

static count<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet>;
static average<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet>;
static minimum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet>;
static maximum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet>;
static sum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet>;
static count<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet | number>;
static average<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet | number>;
static minimum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet | number>;
static maximum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet | number>;
static sum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet | number>;

/**
* UPDATE rows.
Expand Down

0 comments on commit 1255902

Please sign in to comment.