thinky icon indicating copy to clipboard operation
thinky copied to clipboard

Creating a model using the modern `class` syntax

Open demux opened this issue 9 years ago • 7 comments
trafficstars

I've added a method called createModelFromClass to my branch

This does not change the inner workings of thinky but rather takes a class and constructs a model from it.

If there is interest, I can create some unittests, write documentation and submit a pull request. Let me know.

What could also be nice, would be to create hook decorators, for example:

@createModelFromClass
class MyModel {
  static schema = {
    ...
  };

  static index = ['id', ...];

  @hook('preSave')
  doSomeStuff() {
    // Change the data before saving
  }
}

demux avatar Jul 18 '16 02:07 demux

I'm not super familiar with this syntax. Is there a benefit beside the different syntax?

I'm a bit wary of adding it as I'm not super familiar with this syntax and that it's more work to maintain.

neumino avatar Aug 15 '16 21:08 neumino

The syntax has a lot to offer, but one major benefit, in my opinion, is getters and setters:

@createModelFromClass
class User {
  ...
  get name() {
    return [this.firstName, this.lastName].filter((x) => x && x.length).join(' ')
  }

  set name(val) {
    [this.firstName, this.lastName] = val.split(' ')
  }
}

const user = new User({
  firstName: 'Fluffy',
  lastName: 'Bunny'
})

myModel.name
// -> Fluffy Bunny

You could, of course, also add defineProperty and defineStaticProperty.

But the main reason I created this is that this cleaner syntax is just much easier to comprehend for me.

I avoided making changes to existing code in order to make this easier to integrate and maintain. If you're still concerned about the maintenance, I'd be open to creating another project for this, but I'd still need you to export the actual Thinky class rather than just a function that creates a new instance of Thinky. And I need the _docInit event or something to that effect.

demux avatar Aug 21 '16 20:08 demux

@demux It's an interesting feature, and I have been thinking about the same thing, an ORM that creates models based on pure ES6 classes. But how would you handle type definitions ? From my understanding, Thinky requires each field to have a type, in order to perform validations.

WaldoJeffers avatar Aug 24 '16 18:08 WaldoJeffers

@WaldoJeffers Yeah, just define the schema as a static variable on the class. I've already implemented this on my forked version of thinky. It just needs unittesting.

demux avatar Aug 24 '16 19:08 demux

Ah ! I see. I'll give it a try

WaldoJeffers avatar Aug 24 '16 19:08 WaldoJeffers

If the last commit doesn't work, you can use the one before it. I'm using that one in production.

On Wed, Aug 24, 2016, 19:59 WaldoJeffers [email protected] wrote:

Ah ! I see. I'll give it a try

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/neumino/thinky/issues/532#issuecomment-242189009, or mute the thread https://github.com/notifications/unsubscribe-auth/AAMWwCOzSPRkTdAlEcoZrBgGM_nIKY1xks5qjKKMgaJpZM4JOYGv .

demux avatar Aug 24 '16 20:08 demux

decorator syntax is slightly brand new feature adopted at es7 (like python syntax). However, I think that new syntax of es6,7 should be used initiatively.

yoshiya0503 avatar Dec 29 '16 13:12 yoshiya0503