In this post we will be discussing about getting started with RethinkDB in Node.js. Most of the web applications required to serve data to their users or ability to handle data. Now a days there are quite a few options for a web developer to think which database to connect, be it a relational database or a No SQL etc. In this article we will be focusing on RethinkDB, which is an open-source database for the real-time web.
RethinkDB is an open-source JSON database built for the real-time web. Their official documentation says that
So in simple terms RethinkDB will be best suited when you have real time data coming in and you want that data to continuously refresh in your front end for the users to see. Examples which can use RethinkDB as their back-end can be a stock market application or a multiplayer game.
For installing REthinkDb in Windows you need to download the ZIP archive and unpack in a directory and run rethinkdb.exe which will create rethinkdb_data folder in the same folder you are in.
Next is to install a Node.js driver for rethinkDB with promises and connection pool suing the below command
npm install rethinkdbdash
We can connect to the rethink database with the simple command as shown below where you need to execute the module when you import it. It will run on the default port address 28015 and hosts localhost.
var rethinkDb = require('rethinkdbdash')();
For creating database you need to run the below code which will create a database example
var rethinkDb = require('rethinkdbdash')();
rethinkDb.dbCreate('example')
.run().then(function(response){
console.log(response);
}).error(function(err){
console.log(err);
});
But before running this file make sure you have started your rethink database server using the command line. You need to run rethink command where your rethinkdb.exe is kept. Rethink by default runs on 8080. So if you have any other application running on this port you will get the series of error. One such error would be "Fail to create a new connection for the connection pool: ECONNREFUSED". This is because your rethinkdb server has not started.
To solve this error you need to run rethinkdb server on different port using the command rethinkdb --http-port 8075.
You can chaeck that the RethinkDB server is running or not by going to the url http://localhost:8075/ which will give you a nice looking dashboard as shown below
When you have created your database using the script shown above and if everything is correct you will get the below success message.
You can also check that your database is been created or not using the RethinkDB Administrative Console and navigating to http://localhost:8075/#tables as shown below
We will be using tableCreate method to create table table1 in the database. By default the primary key will be id.
rethinkDb.db('example').tableCreate('table1')
.run().then(function(response){
console.log(response);
}).error(function(err){
console.log('Error ' + err);
});
The above query will return the following response
In this post we have seen how to get started with RethinkDB with Node.js applications. In our next post we will be discussing about inserting data and getting real time data out of it in our application.
Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.
Introducing RethinkDB
RethinkDB is an open-source JSON database built for the real-time web. Their official documentation says that
RethinkDB is the first open-source, scalable JSON database built from the ground up for the real-time web. It inverts the traditional database architecture by exposing an exciting new access model - instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results to applications in real-time. RethinkDB's real-time push architecture dramatically reduces the time and effort necessary to build scalable real-time apps.
So in simple terms RethinkDB will be best suited when you have real time data coming in and you want that data to continuously refresh in your front end for the users to see. Examples which can use RethinkDB as their back-end can be a stock market application or a multiplayer game.
Install RethinkDB and Node.js Client rethinkdbdash
For installing REthinkDb in Windows you need to download the ZIP archive and unpack in a directory and run rethinkdb.exe which will create rethinkdb_data folder in the same folder you are in.
Next is to install a Node.js driver for rethinkDB with promises and connection pool suing the below command
npm install rethinkdbdash
Connecting to Rethink Database
We can connect to the rethink database with the simple command as shown below where you need to execute the module when you import it. It will run on the default port address 28015 and hosts localhost.
var rethinkDb = require('rethinkdbdash')();
Creating Database in RethinkDB
For creating database you need to run the below code which will create a database example
var rethinkDb = require('rethinkdbdash')();
rethinkDb.dbCreate('example')
.run().then(function(response){
console.log(response);
}).error(function(err){
console.log(err);
});
But before running this file make sure you have started your rethink database server using the command line. You need to run rethink command where your rethinkdb.exe is kept. Rethink by default runs on 8080. So if you have any other application running on this port you will get the series of error. One such error would be "Fail to create a new connection for the connection pool: ECONNREFUSED". This is because your rethinkdb server has not started.
To solve this error you need to run rethinkdb server on different port using the command rethinkdb --http-port 8075.
You can chaeck that the RethinkDB server is running or not by going to the url http://localhost:8075/ which will give you a nice looking dashboard as shown below
When you have created your database using the script shown above and if everything is correct you will get the below success message.
You can also check that your database is been created or not using the RethinkDB Administrative Console and navigating to http://localhost:8075/#tables as shown below
Creating Table in RethinkDB
We will be using tableCreate method to create table table1 in the database. By default the primary key will be id.
rethinkDb.db('example').tableCreate('table1')
.run().then(function(response){
console.log(response);
}).error(function(err){
console.log('Error ' + err);
});
The above query will return the following response
Conclusions
In this post we have seen how to get started with RethinkDB with Node.js applications. In our next post we will be discussing about inserting data and getting real time data out of it in our application.
Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.
No comments:
Post a Comment