Adds an admin button to toggle the Payload database connection between production and development, or to copy the production database into a development one. It works with the MongoDB adapter and production setups that use a cloud storage adapter for uploads. It must be last in the plugins array, after the cloud storage plugin.
Payload plugin to switch environments
Installation
npm i @elliott-w/payload-plugin-switch-env5
Stars
1
Forks
0
Open Issues
3 months ago
Last Updated
0
Weekly Downloads
v2.0.3
npm Version
235.8 KB
Unpacked Size
0
Dependencies
Trends appear after 7 days of data. Daily snapshots have not started yet.
Trends appear after 7 days of data. Daily snapshots have not started yet.
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/885749499-root)<a href="https://payloaddirectory.dev/plugins/885749499-root"><img src="https://payloaddirectory.dev/badge/885749499-root.svg" alt="Payload Directory"></a>README
⚠️ This plugin has moved location to: https://github.com/what-works-global/payload-packages/tree/main/packages/switch-env
pnpm i @elliott-w/payload-plugin-switch-env
⚠️ Warning: The switchEnv plugin must come last in the plugins array. Your cloud storage plugin must be second last.
// 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,
},
],
},
},
],
})
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.Does not support database migrations.
Only works with:
Notes:
developmentFileStorage.
You're working with a production database that is getting frequently updated and you want to push a feature that requires code + database changes.
Quickly clone the database to your local machine, make the data changes and then overwrite the production database with your local database.
Pros
Cons
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:
Cons:
You want to replicate the production environment to your development environment as quickly and simply as possible.
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
Cons
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
Cons
switch env
Switch the local admin between development and production databases and copy production data into development
590/wk15v3
payload plugin janitor
Find orphaned files, missing uploads and unreferenced documents, then delete only what you approve
7031/wk5v4
payload upstash
Upstash Redis key-value adapter for Payload's built-in KV store
690/wk7v3
payload revalidate
Field-driven Next.js cache revalidation and tagged cached finders for Payload collections and globals
67151/wk18v3
payloadcms redis cache
Redis client helper for caching Payload collection data in Next.js apps
410/wk2v?
payloadcms redis plugin
Transparent Redis caching layer for Payload database read queries with automatic invalidation on writes
400/wk15v3