Skip to content

Transformations

Maximilian Mittelhammer edited this page Aug 1, 2023 · 1 revision

Transformations

The Transformation class provides an easy way to transform items in a DynamoDB table. It allows you to add, remove, rename, and modify attributes on items, as well as filter and remove items from the table.

Creating a Transformation

To create a transformation use the create-transformation command:

npx dynormo create-transformation <name>

This will create a new file in the transformations directory with the following contents:

import { Transformation, TransformationInterface } from 'dynormo'
import { DynormoClient } from '.dynormo'

// TODO: Add the entity types affected in this transformation here (e.g. Entity1 | Entity2)
type ItemTypes = any; 

class TestTransformation implements TransformationInterface<ItemTypes> {
    table = '<<TABLE_NAME>>';
    client = new DynormoClient({}).rawClient; // TODO: Add your DynamoDBClient here

    public async transform(): Promise<Transformation<ItemTypes>> {
        return new Transformation<ItemTypes>(this.table, this.client);
        // TODO: Add transformation here
        // e.g. .addAttribute('newAttribute', (item: ItemTypes) => {
        //          return 'string'
        //      })
    }

    public async rollback(): Promise<Transformation<ItemTypes>> {
        return new Transformation<ItemTypes>(this.table, this.client);
        // TODO: Add transformation that revert the changes made in transform() here
        // e.g. .removeAttribute('newAttribute')
    }

    public async backup(name: string): Promise<void> {
        new Transformation<ItemTypes>(this.table, this.client).backup(name);
    }
}

export default TestTransformation;

Fill in the table and client properties with the name of the table and an instance of the DynamoDBClient class from the @aws-sdk/client-dynamodb package. You can also use the DynormoClient.rawClient to get the underlying DynamoDBClient instance.

Then add the transformation logic to the transform() method. This method should return a Transformation object that contains the transformation logic. You can use the rollback() method to revert the changes made in the transform() method. The backup() method should be used to create a backup of the table before transforming it.

Running a Transformation

To run a transformation use the run-transformation command:

npx dynormo run-transformation <name>

This will ask you if you want to perform a backup before transforming the table. If you choose to perform a backup, it will ask you for the name of the backup. Keep in mind this command depending on the size of the table, it may take a while to complete.

Previewing a Transformation

To preview a transformation use the preview-transformation command:

npx dynormo preview-transformation <name>

This will ask you for the name of the file to store the preview in. It will then show you the items that will be transformed. Without actually transforming the table.

Rollback a Transformation

To rollback a transformation use the rollback-transformation command:

npx dynormo rollback-transformation <name>

This will ask you if you want to perform a backup before rolling back the transformation. Keep in mind this might not be able to restore all the items that were removed by the transformation. Restoring from a backup is the safest way to rollback a transformation.

Constructor

constructor(table: string, client: DynamoDBClient)

Creates a new Transformation object.

Parameters

  • table - The name of the DynamoDB table to transform.
  • client - An instance of the DynamoDBClient class from the @aws-sdk/client-dynamodb package. You can also use the DynormoClient.rawClient to get the underlying DynamoDBClient instance.

Public Methods

addAttribute(attribute: string, value: (item: T) => any): Transformation<T>

Adds an attribute to the item. If the attribute already exists, it will be overwritten.

Parameters

  • attribute - The name of the attribute to add.
  • value - A function that returns the value of the attribute.

Returns

The Transformation object.

removeAttribute(attribute: string): Transformation<T>

Removes an attribute from the item.

Parameters

  • attribute - The name of the attribute to remove.

Returns

The Transformation object.

renameAttribute(oldName: string, newName: string): Transformation<T>

Renames an attribute on the item.

Parameters

  • oldName - The current name of the attribute.
  • newName - The new name of the attribute.

Returns

The Transformation object.

mapAttribute(attribute: string, value: (item: T) => any): Transformation<T>

Modifies an attribute on the item.

Parameters

  • attribute - The name of the attribute to modify.
  • value - A function that returns the new value of the attribute.

Returns

The Transformation object.

setAttribute(attribute: string, value: any): Transformation<T>

Sets the value of an attribute on the item to a static value.

Parameters

  • attribute - The name of the attribute to set.
  • value - The value to set the attribute to.

Returns

The Transformation object.

filter(filter: (item: T) => boolean): Transformation<T>

Filters items that should not be transformed.

Parameters

  • filter - A function that returns true if the item should be transformed.

Returns

The Transformation object.

map(map: (item: T) => T): Transformation<T>

Maps an item to a new item.

Parameters

  • map - A function that returns the new item.

Returns

The Transformation object.

remove(filter: (item: T) => boolean): Transformation<T>

Removes an item from the table.

Parameters

  • filter - A function that returns true if the item should be removed.

Returns

The Transformation object.

backup(name: string): Promise<void>

Backs up the table before transforming it.

Parameters

  • name - The name of the backup.

Returns

A promise that resolves when the backup is complete.

execute(): Promise<void>

Transforms the items in the table.

Returns

A promise that resolves when the transformation is complete.

preview(filename: string): Promise<void>

Preview the items that will be transformed and store them in a file.

Parameters

  • filename - The name of the file to store the items in.

Returns

A promise that resolves when the preview is complete.

Examples

Here are some examples of how to use the Transformation class:

import { DynormoClient, FindOne1Entity } from '.dynormo';
import { Transformation, TransformationInterface } from 'dynormo';

type ItemTypes = FindOne1Entity;

class ExampleTransformation implements TransformationInterface<ItemTypes> {
    table = 'your-table-name';
    client = new DynormoClient({}).rawClient;

    public async transform(): Promise<Transformation<ItemTypes>> {
        return new Transformation<ItemTypes>(this.table, this.client)
            .addAttribute('combinedAttr', (item: ItemTypes) => {
                return `${item.stringAttr1} ${item.stringAttr2}`;
            })
            .filter((item: ItemTypes) => {
                return item.stringAttr1 !== 'test';
            })
            .renameAttribute('stringAttr1', 'newStringAttr1');
    }

    public async rollback(): Promise<Transformation<ItemTypes>> {
        return new Transformation<ItemTypes>(this.table, this.client)
            .renameAttribute('newStringAttr1', 'stringAttr1')
            .removeAttribute('combinedAttr');
    }

    public async backup(name: string): Promise<void> {
        new Transformation<ItemTypes>(this.table, this.client).backup(name);
    }
}

export default ExampleTransformation;