Parser/Interpreter for JSON queries mongodb

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Parser/Interpreter for JSON queries mongodb

chrismihaylyk
This post was updated on .
Hello, friends!

I'm creating MongoDBBrowser that have all CRUD operations and I need a parser that translates javascript queries to MongoDB into the queries (for making selects, inserts etc.) using MongoQuery class.
For example, I have query

db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, { description: { $regex: /test/i } } ] } )

that is manually rewriting into
someCollection select: {
        '$or' -> {
                { 'name' -> {
                        '$regex' -> 'test' .
                        '$options' -> 'i' } asDictionary } asDictionary.
                { 'description' -> {
                        '$regex' -> 'test' .
                        '$options' -> 'i' } asDictionary } asDictionary
        }
} asDictionary

But I need to have a parser that will do it automatically.

Maybe there are already exist one? If not, how to write my own?
Please help.

Thanks a lot, Khrystyna=)
Reply | Threaded
Open this post in threaded view
|

Re: Parser for javascript mongodb (json) notation selects

Tudor Girba-2
Hi,

SmaCC ships with a JS parser. Please take a look at it.

Cheers,
Doru


> On Apr 16, 2017, at 3:42 PM, chrismihaylyk <[hidden email]> wrote:
>
> Hello, friends!
>
> I'm creating MongoDBBrowser that have all CRUD operations and I need a
> parser that translates javascript queries to MongoDB into the queries (for
> making selects, inserts etc.) using MongoQuery class.
> For example, I have query
>
> db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {
> description: { $regex: /test/i } } ] } )
>
> that is manually rewriting into
> someCollection select: {
>        '$or' -> {
>                { 'name' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary.
>                { 'description' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary
>        }
> } asDictionary
>
> But I need to have a parser that will do it automatically.
>
> Maybe there are already exist one? If not, how to write my own?
> Please help.
>
> Thanks a lot, Khrystyna=)
>
>
>
> --
> View this message in context: http://forum.world.st/Parser-for-javascript-mongodb-json-notation-selects-tp4942299.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

--
www.tudorgirba.com
www.feenk.com

"Reasonable is what we are accustomed with."


Reply | Threaded
Open this post in threaded view
|

Re: Parser for javascript mongodb (json) notation selects

chrismihaylyk
This post was updated on .
Thank you very much!
But could you help me, please, to setup SmaCC into my Pharo?
And some tutorial how use it?

Your's faithfully,
Khrystyna Mykhailiuk

2017-04-16 20:57 GMT+03:00 Tudor Girba <tudor@tudorgirba.com>:

> Hi,
>
> SmaCC ships with a JS parser. Please take a look at it.
>
> Cheers,
> Doru
>
>
> > On Apr 16, 2017, at 3:42 PM, chrismihaylyk <chrismihaylyk@gmail.com>
> wrote:
> >
> > Hello, friends!
> >
> > I'm creating MongoDBBrowser that have all CRUD operations and I need a
> > parser that translates javascript queries to MongoDB into the queries
> (for
> > making selects, inserts etc.) using MongoQuery class.
> > For example, I have query
> >
> > db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {
> > description: { $regex: /test/i } } ] } )
> >
> > that is manually rewriting into
> > someCollection select: {
> >        '$or' -> {
> >                { 'name' -> {
> >                        '$regex' -> 'test' .
> >                        '$options' -> 'i' } asDictionary } asDictionary.
> >                { 'description' -> {
> >                        '$regex' -> 'test' .
> >                        '$options' -> 'i' } asDictionary } asDictionary
> >        }
> > } asDictionary
> >
> > But I need to have a parser that will do it automatically.
> >
> > Maybe there are already exist one? If not, how to write my own?
> > Please help.
> >
> > Thanks a lot, Khrystyna=)
> >
> >
> >
> > --
> > View this message in context: http://forum.world.st/Parser-
> for-javascript-mongodb-json-notation-selects-tp4942299.html
> > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
> >
>
> --
> www.tudorgirba.com
> www.feenk.com
>
> "Reasonable is what we are accustomed with."
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Parser for javascript mongodb (json) notation selects

Tudor Girba-2
Hi,

Here is how to load it in Pharo 6:
 Metacello new
smalltalkhubUser: 'JohnBrant' project: 'SmaCC';
configuration: 'SmaCC';
version: #'development';
load

