elliott-w

payload plugin switch env

by elliott-w
Databases & data
v3
>=3.0.2
MIT

Overview

Summary generated from the README

Adds an admin button to toggle the database connection between production and development, or to copy the production database into a development one. Configured through the plugin array, it must come last, after a cloud storage plugin. Copied upload documents keep pointing at production cloud files, which are protected from edits. Requires Payload 3.0.2 or later, MongoDB and cloud storage for production uploads; migrations are not supported.

Features

  • Toggle between production and development databases from the admin panel
  • Copy the production database to development with one button
  • Copy only a subset of documents, including a limited number of recent versions
  • Protects production cloud storage files from changes made in development
  • Optional quick switch mode and a safety check that development uses localhost
  • Helper for admin thumbnails that link directly to cloud storage

Use cases

  • Edit production content and data while running new field code locally, without losing live updates
  • Replicate the production environment locally without syncing uploaded files
  • Refresh a staging database from production with a dedicated copy button

From the author

Payload plugin to switch environments

Installation

npm i @elliott-w/payload-plugin-switch-env

5

Stars

1

Forks

0

Open Issues

4 months ago

Last Updated

84

Weekly Downloads

v2.0.3

npm Version

235.8 KB

Unpacked Size

0

Dependencies

GitHub stars

50 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20265
Dec 4, 20265
Dec 3, 20265
Dec 2, 20265
Dec 1, 20265
Nov 30, 20265
Nov 29, 20265
Nov 28, 20265
Nov 27, 20265
Nov 26, 20265
Nov 25, 20265
Nov 24, 20265
Nov 23, 20265
Nov 22, 20265
Nov 21, 20265
Nov 20, 20265
Nov 19, 20265
Nov 18, 20265
Nov 17, 20265
Nov 16, 20265
Nov 15, 20265
Nov 14, 20265
Nov 13, 20265
Nov 12, 20265
Nov 11, 20265
Nov 10, 20265
Nov 9, 20265
Nov 8, 20265
Nov 7, 20265
Nov 6, 20265
Nov 5, 20265
Nov 4, 20265
Nov 3, 20265
Nov 2, 20265
Nov 1, 20265
Oct 31, 20265
Oct 30, 20265
Oct 29, 20265
Oct 28, 20265
Oct 27, 20265
Oct 26, 20265
Oct 25, 20265
Oct 24, 20265
Oct 23, 20265
Oct 22, 20265
Oct 21, 20265
Oct 20, 20265
Oct 19, 20265
Oct 18, 20265
Oct 17, 20265
Oct 16, 20265
Oct 15, 20265
Oct 14, 20265
Oct 13, 20265
Oct 12, 20265
Oct 11, 20265
Oct 10, 20265
Oct 9, 20265
Oct 8, 20265
Oct 7, 20265
Oct 6, 20265
Oct 5, 20265
Oct 4, 20265
Oct 3, 20265
Oct 2, 20265
Oct 1, 20265
Sep 30, 20265
Sep 29, 20265
Sep 28, 20265
Sep 27, 20265
Sep 26, 20265
Sep 25, 20265
Sep 24, 20265
Sep 23, 20265
Sep 22, 20265
Sep 21, 20265
Sep 20, 20265
Sep 19, 20265
Sep 18, 20265
Sep 17, 20265
Sep 16, 20265
Sep 15, 20265
Sep 14, 20265
Sep 13, 20265
Sep 12, 20265
Sep 11, 20265
Sep 10, 20265
Sep 9, 20265
Sep 8, 20265
Sep 7, 20265
Sep 6, 20265
npm weekly downloads

