loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Goodbye Static Methods, Static Settings, Etc

Open ritch opened this issue 11 years ago • 9 comments

I'd like to create a new ORM that works significantly differently than the juggler. The basic building blocks would be:

var json = require('../models/customer.json');
var schema = new Schema(json.properties, json.relations);
var config = new Config(json); // ignores properties, relations, etc

// config should include `connector` and connector settings
config.connector = {name: 'mysql', host: '...', ...}; 

// a vanilla JS class would be required
function Customer() {
  // default values go here
  this.created = new Date();
  // this would be a plain object without any setters / getters unless you use them
  // also no more _data
}

// all prior Static methods would be available on the collection instance
var customers = new Collection(Customer, schema, config);
var customer = new Customer();
customer.name = 'foo bar';


// validation would be completely separate from the Collection methods
var validator = new Validator(customer, require('./my-validation-decl.json'));

if(validator.isValid()) {
  var options = {}; // database specific options
  // promises should be supported from the beginning
  var promise = customers.insert(customer, options);
}

What is tricky (and open for discussion) is using this new ORM style with existing loopback apps (BC). Eg. strong remoting would have to change significantly to support this.

@raymondfeng @bajtos @fabien

ritch avatar Apr 01 '15 18:04 ritch

One idea is that we could support both ORMs in 3.0 and eventually drop support for juggler in 4.0.

ritch avatar Apr 01 '15 19:04 ritch

Would there be datasource specific stuff that it would allow to do that juggler doesnt? (i.e. join)

seriousben avatar Apr 01 '15 19:04 seriousben

Would there be datasource specific stuff that it would allow to do that juggler doesnt? (i.e. join)

Probably not... unless we want to rewrite a bunch of connectors

ritch avatar Apr 01 '15 20:04 ritch

+1 for using vanilla JS classes (ES6/coffescript classes), I've been pondering the same idea since our F2F.

The tricky part is how to expose customers via strong-remoting and allow the Customer class to provide remoting metadata.

What's even more interesting: how to enable connectors like loopback-connector-rest and loopback-connector-soap to 1) add custom methods to your model class 2) expose these custom methods via strong-remoting.

bajtos avatar Apr 02 '15 06:04 bajtos

I've been pondering the same idea since our F2F

Haha... I've literally been thinking about this for years. Its easy to do outside of loopback. The tough bits are making them work together in a BC way or with a practical migration path.

ritch avatar Apr 03 '15 18:04 ritch

don't forget that we've been able to claim some kind of broader compatibility with JugglingDB adapters, it's a provocative idea for sure, but do we have limitations or feedback that begs the question of needing to reinvent this?

altsang avatar Apr 06 '15 23:04 altsang

do we have limitations or feedback that begs the question of needing to reinvent this?

Coffee-script users (e.g. GoDaddy, cc @shelbys) would like to write their models as coffee-script classes. I'm sure ES6 users will want the same once ES6 classes are more widely available.

For the completeness, I think it's possible to satisfy coffee-script users even with the current implementation, though the solution is a bit hacky:

class CarImpl
  honk -> console.log('Making noise like ' + this.model)

module.exports = Car ->
  // this copies all static methods and properties to Car ctor,
  // and prototype methods and properties to Car prototype
  mixClassIntoModel(CarImpl, Car)

bajtos avatar Apr 07 '15 17:04 bajtos

We've resorted to moving most of the logic out of the Models anyway. We're headed to Service classes that can be reused as simple JS functions, and then the Models only contain the calls to remoteMethod(), and a CONSTANT for the options for each of those, so they're just the glue from REST Request to JS Function.

The more annoying thing is the async wiring during startup causing delayed wiring between Models, but that's probably just a REST/SOAP thing that doesn't happen in CRUD so often

shelbys avatar Apr 21 '15 03:04 shelbys

Removing the support label based on the comment above (https://github.com/strongloop/loopback/issues/1267#issuecomment-94629775)

bajtos avatar Dec 10 '15 19:12 bajtos