Connecting Node.js application to RavenDB Cloud

What you will learn

  • How to connect your Node.js application to a RavenDB Cloud instance
  • How to verify that your connection is successfully established

Introduction

RavenDB Cloud is a secure fully managed cloud database ready to launch in minutes. It serves as database hosting, management studio, and managed services of backing up data. All in one place. Connecting your Node.js console application to RavenDB Cloud is a simple process. By the end of this tutorial, we will connect your Node.js app to the RavenDB Cloud database and be ready to integrate it into your projects. Whether you’re new to using RavenDB or simply looking for a straightforward setup, this tutorial will smoothly guide you through.

Prerequisites

What you will need:

Let’s ensure that we have all the prerequisites to connect your application to the RavenDB Cloud instance. First, you need to download and install Node.js. This tutorial uses Node.js in version 22.10.0. To check if your Node.js installation was successful, and check your version. open terminal and run the following command.

node -v

If you see the Node.js version in your output, it means the installation was successful. If this command doesn’t output your version, download Node.js from the link above.

With your Node.js set up, let’s turn our attention to the RavenDB Cloud. We’ll need a RavenDB Cloud instance and a certificate necessary to encrypt the connection between the database and your application. RavenDB Cloud generates the certificate automatically for your instance. If you’re new to RavenDB Cloud, don’t worry – in this guide, we will also cover how to get started with RavenDB Cloud and how to claim your free instance.

To create your account, start by clicking ‘Get Started Free’(1). If you’d like a free account, simply click ‘Start’(2) without making any changes. Next, select ‘Register Here’. Enter your email address(3) to proceed to the domain name registration step(4). Then, enter the domain name you want, and complete the billing. Review the summary to finish setting up your account. Then you get an activation link on your email address.

We’ve successfully created a RavenDB Cloud account. Now, let’s create a free instance. Instance is like your personal database server. RavenDB Cloud handles setup, maintenance, and other time-consuming tasks to let you focus on the code. To create an instance, click ‘Add Product’ on the right of the ‘Products’ page or just click ‘Add new product to start’ at the center of the page.

If you would like a free instance, don’t change anything – just scroll down and click ‘Next’(1). Enter your account information(2) and then go through billing. Next, you have a ‘Customize’ step(3), where you need to put in the display name of your instance. You also need to set the instance as open to the world (Leave 0.0.0.0/0 if you want it to be open to the world) or delete IP 0.0.0.0/0 and click ‘Add my IP’, to make this only accessible with your IP. You can also change it later so don’t be afraid to delete 0.0.0.0/0. If you want to learn more about CIDR notation, you can read more here. Then, just hit ‘Next’, review the summary and if all looks good, click ‘Create’. For more details on adding a new product, click here.

After configuring your product you will need to wait for the instance to complete the provisioning process before you can access your studio. You will also need a certificate installed.

For a secure connection with RavenDB Studio, let’s get a TLS certificate for your instance. We’ll also use it in our app later. You can download it by going to the ‘Products’ tab on the left bar. Then you select the green button ‘Download Certificate’ on your chosen instance and follow the instructions you get in the pop-up. This pop-up includes a certificate download button and instructions for installing certificates for both Windows and Linux. If you clicked too quickly and got an authentication error, please reload your browser and try again.

Remember to restart your browser after installing the certificate to ensure it recognizes the new certificate.

In order to access RavenDB Studio you require a working certificate. While using RavenDB Cloud, the RavenDB Studio is secured by default – you’ll need a certificate to access the server UI. You’ll also need it to connect to your database within the application code, so this step cannot be skipped. Use your certificate to authorize against RavenDB Studio. Just choose your certificate from the popup window that your browser will display. Once doing so, you should be able to see the UI. Now, let’s create a new database for your application. Select the ‘Databases’ tab and press ‘New database’ to create a new one.

For the purpose of testing, you can also create Sample Data by clicking your database and selecting ‘Tasks’ on the left bar where you can find the option to create it. This data will come in handy later if you want to test the connection between your application and the database.

Setup

