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 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-versioned

v1.0.1

Published

A TypeScript library for managing JSON data versioning and migrations with a clean, declarative API.

Downloads

6

Readme

JSON Versioning Library

A TypeScript library for managing JSON data versioning and migrations with a clean, declarative API. Supports both decorator and functional approaches.

Installation

npm install json-versioned

Features

  • ✨ Version your data structures with ease
  • 🔄 Automatic data migrations
  • 🎨 Decorator and functional programming styles
  • 🔒 Type-safe migrations
  • 📦 Zero dependencies

Usage

Decorator Approach

Use decorators to define your versioned class with migrations:

interface UserV1 {
    name: string;
    age: number;
}
interface UserV2 {
    firstName: string;
    lastName: string;
    age: number;
}
interface UserV3 {
    firstName: string;
    lastName: string;
    age: number;
    email: string;
}
@Versioned<UserV3>(3) // Current version is 3 and the current interface is UserV3
@Migration(2, (data: UserV1): UserV2 => {
    const [firstName, lastName] = data.name.split(' ');
    return { firstName, lastName, age: data.age };
})
@Migration(3, (data: UserV2): UserV3 => {
    return {
    ...data,
    email: ${data.firstName.toLowerCase()}.${data.lastName.toLowerCase()}@example.com,
    };
})
class UserManager extends WithVersioning<UserV3>(BaseStorage) {
// Your class implementation
}
// Usage
const manager = new UserManager();
// Serialize
const data = manager.versionedSerialize({
    firstName: 'John',
    lastName: 'Doe',
    age: 30,
    email: '[email protected]'
});
// Deserialize (with automatic migrations to the latest version)
const oldData = JSON.stringify({
    version: 1,
    data: { name: 'John Doe', age: 30 }
});
const migratedUser = manager.versionedDeserialize(oldData);
// or 
const migratedUser = UserManager.versionedDeserialize(oldData);

Functional Approach

If you prefer not to use decorators, you can use the functional API:

const manager = createVersioned<UserV3>(3);
// Define migrations
manager.migrations.toVersion(2).withTransform((data: UserV1): UserV2 => {
    const [firstName, lastName] = data.name.split(' ');
    return {
    firstName,
    lastName,
    age: data.age,
    };
});
manager.migrations.toVersion(3).withTransform((data: UserV2): UserV3 => {
    return {
    ...data,
    email: ${data.firstName.toLowerCase()}.${data.lastName.toLowerCase()}@example.com,
    };
});
// Usage
const serialized = manager.versionedSerialize(currentUser);
const deserialized = manager.versionedDeserialize(oldUserData);

Data Format

The library serializes data in the following format:

{
    version: number;
    data: T;
}

Migration Chain

Migrations are applied sequentially. For example, if you have data in version 1 and want to migrate to version 3:

  1. First, the v1 → v2 migration is applied
  2. Then, the v2 → v3 migration is applied

Type Safety

The library is fully typed and will ensure that:

  • Migration functions receive the correct input type
  • Migration functions return the correct output type
  • The final version matches your current schema

Error Handling

The library will throw errors in the following cases:

  • Missing migration for a version
  • Invalid version number
  • Invalid data format

Best Practices

  1. Always define migrations for each version change
  2. Keep migrations pure and deterministic
  3. Version your interfaces/types
  4. Test your migrations thoroughly

License

MIT