strongloop / loopback

LoopBack makes it easy to build modern applications that require complex integrations.
http://loopback.io
Other
13.23k stars 1.2k forks source link

Replace the built-in ORM with another package #3407

Closed jeff3yan closed 7 years ago

jeff3yan commented 7 years ago

I've been working around the limitations for loopback when attempting to query based on related model properties (e.g. Customer belongsTo City belongsTo Country, then trying to query all customers in "Portland").

This issue has been brought up in #517, #683 and many others. However, I'm not sure what the status is on any recommended workarounds besides writing native SQL.

To that point it would be convenient if I could replace the loopback ORM with another package (maybe objection.js or something backed by knex.js) which can filter on related properties.

There was this question relating to providing the loopback datasource connection to use in knex, but it didn't get anywhere.

Is there any way I could incrementally remove the reliance on the built-in ORM and replace it with something else?

bajtos commented 7 years ago

My recommendation is to use plain models (based on Model, not PersistedModel) as controllers, and then implement any data-access methods as custom model (controller) methods backed by whatever ORM you prefer.

// common/models/CustomerController.json
{
  "name": "CustomerController",
  "base": "Model",
  "methods": {
    "find": {
      "accepts": // ...
      "returns": // ...
    }
  }
}

// common/models/CustomerController.js
module.exports = function(CustomerController) {
  CustomerController.find = function(...) {
    return knex(...);
  };
}

We are working on making Controllers a first-class concept in https://github.com/strongloop/loopback-next.