Skip to content

ES6 in Sequelize codebase #3951

Closed
Closed

Description

@overlookmotel

@mickhansen recently raised the possibility of using babel to allow Sequelize to be written using ES6 features while retaining compatibility with versions of node earlier than v0.12.

@janmeier and @mbroadst were in favour, @BridgeAR has reservations.

The original discussion is here: https://github.com/sequelize/sequelize/pull/3929/files#r32521460

I thought it worthwhile to give this its own issue for discussion as it's a big topic.

Activity

  1. overlookmotel commented on Jun 17, 2015

    @overlookmotel
    ContributorAuthor

    My opinion is:

    I'm writing node apps in ES6. Using generators, co and yield makes it sooooo much simpler to write async code. No more endless .then() promise chains...

    But I'm running these apps in node v0.12 with --harmony so the ES6 features are native.

    I'd be broadly in favour of transitioning Sequelize to ES6, because the benefits in terms of writing shorter, more comprehensible code are so large.

    However, what are the implications for debugging? If the source code goes through a babel compilation, does that make it hard to locate the source of a bug, if a thrown error references a line number which doesn't correspond to the right line in the source code?

  2. overlookmotel commented on Jun 17, 2015

    @overlookmotel
    ContributorAuthor

    I'd suggest if you wanted to go this way, to restrict ES6 features used to those available natively in node v0.12 with --harmony, so it can run without compiling on node v0.12. Babel compilation could be switched on only for older node versions.

  3. added
    status: in discussion
    on Jun 23, 2015
  4. mickhansen commented on Jun 23, 2015

    @mickhansen
    Contributor

    @overlookmotel The beautiful dynamic object keys and structural deassignments aren't in harmony though :/

  5. mbroadst commented on Jun 23, 2015

    @mbroadst
    Contributor

    IMHO I think the suggestion to just use harmony is sort of dead in the water since you'd basically be telling users that sequelize no longer works with 0.10.x and if they want to use it with 0.12.x they have to run with --harmony: no thanks.

    I think babel is your only choice if you want to go down this path, it provides a nice platform for which to support all the things you need to support here while still looking forward.

  6. overlookmotel commented on Jun 24, 2015

    @overlookmotel
    ContributorAuthor

    @mbroadst My suggestion was that Sequelize would work natively on 0.12.x with --harmony, but that when run on 0.10.x it would be babelized. So it'd run on both platforms, but with no need for babelization if you're on node 0.12.x.

    But in any case, I didn't realize that as @mickhansen said some of the best ES6 features aren't available in 0.12.x harmony. So my whole idea is dead in the water for that reason!

    My question remains though: What are the implications for debugging babelized code? Presumably the cross-compile changes line numbers, so when looking at an error stack trace, it could be hard to follow what's going on - the stack trace wouldn't correspond to the source code. This would be a deal-breaker for me as it could make Sequelize unmaintainable.

    That said, I haven't tried babel, so there may be solution for this. Anybody know?

  7. mickhansen commented on Jun 24, 2015

    @mickhansen
    Contributor

    It would break stack traces, i'm not sure if node.js has great sourcemap support, likely not.

  8. overlookmotel commented on Jun 24, 2015

    @overlookmotel
    ContributorAuthor

    I'd imagine it'd be possible for babel to be written so that line numbers are preserved (by making very long lines in some cases!), but I don't know if it does this.

  9. mbroadst commented on Jun 24, 2015

    @mbroadst
    Contributor

    https://github.com/evanw/node-source-map-support ? This was literally just a google search, I'm putting minimal effort into furthering this case ;) just want to see where it goes!

  10. SpainTrain commented on Jul 14, 2015

    @SpainTrain

    $0.02 💰

    In my experience using sourcemaps in node (which does not include a project as large as Sequelify), the source maps support using the module linked by @mbroadst has been fine for any debugging I've needed to do.

    The tech for ES6 dev is pretty good, so if I were maintaining a popular ES5 project, I would probably worry most about how it affects the core contributors and community. A few examples:

    • What do maintainers spend most of their time on? Would the improvement in productivity be worth the investment in learning and creating the tooling to use ES6?
    • How important are PRs from non-core contributors? What is the culture of the community? Would using ES6 in the project reduce the number of PRs and negatively affect the project?
    • Fork vs. evolve - What are the trade offs of evolving the current project rather than creating a new ground-up ES6 porject (a la koa)?

    I love writing in ES6, but for a mature OSS project, I suspect the considerations that have the most gravity are the non-technical ones.

    Related: #3840

  11. cusspvz commented on Sep 15, 2015

    @cusspvz

    In my opinion all node LTS versions should be supported.

    Regarding to 0.10 and 0.12 versions support I agree with @mbroadst , babel register seems the only reasonable choice.

  12. xpepermint commented on Sep 20, 2015

    @xpepermint

    +1

  13. overlookmotel commented on Sep 21, 2015

    @overlookmotel
    ContributorAuthor

    Now that Node v4 is released and so there are two versions of node (v.0.12 with harmony flag and v4) which support some ES6 features, I think it's worthwhile looking at this again.

    @mickhansen has mentioned dynamic object keys and destructuring assignment, but for me the killer feature of ES6 is being able to use co and generators. This makes it so much easier to write async code with is both faster to write and much more comprehensible. I've started using it everywhere and it's revolutionised my coding.

    Example code in current Promises style:

    var create = function(options) {
      var self = this;
      return Promise.try(function() {
        if (options.hooks) return self.runHooks('beforeCreate', options);
      }).then(function() {
        return self.runQuery( /* ... etc ... */ );
      }).tap(function() {
        if (options.hooks) return self.runHooks('afterCreate', options);
      }).then(function(instance) {
        return instance;
      });
    };

    With co...

    var create = co.wrap(function*(options) {
      if (options.hooks) yield self.runHooks('beforeCreate', options);
      var instance = yield self.runQuery( /* ... etc ... */ );
      if (options.hooks) yield self.runHooks('afterCreate', options);
      return instance;
    });

    This is available natively in node v0.12 with --harmony flag without any transpiling. And also in node v4. To continue support for node v0.10 (which I think Sequelize should), it would require babel.

    I see 3 options:

    1. Keep everything as is, natively supporting node v0.10 onwards
    2. Restrict Sequelize code to only what is natively provided in node v0.12 with --harmony, and transpile with babel for node v0.10 (which would allow for co and generators)
    3. Go the whole hog, and transpile with babel for all versions of node, to allow all ES6 features (and also async/await from ES7)

    Personally, I think option (2) is a good middle way allowing users of modern node to run Sequelize code natively, while still supporting node v0.10. Extra language features could also be brought into use (e.g. destructuring assignment) as node v4 matures and these ES6 features become natively supported.

    But that's just my personal opinion...

  14. rickihastings commented on Nov 12, 2015

    @rickihastings

    Node 4 supports a lot of ES6 functionality now too, including native promises meaning a dependency on bluebird could be removed. This may come with a performance improvement too.

  15. 16 remaining items

  16. overlookmotel commented on Jan 13, 2016

    @overlookmotel
    ContributorAuthor

    Having switched to co-routines a while back, I really can't sing their praises enough. It makes code so much easier, faster and more pleasurable to write.

    I've found I produce less buggy code as you don't have to worry about all the code inside .then(function() { /* code here */ }) constructs having a different scope (and this context) than the next .then() block.

    It's a beautiful thing man!

  17. mickhansen commented on Jan 13, 2016

    @mickhansen
    Contributor

    I use them extensively in all my code aswell, i'm just not sure it would be responsible to switch our entire underlying library to use it yet :)

  18. janmeier commented on Jan 13, 2016

    @janmeier
    Member

    I'd hate having to switch away from CLS, but bluebird has built in support for domains - which also allow you to store values on them, and maintain callback context, see petkaantonov/bluebird#583

  19. mickhansen commented on Jan 13, 2016

    @mickhansen
    Contributor
  20. janmeier commented on Jan 13, 2016

    @janmeier
    Member

    So basically there is no universally supported method for callback context ...

    Edit: nodejs/node#66

  21. overlookmotel commented on Jan 13, 2016

    @overlookmotel
    ContributorAuthor

    @janmeier Well CLS should work, but bluebird author has rejected suggestions in the past to include CLS support. The shim of bluebird used in Sequelize to add CLS support is I suspect a little buggy, so that's what's meaning we don't have full bullet-proof CLS support.

    But, in theory, CLS is a universal method for callback context. Just got to iron out the bugs of integrating it with bluebird.

  22. verdier commented on Jan 14, 2016

    @verdier
    Contributor

    @overlookmotel @mickhansen there is absolutely no need to use promisify-any since Promise is the way async functions will work tomorrow. If you want to use generators with today Sequelize, you juste have to do something like that:

    beforeValidate: co.wrap(function *() { ... })

    and tomorrow (has said by @mickhansen):

    beforeValidate: async function() { ... }

    both of the solutions return a Promise. Even koa has drop generators support in favore of co.wrap or async used by the library consumer.

  23. MarkMurphy commented on Jan 15, 2016

    @MarkMurphy

    I'm surprised no one has mentioned async/await here.

    myAsyncTask().then(function (result) {
      return result.something()
    }).then(function (doc) {
      console.log(doc)
    }).catch(function (err) {
      console.log(err);
    });

    becomes:

    try {
      let result = await myAsyncTask()
      let doc = await result.something()
      console.log(doc);
    } catch (err) {
      console.log(err);
    }

    have a look here.

  24. mickhansen commented on Jan 15, 2016

    @mickhansen
    Contributor

    @MarkMurphy It has actually been mentioned :) We're not going with it internally since it would require bluebird-coroutines are we're not sure of the perf implications.

    However users are as always welcome to us it, and we might update docs to reflect it.

  25. overlookmotel commented on Jan 15, 2016

    @overlookmotel
    ContributorAuthor

    @verdier I just suggested using promisify-any as currently Sequelize hooks can be written either as classic node callback functions OR promise-returning functions. promisify-any takes care of that, so handling callback functions could be removed from the Sequelize codebase.

    The additional support for generator co-routines is an extra benefit.

  26. overlookmotel commented on Jan 15, 2016

    @overlookmotel
    ContributorAuthor

    @verdier There are also some died-in-the-wool callback devotees who like that style of programming and will probably never change. Seems bonkers to me, but different strokes for different folks... our vision of the future isn't necessarily shared by everyone.

  27. verdier commented on Jan 15, 2016

    @verdier
    Contributor

    Yep, @overlookmotel I agree!

  28. mickhansen commented on Jan 15, 2016

    @mickhansen
    Contributor

    I say we remove callback support though: #5228

  29. sushantdhiman commented on Jun 21, 2016

    @sushantdhiman
    Contributor

    Following is the summary for ES6 adoption in Sequelize codebase

    • Supporting only Node v4 and up for upcoming [email protected]
    • No Babel or TypeScript will be used. We want Sequelize to run directly after git clone without any transpilation overhead
    • Use of only default enabled ES6 feature from Node v4 , so we have template strings, Class and arrow functions
    • We may adopt co routines Prepare for coroutines #6116 , if CLS support is guaranteed

    Discussion moved to #5898

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changestatus: in discussion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions