Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement interpreter for mikro-orm #5

Closed
7 tasks
stalniy opened this issue Jul 7, 2020 · 19 comments
Closed
7 tasks

Implement interpreter for mikro-orm #5

stalniy opened this issue Jul 7, 2020 · 19 comments
Labels
wontfix This will not be worked on

Comments

@stalniy
Copy link
Owner

stalniy commented Jul 7, 2020

  • this interpreter should be part of a new package @ucast/mikro-orm (put it in packages/mikro-orm)
  • it should support the same operators as js interpreter from @ucast/js (the most complex is $elemMatch)
  • it should allow to pass custom operators
  • it should use createInterpreter function from @ucast/core to create interpret function
  • the return value of interpret function is a query builder
  • it should take initial query builder as an argument
  • when it finds condition.field with dots inside it's a hint that this property may be a relation to another table. Using mikro-orm API we should be able to understand and if a field construct query for JSON field and if it's a relation add INNER JOIN + where conditions
@stalniy stalniy added the help wanted Extra attention is needed label Jul 8, 2020
@ujwal-setlur
Copy link

I can take a stab at this.

@stalniy
Copy link
Owner Author

stalniy commented Jul 8, 2020

Awesome! Let me know if you have any questions!

@stalniy
Copy link
Owner Author

stalniy commented Jul 13, 2020

Hi @ujwal-setlur

Do you have any news on the progress?

@ujwal-setlur
Copy link

ujwal-setlur commented Jul 13, 2020 via email

@stalniy
Copy link
Owner Author

stalniy commented Jul 30, 2020

@ujwal-setlur , do you have free time for this task now?

@stalniy
Copy link
Owner Author

stalniy commented Jul 30, 2020

Interpreter for Objectionjs was finished and it may be a very good reference implementation for mikro orm

@Asoftyn
Copy link

Asoftyn commented Aug 11, 2020

Hi, I wonder if there is any progress on this?
And if I would try to do this, where would I start so I could get a better understanding of ucast? Since I am a little confused about what it does right now.

@stalniy
Copy link
Owner Author

stalniy commented Aug 11, 2020

Hi @Asoftyn

No any progress on this. Your help would be very useful!

@stalniy
Copy link
Owner Author

stalniy commented Aug 11, 2020

The best way to start is to read this https://github.com/stalniy/ucast/tree/master/packages/core

UCAST is a universal conditions translator. It allows to translate conditions from one language to another. More formally, there are 3 things:

  • Parser. parses language which we want to translate into intermediate language (i.e., object structure called AST)
  • Intermediate language (AST). Recursive composition of objects that represent conditions
  • Interpreter. Function that interprets intermediate language (AST) in some way.

What does this allow?

@ucast/mongo contains a parser that translates MongoDB into AST. Now, we need an interpreter that can convert this AST into mikro-orm query builder

@ujwal-setlur
Copy link

Sorry folks, I got extremely busy on my day job. Any help would be appreciated. I hope to get a break at work in a couple of weeks

@stalniy
Copy link
Owner Author

stalniy commented Aug 11, 2020

This may sound confusing and complicated but in fact, interpreter is a simple set of functions. Take a look at @ucast/objection implementation (https://github.com/stalniy/ucast/blob/master/packages/objection/src/interpreters.ts#L11) which is similar thing for Objection.js ORM.

Let me know if you have further questions

@Asoftyn
Copy link

Asoftyn commented Aug 11, 2020

I had a look around and from what I understand is that mikro-orm has support for mysql, postgresql, sqlite and mongo and is using knex.
So I was thinking that an mongodb interpreter already exists and so does one for the others using objection.js with knex. So wouldn't the interpreter part already be kinda done? Could we Frankenstein something together using code from those interpreters for the mikro-orm interpreter?

Sorry if I sound stupid I'm not used to these kinda things.

@stalniy
Copy link
Owner Author

stalniy commented Aug 11, 2020

I thought about similar possibilities, this requires implementation of Adapter pattern that will unify interfaces of different query builders. As a result this adds additional level of abstraction, basically another query builder interface. The issue is that for some edge cases this abstraction won't work well:

  • to generate raw queries. Different ORMs may use even different binding parameters.
  • to wrap SQL expressions in brackets. Some supports nested query builder and others - not. So, this will make implementation of Adapter pattern for some ORMs more complicated than it should be. For example, sequelize uses objects to construct query. So, for example to implement and operator, we will need to create an instance of nested query builder, instead of merging conditions using regular Array.

Being able to implement operators for particular ORM may allow to simplify and optimize implementation for particular situation.

I'd like to start with this approach at least for now, and find out/refactor as we go

@Asoftyn
Copy link

Asoftyn commented Aug 12, 2020

I have been trying and looking around at the code for a few hours but i cant get my head around it. So I sadly wont be of any use since i do not quite understand it.
I wish you all good luck with this.

@stalniy
Copy link
Owner Author

stalniy commented Aug 14, 2020

Guys, actually yesterday I tried to implement interpreter for pure SQL. and have pretty good results. Tested it with objectionjs and it works really good.

today, I plan to try it with mikro orm and sequelize and if it works good I’ll publish it and close this issue as won’t fix

@Asoftyn
Copy link

Asoftyn commented Aug 14, 2020

Im not sure i understand the "as won't fix" part but good luck.

@ujwal-setlur
Copy link

I guess it means it’s generic enough to cover all the SQL ORMs?

@stalniy
Copy link
Owner Author

stalniy commented Aug 14, 2020

right, I believe that all ORMs in one or another way allow to combine raw sql with their query builder interface, this allows to implement conversion to SQL and use this SQL to set conditions.

@stalniy stalniy added wontfix This will not be worked on and removed help wanted Extra attention is needed labels Aug 14, 2020
@stalniy
Copy link
Owner Author

stalniy commented Aug 14, 2020

close in favor of #8

@stalniy stalniy closed this as completed Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants