
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.
2.1 Create Database and Get Public Endpoint
Then go to redislabs.com 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.

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(){
try{
await client.open(url); //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;
singleUser.name = ‘BuildItMasters’;
singleUser.age = 30;
singleUser.address = ‘123/5, Daa street, United States, 90000’;
const enitiyID = await userRepository.save(singleUser); //if user saved, it will return an entity id.
return enitiyID;
}catch(err) {
console.log(err);
}
}
3. Reading, Writing (Updating) and Removing with Repository
Let’s see how to read data from Redis ORM.
// GET all User Data
const userRepository = client.fetchRepository(userSchema);
await userRepository.createIndex();
const getUserData = await userRepository.search().return.all(); //Get all data from the Redis DB
// GET single User data
const getUserDataRepository = client.fetchRepository(userSchema);
const data = await getUserDataRepository.search().where(‘user_name’).matches(THE_VALUE_YOU_NEED_TO_MATCH).return.all();
Now we are going to see how to update data in Redis ORM.
const user = client.fetch(ENTITY_ID);
user.index = 67890;
user.name = ‘BuildItMasters update’;
user.age = 10;
user.address = ‘Guru Rd, Matara’;
await userRepository.save(user);
Let’s see how to delete data from Redis ORM.
await client.open(url);
const userRepository = client.fetchRepository(userSchema);
await userRepository.createIndex();
const getUserData = await userRepository.search().return.all(); //Get all data from the Redis DB
if(getUserData.length){
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 – Redis.io
Thank you for your valuable time. Please leave a comment if you are interested this article.
- Android Studio Articles – https://builditmasters.com/category/android-studio/
- Android Studio Firebase Tutorial – https://builditmasters.com/category/android-studio-firebase-tutorial/
- C Programming – https://builditmasters.com/category/programming/
- Flutter – https://builditmasters.com/category/flutter/
- GitHub Tutorials – https://builditmasters.com/category/github/
- Java Programming – https://builditmasters.com/category/java-programming/
- MERN / MEVN Stacks – https://builditmasters.com/category/mern_mevn_stacks/
- Tech News – https://builditmasters.com/category/tech-news/
- Theory Lessons – https://builditmasters.com/category/theory-lessons/
- Adobe Tutorials – https://builditmasters.com/category/adobe-tutorials/