Skip to content

Commit

Permalink
✨ Added support for 1 and conjunction case
Browse files Browse the repository at this point in the history
refs TryGhost#5

- $and: [tags.slug:en, tags.slug:de]
  • Loading branch information
kirrg001 committed Nov 15, 2018
1 parent 600ac9d commit 8952b48
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,36 @@ class MongoToKnex {
debugExtended(`(buildRelationQuery) ${JSON.stringify(relations)}`);
}

const groupedRelations = _.groupBy(relations, 'table');
const groupedRelations = {};

_.each(relations, (relation) => {
if (!groupedRelations[relation.table]) {
groupedRelations[relation.table] = [];
}

if (groupedRelations[relation.table].length) {
const columnExists = _.find(groupedRelations[relation.table], (statement) => {
// CASE: we should not use the same sub query if the column name is the same (two sub queries)
if (statement.column === relation.column) {
return true;
}
});

if (columnExists) {
if (!groupedRelations[relation.table + '_']) {
groupedRelations[relation.table + '_'] = [];
}

groupedRelations[relation.table + '_'].push(relation);
return;
}
}

groupedRelations[relation.table].push(relation);
});

if (debugExtended.enabled) {
debugExtended(`(buildRelationQuery) grouped: ${JSON.stringify(relations)}`);
debugExtended(`(buildRelationQuery) grouped: ${JSON.stringify(groupedRelations)}`);
}

// CASE: {tags: [where clause, where clause], authors: [where clause, where clause]}
Expand Down
23 changes: 23 additions & 0 deletions test/integration/relations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@ describe('Relations', function () {
});
});

describe('AND', function () {
it('tags.slug is animal and classic', function () {
const mongoJSON = {
$and: [
{
'tags.slug': 'animal'
},
{
'tags.slug': 'classic'
}
]
};

const query = makeQuery(mongoJSON);

return query
.select()
.then((result) => {
result.should.be.an.Array().with.lengthOf(2);
});
});
});

describe('OR', function () {
it('tags.slug IN (animal)', function () {
const mongoJSON = {
Expand Down

0 comments on commit 8952b48

Please sign in to comment.