Skip to content

Commit

Permalink
feat(sql): initialize query builder in select mode (#565)
Browse files Browse the repository at this point in the history
This allows to skip `qb.select('*')` call.
  • Loading branch information
B4nan committed May 11, 2020
1 parent e4a64bc commit ac35d07
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 8 additions & 2 deletions packages/knex/src/query/QueryBuilder.ts
Expand Up @@ -43,7 +43,9 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
private readonly context?: Transaction,
readonly alias = `e0`,
private readonly connectionType?: 'read' | 'write',
private readonly em?: SqlEntityManager) { }
private readonly em?: SqlEntityManager) {
this.select('*');
}

select(fields: string | KnexQueryBuilder | (string | KnexQueryBuilder)[], distinct = false): this {
this._fields = Utils.asArray(fields);
Expand Down Expand Up @@ -311,7 +313,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
}

getKnex(): KnexQueryBuilder {
const tableName = this.helper.getTableName(this.entityName) + ([QueryType.SELECT, QueryType.COUNT].includes(this.type) ? ` as ${this.alias}` : '');
const tableName = this.helper.getTableName(this.entityName) + (this.finalized && [QueryType.SELECT, QueryType.COUNT].includes(this.type) ? ` as ${this.alias}` : '');
const qb = this.knex(tableName);

if (this.context) {
Expand Down Expand Up @@ -383,6 +385,10 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
this.type = type;
this._aliasMap[this.alias] = this.entityName;

if (![QueryType.SELECT, QueryType.COUNT].includes(type)) {
delete this._fields;
}

if (data) {
this._data = this.helper.processData(data);
}
Expand Down
9 changes: 4 additions & 5 deletions tests/QueryBuilder.test.ts
Expand Up @@ -134,8 +134,7 @@ describe('QueryBuilder', () => {

test('select multiple orWhere', async () => {
const qb = orm.em.createQueryBuilder(Publisher2);
qb.select('*')
.where({ name: 'test 123' })
qb.where({ name: 'test 123' })
.orWhere({ type: PublisherType.GLOBAL })
.orWhere({ name: 'test 321' })
.orWhere({ name: 'lol 321' })
Expand All @@ -146,8 +145,7 @@ describe('QueryBuilder', () => {

test('select complex where', async () => {
const qb = orm.em.createQueryBuilder(Publisher2);
qb.select('*')
.where({ name: 'test 123', $or: [{ name: 'test 321' }, { type: PublisherType.GLOBAL }] })
qb.where({ name: 'test 123', $or: [{ name: 'test 321' }, { type: PublisherType.GLOBAL }] })
.limit(2, 1);
expect(qb.getQuery()).toEqual('select `e0`.* from `publisher2` as `e0` where `e0`.`name` = ? and (`e0`.`name` = ? or `e0`.`type` = ?) limit ? offset ?');
expect(qb.getParams()).toEqual(['test 123', 'test 321', PublisherType.GLOBAL, 2, 1]);
Expand Down Expand Up @@ -1287,7 +1285,8 @@ describe('QueryBuilder', () => {
const node = new CriteriaNode(orm.em.getMetadata(), Author2.name);
node.payload = { foo: 123 };
const qb = orm.em.createQueryBuilder(Author2, 'a');
expect(qb.getAliasForEntity(Author2.name, node)).toBeUndefined();
expect(qb.getAliasForEntity(Author2.name, node)).toBe('a');
expect(qb.getAliasForEntity(Book2.name, node)).toBeUndefined();
});

afterAll(async () => orm.close(true));
Expand Down

0 comments on commit ac35d07

Please sign in to comment.