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 1 commit
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
8 changes: 5 additions & 3 deletions lib/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ 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];
}
}
const statementValue = !_.isArray(statement.value) ? [statement.value] : statement.value;
kirrg001 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
60 changes: 59 additions & 1 deletion 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 Expand Up @@ -248,7 +306,7 @@ describe('Relations', function () {
};

const query = makeQuery(mongoJSON);

console.log(query.toSQL());
return query
.select()
.then((result) => {
Expand Down
12 changes: 8 additions & 4 deletions test/integration/suite1/fixtures/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,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"
}
]
}
1 change: 1 addition & 0 deletions test/integration/suite1/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports.up = function (knex) {
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