We’ve prepared everything you need: a functional Node.js environment for building your app and a RavenDB database running on RavenDB Cloud to manage your application’s data. Let’s try to connect a test application to our instance. In this guide, we are going to create the app from scratch. First, we will create a new directory for the application. Next, we need to create your application file. You can do this by opening the terminal inside the previously created directory, and executing the following command:

npm init -y

Next, we’ll need the RavenDB Client package to communicate with our database within the code. You have to add it to your project by executing the following command:

npm install --save ravendb

Then, we need to edit package.json manually and add “type”: “module”. Applying this prevents an error message, minimizing output during testing. Your file should look like this:

{
    "name": "node.js_tutorial",
    "version": "1.0.0",
    "main": "index.js",
    "type": "module",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "keywords": [],
    "description": "",
    "dependencies": {
      "ravendb": "^6.0.0"
    }
}

Now let’s create the main file for your Node.js application, named ‘index.js’. Let’s open it and start writing some code.

To establish connection with RavenDB Cloud instance, you have to import DocumentStore. DocumentStore is the base for all RavenDB actions. It communicates with your cluster and can handle multiple databases. It’s important to have a single instance of DocumentStore in place, to maintain a single connection definition to your database. You can learn more about DocumentStore here or here.

import { DocumentStore } from "ravendb";
import * as fs from "fs";

We are also importing an fs module used for file manipulation. It will come in handy to get our certificate from your file system. The next step is to use the certificate. You can do it using the following code:

const authOptions = {
      certificate: fs.readFileSync("C:\\path_to_your_pfx_file\\cert.pfx"),
      type: "pfx", // or "pem"
};

const store = new DocumentStore("https://your_RavenDB_server_URL", "your_database_name", authOptions);

authOptions lets you specify which database you want to access. You are also using a certificate you get from RavenDB Cloud. If you want to learn more about the security certificate you can check it in the documentation. Remember to change sample data from the code above and don’t forget to call store.initialize() – a critical step as it initializes the connection to the database. Your complete code should resemble the following:

import { DocumentStore } from "ravendb";
import * as fs from "fs";

const authOptions = {
    certificate: fs.readFileSync("C:\\path_to_your_pfx_file\\cert.pfx"),
    type: "pfx", // or "pem"
};

const store = new DocumentStore("https://your_RavenDB_server_URL", "your_database_name", authOptions);
store.initialize();

Let’s check the connection by loading a document from your database in the RavenDB Cloud. We use DocumentSession to interact with document. Session is a basic mechanism that is used to modify, load, create, and keep track of documents. If you wish to learn more about Sessions you can read more in the documentation. In the RavenDB Studio choose one of the documents you want to use. For this example, let’s load a single Employee document and write its details in the console. Check the code below:

const session = store.openSession('Tutorial');

const employee = await session.load('employees/1-A');
console.log("Employee LastName:", employee.LastName);

The code above loads our employee with a selected ID and downloads its information. Let’s also add this:

store.dispose();

This will terminate Session upon completing its operations. With this addition, your code should look like this:

import { DocumentStore } from "ravendb";
import * as fs from "fs";

const authOptions = {
    certificate: fs.readFileSync("C:\\path_to_your_pfx_file\\cert.pfx"),
    type: "pfx", // or "pem"
};

const store = new DocumentStore("https://your_RavenDB_server_URL", "Tutorial", authOptions);
store.initialize();

const session = store.openSession('Tutorial');

const employee = await session.load('employees/1-A');
console.log("Employee LastName:", employee.LastName);

store.dispose();

Now using the terminal type node index.js and wait a few seconds. In the case of using sample data and using this ID, you should receive Davolio. This indicates that our connection is set and we can hook it up to other code.

As an ending tip, I would like to remind you that if everything is working properly you should delete sample data from your database so it is clean and ready for your data, or just create a new one, dedicated for your app.

Summary

In this tutorial, we explained how to connect your Node.js applications to the RavenDB database hosted in the RavenDB Cloud. We used Node.js with the RavenDB Client package to connect to our database. We also tested the basic connection using sample data.

Woah, already finished? 🤯

If you found the article interesting, don’t miss a chance to try our database solution – totally for free!

Try now try now arrow icon