You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

redis4goose

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis4goose

Simulate mongo command on redis

1.0.3
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

redisgoose

Simulate mongo command on redis

Redis Model Usage Guide

This guide provides comprehensive instructions on how to use the RedisModel class, which offers MongoDB-like operations for working with Redis.

Table of Contents

  • Installation
  • Initialization
  • CRUD Operations
  • Query Operations
  • Count Documents
  • Error Handling
  • Closing the Connection

Installation

Before using the RedisModel, make sure you have the required dependencies installed:

npm install ioredis uuid

Initialization

To use the RedisModel, first import it and create an instance:

const RedisModel = require('./RedisModel');

const userSchema = {
  name: { type: 'string' },
  age: { type: 'number' },
  email: { type: 'string' }
};

// Default initialization (uses default Redis connection settings)
const UserModel = new RedisModel('User', userSchema);

// Initialization with custom Redis options
const redisOptions = {
  host: 'your-redis-host',
  port: 6379,
  password: 'your-redis-password',
  // Add other options as needed
};
const CustomUserModel = new RedisModel('User', userSchema, redisOptions);

CRUD Operations

Create

To create a new document:

const newUser = await UserModel.create({
  name: 'John Doe',
  age: 30,
  email: 'john@example.com'
});
console.log(newUser); // { id: '...', name: 'John Doe', age: 30, email: 'john@example.com' }

Read

To find a document by ID:

const user = await UserModel.findById('some-uuid');
console.log(user);

To find multiple documents:

const users = await UserModel.find({ age: { $gte: 18 } });
console.log(users);

To find a single document:

const user = await UserModel.findOne({ email: 'john@example.com' });
console.log(user);

Update

To update a single document:

const updated = await UserModel.updateOne(
  { email: 'john@example.com' },
  { age: 31 }
);
console.log(updated); // true if a document was updated, false otherwise

To update multiple documents:

const result = await UserModel.updateMany(
  { age: { $lt: 18 } },
  { status: 'minor' }
);
console.log(result.modifiedCount);

Delete

To delete a single document:

const deleted = await UserModel.deleteOne({ email: 'john@example.com' });
console.log(deleted); // true if a document was deleted, false otherwise

To delete multiple documents:

const result = await UserModel.deleteMany({ age: { $lt: 18 } });
console.log(result.deletedCount);

Query Operations

The find method supports various query operations:

// Greater than
const adultsOnly = await UserModel.find({ age: { $gt: 18 } });

// Greater than or equal to
const adultsAndTeens = await UserModel.find({ age: { $gte: 13 } });

// Less than
const minors = await UserModel.find({ age: { $lt: 18 } });

// Less than or equal to
const notSeniors = await UserModel.find({ age: { $lte: 65 } });

// Not equal to
const notJohn = await UserModel.find({ name: { $ne: 'John' } });

// In array
const specificAges = await UserModel.find({ age: { $in: [20, 30, 40] } });

You can also use options for pagination and sorting:

const options = {
  limit: 10,
  skip: 20,
  sort: 'age:desc'
};

const users = await UserModel.find({}, options);

Count Documents

To count documents matching a query:

const adultCount = await UserModel.countDocuments({ age: { $gte: 18 } });
console.log(adultCount);

Error Handling

The RedisModel will throw errors for invalid data types. Always use try-catch blocks:

try {
  await UserModel.create({ name: 'John', age: '30' }); // This will throw an error
} catch (error) {
  console.error('Error creating user:', error.message);
}

Remember to handle potential Redis connection errors as well.

Closing the Connection

When you're done using the RedisModel, it's important to close the connection to free up resources:

await UserModel.close();

This guide covers the basic usage of the RedisModel class. For more advanced use cases or specific Redis operations not covered by this model, you may need to extend the class or use the Redis client directly.

Keywords

redis

FAQs

Package last updated on 02 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts