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

Why is required connectors? #6773

Closed
mdbetancourt opened this issue Nov 11, 2020 · 4 comments
Closed

Why is required connectors? #6773

mdbetancourt opened this issue Nov 11, 2020 · 4 comments
Labels
Repository Issues related to @loopback/repository package

Comments

@mdbetancourt
Copy link
Contributor

Why we need a Connector or datasource? is not enough with just repositories? there's some way to build connector at the loopback 4 way? or connectors are just legacy code from lb3?

@achrinza
Copy link
Member

achrinza commented Nov 13, 2020

A datasource...

  • defines the connector and the connector-specific configuration to create a database or service connection.
  • defines a set of common functions that is used by repositories (e.g. CRUD)
  • is a lift-and-shift form LoopBack 3

A repository...

  • defines a set of APIs to be used by the LoopBack 4 application (e.g. CRUD)
  • does not posses connector-specific information (e.g. how to establish a connection, generate the query)
  • works at a higher abstraction level thereby...
    • allowing for cross-database relations
    • allowing for custom functions

Though datasources are concepts from LoopBack 3, there are no plans to deprecate the concept.

Somewhat related - There were some early discussions on replacing Juggler (and in turn, DefaultCrudRepository) with CrudRepositoryImpl (see: #889 (comment)), though AFAIK it's low-priority as it's mandates putting significant resources on non-trivial tasks such as rewriting the connectors.

@achrinza achrinza added the Repository Issues related to @loopback/repository package label Nov 13, 2020
@mdbetancourt
Copy link
Contributor Author

and how could i write a connector for loopback 4? i can't find anything about it in documentation, ok i understood repositories and connectors are different things but service can replace a connector?

@achrinza
Copy link
Member

achrinza commented Nov 14, 2020

We haven't ported the documentation to LoopBack 4, but AFAIK, the LoopBack 3 docs still apply as Juggler itself (the LB3/LB4 ORM) hasn't changed much.

The docs demonstrate with prototypes, but classes should work fine too.

Some things to note:

  1. "Compound" interfaces such as findOrCreate aren't exposed in LB4 repositories directly; though I believe that's beneficial as there's no contract to guarantee atomicity. Taking from the same docs (emphasis mine):

    Yes, loopback-datasource-juggler will handle updateOrCreate and findOrCreate if they are not provided by the connector. However, I am strongly recommending to implement these methods, because only the connector can provide a version that guarantees atomicity.

  2. AFAIK, Weak Relations in LB4 are handled at the repository-level instead of the connectors. This is different from LB3 where Juggler is relation-aware.

There's also some brief LoopBack 4 docs on using CrudRepositoryImpl to bypass Juggler. AFAIK, this is more of a PoC demonstration for how CrudRepositoryImpl may be used when we migrate to it. The main benefit of this is first-class TypeScript support and more rigid interfaces, hence less "blackbox behaviours", but IIRC, basic things such as Relations hasn't been ported over yet.

Services are JavaScript classes with any arbitrary properties with no arbitrary restrictions or interfaces. Hence, anything can be classes. This is where Connectors differ: Connectors have the benefit of built-in connection lifecycle management and a set of well-defined interfaces such as migration and discovery (which the lb4 CLI uses).

@achrinza
Copy link
Member

Hi @mdbetancourt, I'll proceed to close this issue as solved due to inactivity. Feel free to comment if you believe that this is not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Repository Issues related to @loopback/repository package
Projects
None yet
Development

No branches or pull requests

2 participants