Skip to content

Commit

Permalink
fix(core): handle convertToJSValueSQL at QB level too
Browse files Browse the repository at this point in the history
Closes #1432
  • Loading branch information
B4nan committed Feb 14, 2021
1 parent 3f0a7b2 commit fbb2825
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
ret.push(this.helper.mapper(f, this.type) as string);
});

const meta = this.metadata.find(this.entityName);
/* istanbul ignore next */
const requiresSQLConversion = (meta?.props || []).filter(p => p.customType?.convertToDatabaseValueSQL || p.customType?.convertToJSValueSQL);

if (this.flags.has(QueryFlag.CONVERT_CUSTOM_TYPES) && (fields.includes('*') || fields.includes(`${this.alias}.*`)) && requiresSQLConversion.length > 0) {
requiresSQLConversion.forEach(p => ret.push(this.helper.mapper(p.name, this.type)));
}

Object.keys(this._populateMap).forEach(f => {
if (!fields.includes(f) && type === 'where') {
ret.push(...this.helper.mapJoinColumns(this.type, this._joins[f]) as string[]);
Expand Down
12 changes: 12 additions & 0 deletions tests/custom-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ describe('custom types [mysql]', () => {
expect(mock.mock.calls[4][0]).toMatch('begin');
expect(mock.mock.calls[5][0]).toMatch('update `location` set `point` = ST_PointFromText(\'point(2.34 9.87)\') where `id` = ?');
expect(mock.mock.calls[6][0]).toMatch('commit');
orm.em.clear();

const qb1 = orm.em.createQueryBuilder(Location, 'l');
const res1 = await qb1.select('*').where({ id: loc.id }).getSingleResult();
expect(mock.mock.calls[7][0]).toMatch('select `l`.*, ST_AsText(`l`.point) as `point` from `location` as `l` where `l`.`id` = ?');
expect(res1).toMatchObject(l1);
orm.em.clear();

const qb2 = orm.em.createQueryBuilder(Location);
const res2 = await qb2.select(['e0.*']).where({ id: loc.id }).getSingleResult();
expect(mock.mock.calls[8][0]).toMatch('select `e0`.*, ST_AsText(`e0`.point) as `point` from `location` as `e0` where `e0`.`id` = ?');
expect(res2).toMatchObject(l1);
});

});

0 comments on commit fbb2825

Please sign in to comment.