Skip to main content
Userscripts allow you to define custom schema and table transformations. When specified with the , userscripts modify the data that migrates from a source database to CockroachDB. This cookbook provides ready-to-use examples that demonstrate real-world uses of the . You can copy and paste them into your own code, and you can adapt them for your specific use cases. Userscripts are comparable to MOLT Fetch’s , which are used during the initial bulk load phase of a migration. When performing an , apply equivalent transformations in both the Fetch command and Replicator userscript to ensure data consistency. Below each example, you will see the equivalent way of carrying out that transformation using MOLT Fetch, if it’s possible to do so.

Before you begin

  • Make sure that you understand the . Take a look at the . Understand in a userscript.
  • . The userscript API is accessible via the replicator library, which is already included in MOLT Replicator.
  • Install TypeScript, and install a TypeScript-compatible IDE (for example, VS Code).
  • Download the userscript type definitions file and the tsconfig.json file. Place these files in your working directory to enable autocomplete, inline documentation, and real-time error detection directly in your IDE.

Example userscripts

Filter a single table

The following example shows how to use to exclude a specific table from replication, while still replicating everything else in the same schema. This could be useful when you have internal, staging, or audit tables that appear in the but shouldn’t be written to the target. Make sure to set the TARGET_SCHEMA_NAME and TABLE_TO_SKIP constants to match your environment.

MOLT Fetch equivalent

You can filter a single table using the MOLT Fetch flag. Make sure to replace the TABLE_TO_SKIP placeholder with the name of the appropriate table, and make sure that the source and target connection strings have been exported to the environment.

Filter multiple tables

The following example shows how to use to exclude multiple tables from the replication process. This extends the example shown in Filter a single table to allow for multiple tables to be filtered instead of just one. Make sure to set the TARGET_SCHEMA_NAME and TABLES_TO_SKIP constants to match your environment.

MOLT Fetch equivalent

You can filter multiple tables using the MOLT Fetch flag. Make sure to replace the TABLE_TO_SKIP placeholder regex with the appropiate regex to suit your use case, and make sure that the source and target connection strings have been exported to the environment. To capture multiple tables, use regex alternation:
Or use a regex pattern:

Select data to replicate

The following example demonstrates how to use to conditionally replicate rows. Many applications mark rows as deleted using an is_deleted column rather than actually deleting the row. The following example will demonstrate how to use a conditional to ignore “soft-deleted” rows during upsert replication. This implementation avoids writing these rows to the target, while still propograting explicit delete events. Make sure to set the TARGET_SCHEMA_NAME constant to match your environment.

Source/target table schema

The source and target tables have the same schema:

MOLT Fetch equivalent

You can selectively replicate data using the flag, which accepts a path to a JSON file that specifies row-level filter expressions. Make sure to replace the /path/to/soft_delete_filter.json placeholder with the path to your json file, and make sure that the source and target connection strings have been exported to the environment.

Filter columns

The following example shows how to use to remove specific columns from replicated rows. For example, the source table may include internal metadata columns or values intended only for the source system. The following example removes a single column qty before writing the row to the target. Make sure to set the TARGET_SCHEMA_NAME and TABLE_TO_EDIT constants to match your environment.

Source/target table schema

The target schema of the preceding example uses the same columns as the source table, except the qty column is removed:

MOLT Fetch equivalent

Filter columns using the flag, which accepts a path to a JSON file that specifies column exclusions. Make sure to replace the /path/to/exclude_qty_column.json placeholder with the path to your json file, and make sure that the source and target connection strings have been exported to the environment.
Replace the YOUR_TABLE_HERE placeholder with the name of the table to edit.

Rename columns

The following example shows how you can use to rename a table’s columns on the target database. It demonstrates how you might handle column renaming in the case of both upserts and deletes. Make sure to set the TARGET_SCHEMA_NAME and TABLE_TO_EDIT constants to match your environment.

Source/target table schema

