Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sql): initialize query builder in select mode #565

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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