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

upstash-as-a-db

v1.0.2

Published

Simple, type-safe interface to use Upstash Redis as Database incl. Encryption.

Downloads

66

Readme

Upstash-as-a-DB

Ever needed a simple database to store flexible data in a structured way with a clear type-safe interface?

This package has been created to interact with Upstash Redis Service and make it work as a database. Ideal for data processing applications or small projects.

The package is able to:

  • Production-ready and type-safe way to connect to Upstash Redis Databases.
  • Provide additional search indexes (e.g. for virtual relations).
  • Detecting concurrent updates, which could cause inconsistency.
  • High standard e2e-encryption, preventing possible data leaks.

Why does it exist?

The @upstash/redis package has already a nice interface, but handling the different set's and get's could get messy over time.

Also, a big issue was the missing encryption to prevent leaking sensitive customer data.

That's why the package has a Collection interface which can be used to handle each data type individually, fully type-safe and if needed with E2E Encryption (means only the app can read the data in Redis).

Installation

bun add upstash-as-a-db
# OR
pnpm add upstash-as-a-db
# OR
yarn add upstash-as-a-db
# OR
npm install upstash-as-a-db

You can generate the secrets and vector for encryption like this:

# For Secret (at least 32 bytes)
openssl rand -base64 32

# For IV (at least 16 bytes)
openssl rand -base64 16

Usage

It's recommended to use Typescript to profit from extra type-safety.

import Collection from "upstash-as-a-db";
import { Redis } from "@upstash/redis";

// Init the redis instance as usual
const redis = new Redis();

interface SensitiveCustomer {
  id: string;
  customerName: string;
  customerAddress: {
    // ...
  };
  paymentToken: string;
  stripeCustomerId: string;
}

// Init the Collection instance with
const customers = new Collection<SensitiveCustomer>(
  redis, // The redis instance
  {
    // The redis key prefix, e.g. id will be `customers:<id>` (default none)
    keyPrefix: "customers",

    // Default TTL of every key set, excl. from `setex` (default none)
    defaultTTL: 600,

    // When set, this will be used to encrypt/decrypt the data entries.
    // Please note, the "id" parameter (main index) and other indexes are not encrypted!
    // By default disabled (undefined = disabled)
    enableEncryption: {
      secret: process.env.INTERNAL_ENCRYPTION_SECRET!, // Recommended: Use ENV-Variables to inject, don't hardcode.
      iv: process.env.INTERNAL_ENCRYPTION_IV!,
    },
  },
);

// Additional Search Index for later search or to form virtual relations.
// (This needs to be set before first insert)
const stripeCustomersIdx = customers.addIndex(
  "stripeCustomerId", // Key of your type, where a search index should be set
);

// Creating a first customer to the database, will get added to the index automatically.
await customers.set({
  id: "first-customer"
  name: "John Doe",
  customerAddress: {
    line: "71 World-Trade-Center",
    city: "New York",
    postalCode: "10007",
    state: "NY",
    country: "US"
  },
  paymentToken: "very-secret-token",
  stripeCustomerId: "c_12345678934567"
})

// Searching by ID
const firstCustomer = await customers.get("first-customer");

// Searching by other field
const stripeCustomer = await stripeCustomersIdx.getItems("c_12345678934567")

// Persisting (since default TTL is set in Collection)
await customers.persist("first-customer")

// Deleting the object
await customers.delete("first-customer")

You see, it's very straight forward. In case of questions, please open any issues.

Limits & Constraints

Important security related note: The main index (id-value) and other index values are not encrypted.

The package was tried to made very safe, by detecting concurrent updates or encrypting data. The package is used in production and not made any issues so far. But still, everyone would be happy if you catch the last bugs in the code. 😉

The underlaying Upstash Database is very limited in space, so it is not made for very big datasets. But that's on your plate.

Beyond Upstash the package is currently not working with ioredis package or similar ones!

Development

This project is using Bun.js as a package manager and for running the project.