From 3cf118f7740c63fdd3ad2ecb53ccf9364b8c4d92 Mon Sep 17 00:00:00 2001 From: Fizcko <20943860+Fizcko@users.noreply.github.com> Date: Wed, 17 Apr 2024 12:38:21 +0200 Subject: [PATCH] Now return an Object id when expression "_id" is found --- Readme.md | 5 ++++- package.json | 5 ++++- rsql-mongodb.js | 27 ++++++++++++++++++++++++--- test.js | 7 +++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index 1cb5e46..f1bc9ff 100644 --- a/Readme.md +++ b/Readme.md @@ -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); diff --git a/package.json b/package.json index a4e8156..3b1d16e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -42,5 +42,8 @@ "coveralls": "3.0.5", "mocha": "6.2.0", "nyc": "14.1.1" + }, + "dependencies": { + "bson": "6.6.0" } } diff --git a/rsql-mongodb.js b/rsql-mongodb.js index b32e91f..7dcd415 100644 --- a/rsql-mongodb.js +++ b/rsql-mongodb.js @@ -1,3 +1,5 @@ +const { ObjectId } = require('bson'); + function setType(input) { var typedInput = input; @@ -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 @@ -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 // @@ -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){ @@ -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; @@ -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; diff --git a/test.js b/test.js index 2047467..e2211f7 100644 --- a/test.js +++ b/test.js @@ -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 () { @@ -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" } }); @@ -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] } }); @@ -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" } ] });