30+5 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202630
Dec 4, 202632
Dec 3, 202632
Dec 2, 202632
Dec 1, 202636
Nov 30, 202637
Nov 29, 202637
Nov 28, 202636
Nov 27, 202638
Nov 26, 202637
Nov 25, 202691
Nov 24, 202687
Nov 23, 2026115
Nov 22, 2026117
Nov 21, 2026117
Nov 20, 2026113
Nov 19, 2026113
Nov 18, 202685
Nov 17, 202657
Nov 16, 202628
Nov 15, 202626
Nov 14, 202626
Nov 13, 202629
Nov 12, 202632
Nov 11, 20266
Nov 10, 202633
Nov 9, 202634
Nov 8, 202634
Nov 7, 202645
Nov 6, 202642
Nov 5, 202640
Nov 4, 202640
Nov 3, 202615
Nov 2, 202614
Nov 1, 202617
Oct 31, 20266
Oct 30, 20266
Oct 29, 20265
Oct 28, 20265
Oct 27, 20263
Oct 26, 20263
Oct 25, 20261
Oct 24, 20262
Oct 23, 20262
Oct 22, 20266
Oct 21, 20266
Oct 20, 20266
Oct 19, 20266
Oct 18, 20265
Oct 17, 20264
Oct 16, 20265
Oct 15, 20261
Oct 14, 20261
Oct 13, 20261
Oct 12, 20261
Oct 11, 20262
Oct 10, 20264
Oct 9, 20263
Oct 8, 20266
Oct 7, 202611
Oct 6, 202611
Oct 5, 202613
Oct 4, 202614
Oct 3, 202616
Oct 2, 202616
Oct 1, 202617
Sep 30, 202617
Sep 29, 202617
Sep 28, 202615
Sep 27, 202640
Sep 26, 202643
Sep 25, 202643
Sep 24, 202639
Sep 23, 202634
Sep 22, 202635
Sep 21, 202672
Sep 20, 202645
Sep 19, 202638
Sep 18, 202638
Sep 17, 202638
Sep 16, 202639
Sep 15, 202639
Sep 14, 20262
Sep 13, 20263
Sep 12, 202610
Sep 11, 202611
Sep 10, 202612
Sep 9, 202614
Sep 8, 202625
Sep 7, 202626
Sep 6, 202625

Each point is npm's rolling 7-day download count.

Health Score

