Skip to content

Commit

Permalink
fixes #842 fixes #1205
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Feb 13, 2019
1 parent 1adc68f commit 524e0be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
8 changes: 1 addition & 7 deletions lib/queryBuilder/QueryBuilderOperationSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const promiseUtils = require('../utils/promiseUtils');

const { isString, isFunction, isRegExp, mergeMaps, last } = require('../utils/objectUtils');
const { isString, isFunction, isRegExp, last } = require('../utils/objectUtils');
const { isSubclassOf } = require('../utils/classUtils');
const { QueryBuilderOperation } = require('./operations/QueryBuilderOperation');
const { QueryBuilderContextBase } = require('./QueryBuilderContextBase');
Expand Down Expand Up @@ -183,12 +183,6 @@ class QueryBuilderOperationSupport {

subqueryOf(query) {
if (query) {
const ctx = this.internalContext();

// Merge alias and table name maps for subqueries.
ctx.aliasMap = mergeMaps(query.internalContext().aliasMap, ctx.aliasMap);
ctx.tableMap = mergeMaps(query.internalContext().tableMap, ctx.tableMap);

this._parentQuery = query;

if (this.unsafeKnex() === null) {
Expand Down
19 changes: 0 additions & 19 deletions lib/utils/objectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,24 +252,6 @@ function chunk(arr, chunkSize) {
return out;
}

function mergeMaps(map1, map2) {
const map = new Map();

if (map1) {
for (let key of map1.keys()) {
map.set(key, map1.get(key));
}
}

if (map2) {
for (let key of map2.keys()) {
map.set(key, map2.get(key));
}
}

return map;
}

module.exports = {
isEmpty,
isString,
Expand All @@ -281,7 +263,6 @@ module.exports = {
difference,
upperFirst,
zipObject,
mergeMaps,
cloneDeep,
asSingle,
asArray,
Expand Down
37 changes: 30 additions & 7 deletions tests/integration/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,21 @@ module.exports = session => {
});
});

if (!session.isMySql()) {
it('with', () => {
return Model1.query()
.with('subquery1', Model1.query().unionAll(Model1.query()))
.with('subquery2', Model1.query())
.count('* as count')
.from('subquery1')
.first()
.debug()
.then(result => {
expect(result.count).to.eql(4);
});
});
}

if (session.isPostgres()) {
it('timeout should throw a TimeOutError', done => {
const knexQuery = Model1.query()
Expand Down Expand Up @@ -1154,6 +1169,10 @@ module.exports = session => {
.select([
'id',

Model1.relatedQuery('model1Relation1')
.count()
.as('rel1Count'),

Model1.relatedQuery('model1Relation2')
.count()
.as('rel2Count'),
Expand All @@ -1165,9 +1184,9 @@ module.exports = session => {
.orderBy('id')
.then(res => {
expect(res).to.eql([
{ id: 1, rel2Count: '2', rel3Count: '1', $afterGetCalled: 1 },
{ id: 2, rel2Count: '1', rel3Count: '2', $afterGetCalled: 1 },
{ id: 3, rel2Count: '0', rel3Count: '0', $afterGetCalled: 1 }
{ id: 1, rel1Count: 1, rel2Count: '2', rel3Count: '1', $afterGetCalled: 1 },
{ id: 2, rel1Count: 0, rel2Count: '1', rel3Count: '2', $afterGetCalled: 1 },
{ id: 3, rel1Count: 0, rel2Count: '0', rel3Count: '0', $afterGetCalled: 1 }
]);
});
});
Expand All @@ -1176,7 +1195,11 @@ module.exports = session => {
return Model1.query()
.alias('m')
.select([
'm.id',
'id',

Model1.relatedQuery('model1Relation1')
.count()
.as('rel1Count'),

Model1.relatedQuery('model1Relation2')
.count()
Expand All @@ -1189,9 +1212,9 @@ module.exports = session => {
.orderBy('id')
.then(res => {
expect(res).to.eql([
{ id: 1, rel2Count: '2', rel3Count: '1', $afterGetCalled: 1 },
{ id: 2, rel2Count: '1', rel3Count: '2', $afterGetCalled: 1 },
{ id: 3, rel2Count: '0', rel3Count: '0', $afterGetCalled: 1 }
{ id: 1, rel1Count: 1, rel2Count: '2', rel3Count: '1', $afterGetCalled: 1 },
{ id: 2, rel1Count: 0, rel2Count: '1', rel3Count: '2', $afterGetCalled: 1 },
{ id: 3, rel1Count: 0, rel2Count: '0', rel3Count: '0', $afterGetCalled: 1 }
]);
});
});
Expand Down

0 comments on commit 524e0be

Please sign in to comment.