npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

  1. Installation
  2. Initialization
  3. CRUD Operations
  4. Query Operations
  5. Count Documents
  6. Error Handling
  7. 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.