Open-sourcing Ztorage: A Type-Safe Wrapper for Key-Value Storage in JavaScript

Kevin Hermawan
Hyperjump Tech
Published in
3 min readFeb 20, 2023
Ztorage in Action

The Problem

Key-value storage is a commonly used storage mechanism in web development. As developers, we at Hyperjump have built many websites that require local storage to persist data between sessions. However, we’ve always been restless about the lack of type safety in key-value storage. Bugs can easily slip into our code without the ability to ensure that the stored data conforms to a particular schema, making it difficult to maintain and extend our applications.

For example, imagine a user setting for a web application that should always be a boolean value. Still, because we’re using a key-value storage system without proper type safety, Hyperjump accidentally stores it as a string. Later on, when we try to retrieve this setting and use it in our application, we may encounter unexpected behavior that’s difficult to debug. This is just one example of the types of issues that can arise without proper type safety, and it’s something that we at Hyperjump and many developers have struggled with for years.

This is precisely why we at Hyperjump created Ztorage: to solve the type-safety problem in key-value storage. With Ztorage, we can define schemas for our data, enabling automatic validation and parsing of stored data to ensure that it conforms to the expected format. By providing robust type safety features, Ztorage makes it easier for us to maintain, extend, and debug our applications.

Features of Ztorage

Compile-time and runtime validation

Screenshot showing Ztorage’s compile-time type checking in action

Ztorage uses the Zod library to enable compile-time and runtime validation of stored data. When we define a schema for our stored data, we can leverage Zod’s type inference capabilities to generate TypeScript types for our data automatically. This makes it easier to catch errors during the development process since the TypeScript compiler will detect inconsistencies between the defined schema and the stored data.

At runtime, Zod’s validation capabilities ensure that our stored data is parsed correctly and that any errors are caught and handled appropriately. This combination of compile-time and runtime validation helps ensure that our data is always properly formatted and that any issues are caught early in the development process. This way, we can ensure that our applications are more reliable and less prone to unexpected bugs.

Support for various storage mediums

Ztorage is a versatile wrapper we at Hyperjump designed to work with various storage systems. It supports popular options like localStorage and sessionStorage and more advanced solutions such as localForage and React Native’s AsyncStorage. This flexibility allows us to use Ztorage across different projects and platforms without worrying about compatibility issues or rewriting code to work with other storage systems.

Closing

In conclusion, Ztorage, created by us at Hyperjump, is a lightweight and flexible library that provides a simple and type-safe wrapper for key-value storage in our JavaScript applications. By adding support for compile-time and runtime validation, as well as a range of storage mediums, Ztorage makes it easier to ensure that our data is always properly formatted and easy to work with. As an open-source project, Ztorage is constantly evolving, and we look forward to seeing how the community will continue to use and build upon its features in the future.

Hyperjump is an open-source-first company providing engineering excellence service. We aim to build and commercialize open-source tools to help companies streamline, simplify, and secure the most important aspects of their modern DevOps practices.

--

--