> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Userscript Quickstart

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

This quickstart guides you through creating, validating, and deploying your first <InternalLink path="userscript-overview">userscript</InternalLink> based on a <InternalLink path="userscript-cookbook">cookbook</InternalLink> example.

## Before you begin

* <InternalLink path="molt-replicator-installation">Install MOLT Replicator **v1.3.0 or later**</InternalLink> for full compatibility with the userscript API.
* Install a TypeScript-compatible IDE (for example, [VS Code](https://code.visualstudio.com/)).

## Step 1: Set up your environment

Create a new TypeScript file for your userscript in your IDE (for example, `userscript.ts`).

For IDE autocomplete and type-checking support, optionally add the Replicator TypeScript definitions. For details, refer to <InternalLink path="userscript-api#access-this-api">Access this API</InternalLink>.

## Step 2: Write a userscript

Use the <InternalLink path="userscript-cookbook">userscript cookbook</InternalLink> as a starting point. The cookbook provides ready-to-use examples that you can copy and adapt.

Copy and adapt a cookbook example that matches your use case. For example, adapt the template to <InternalLink path="userscript-cookbook#filter-a-single-table">filter a single table</InternalLink> to exclude the `audit_logs` table from the `public` schema during replication:

```ts theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
import * as api from "replicator@v2";

const TARGET_SCHEMA_NAME = "defaultdb.public";
const TABLE_TO_SKIP = "audit_logs";

api.configureTargetSchema(TARGET_SCHEMA_NAME, {
  onRowUpsert: (row, meta) => {
    if (meta.table === TABLE_TO_SKIP) {
      return null; // Skip this table
    }
    return row;
  },
  onRowDelete: (row, meta) => {
    if (meta.table === TABLE_TO_SKIP) {
      return null;
    }
    return row;
  }
});
```

For details on the userscript API and handler functions, refer to the <InternalLink path="userscript-api">Userscript API</InternalLink>.

<Tip>
  When writing userscripts, follow the <InternalLink path="userscript-api#best-practices">best practices</InternalLink>.
</Tip>

## Step 3: Use with Replicator

Include the <InternalLink path="replicator-flags">`--userscript`</InternalLink> flag to have <InternalLink path="molt-replicator">MOLT Replicator</InternalLink> apply the userscript. For example, to apply your userscript during PostgreSQL replication:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
replicator pglogical \
--sourceConn $SOURCE \
--targetConn $TARGET \
--targetSchema defaultdb.public \
--slotName molt_slot \
--publicationName molt_fetch \
--stagingSchema defaultdb._replicator \
--stagingCreateSchema \
--userscript userscript.ts
```

## See also

* <InternalLink path="userscript-overview">Userscript Overview</InternalLink>
* <InternalLink path="userscript-api">Userscript API</InternalLink>
* <InternalLink path="userscript-cookbook">Userscript Cookbook</InternalLink>
* <InternalLink path="molt-replicator">MOLT Replicator</InternalLink>
