https://github.com/hyperjumptech/ztorage
A type-safe wrapper for key-value storage in JavaScript
https://github.com/hyperjumptech/ztorage
Last synced: 8 months ago
JSON representation
A type-safe wrapper for key-value storage in JavaScript
- Host: GitHub
- URL: https://github.com/hyperjumptech/ztorage
- Owner: hyperjumptech
- License: mit
- Created: 2023-02-19T11:35:40.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-05T03:37:02.000Z (over 2 years ago)
- Last Synced: 2025-02-21T15:49:53.133Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 283 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Ztorage
  
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](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API) and other storage backends. It takes advantage of [Zod](https://github.com/colinhacks/zod), a powerful TypeScript schema validation library, to provide automatic parsing and validation of data.
## Articles
- [Open-sourcing Ztorage: A Type-Safe Wrapper for Key-Value Storage in JavaScript](https://medium.com/hyperjump-tech/open-sourcing-ztorage-a-type-safe-wrapper-for-key-value-storage-in-javascript-2cfe9879ea1a?source=rss----fabfd42372dc---4)
## 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
```ts
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](/LICENSE)