Skip to content

A type-safe wrapper for key-value storage in JavaScript

License

Notifications You must be signed in to change notification settings

hyperjumptech/ztorage

Repository files navigation

Ztorage

Minified size Test coverage Monthly download

Ztorage is a flexible and type-safe wrapper for key-value storage that is designed to work with various storage mediums, including Web Storage API and other storage backends. It takes advantage of Zod, a powerful TypeScript schema validation library, to provide automatic parsing and validation of data.

Articles

Features

  • Works with various storage mediums
  • Provides type safety for key-value storage
  • Enables automatic parsing and validation of data
  • Zero dependencies

Installation

To use Ztorage, you must have Zod installed. You can install @hyperjumptech/ztorage and zod, by running the following command:

NPM

npm install @hyperjumptech/ztorage zod

Yarn

yarn add @hyperjumptech/ztorage zod

pnpm

pnpm add @hyperjumptech/ztorage zod

Usage

import { z } from "zod";
import Ztorage from "@hyperjumptech/ztorage";

// Define a schema for the data you want to store
const schema = z.object({
  isDarkModeEnabled: z.boolean(),
});

// Instantiate a Ztorage object with a schema and storage functions
const storage = new Ztorage({
  schema,
  onGetItem: async (key) => {
    return localStorage.getItem(key);
  },
  onSetItem: async (key, value) => {
    localStorage.setItem(key, value);
  },
  onRemoveItem: async (key) => {
    localStorage.removeItem(key);
  },
});

// Set item to storage
await storage.setItem("isDarkModeEnabled", true);

// Get item from storage
const isDarkModeEnabled = await storage.getItem("isDarkModeEnabled");
console.log(isDarkModeEnabled); // true

// Remove item from storage
await storage.removeItem("isDarkModeEnabled");

License

MIT License