redis4goose
v1.0.3
Published
Simulate mongo command on redis
Downloads
23
Readme
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: '[email protected]'
});
console.log(newUser); // { id: '...', name: 'John Doe', age: 30, email: '[email protected]' }
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: '[email protected]' });
console.log(user);
Update
To update a single document:
const updated = await UserModel.updateOne(
{ email: '[email protected]' },
{ 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: '[email protected]' });
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.