Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalence of Rails console for Node.js

I am trying out Node.js Express framework, and looking for plugin that allows me to interact with my models via console, similar to Rails console. Is there such a thing in NodeJS world?

If not, how can I interact with my Node.js models and data, such as manually add/remove objects, test methods on data etc.?

like image 696
AdamNYC Avatar asked Jan 27 '13 17:01

AdamNYC


People also ask

Is Ruby on rails similar to Nodejs?

Rails is a Ruby-based framework, which is written in Ruby language. 2. Node JS is the best suited for developing small size projects and for I/O non-blocking, event-based application. Rails is a web application framework, well suited for database-backed web application in MVC pattern and for metaprogramming.

Does node have a console?

The node:console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: A Console class with methods such as console.

Is node js faster than Ruby on Rails?

Performance speed If we compare the speed of Ruby on Rails vs Node. js, even experienced Ruby on Rails developers acknowledge that the framework is slow. While its speed improves with every version, it's still a lot lower than Node's. There are many reasons for the performance delay.

Why is node better than Rails?

While Ruby on Rails is a framework and, no doubt, has its strength, there are many situations where it falls back. Node. js, on the other hand, can offer the non-blocking I/O paradigm that is excellent for creating data-intensive and real-time apps. That's something RoR can't provide.


1 Answers

Create your own REPL by making a js file (ie: console.js) with the following lines/components:

  1. Require node's built-in repl: var repl = require("repl");
  2. Load in all your key variables like db, any libraries you swear by, etc.
  3. Load the repl by using var replServer = repl.start({});
  4. Attach the repl to your key variables with replServer.context.<your_variable_names_here> = <your_variable_names_here>. This makes the variable available/usable in the REPL (node console).

For example: If you have the following line in your node app: var db = require('./models/db') Add the following lines to your console.js

 var db = require('./models/db');  replServer.context.db = db; 
  1. Run your console with the command node console.js

Your console.js file should look something like this:

var repl = require("repl");  var epa = require("epa"); var db = require("db");  // connect to database db.connect(epa.mongo, function(err){   if (err){ throw err; }    // open the repl session   var replServer = repl.start({});    // attach modules to the repl context   replServer.context.epa = epa;   replServer.context.db = db;   }); 

You can even customize your prompt like this:

var replServer = repl.start({   prompt: "Node Console > ", }); 

For the full setup and more details, check out: http://derickbailey.com/2014/07/02/build-your-own-app-specific-repl-for-your-nodejs-app/

For the full list of options you can pass the repl like prompt, color, etc: https://nodejs.org/api/repl.html#repl_repl_start_options

Thank you to Derick Bailey for this info.


UPDATE:

GavinBelson has a great recommendation for running with sequelize ORM (or anything that requires promise handling in the repl).

I am now running sequelize as well, and for my node console I'm adding the --experimental-repl-await flag.

It's a lot to type in every time, so I highly suggest adding:

"console": "node --experimental-repl-await ./console.js"

to the scripts section in your package.json so you can just run:

npm run console

and not have to type the whole thing out.

Then you can handle promises without getting errors, like this:

const product = await Product.findOne({ where: { id: 1 });

like image 121
Ira Herman Avatar answered Oct 03 '22 09:10

Ira Herman