Skip to content

Instantly share code, notes, and snippets.

@vemarav
Last active October 22, 2020 03:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vemarav/b86d59026a22082d71c8b3de3ed21ee0 to your computer and use it in GitHub Desktop.
Save vemarav/b86d59026a22082d71c8b3de3ed21ee0 to your computer and use it in GitHub Desktop.
Build rails like console in nodejs
/**
* add console.js at ther root of your app
* Modify to make it more helpful for your project
* > Note: `esm` adds ES6 support in Node
* In package.json
* ...
* "scripts": {
* "start": "nodemon -r esm ./server.js"
* "console": "node -r esm --experimental-repl-await console",
* }
*
* check whether you have yarn installed
* $ yarn -v
* 1.12.3
* If not then install it using
* $ npm install -g yarn
* /usr/local/bin/yarn -> /usr/local/lib/node_modules/yarn/bin/yarn.js
* /usr/local/bin/yarnpkg -> /usr/local/lib/node_modules/yarn/bin/yarn.js
* + yarn@1.12.3
* updated 1 package in 1.459s
*
* Now you can run command
* $ yarn console
* app > // Here we have all sequelize models
* app > (await User.findOne()).toJSON() // return {..user} json object
*/
let repl = require('repl');
let models = require('./app/models');
Object.keys(models).forEach(modelName => {
global[modelName] = models[modelName];
});
global['DateTime'] = require('./lib/DateTime').default;
let replServer = repl.start({
prompt: 'app > '
});
replServer.context.db = models;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment