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

zod-redis

v0.1.3

Published

Typesafe Redis storage made easy

Downloads

10

Readme

Introduction

Do you love the speed and simplicity of Redis but miss the autocompletion and peace of mind that comes with static typing in TypeScript? Built on top of Zod, zod-redis is a drop-in replacement for ioredis that'll help you skip the headache of keeping track of your data structures, stringifying/destringifying your payload, and remembering what you're storing under which key. Specify the schema once and get/set with peace of mind.

Under development 🚧

This package is still under development and APIs are subject to frequent changes. Production use is not yet intended.

Installation

Install zod-redis on npm using your favorite package manager:

npm install zod-redis
yarn add zod-redis
bun add zod-redis
pnpm add zod-redis

Basic Usage

Create a zod-redis instance using the same constructor as ioredis by specifying an additional schema option:

const zredis = new ZRedis(6379, '127.0.0.1', {
  schema: {
    birthday: {
      zod: z.date(),
      getKey: <T extends string>(userId: T) => {
        return `birthday:${userId}` as const;
      },
      expirationSeconds: 30,
    },
  },
});

Construct keys using custom input:

const birthdayKey = zredis.model('birthday').getKey('12345');
//     ^? const birthdayKey: "birthday:12345"

Set values with full typesafe input verification:

// Error: Argument of type '"birthday;12345"' is not assignable to parameter of type '`birthday:${string}`.
await zredis.model('birthday').set('birthday;12345', new Date(2015, 0, 5));

// Error: Argument of type 'string' is not assignable to parameter of type 'Date'.
await zredis.model('birthday').set(birthdayKey, '2023-10-08T00:36:30.104Z');

// Valid
await zredis.model('birthday').set(birthdayKey, new Date(2015, 0, 5));

Retrieve and use values with confidence:

const result = await zredis.model('birthday').get(birthdayKey);
//     ^? const result: Date | null

console.log(result?.toDateString());

And skip the type theatrics whenever it suits you:

await zredis.set('birthday:12345', 'this is not a date');

// Protected accessor returns null
const protectedResult = await zredis.model('birthday').get(birthdayKey);

// Returns 'this is not a date'
const rawResult = await zredis.get(birthdayKey);

How it works

Zod-redis is a wrapper around the ioredis package and tested against the entire ioredis test suite. The ZRedis class extends Redis with an additional constructor option for passing in the schema and a model function which provides access to commands that operate on your schema models.

Contributing

Issues and pull requests are welcome. I'll review them as soon as possible!