question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ES6 class support

See original GitHub issue

Would be great to support Sequelize models as ES6 classes.

Issues:

  • No static properties in ES6 classes
  • Models need to be init’ed to perform normalization etc
  • Models need a way to be connected to an active sequelize instance
  • Would break instanceof Sequelize.Model, so we might need an interface check inself.

Should instances be directly initiated from a model via new User()? Would be more semantic.

// models/user.js
class User extends Sequelize.Model {
  static attributes() {
    return [
      // ...
    ];
  }

  // Class methods
  static findByEmail(email) {
    return this.findOne();
  }

  // Instance methods
  verifyPassword(password) {

  }
}

// models/index.js
import User from './user';
import sequelize from './sequelize-instance';

// Need some way to attach the model to a sequelize instance, specifically while allowing multiple attachments. Also need to run an sequelize init script on the model to normalize attributes etc.
User = sequelize.import(User); // ?

Internally this need to be implemented for how extends is done under the hood in babel etc, so prototype inheritance.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:26
  • Comments:23 (17 by maintainers)

github_iconTop GitHub Comments

3reactions
intellixcommented, Mar 26, 2016

There were a few people talking about using Decorators in another thread. Just wanted to add that I think Decorators here would be awesome. If you want an idea of how it would look then checkout this repository: https://github.com/martinmcwhorter/modelmetadata

It provides validation decorators for properties of a Class (Required, Pattern, Min, Max etc) and then has adapters for exporting the meta data into Angular2 FormBuilder instances, MongooseSchemas etc. Think it’s brilliant and would really like to use Interfaces/Types.

Doctrine uses annotations for decorating PHP Classes and am loving it. Random example of Class Table Inheritance:

@Model({
  underscored: true,
  timestamps: true
})
abstract class GameEvent {

    @Column({
        type: SequelizeDate,
        field: 'start_time',
        required: true
    })
    startTime: Date;

    @Column({
        type: SequelizeDate,
        field: 'end_time',
        required: true
    })
    endTime: Date;

    construct() {
        this.startTime = new Date;
    }
}

@Model()
class Movement extends GameEvent {

    @HasOne({
        required: true
    })
    mapSquare: MapSquare;
}

Decorator spec: https://github.com/wycats/javascript-decorators/blob/master/README.md Babel plugin to parse them: https://babeljs.io/docs/plugins/syntax-decorators/

0reactions
RobinBuschmanncommented, Mar 21, 2017

This is all very theoretical, since there is no final specification of how es-modules should be loaded. But all articles I’ve found (not official: altereos.com, medium.com, …) saying it would be possible to do it like that.

At least I mostly believe in Guy Bedfords system.js. Since its es-module-loader implementation tries to follow the whatwg specification as much as possible. When assuming that loading es modules will work like this

(function(System, SystemJS) {System.register(['./b'], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var b_1;
    function a() {
        return b_1.b();
    }
    exports_1("a", a);
    function a1() {
        return 'a1';
    }
    exports_1("a1", a1);
    return {
        setters:[
            function (b_1_1) {
                b_1 = b_1_1;
            }],
        execute: function() {
        }
    }
});

})(System, System);

…the () => Model approach will work as well.

I do understand, what you’re trying to say. But it isn’t said, that import/export will work internally like this:

const { A } = require('./a')
__decorate(class B {}, {
  'a': HasMany(() => A)
})

Or do you have some proof in the spec for that?

Nevertheless, decorators are experimental as well. So I see no conclusion unless a specification for both is finalized 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6 classes | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
Classes - JavaScript - MDN Web Docs
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on...
Read more >
Browser support for class syntax in Javascript - Stack Overflow
You can use a javascript to javascript compiler such as Babel to compile ES6 javascript into ES5 code. It covers most of the...
Read more >
Browser Compatibility Testing of JAVASCRIPT with ES6 classes
BROWSER SUPPORT FOR JAVASCRIPT ES6 classes · Google Chrome · Mozilla Firefox · Internet Explorer · Safari · Microsoft Edge · Opera.
Read more >
Classes (ES6) - Chrome Platform Status
Language support for classes. ... Demos and samples. https://github.com/GoogleChrome/samples/tree/gh-pages/classes-es6 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found