Skip to content

Conversation

@raymondfeng
Copy link
Contributor

@raymondfeng raymondfeng commented Nov 27, 2017

Description

Related issues

  • connect to <link_to_referenced_issue>

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide

@raymondfeng raymondfeng requested a review from bajtos as a code owner November 27, 2017 23:17
@raymondfeng raymondfeng force-pushed the repository-typeorm branch 13 times, most recently from 3087849 to 5cef9a1 Compare December 1, 2017 19:47
@kjdelisle
Copy link
Contributor

kjdelisle commented Dec 4, 2017

@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';
Copy link
Member

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.

Copy link
Member

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.

Copy link

@jonathan-casarrubias jonathan-casarrubias Dec 7, 2017

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);
Copy link
Member

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');
}
Copy link
Member

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[];
Copy link
Member

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;
Copy link
Member

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?

Copy link
Member

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.

Copy link

@jonathan-casarrubias jonathan-casarrubias Dec 7, 2017

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> {
Copy link
Member

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>?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps Repository<Entity>?

@bajtos
Copy link
Member

bajtos commented Dec 8, 2017

@jonathan-casarrubias thank you for joining the discussion!

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.
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.

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?

@jonathan-casarrubias
Copy link

jonathan-casarrubias commented Dec 8, 2017

@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 :)

@raymondfeng
Copy link
Contributor Author

Closing it now as we now have consensus around LB4 repositories based on this PoC.

I also cherry-pick a few commits into #792.

@raymondfeng
Copy link
Contributor Author

FYI: I might move it into my personal repo as an illustration of how to adapt other ORMs into LB repository based persistence.

@jonathan-casarrubias
Copy link

I actually think that is an excellent Idea, having that as an option sounds legitimate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants