Redis OM for Node.js

Photo by ian dooley on Unsplash

Redis OM makes it easy to add Redis (not going to explain what Redis is. If you need to refer what is Redis – Click here), to your Node.js application by mapping the Redis data structures you know and love to classes that you define. There are some easy steps to configure Redis OM to Node.js. No more pesky, low-level commands, just pure code with a fluent interface.

1. Setup NodeJS ( If you don’t need this, Go to step 2 )

Very firstly, You have to configure your project environment (Creating a Package.json file) using “npm init”. Then install “nodemon”. Click here to refer “Nodemon”. After that, install express using “npm i express”. Then create a server.js file. (App.js root file) Click the below link to learn Node.js more.

2. Redis OM Setup and Store Some Data

Install Redis OM to your node.js project.

npm install redis-om –save

2.1 Create Database and Get Public Endpoint

Then go to and create an account (It’s simple as normal account creating). It’s free for 30 MB usage. There are simple steps to create a Database. Go follow it. After created the DB, go to “Data Access Control” tab on the left sidebar.

Redis Labs – Data Access Control

After that you have to create a Role and Users to access the database (cluster). When you are creating the user, make sure to define the Role.

Go to the “Database” tab on the left sidebar and go inside your database. Then you can see the “Public Endpoint”. This is the URL that needs to connect node.js with Redis. You have to make this endpoint as follows.


You can use your username that you already created in “Data Access Control”, user tab. The password is also the same password that you created in the users tab. The port will come automatically when you copy the public endpoint.

2.2 Connect NodeJS and Save data to Redis

In my project, there is a .env file and I am pasting my Redis URL. If you don’t have one, you can create a constant variable to store the URL or create a redis.js file to configure redis settings.

First we need to create a new file as schema. Here is my schema file code.

const {Schema, Client, Entity} = require(‘redis-om’);
class user_info extends Entity {};

const userSchema = new Schema(user_info, {
          index: { type: ‘number’ },
          name : { type: ‘string’ },
          age : { type: ‘number’ },
          address: { type: ‘text’ }
}, {
          dataStructure: ‘JSON’

module.exports = userSchema;

In here, I created an Entity called ‘user_info’ to use inside the schema. Then you can import the schema file to your node file. Let’s get into save method. Here is the save method.

const url = process.env.REDIS_URL;           //Redis url from .env file
const {Client} = require(‘redis-om’);            //Importing the client from redis-om
const userSchema = require(‘./user_schema.js’);           //Importing the user schema
const client = new Client();            //create the client

async function storeDataToRedis(){
          await;            //Open the redis URL, means connect the redis client
          const userRepository = client.fetchRepository(userSchema);            //fetch the schema with a repository
          await userRepository.createIndex();
          const singleUser = userRepository.createEntity();

          singleUser.index = 12345;
 = ‘BuildItMasters’;
          singleUser.age = 30;
          singleUser.address = ‘123/5, Daa street, United States, 90000’;
          const enitiyID = await; //if user saved, it will return an entity id.

          return enitiyID;

}catch(err) {

3. Reading, Writing (Updating) and Removing with Repository

Let’s see how to read data from Redis ORM.

await; //url is your redis url

// GET all User Data
const userRepository = client.fetchRepository(userSchema);
await userRepository.createIndex();
const getUserData = await; //Get all data from the Redis DB

// GET single User data
const getUserDataRepository = client.fetchRepository(userSchema);
const data = await‘user_name’).matches(THE_VALUE_YOU_NEED_TO_MATCH).return.all();

Now we are going to see how to update data in Redis ORM.

// GET single User data
const user = client.fetch(ENTITY_ID);

user.index = 67890; = ‘BuildItMasters update’;
user.age = 10;
user.address = ‘Guru Rd, Matara’;


Let’s see how to delete data from Redis ORM.

//Delete all data in Redis DB
const userRepository = client.fetchRepository(userSchema);
await userRepository.createIndex();
const getUserData = await; //Get all data from the Redis DB

       for(var i = 0; i < getUserData.length; i++){
             await userRepository.remove(getData[i].entityId);
            //Remove data by entity ID from the Redis DB

For more information –

Thank you for your valuable time. Please leave a comment if you are interested this article.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x