The column names in the preceding target table are longer versions of those in the source table:

MOLT Fetch equivalent

MOLT Fetch does not have direct support for column renaming. You may need to rename the column on the target database after the .

Rename primary keys

This example shows how you can use to rename a table’s primary keys. Because of how the onRowDelete handler uses primary keys to identify rows, this scenario requires a different implementation than the Rename columns example. This example maps source primary key columns (pk1, pk2) to target primary key columns (id1, id2). Note how the onRowDelete handler includes conditionals to see if the primary keys are defined using the source or the target key names. Make sure to set the TARGET_SCHEMA_NAME and TABLES_TO_EDIT constants to match your environment.

Source/target table schema

MOLT Fetch equivalent

MOLT Fetch does not have direct support for primary key renaming. You may need to reconfigure the primary keys on the target database after the .

Route table partitions

The following example demonstrates how you can use to distribute the rows of a single source table across multiple target tables based on partitioning rules. Make sure to set the TARGET_SCHEMA_NAME and TABLES_TO_PARTITION constants to match your environment.

MOLT Fetch equivalent

1-to-n table transformations are not supported by MOLT Fetch transforms.

Rename tables

This example shows how to use to route data from a source table to a target table of a different name. This must be implemented in the schema-level handlers, as the row will need to be routed to the correct table-level handler using the correct target table name. Make sure to set the TARGET_SCHEMA_NAME and TABLE_MAPPINGS constants to match your environment.

MOLT Fetch equivalent

Rename tables using the flag, which accepts a path to a JSON file that specifies table mappings. Make sure to replace the /path/to/rename_tables.json placeholder with the path to your json file, and make sure that the source and target connection strings have been exported to the environment.
Replace the YOUR_*_TABLE_NAME_HERE placeholders with the names of your source and target tables.

Compute new columns

The following example shows how to use to create computed columns on the target that do not exist on the source. In this example, we derive a total column, and add it to each replicated row on the target. Make sure to set the TARGET_SCHEMA_NAME and TABLE_TO_COMPUTE constants to match your environment.

Source/target table schema

The target schema of the preceding example uses the same columns as the source table, plus an additional total column:

MOLT Fetch equivalent

Creating computed columns is not supported by MOLT Fetch transforms. MOLT Fetch can only preserve computed columns that exist on the source. You may need to calculate this column for the target database table after the .

Combine multiple transforms

You can combine multiple transformations into a single userscript. The following userscript first filters for a certain table, then it filters out “soft-deleted” rows and removes sensitive columns from the table on the replication target. Make sure to set the TARGET_SCHEMA_NAME and TABLE_TO_EDIT constants to match your environment.

Source/target table schema

The target schema of the preceding example uses the same columns as the source table, except the ssn and credit_card_number columns are removed:

MOLT Fetch equivalent

To implement this transformation with MOLT Fetch, create:
  • A soft_delete_filter.json file (to be included via the flag).
  • A pii_removal_transform.json file (to be included via the flag).
Run MOLT Fetch with both the --filter-path and --transformations-file flags. Make sure to replace the /path/to/soft_delete_filter.json and /path/to/pii_removal_transform.json placeholders, and make sure that the source and target connection strings have been exported to the environment.
Replace the YOUR_TABLE_HERE placeholder with the name of the table to clean up.
Replace the YOUR_TABLE_HERE placeholder with the name of the table to clean up.

Implement a dead-letter queue

The following example demonstrates how you can use the userscript API to implement a dead-letter queue (DLQ) strategy for NOT NULL errors by retrying the batch at row-level granularity and recording failures. Failed writes go to a dead-letter queue table. Make sure to set the TARGET_SCHEMA_NAME and DLQ_TABLE, and TABLES_WITH_DLQ constants to match your environment.

MOLT Fetch equivalent

There is no MOLT Fetch equivalent for DLQ. DLQ handling is part of a live replication, not an initial data load.

See also