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

entity-migrate-alpha

v1.0.0

Published

Typescript package for defining and executing migrations for a specific entity, ensuring data consistency and type safety during version updates

Downloads

10

Readme

entity-migrate

A Typescript package for defining and executing migrations for a specific entity, ensuring data consistency and type safety during version updates


Installation

The library is available as a npm package. To install the package run:

npm install entity-migrate
# or with yarn
yarn add entity-migrate

Usage

Importing the Package

To use the Entity Migration Package in your project, import the createEntityMigration function from the package:

import { createEntityMigration } from 'entity-migrate';

Creating Entity Migration

To create migrations for your entity, invoke the createEntityMigration function, passing the entity tag and an array of all available versions:

const { addMigration, migrate } = createEntityMigration('yourEntity', ['version1', 'version2', 'version3']);

Defining Migrations

Use the addMigration function to define migrations between different versions of your entity. Each migration requires the source version, target version, a guard function, and a migrate function. As a convention all the versions of data should contain a version property that contains the numeric or string tag of the version.

Here's an example of defining a migration from 'version1' to 'version2':

const migrateV1toV2 = (data: Version1): Version2 => ({
  version: 'version2'
  // Migration logic from version1 to version2
});

addMigration({
  sourceVersion: 'version1',
  targetVersion: 'version2',
  guard: (data): data is Version1 => data.version === 'version1' && 'someProp' in data /* checks on other critical properties for migration */,
  migrate: migrateV1toV2,
});

You can define migrations for multiple version by calling addMigration for each version migration.

Performing Migrations

To perform a migration on your entity data, use the migrate function, passing the data object. The function will automatically apply the appropriate migrations based on the data's version property:

const migratedData = migrate(entityData);

The migrate function returns the migrated data object. If there are migrations from v1 to v2 and also from v2 to v3 when passing an entity in v1 the function will apply both migrations and return an entity with v3 type

Example

Here is an examples to help you understand the usage of the Entity Migration Package:

// Import the package
import { createEntityMigration } from 'entity-migration';

// different versions of the data
const TestEntityVersionEnum = {
    v1: 'version1',
    v2: 'version2',
    v3: 'version3',
} as const
type TestEntityVersion =
    (typeof TestEntityVersionEnum)[keyof typeof TestEntityVersionEnum]

// interfaces for different versions of the data
type TestEntityVersion1 = {
    version: typeof TestEntityVersionEnum.v1
    name: string
}
type TestEntityVersion2 = {
    version: typeof TestEntityVersionEnum.v2
    fullName: string
}
type TestEntityVersion3 = {
    version: typeof TestEntityVersionEnum.v3
    firstName: string
    lastName: string
}

// Create entity migration for 'yourEntity'
const { addMigration, migrate } = createEntityMigration(
    'yourEntity',
    Object.values(TestEntityVersionEnum) as TestEntityVersion[],
)

// Define migration from V1 to V2
const isV1 = (data: any): data is TestEntityVersion1 =>
    'version' in data &&
    data.version === TestEntityVersionEnum.v1 &&
    'name' in data &&
    typeof data.name === 'string'

const migrateV1toV2 = (data: TestEntityVersion1): TestEntityVersion2 => {
    return {
        version: TestEntityVersionEnum.v2,
        fullName: data.name,
    }
}

addMigration({
    sourceVersion: TestEntityVersionEnum.v1,
    targetVersion: TestEntityVersionEnum.v2,
    guard: isV1,
    migrate: migrateV1toV2,
})

// Define migration from V2 to V3
const isV2 = (data: any): data is TestEntityVersion2 =>
    'version' in data &&
    data.version === TestEntityVersionEnum.v2 &&
    'fullName' in data &&
    typeof data.fullName === 'string'

const migrateV2toV3 = (data: TestEntityVersion2): TestEntityVersion3 => {
    const parts = data.fullName.split(' ')
    console.log('test', { data })
    return {
        version: TestEntityVersionEnum.v3,
        firstName: parts.shift(),
        lastName: parts.join(' '),
    }
}

addMigration({
    sourceVersion: TestEntityVersionEnum.v2,
    targetVersion: TestEntityVersionEnum.v3,
    guard: isV2,
    migrate: migrateV2toV3,
})

// Defining the data
const testEntityVersion1: TestEntityVersion1 = {
    version: TestEntityVersionEnum.v1,
    name: 'John Smith',
}

// Perform a migration
const migratedData = migrate(testEntityVersion1)

console.log(migratedData)
/*
internally the migrate function will migrate to v2:
{
    version: TestEntityVersionEnum.v2,
    fullName: 'John Smith'
}
and then to v3 because the resulting value is v2 and
there is a migration from v2 to v3:
{
    version: TestEntityVersionEnum.v3,
    firstName: 'John',
    lastName: 'Smith'
}
and logs it in console
 */

Exceptions

Infinite loop

If a looping set of migration are added like v1ToV2, v2ToV3 and v3ToV1 as soon as the migration that causes the loop is added the addMigration function will throw an error with following message:

migrating <entityTag> from <sourceVersion> to <targetVersion> can create an infinite loop

Wrong migration step

If in any of the migrations steps the data provided to migrate function does not pass the guard function the migrate function will throw an error with the following message:

wrong <entityTag> migration from <sourceVersion> to <targetVersion>>

License

This package is provided under the MIT License.