Hi, currently I write ES6+ code and use Babel transform them.
Maybe we can include it and add an option for it.
Like this below:
# lib/gulpfile.js
"use strict";
require("babel/register");
var gulp = require("gulp");
var path = require("path");
var fs = require("fs");
var helpers = require(path.resolve(__dirname, "helpers"));
require("gulp-help")(gulp, {
aliases: ["h"],
afterPrintCallback: helpers.gulp.printManuals
});
...
If you like this, I can submit a PR.
While I don't really need ES6 for the CLI at this point, I wonder if the end-user will benefit from such a change?
I refactor my project and use ES6+ by Babel.
The database.js below and the migrations/*.js files will do that.
So I need babel to transform code before task execute.
export default {
"development": {
"url": process.env.DATABASE_URL,
"username": "trek",
"password": "star trek",
"database": "trek",
"host": "192.168.59.103",
"port": "5432",
"dialect": "postgres",
"native": true,
"timezone": "+00:00",
"query": {
"pool": true,
"debug": true
},
"define": {
"timestamps": true,
"underscored": true,
"charset": "utf8"
},
"logging": true
},
"test": {
"url": process.env.DATABASE_URL,
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"url": process.env.DATABASE_URL,
"username": process.env.DATABASE_USERNAME,
"password": process.env.DATABASE_PASSWORD,
"database": process.env.DATABASE_DATABASE,
"host": process.env.DATABASE_HOST,
"port": process.env.DATABASE_PORT,
"dialect": "postgres"
}
};
BTW, likes the --coffee flag.
+1
@sdepold Any progress on this? My projects are all es6 (babel). What do you suggest?
Btw... I think it would be enough if you just add require("babel/register"); into lib/gulpfile.js and bin/sequelize. I made a fork https://github.com/xpepermint/cli just to test that and it works but I think you should check if that's true.
+1
@sdepold there are a lot of benefits. Look at this:
async function up(query, Sequelize) {
await query.createTable('users', ...);
await query.createIndex('users', ...);
}
Simple advise: move your sequelize configuration info separated file, where you can require babel hook. That's how it looks like in my project:
// src/config/sequelize.js
require('babel-core/register');
const config = require('./index').database;
module.exports = {
development: config,
test: config,
production: config
};
Any progress on that? @sdepold?
I closed it.
@fundon You say that is works? The example below doesn't work and I still have to use my patch https://github.com/xpepermint/cli.
export default {
up(migration, Sequelize) {
return migration.createTable('Projects', {
id: {allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER},
name: {type: Sequelize.STRING}
});
},
down(migration, Sequelize) {
return migration.dropTable('Projects');
}
};
Ow... you mean this https://github.com/sequelize/cli/pull/113? Doesn't work. Can you please point me to the docs?
@xpepermint Sorry, I do not finish this PR.
Please re-open this issue. Thanks.
@xpepermint Could you have time to submit a PR for this?
Done https://github.com/sequelize/cli/pull/189. You can use https://github.com/xpepermint/cli fork for now.
@sdepold?
@xpepermint Okay, I'd like to review it.
+1
any progress on this one?
+1
would be really nice to have!
👍
I have a .babelrc file and would love if Sequelize CLI would enable Babel with those settings. Any update on this issue/PR?
To use babel for migrations, seeders and models I have found that running sequelize with babel works fine. If you run it from your project root it will also pickup your .babelrc file.
Steps to make this work:
$ npm install --save babel-cli sequelize-cli sequelize <all relevant babel transforms you want to use> <relevant db drivers>$ .node_modules/.bin/babel-node ./node_modules/.bin/sequelize init for example.For convienience I've added sequelize to my npm scripts, they now look like this:
{
...
"scripts": {
"start": "./node_modules/.bin/babel-node $* ./index.js",
"test": "./node_modules/.bin/_mocha --compilers js:./node_modules/babel-register $* ./test/index.js",
"sequelize": "./node_modules/.bin/babel-node ./node_modules/.bin/sequelize $*"
},
...
}
The $* makes sure the arguments following the command are placed at the correct position.
To make sure this actually worked I've added some import statements and ES6 to my .sequelizerc file and then ran an init with the following command: $ npm run sequelize -- init
+1
@AndreSteenveld Unfortunately this is not a cross platform solution, since it will not work on windows machines.
@AndreSteenveld Your solution worked 99% for me. When running database-dependent sequelize-cli commands, it demands that I use module.exports; everywhere else I am able to use es6 modules just fine
For those of us that would like to have the migration deps be packed into each migration file,... is there a way that could work? These need to run on a production box where deps are baked in.
Basically, the only reason at all that we want to do this is to reuse our App Model definitions for the init migration. So if there is even a better way to do that.... that would help.
@AndreSteenveld solution works and as @felipeochoa stated export didnt work for me either.
@AndreSteenveld i'm witnessing the following when attempting to use babel in seeder file:
~/g/foo (tk/mysql) $ ./node_modules/.bin/babel-node ./node_modules/.bin/sequelize db:seed --seed 20170509042805-users.js
Sequelize [Node: 7.10.0, CLI: 2.7.0, ORM: 3.30.4]
Loaded configuration file "sequelize/config.js".
Using environment "development".
== 20170509042805-users: migrating =======
Seed file failed with error: Unexpected token import /Users/tony/git/foo/sequelize/seeders/20170509042805-users.js:3
import {hash} from '../../src/shared/helper'
^^^^^^
SyntaxError: Unexpected token import
you have gotten this to work?
@sdepold Agreed that cli doesn't really need ES6 at this point but especially for seeds we definitely import models which are in ES6 in my case. That makes it a pain. Any resolution for that ?
@sladebot I spent the last two hours wrestling with this and eventually found a solution that isn't too bad.
After digging through the cli source, I realised that it executes your config file first. This is a good place to add a babel hook so subsequent requires are transpiled through babel. The only caveat I found is that babel register doesn't transpile the current file, so I made a quick hook.
.sequelizerc - no es6 here :(
const path = require('path');
module.exports = {
'config': path.resolve('migrations/config/babelHook.js'),
'migrations-path': path.resolve('migrations'),
'seeders-path': path.resolve('migrations/seeders'),
'models-path': path.resolve('migrations/models')
};
./migrations/config/babelhook.js - no es6 here :(
require('babel-core/register');
module.exports = require('./config');
./migrations/config/config.js - es6 to your hearts content :)
import config from '~/modules/config';
const env = process.env.NODE_ENV || 'development';
export default {
[env]: {
url: config.mysql.migrate,
dialect: 'mysql',
migrationStorageTableName: 'SequelizeMeta'
}
};
The above should allow you to use babel to transpile es6 in your migrations, models, seeders and config.
I second @n1ru4l @AndreSteenveld's idea doesn't work on Windows as we gotta add .cmd as the end of sequelize to make it run babel-node ./node_modules/.bin/sequelize.cmd $* but babel-node would try to read the cmd and as it is windows gibberish it doesn't understand...
None of the solutions in this thread work for me. How are you supposed to run Sequelize database migrations if your code is in ES6?
What's currently the best way to use the sequelize-cli with a project using es6? The models I generate and the index file are all in an older syntax, i think. I'm seeing 'require' not 'import' at the top of the files.
@Jadex1 @codyrobbins This should be enough to get you started.
https://gist.github.com/andrewmunro/030f0bf62453239c495b0347c8cd1247
This is how I have our project set up. The babel hook will transpile everything into ES5 so the sequelize cli is happy.
Let me know if you want an actual working example and I can knock together a repo.
@andrewmunro how does that work in production where there is no babel? It seems like this configuration is imported independently from the environment.
@aledalgrande I'd argue if you are including sequelize-cli in production, you would also include babel. Personally I wouldn't use either for production.
We don't use sequelize-cli in production, only for development. I'd suggest using something like umzug to manage your migrations programatically in production. sequelize-cli just aids us with testing etc.
If you want to use ES6+ with for you migrations and seeders. Do:
npm install babel-cli
then on your npm script, make sure that you run babel-node instead of node. Also make sure that you have a .babelrc file or any configuration that you use for babel. Make sure you have necessary plugins like babel-preset-env, babel-plugin-transform-object-rest-spread, and other things that you need.
Now you can do:
export default {
up: () => {
// do your stuff
},
down: () => {
// do your stuff
}
}
then to run it: node-babel node_modules/.bin/sequelize db:migrate.
run commands like this from package.json, scripts key:
"sequelize:drop": "babel-node node_modules/.bin/sequelize db:drop --env=development",
at the top of your config file
/* eslint-disable global-require */
// make dynamically loading sequelize imports possible (several limitations: no hot reloading, no aliases)
if (process.env.NODE_ENV === 'development') require('babel-register');
require('babel-polyfill');
Wanted to mention it here, I have opened up #651 for support for import and export statement support using the esm package.
This doesn't solve 100% of the Babel question, but it seemed like for most people (at least in more recent posts) the real issue was around modules rather than full code transpilation (since Node latest and even Node LTS support many of the ES2017+ syntax features natively).
There's still value in separately supporting Babel and full build/bundling support since there performance gains that can be made by bundling even Node code and I think this thread would live on even if/when ESM or something similar would be supported.
This is how you can use babel with sequelize-cli https://github.com/sequelize/sequelize/blob/master/docs/migrations.md#using-babel
These docs will be uploaded to our docs website once Sequelize v5 is launched, for now please utitlize direct repository link.
Babel register should not be recommended for production runtimes. It adds a
significant parse/transpile overhead. It also, transpiles all dependencies
in the dependency tree after requiring Babel Register: this introduces
possible runtime errors due to transpilation.
--
Ryan Tablada
On June 3, 2018 at 11:01:37 AM, Sushant ([email protected]) wrote:
Closed #112 https://github.com/sequelize/cli/issues/112.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/sequelize/cli/issues/112#event-1660054364, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACaipPCFZJTSvAw4BomN1b9hQ_509YSVks5t5AhhgaJpZM4D2A0B
.
@rtablada This will only load when you are actually running cli from command line, for that purpose this should be ok. .sequelizerc file will not be used when including models / sequelize instance in your application
Most helpful comment
To use babel for migrations, seeders and models I have found that running sequelize with babel works fine. If you run it from your project root it will also pickup your
.babelrcfile.Steps to make this work:
$ npm install --save babel-cli sequelize-cli sequelize <all relevant babel transforms you want to use> <relevant db drivers>$ .node_modules/.bin/babel-node ./node_modules/.bin/sequelize initfor example.For convienience I've added sequelize to my npm scripts, they now look like this:
The
$*makes sure the arguments following the command are placed at the correct position.To make sure this actually worked I've added some import statements and ES6 to my
.sequelizercfile and then ran an init with the following command:$ npm run sequelize -- init