Then you can do:
JSParser parse: 'db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {description: { $regex: /test/i } } ] } )’

And you get the AST that you can play with (and transform into what you want):


Cheers,
Doru


On Apr 16, 2017, at 8:06 PM, Христина Михайлюк <[hidden email]> wrote:

Thank you very much! 
But could you help me, please, to setup SmaCC into my Pharo?

Your's faithfully,
Khrystyna Mykhailiuk

2017-04-16 20:57 GMT+03:00 Tudor Girba <[hidden email]>:
Hi,

SmaCC ships with a JS parser. Please take a look at it.

Cheers,
Doru


> On Apr 16, 2017, at 3:42 PM, chrismihaylyk <[hidden email]> wrote:
>
> Hello, friends!
>
> I'm creating MongoDBBrowser that have all CRUD operations and I need a
> parser that translates javascript queries to MongoDB into the queries (for
> making selects, inserts etc.) using MongoQuery class.
> For example, I have query
>
> db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {
> description: { $regex: /test/i } } ] } )
>
> that is manually rewriting into
> someCollection select: {
>        '$or' -> {
>                { 'name' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary.
>                { 'description' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary
>        }
> } asDictionary
>
> But I need to have a parser that will do it automatically.
>
> Maybe there are already exist one? If not, how to write my own?
> Please help.
>
> Thanks a lot, Khrystyna=)
>
>
>
> --
> View this message in context: http://forum.world.st/Parser-for-javascript-mongodb-json-notation-selects-tp4942299.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

--
www.tudorgirba.com
www.feenk.com

"Reasonable is what we are accustomed with."




--
www.tudorgirba.com
www.feenk.com

"Every thing should have the right to be different."




Reply | Threaded
Open this post in threaded view
|

Re: Parser for javascript mongodb (json) notation selects

chrismihaylyk
That's so cool plugin. Thank you very very much!

But I'm a bit confused how to transform that tree into needed code f.e.
into  
someCollection select: {
        '$or' -> {
                { 'name' -> {
                        '$regex' -> 'test' .
                        '$options' -> 'i' } asDictionary } asDictionary.
                { 'description' -> {
                        '$regex' -> 'test' .
                        '$options' -> 'i' } asDictionary } asDictionary
        }
} asDictionary

Could you help me, please?

2017-04-16 21:41 GMT+03:00 Tudor Girba <[hidden email]>:
Hi,

Here is how to load it in Pharo 6:
 Metacello new
smalltalkhubUser: 'JohnBrant' project: 'SmaCC';
configuration: 'SmaCC';
version: #'development';
load

Then you can do:
JSParser parse: 'db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {description: { $regex: /test/i } } ] } )’

And you get the AST that you can play with (and transform into what you want):


Cheers,
Doru


On Apr 16, 2017, at 8:06 PM, Христина Михайлюк <[hidden email]> wrote:

Thank you very much! 
But could you help me, please, to setup SmaCC into my Pharo?

Your's faithfully,
Khrystyna Mykhailiuk

2017-04-16 20:57 GMT+03:00 Tudor Girba <[hidden email]>:
Hi,

SmaCC ships with a JS parser. Please take a look at it.

Cheers,
Doru


> On Apr 16, 2017, at 3:42 PM, chrismihaylyk <[hidden email]> wrote:
>
> Hello, friends!
>
> I'm creating MongoDBBrowser that have all CRUD operations and I need a
> parser that translates javascript queries to MongoDB into the queries (for
> making selects, inserts etc.) using MongoQuery class.
> For example, I have query
>
> db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {
> description: { $regex: /test/i } } ] } )
>
> that is manually rewriting into
> someCollection select: {
>        '$or' -> {
>                { 'name' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary.
>                { 'description' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary
>        }
> } asDictionary
>
> But I need to have a parser that will do it automatically.
>
> Maybe there are already exist one? If not, how to write my own?
> Please help.
>
> Thanks a lot, Khrystyna=)
>
>
>
> --
> View this message in context: http://forum.world.st/Parser-for-javascript-mongodb-json-notation-selects-tp4942299.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

--
www.tudorgirba.com
www.feenk.com

"Reasonable is what we are accustomed with."




--
www.tudorgirba.com
www.feenk.com

"Every thing should have the right to be different."





Reply | Threaded
Open this post in threaded view
|

Re: Parser for javascript mongodb (json) notation selects

chrismihaylyk
How to iterate over it result parse tree?

2017-04-16 22:44 GMT+03:00 Христина Михайлюк <[hidden email]>:
That's so cool plugin. Thank you very very much!

But I'm a bit confused how to transform that tree into needed code f.e.
into  
someCollection select: {
        '$or' -> {
                { 'name' -> {
                        '$regex' -> 'test' .
                        '$options' -> 'i' } asDictionary } asDictionary.
                { 'description' -> {
                        '$regex' -> 'test' .
                        '$options' -> 'i' } asDictionary } asDictionary
        }
} asDictionary

Could you help me, please?

2017-04-16 21:41 GMT+03:00 Tudor Girba <[hidden email]>:
Hi,

Here is how to load it in Pharo 6:
 Metacello new
smalltalkhubUser: 'JohnBrant' project: 'SmaCC';
configuration: 'SmaCC';
version: #'development';
load

Then you can do:
JSParser parse: 'db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {description: { $regex: /test/i } } ] } )’

And you get the AST that you can play with (and transform into what you want):


Cheers,
Doru


On Apr 16, 2017, at 8:06 PM, Христина Михайлюк <[hidden email]> wrote:

Thank you very much! 
But could you help me, please, to setup SmaCC into my Pharo?

Your's faithfully,
Khrystyna Mykhailiuk

2017-04-16 20:57 GMT+03:00 Tudor Girba <[hidden email]>:
Hi,

SmaCC ships with a JS parser. Please take a look at it.

Cheers,
Doru


> On Apr 16, 2017, at 3:42 PM, chrismihaylyk <[hidden email]> wrote:
>
> Hello, friends!
>
> I'm creating MongoDBBrowser that have all CRUD operations and I need a
> parser that translates javascript queries to MongoDB into the queries (for
> making selects, inserts etc.) using MongoQuery class.
> For example, I have query
>
> db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {
> description: { $regex: /test/i } } ] } )
>
> that is manually rewriting into
> someCollection select: {
>        '$or' -> {
>                { 'name' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary.
>                { 'description' -> {
>                        '$regex' -> 'test' .
>                        '$options' -> 'i' } asDictionary } asDictionary
>        }
> } asDictionary
>
> But I need to have a parser that will do it automatically.
>
> Maybe there are already exist one? If not, how to write my own?
> Please help.
>
> Thanks a lot, Khrystyna=)
>
>
>
> --
> View this message in context: http://forum.world.st/Parser-for-javascript-mongodb-json-notation-selects-tp4942299.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

