-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[WIP - DO NOT MERGE] feat: PoC for typeorm repository integration #763
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
Conversation
3087849 to
5cef9a1
Compare
|
@raymondfeng Would we be able to see an acceptance test that covers off a more advanced use case involving filters and joins (INNER JOIN and LEFT JOIN)? I'm curious to see how the DSL would support nesting of filters with the query building. EDIT: I just realized the schema in use for the acceptance tests is a one-table schema, which prevents validating these use cases. |
| import {MysqlConnectionOptions} from 'typeorm/driver/mysql/MysqlConnectionOptions'; | ||
|
|
||
| import {Entity as Base} from '@loopback/repository'; | ||
| import {Entity, Column, PrimaryGeneratedColumn, Repository} from 'typeorm'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. So you are not leveraging decorators from @loopback/repository to define models. I think that makes @loopback/repository less useful - for example client SDK generators cannot rely on a single set of decorators describing model schemas. Not a big deal, just something to keep in mind and document. I think this also means that all juggler-related stuff should be moved out of @loopback/repository into a standalone package, e.g. @loopback/repository-juggler so that people wishing to use their own ORM don't have to install juggler dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed online, if we want a single Repository instance for consumers (REST API, GraphQL API, etc.), then we need also a single model schema. To get that, I think we need to map our decorators to TypeORM decorators, or provide a method for obtaining LDL definition for a TypeORM model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bajtos IMO I would like to have a 100% typeorm model, therefore I would expect to decorate my models using typeorm decorators.
As a platform developer I would like to have a pure typeorm implementation, not being wrapped or decorated by loopback.
I just would expect loopback to help me load the connection and automatically register my models for me.
|
|
||
| async update(entity: DataObject<T>, options?: Options): Promise<boolean> { | ||
| await this.init(); | ||
| await this.typeOrmRepo.updateById(entity.getId(), <DeepPartial<T>>entity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we should not need to change the type of entity here, we should change the API signature or DataObject<T> interface to support DeepPartial<T>.
| options?: Options, | ||
| ): Promise<AnyObject> { | ||
| throw new Error('Not implemented'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of execute method makes a lot of sense to me, I think this should be added to loopback-next in a standalone PR, including tests etc.
| where?: Where; | ||
| fields?: Fields; | ||
| order?: Order[]; | ||
| order?: string[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this change needed? Should we land it independently of this patch?
| * Order by direction | ||
| */ | ||
| export type Direction = 'ASC' | 'DESC'; | ||
| export type Direction = 'ASC' | 'DESC' | 1 | -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 1 and -1 compatible with our existing connectors? I think this is showing the problem with trying to implement a solution that supports all ORMs - juggler uses ASC | DESC, TypeORM uses 1 | -1, how are we going to design a type system that allows only the valid values, depending on what ORM is used under the hood?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we should support only ASC and DESC, and the TypeORM bridge should convert these values to whatever TypeORM uses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on not trying to get 1 solution that supports all ORMs, you will end with code really difficult to maintain and unnecessary footprint.
I still think TypeORM should be an extension component (not mixin, nor an integrated solution) and must not be coupled in any way to the juggler. I honestly won't build on top of the Juggler, nor the REST components, I expect to cleanly install TypeORM in LoopBack 4.
| class AppWithRepoMixin extends RepositoryMixin(Application) {} | ||
|
|
||
| class NoteRepo { | ||
| class NoteRepo implements Repository<any> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be Repository<Note>?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or perhaps Repository<Entity>?
5cef9a1 to
1c585f2
Compare
|
@jonathan-casarrubias thank you for joining the discussion!
We are actually thinking the same! See @kjdelisle's proof-of-concept showing an external component that allows people to use TypeORM in their LoopBack4 applications here: https://github.com/strongloop/loopback4-extension-typeorm @raymondfeng @kjdelisle I think this pull request can be closed now, what do you think? There are some bits of this patch that are fixing issues in our existing repository/juggler-integration code, I think it make sense to eventually land them. Any volunteers to extract them into a new pull request? |
|
@bajtos I saw that repo, I even created this issue strongloop-archive/loopback4-extension-typeorm#4 that is how I landed in here. I agree this should be an extension but as I explained before and also did in that ticket, I'm not keen on the mixin approach -is too basic-, look the issue and I describe the experience I would expect. I would expect a more robust component that can discover models and register these automatically, like I'm doing it with the proto files in the gRPC module. Once we get repository instances I would expect the component to expose these through providers, so developers would use these repositories using DI. My expectation would be the extension to be a black box that automatically executes the described above with a set of typed options. Agan I'm able to help on this one if needed :) |
|
Closing it now as we now have consensus around LB4 repositories based on this PoC. I also cherry-pick a few commits into #792. |
|
FYI: I might move it into my personal repo as an illustration of how to adapt other ORMs into LB repository based persistence. |
|
I actually think that is an excellent Idea, having that as an option sounds legitimate. |
Description
Related issues
Checklist