ES6 class support
See original GitHub issueWould 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:
- Created 8 years ago
- Reactions:26
- Comments:23 (17 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Decorator spec: https://github.com/wycats/javascript-decorators/blob/master/README.md Babel plugin to parse them: https://babeljs.io/docs/plugins/syntax-decorators/
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
…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:
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 😉