Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Comparison operator support #26

Merged
merged 3 commits into from
Dec 6, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const isOp = key => key.charAt(0) === '$';
const isLogicOp = key => isOp(key) && _.includes(logicOps, key);
const isCompOp = key => isOp(key) && _.includes(_.keys(compOps), key);
const isNegationOp = key => isOp(key) && _.includes(['$ne', '$nin'], key);
const isStatementGroupOp = key => _.includes([compOps.$in, compOps.$nin], key);

class MongoToKnex {
constructor(options = {}, config = {}) {
Expand Down Expand Up @@ -252,11 +253,19 @@ class MongoToKnex {
if (negateGroup) {
statementOp = compOps.$in;
} else {
statementOp = isNegationOp(statement.operator)
? compOps.$nin
: compOps.$in;
if (isNegationOp(statement.operator)) {
kirrg001 marked this conversation as resolved.
Show resolved Hide resolved
statementOp = compOps.$nin;
} else {
statementOp = compOps[statement.operator];
}
}

let statementValue = statement.value;

// CASE: need to normalize value to array when it's a group operation
if (isStatementGroupOp(statementOp)) {
statementValue = !_.isArray(statement.value) ? [statement.value] : statement.value;
}
const statementValue = !_.isArray(statement.value) ? [statement.value] : statement.value;

innerQB[statement.whereType](statementColumn, statementOp, statementValue);
});
Expand Down
58 changes: 58 additions & 0 deletions test/integration/relations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,64 @@ describe('Relations', function () {
});
});

describe('COMPARISONS $gt / $gte / $lt / $lte', function () {
it('tags.created_at is > 2015-06-21', function () {
const mongoJSON = {'tags.created_at': {
$gt: '2015-06-21'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(1);
result.should.matchIds([8]);
});
});

it('tags.created_at is >= 2015-06-21', function () {
const mongoJSON = {'tags.created_at': {
$gte: '2015-06-21'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(2);
result.should.matchIds([3, 8]);
});
});

it('tags.created_at is < 2015-01-02', function () {
const mongoJSON = {'tags.created_at': {
$lt: '2015-01-02'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(4);
result.should.matchIds([1, 4, 5, 6]);
});
});

it('tags.created_at is <= 2015-01-02', function () {
const mongoJSON = {'tags.created_at': {
$lte: '2015-01-02'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(5);
result.should.matchIds([1, 2, 4, 5, 6]);
});
});
});

describe('AND $and', function () {
it('tags.slug is animal and classic', function () {
const mongoJSON = {
Expand Down
59 changes: 59 additions & 0 deletions test/integration/same-table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,63 @@ describe('Same Table', function () {
});
});
});

describe('COMPARISONS $gt / $gte / $lt / $lte', function () {
it('published_at is > 2015-06-04', function () {
const mongoJSON = {published_at: {
$gt: '2015-06-04'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(1);
result.should.matchIds([8]);
});
});

it('published_at is >= 2015-06-04', function () {
const mongoJSON = {published_at: {
$gte: '2015-06-04'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(2);
result.should.matchIds([7, 8]);
});
});

it('published_at is < 2015-06-04', function () {
const mongoJSON = {published_at: {
$lt: '2015-06-04'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(5);
result.should.matchIds([1, 3, 4, 5, 6]);
});
});

it('published_at is <= 2015-06-04', function () {
const mongoJSON = {published_at: {
$lte: '2015-06-04'
}};

const query = makeQuery(mongoJSON);

return query
.then((result) => {
result.length.should.eql(6);
result.should.matchIds([1, 3, 4, 5, 6, 7]);
});
});
});
});

20 changes: 16 additions & 4 deletions test/integration/suite1/fixtures/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"featured": false,
"image": null,
"status": "published",
"published_at": "2015-01-01",
"author_id": 1
},
{
Expand All @@ -31,6 +32,7 @@
"featured": false,
"image": "myimage.jpg",
"status": "draft",
"published_at": null,
"author_id": 2
},
{
Expand All @@ -39,6 +41,7 @@
"featured": true,
"image": null,
"status": "published",
"published_at": "2015-01-03",
"author_id": 1
},
{
Expand All @@ -47,6 +50,7 @@
"featured": true,
"image": null,
"status": "published",
"published_at": "2015-06-01",
"author_id": 2
},
{
Expand All @@ -55,6 +59,7 @@
"featured": true,
"image": null,
"status": "published",
"published_at": "2015-06-02",
"author_id": 1
},
{
Expand All @@ -63,6 +68,7 @@
"featured": true,
"image": null,
"status": "published",
"published_at": "2015-06-03",
"author_id": 1
},
{
Expand All @@ -71,6 +77,7 @@
"featured": true,
"image": null,
"status": "published",
"published_at": "2015-06-04",
"author_id": 1
},
{
Expand All @@ -79,6 +86,7 @@
"featured": true,
"image": null,
"status": "published",
"published_at": "2015-06-05",
"author_id": 2
}
],
Expand All @@ -87,25 +95,29 @@
"id": 1,
"name": "Classic",
"slug": "classic",
"visibility": "public"
"visibility": "public",
"created_at": "2015-01-01"
},
{
"id": 2,
"name": "Animal",
"slug": "animal",
"visibility": "public"
"visibility": "public",
"created_at": "2015-01-02"
},
{
"id": 3,
"name": "CGI",
"slug": "cgi",
"visibility": "public"
"visibility": "public",
"created_at": "2015-06-21"
},
{
"id": 4,
"name": "#internal",
"slug": "hash-internal",
"visibility": "internal"
"visibility": "internal",
"created_at": "2015-06-22"
}
]
}
2 changes: 2 additions & 0 deletions test/integration/suite1/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ module.exports.up = function (knex) {
table.boolean('featured').defaultsTo(false);
table.string('image', 191).nullable();
table.string('status', 191).nullable();
table.dateTime('published_at').nullable();
table.integer('author_id').unsigned().references('users.id');
}))
.then(() => knex.schema.createTable('tags', (table) => {
table.increments('id').primary();
table.string('name', 191);
table.string('slug', 191);
table.string('visibility', 191).defaultTo('public');
table.dateTime('created_at');
}))
.then(() => knex.schema.createTable('posts_tags', (table) => {
table.increments('id').primary();
Expand Down