--
www.tudorgirba.com
www.feenk.com

"Reasonable is what we are accustomed with."




--
www.tudorgirba.com
www.feenk.com

"Every thing should have the right to be different."






Reply | Threaded
Open this post in threaded view
|

Re: Parser for javascript mongodb (json) notation selects

Tudor Girba-2
Hi,

Take a look at the SmaCCParseNode to see the API to traverse the resulting AST.

Furthermore, you can also use the query engine that SmaCC ships with. In your case, you seem to be interested only in the one argument of the find call. So, you can do this:

ast := JSParser parse: 'db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {description: { $regex: /test/i } } ] } )'.
pattern := JSParser parse: '`something`.find(`arg1`)' startingAt: JSParser startingStateForExpression.
matches := OrderedCollection new.
ast withAllNodesDo: [:each |
                mapping := Dictionary new.
                (pattern match: each inContext: mapping)
                        ifTrue: [
                                matches add: (each -> (mapping associations collect: [:assoc | assoc key name -> assoc value]) asDictionary) ] ].
matches.

The “matches” variable will contain all matches of all find arguments. In our case, you can do:
        (matches anyOne value at: 'arg1’)
and this will give you the literal node with the argument from your script:
        {$or: [ { name: { $regex: /test/i } }, {description: { $regex: /test/i } } ] }

Next, you would need to parse the contents of the source as a JSON document. For example, the NeoJSONReader would be able to get the dictionary you need like this:
        NeoJSONReader fromString: '{"or": [ { "name": { "regex": "/test/i" } }, {"description": { "regex": "/test/i" } } ] }’
Note that the sources contain “”, so this means that you would need to traverse the original source through a visitor and print it so that you can parse it with a JSON parser. This is a piece of code you should write.

Once you get the dictionary, you can use the storeString:

(NeoJSONReader fromString: '{"or": [ { "name": { "regex": "/test/i" } }, {"description": { "regex": "/test/i" } } ] }') storeString
==>
((Dictionary new) add: ('or'->((Array new: 2) at: 1 put: ((Dictionary new) add: ('name'->((Dictionary new) add: ('regex'->'/test/i'); yourself)); yourself); at: 2 put: ((Dictionary new) add: ('description'->((Dictionary new) add: ('regex'->'/test/i'); yourself)); yourself); yourself)); yourself)

I hope this helps.

Cheers,
Doru


> On Apr 16, 2017, at 10:17 PM, Христина Михайлюк <[hidden email]> wrote:
>
> How to iterate over it result parse tree?
>
> 2017-04-16 22:44 GMT+03:00 Христина Михайлюк <[hidden email]>:
> That's so cool plugin. Thank you very very much!
>
> But I'm a bit confused how to transform that tree into needed code f.e.
> into  
> someCollection select: {
>         '$or' -> {
>                 { 'name' -> {
>                         '$regex' -> 'test' .
>                         '$options' -> 'i' } asDictionary } asDictionary.
>                 { 'description' -> {
>                         '$regex' -> 'test' .
>                         '$options' -> 'i' } asDictionary } asDictionary
>         }
> } asDictionary
>
> Could you help me, please?
>
> 2017-04-16 21:41 GMT+03:00 Tudor Girba <[hidden email]>:
> Hi,
>
> Here is how to load it in Pharo 6:
>  Metacello new
> smalltalkhubUser: 'JohnBrant' project: 'SmaCC';
> configuration: 'SmaCC';
> version: #'development';
> load
>
> Then you can do:
> JSParser parse: 'db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {description: { $regex: /test/i } } ] } )’
>
> And you get the AST that you can play with (and transform into what you want):
>
> <Playground-JS.png>
>
> Cheers,
> Doru
>
>
>> On Apr 16, 2017, at 8:06 PM, Христина Михайлюк <[hidden email]> wrote:
>>
>> Thank you very much!
>> But could you help me, please, to setup SmaCC into my Pharo?
>>
>> Your's faithfully,
>> Khrystyna Mykhailiuk
>>
>> 2017-04-16 20:57 GMT+03:00 Tudor Girba <[hidden email]>:
>> Hi,
>>
>> SmaCC ships with a JS parser. Please take a look at it.
>>
>> Cheers,
>> Doru
>>
>>
>> > On Apr 16, 2017, at 3:42 PM, chrismihaylyk <[hidden email]> wrote:
>> >
>> > Hello, friends!
>> >
>> > I'm creating MongoDBBrowser that have all CRUD operations and I need a
>> > parser that translates javascript queries to MongoDB into the queries (for
>> > making selects, inserts etc.) using MongoQuery class.
>> > For example, I have query
>> >
>> > db.SomeCollection.find( {$or: [ { name: { $regex: /test/i } }, {
>> > description: { $regex: /test/i } } ] } )
>> >
>> > that is manually rewriting into
>> > someCollection select: {
>> >        '$or' -> {
>> >                { 'name' -> {
>> >                        '$regex' -> 'test' .
>> >                        '$options' -> 'i' } asDictionary } asDictionary.
>> >                { 'description' -> {
>> >                        '$regex' -> 'test' .
>> >                        '$options' -> 'i' } asDictionary } asDictionary
>> >        }
>> > } asDictionary
>> >
>> > But I need to have a parser that will do it automatically.
>> >
>> > Maybe there are already exist one? If not, how to write my own?
>> > Please help.
>> >
>> > Thanks a lot, Khrystyna=)
>> >
>> >
>> >
>> > --
>> > View this message in context: http://forum.world.st/Parser-for-javascript-mongodb-json-notation-selects-tp4942299.html
>> > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>> >
>>
>> --
>> www.tudorgirba.com
>> www.feenk.com
>>
>> "Reasonable is what we are accustomed with."
>>
>>
>>
>
> --
> www.tudorgirba.com
> www.feenk.com
>
> "Every thing should have the right to be different."
>
>
>
>
>
>

--
www.tudorgirba.com
www.feenk.com

"We can create beautiful models in a vacuum.
But, to get them effective we have to deal with the inconvenience of reality."