Skip to content

Commit

Permalink
Now return an Object id when expression "_id" is found
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizcko committed Apr 17, 2024
1 parent 5e7453b commit 3cf118f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ try{
// Groups
rsqlMongoDB('(firstName=="john";lastName=="doe"),(firstName==janne;lastName==doe)');
//=> { $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] }


// Using "_id"
rsqlMongoDB('_id_==650a7389a7ab39ddcfbc6832');
//=> { "_id_" : new ObjectId('650a7389a7ab39ddcfbc6832') }
}
catch(err){
console.log(err);
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsql-mongodb",
"version": "2.0.2",
"version": "2.1.0",
"description": "Converting RSQL queries to MongoDB queries",
"main": "rsql-mongodb.js",
"typings": "rsql-mongodb.ts",
Expand Down Expand Up @@ -42,5 +42,8 @@
"coveralls": "3.0.5",
"mocha": "6.2.0",
"nyc": "14.1.1"
},
"dependencies": {
"bson": "6.6.0"
}
}
27 changes: 24 additions & 3 deletions rsql-mongodb.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { ObjectId } = require('bson');

function setType(input) {

var typedInput = input;
Expand Down Expand Up @@ -34,6 +36,16 @@ function setType(input) {
return typedInput;
}

function setTypeObjectId(input) {

// Remove quotes and double quotes
formatedInput = input.replace(/['"]+/g, '');

if(ObjectId.isValid(formatedInput))
return new ObjectId(formatedInput);
else
return setType(input);
}
module.exports = function (input) {

// Define variables
Expand All @@ -45,6 +57,7 @@ module.exports = function (input) {
// Define logical & special operators
var logicals = [';', ','];
var specialOperators = ['=in=', '=out='];
var expForId = ["_id"];

// Apply Shunting-yard algorithm applied to this use case
//
Expand Down Expand Up @@ -239,8 +252,10 @@ module.exports = function (input) {


try{

var typedExp2 = setType(exp2);
if(expForId.indexOf(exp1) !== -1)
typedExp2 = setTypeObjectId(exp2);

var mongoOperatorQuery = {};

switch(operator){
Expand All @@ -267,7 +282,10 @@ module.exports = function (input) {
typedExp2 = typedExp2.replace(")","");
var typedValues = new Array();
for ( var token of typedExp2.split(",") ) {
typedValues.push(setType(token.trim()));
var value = setType(token.trim());
if(expForId.indexOf(exp1) !== -1)
value = setTypeObjectId(value);
typedValues.push(value);
}
mongoOperatorQuery[exp1] = { $in: typedValues };
break;
Expand All @@ -276,7 +294,10 @@ module.exports = function (input) {
typedExp2 = typedExp2.replace(")","");
var typedValues = new Array();
for ( var token of typedExp2.split(",") ) {
typedValues.push(setType(token.trim()));
var value = setType(token.trim());
if(expForId.indexOf(exp1) !== -1)
value = setTypeObjectId(value);
typedValues.push(value);
}
mongoOperatorQuery[exp1] = { $nin: typedValues };
break;
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const chai = require('chai');
const expect = chai.expect;
const rsqlMongoDB = require('./');
const { ObjectId } = require('bson');

describe('rsql-mongodb', function () {
it("Test null return", function () {
Expand All @@ -15,6 +16,10 @@ describe('rsql-mongodb', function () {
expect(rsqlMongoDB('childs==null')).to.deep.include({ "childs" : null });
expect(rsqlMongoDB('childs==2')).to.deep.include({ "childs" : 2 });
expect(rsqlMongoDB('creationDate==2021-10-30T00:00:00.000Z')).to.be.a('object');
expect(rsqlMongoDB('_id==650a7389a7ab39ddcfbc6832')).to.deep.include({ "_id": new ObjectId('650a7389a7ab39ddcfbc6832') });
expect(rsqlMongoDB('_id==650a7389a7ab39ddcfbc683')).to.deep.include({ "_id": '650a7389a7ab39ddcfbc683' });
expect(rsqlMongoDB('_id=="650a7389a7ab39ddcfbc6832"')).to.deep.include({ "_id": new ObjectId('650a7389a7ab39ddcfbc6832') });
expect(rsqlMongoDB("_id=='650a7389a7ab39ddcfbc6832'")).to.deep.include({ "_id": new ObjectId('650a7389a7ab39ddcfbc6832') });
});
it("Test operator Not Equal To ('!=')", function () {
expect(rsqlMongoDB('lastName!="doe"')).to.deep.include({ "lastName": { $ne: "doe" } });
Expand Down Expand Up @@ -49,6 +54,7 @@ describe('rsql-mongodb', function () {
expect(rsqlMongoDB('childs=in=(1,2,3)')).to.deep.include({ "childs": { $in: [1,2,3] } });
expect(rsqlMongoDB('childs=in=("1","2","3")')).to.deep.include({ "childs": { $in: ["1","2","3"] } });
expect(rsqlMongoDB('childs=in=(1, 2, 3 )')).to.deep.include({ "childs": { $in: [1,2,3] } });
expect(rsqlMongoDB('_id=in=(650a7389a7ab39ddcfbc6832,650a7389a7ab39ddcfbc6833)')).to.deep.include({ "_id": { $in: [new ObjectId('650a7389a7ab39ddcfbc6832'),new ObjectId('650a7389a7ab39ddcfbc6833')] } });
});
it("Test operator Out ('=out=')", function () {
expect(rsqlMongoDB('childs=out=(1,2,3)')).to.deep.include({ "childs": { $nin: [1,2,3] } });
Expand All @@ -69,6 +75,7 @@ describe('rsql-mongodb', function () {
});
it("Test logical operator AND (';')", function () {
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });
expect(rsqlMongoDB('lastName=="doe";_id==650a7389a7ab39ddcfbc6832')).to.deep.include({ $and: [ { "lastName" : "doe" }, { "_id" : new ObjectId('650a7389a7ab39ddcfbc6832') } ] });
});
it("Test logical operator OR (',')", function () {
expect(rsqlMongoDB('firstName=="john",firstName=="janne"')).to.deep.include({ $or: [ { "firstName" : "john" } , { "firstName" : "janne" } ] });
Expand Down

0 comments on commit 3cf118f

Please sign in to comment.