53/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/885749499-root.svg)](https://payloaddirectory.dev/plugins/885749499-root)
HTML
<a href="https://payloaddirectory.dev/plugins/885749499-root"><img src="https://payloaddirectory.dev/badge/885749499-root.svg" alt="Payload Directory"></a>
Updated Jul 10, 2026Created Nov 9, 2024Published Mar 6, 2026

README

⚠️ This plugin has moved location to: https://github.com/what-works-global/payload-packages/tree/main/packages/switch-env

Payload plugin switch env

Demo

Install

pnpm i @elliott-w/payload-plugin-switch-env

Example Config

⚠️ Warning: The switchEnv plugin must come last in the plugins array. Your cloud storage plugin must be second last.

Why? The cloud storage plugin adds url fields to upload collections and the switchEnv plugin needs to modify the afterRead hooks on these fields. The cloud storage plugin also adds `beforeChange` and `afterDelete` hooks to upload collections (for uploading/deleting files from cloud storage) and the switchEnv plugin assumes they are the last hook in the array, thus you need to have the cloud storage plugin second last so that no other plugins break this assumption.

// payload.config.ts
import { type Args, mongooseAdapter } from '@payloadcms/db-mongodb'
import { buildConfig } from 'payload'
import { switchEnvPlugin, adminThumbnail } from '@elliott-w/payload-plugin-switch-env'
import { s3Storage } from '@payloadcms/storage-s3'

const dbArgs: Args = {
  url: process.env.DATABASE_URI!,
}

export default buildConfig({
  db: mongooseAdapter(dbArgs),
  plugins: [
    // Your cloud storage plugin must come second last in the plugins array
    s3Storage({
      bucket: process.env.S3_BUCKET!,
      collections: {
        media: true,
      },
      config: {
        credentials: {
          accessKeyId: process.env.S3_ACCESS_KEY_ID!,
          secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
        },
        region: process.env.S3_REGION,
      },
    }),
    // The switchEnvPlugin must come last in the plugins array
    switchEnvPlugin({
      payloadVersion: '3.70.0',
      enable: process.env.NODE_ENV === 'development',
      db: {
        function: mongooseAdapter,
        productionArgs: dbArgs,
        developmentArgs: {
          ...dbArgs,
          url: process.env.DEVELOPMENT_DATABASE_URI || '',
        },
      },
      copy: {
        versions: {
          default: { mode: 'latest-x', x: 3 },
        },
      },
    }),
  ],
  collections: [
    {
      slug: 'media',
      fields: [
        {
          name: 'alt',
          type: 'text',
        },
      ],
      upload: {
        // Helper function if you want admin thumbnails to link directly to cloud storage
        adminThumbnail: adminThumbnail({
          basePath: `https://${process.env.S3_BUCKET}.s3.${process.env.S3_REGION}.amazonaws.com`,
          imageSize: 'thumbnail',
        }),
        imageSizes: [
          {
            name: 'thumbnail',
            width: 300,
            height: 300,
          },
        ],
      },
    },
  ],
})

Plugin Options

  • db (required): Database adapter function + production/development args.
  • payloadVersion (required): Installed Payload version string (for hook compatibility), e.g. '3.70.0'.
  • buttonMode (default: 'switch'):
    • 'switch': toggle between production and development.
    • 'copy': show a dedicated button to copy production DB to development DB. Useful for staging environments.
  • enable (default: true): Enable/disable plugin behavior.
  • quickSwitch (default: false): Skip the switch modal and switch immediately in 'switch' mode.
  • logDatabaseSize (default: false): Logs serialized backup size when copying DB.
  • developmentSafetyMode (default: true): Throws if developmentArgs.url is not localhost/127.0.0.1 during development.
  • developmentFileStorage:
    • { mode: 'file-system' } (default)
    • { mode: 'cloud-storage', prefix, collections }
  • copy: Allows you to copy only a subset of documents to your development database. See type hints for more information.

Limitations

Does not support database migrations.

Only works with:

  • payload 3.0.2 and higher
  • mongodb
  • production setups using a cloud storage adapter for uploads

Notes:

  • Development upload behavior can be either local file system or cloud storage via developmentFileStorage.
  • The plugin does not support production environments that rely solely on local file storage uploads.

 

Why this plugin?

Scenario #1

You're working with a production database that is getting frequently updated and you want to push a feature that requires code + database changes.

Option #1

Quickly clone the database to your local machine, make the data changes and then overwrite the production database with your local database.

Pros

  • It works (maybe)

Cons

  • Depending on how frequently the database (i.e. website) is updated, you might lose some data changes (e.g. e-commerce orders, form submissions, content entry changes from the client or other developers, etc) made to the production database in the time it took you to do all that.
Option #2

Make the code (i.e. field structure) changes on your local machine then use this plugin to switch the database connection to your production database to make data/content updates.

Pros:

  • Never lose any business-as-usual updates made to your production database
  • You can have the data component of a feature requiring code + data primed and ready in the production database, so that when the code is eventually deployed, the code can assume that the data exists in the database
  • You could change your db connection string environment variable, but pressing a button in the admin dashboard is quicker and less prone to error

Cons:

  • You have to train all developers on a project using this plugin to be careful when in "production environment" mode.

Scenario #2

You want to replicate the production environment to your development environment as quickly and simply as possible.

Option #1

Do a manual database dump and restore on your local machine. Make a copy of all the upload collection files on your local machine or in the cloud somehow.

Pros

  • It works

Cons

  • Depending on your setup, can be confusing/time consuming for new developers
Option #2

Use this plugin to quickly copy your production database to local, with all the upload collection documents still referencing the files in your production cloud storage

Pros

  • Copy production environment to development environment at the press of a button
  • Your production cloud storage files are safe because this plugin prevents you from updating/deleting upload collection documents that were NOT created during development
  • Images/files don't have to be synced/copied to your local machine

Cons

  • The plugin does not currently work for production environments using local file storage (although it may be possible to add this as a feature in the future)

Similar plugins