PitchHut
Log in / Sign up
redis-store
46 views
Effortlessly manage your data with Redis as your primary store.
Pitch

Redis Store is a lightweight library for managing data with Redis, enhancing its usability by bringing relational capabilities to the fast KV database. Perfect for handling complex entity relationships without complex migrations, and compatible with various Redis instances.

Description

The @desmat/redis-store library is a lightweight and efficient solution for integrating Redis as your primary data store, taking full advantage of its exceptionally fast in-memory capabilities for managing your application’s entities and their relationships. This library is designed to enhance the use of Redis by incorporating relational aspects into its straightforward key-value store functionality.

Key Features:

  • Efficient Read/Write Operations: Optimized for lots of small read and write operations, ensuring high performance.
  • JSON Keys for Entities: Utilize JSON keys for storing entities without the need for complex migration scripts, facilitating seamless data management.
  • ZSET Keys for Dynamic Relationships: Implement ZSET keys to effectively track lists and relationships between your entities.
  • Compatibility: Designed to work seamlessly with Upstash and Vercel, but flexible enough to connect to any Redis instance using the REST API.

Getting Started:

To initiate usage of the library, set up your environment variables accordingly:

# Values for your .env file or provided through launch command
KV_REST_API_URL=*****
KV_REST_API_TOKEN=*****

Here’s a quick example detailing how to structure entities such as users and their related items:

import RedisStore from '@desmat/redis-store';

type User = {
  id: string;  // required; generated by .create as a short UUID or provided manually
  createdAt: number; // required; automatically set upon creation
  name: string;
};

type Thing = {
  id: string;
  createdAt: number;
  createdBy: string;  // required to look up Things by Users
  label: string;
};

const ThingOptions = {
  lookups: {
    user: "createdBy", // enables find by user ID
  },
};

const store = {
  users: new RedisStore<User>({ key: "user" }),
  things: new RedisStore<Thing>({ key: "thing", options: ThingOptions }),
};

Example Use Cases:

Create users and their associated things:

const users = await Promise.all([
  store.users.create({ name: "User One" }),
  store.users.create({ name: "User Two" }),
]);

const things = await Promise.all([
  store.things.create({ createdBy: users[0].id, label: "A thing for user one" }),
  store.things.create({ createdBy: users[0].id, label: "Another thing for user one" }),
  store.things.create({ createdBy: users[0].id, label: "Yet another thing for user one" }),
  store.things.create({ createdBy: users[1].id, label: "A thing for user two" }),
]);

Query existing things by user:

const latestUserThings = await store.things.find({
  user: users[0].id
}); // Retrieves 3 entries

Clean Up:

The library supports a soft delete functionality, allowing you to easily mark entities as deleted instead of permanently removing them:

await Promise.all([
  ...users.map((user: User) => store.users.delete(user.id)),
  ...things.map((thing: Thing) => store.things.delete(thing.id)),
]);

In summary, the @desmat/redis-store library empowers you to leverage Redis' speed and efficiency while managing complex relationships between entities with ease. Start integrating Redis into your application and unlock powerful data handling capabilities!