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

@dolittle/eventstore-migrator

v1.0.18

Published

Migrator for the Dolittle Event Store

Downloads

60

Readme

EventStore Migrator

npm version

Getting started

This guide will assume TypeScript and using Yarn - it is not a requirement

Start by creating a folder for your migrator.

Then initialize the package.

$ npm init

Since this guide is based on TypeScript, we want to add that as a developer dependency, and we're leveraging 'ts-node' for easy running.

$ yarn add -D typescript ts-node
{
    "compilerOptions": {
        "target": "es2015",
        "module": "CommonJS",
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "rootDir": ".",
        "baseUrl": "./",
        "sourceRoot": "./",
        "outDir": "dist"
    },
    "include": [
        "**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ],
    "references": []
}

In your package.json file, you can now add the following to your scripts key:

{
    "scripts": {
        "start": "ts-node index.ts"
    }
}

The next thing we need is the Dolittle base migrator. It is built leveraging Mongoose. You will provide the connection(s) for it to migrate, so for the TypeScript transpiler to be happy - we should also add the typings for it.

$ yarn add @dolittle/eventstore-migrator
$ yarn add -D @types/mongoose

Creating a basic migrator

Add a file called index.ts.

We then need to import the following:

import mongoose from 'mongoose';
import { EventStoreConverter } from '@dolittle/eventstore-migrator'
import { logger } from '@dolittle/eventstore-migrator';

The logger is optional, but can prove handy for outputting information during the run of the migrator.

(async () => {
    const sourceServer = 'mongodb://localhost:27018';
    const destinationServer = 'mongodb://localhost:27017';

    const sourceConnection = await mongoose.createConnection(sourceServer, {
        useNewUrlParser: true,
        dbName: 'eventstore',
        useFindAndModify: false
    });

    const destinationConnection = await mongoose.createConnection(destinationServer, {
        useNewUrlParser: true,
        dbName: 'eventstore',
        useFindAndModify: false
    });

    logger.info('Starting to convert');

    const converter = new EventStoreConverter(sourceConnection, destinationConnection, logger);
    await converter.convert();

    logger.info('Done converting');
})();

Notice the connection strings at the top and dbName properties for the connections. If you have multiple event stores to migrate, for instance for multiple tenants, you can quite easily create a loop doing this for all.

If you don't need any custom transformations, you can now just run it from your terminal:

$ yarn start

Transforming events during migration

The converter supports a callback mechanism that gets called on every event that is migrated. This enables you to do transformations and also choose whether or not you want the event to be included by returning true from the callback for including or false for not including an event.

Version 4 of the Dolittle event store implementation serialized some types in a way that would not be compatible with version 5 and deserializing back to the client code. One of these is DateTime or DateTimeOffset. In v4, these types ended up as a number; Unix EPOCH. While in v5, it will assume it is an actual ISO date with time.

Another aspect of transformation is to correct any wrong events that could potentially not replay nicely and worst case not get you back to the state you wanted.

import mongoose from 'mongoose';
import { EventStoreConverter } from '@dolittle/eventstore-migrator'
import { logger } from '@dolittle/eventstore-migrator';

(async () => {
    const sourceServer = 'mongodb://localhost:27018';
    const destinationServer = 'mongodb://localhost:27017';

    const sourceConnection = await mongoose.createConnection(sourceServer, {
        useNewUrlParser: true,
        dbName: 'eventstore',
        useFindAndModify: false
    });

    const destinationConnection = await mongoose.createConnection(destinationServer, {
        useNewUrlParser: true,
        dbName: 'eventstore',
        useFindAndModify: false
    });

    logger.info('Starting to convert');

    const converter = new EventStoreConverter(sourceConnection, destinationConnection, logger, (source, destination) => {

        // Transform anything on the event
        // Source has a property called event, which is the object literal for your source event
        // Destination has a property called Content, which is the object literal for your destination event

        // Return true to include the event
        return true;
    });
    await converter.convert();

    logger.info('Done converting');
})();

Event Type

You might want to do certain operations for certain event types. From the generated artifacts.json file you get when using the .NET SDK with the build tool, we can create a simple resolver that enables us to work with event types by their name, rather than their unique identifier. The artifacts.json file can be required directly in your code and you can build maps from it.

const artifacts = require('../Core/.dolittle/artifacts.json');
const typesForArtifact: any = {};

for (const featureName in artifacts) {
    const feature = artifacts[featureName];

    for (const artifactId in feature.events) {
        const artifact = feature.events[artifactId];
        typesForArtifact[artifact.type] = artifactId;
    }
}

Imagining you have events and properties you want to correct - for instance all events with a date on it:

const eventsAndPropertiesWithDate: any = {
    'MyEvent': ['SomeDateProperty'],
    'MyOtherEvent': ['SomeDateProperty', 'SomeOtherDateProperty']
};

We can then create some convenient lookup maps for this:

const typeKeys = Object.keys(typesForArtifact);
const eventTypeAndPropertiesWithDate: any = {};
const artifactToEventName: any = {};
const eventNameToArtifact: any = {};

for (const eventName in eventsAndPropertiesWithDate) {
    const type = typeKeys.find(t => t.indexOf(`.${eventName}`) >= 0);
    if (type) {
        const artifact = typesForArtifact[type];
        artifactToEventName[artifact] = eventName;
        eventNameToArtifact[eventName] = artifact;
        eventTypeAndPropertiesWithDate[artifact] = eventsAndPropertiesWithDate[eventName];
    } else {
        logger.warn(`No type for ${eventName}`);
    }
}

Handling DateTime

With the event type map we can now simply loop through properties for the event types we want to modify:

const converter = new EventStoreConverter(sourceConnection, destinationConnection, logger, (source, destination) => {
    const artifact = source.event_artifact.toString();

    if (eventTypeAndPropertiesWithDate[artifact]) {
        for (const property of eventTypeAndPropertiesWithDate[artifact]) {

            let value = source.event[property];

            if (value === Infinity) {
                value = new Date(1970, 1, 1).valueOf();
            }

            value = new Date(value);
            if (value.valueOf() === Infinity) {
                value = new Date(1970, 1, 1).valueOf();
            }
            destination.Content[property.toCamelCase()] = value.toUTCString();
        }
    }

    return true;
});
await converter.convert();

PS: The toCamelCase() method comes from the @dolittle/eventstore-migrator package and is an extension to the String prototype.