Last active
October 22, 2020 03:21
-
-
Save vemarav/b86d59026a22082d71c8b3de3ed21ee0 to your computer and use it in GitHub Desktop.
Build rails like console in nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
* + [email protected] | |
* 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