How to duplicate a Sanity Dataset for FREE

Rmag Breaking News

Updating a website’s backend can be nerve-wracking, especially when the site is live. Introducing new features often requires code changes, and there’s always a risk of something breaking unintentionally. This article explores a cost-effective way to avoid disrupting your Sanity CMS backed site during development by creating a separate development dataset.

Have you ever screwed up your own site, because you were working on something else?

I have a couple of times, and sometimes It’s understandable because you are working on fixing it and no one is looking at it. Sometimes it’s more important.

I am at that point where it matters, my client wants to start updating the backend with the content, and I’m trying to write code to implement the latest features requested.

An update that for a small bit of time would break the site. The good news is, it doesn’t have to. If we split our datasets and we do our development on another dataset we can avoid issues like these. But I also want all the content that’s already on the site.

When you look up the Sanity documentation you’ll come across a very handy option to copy the dataset into another dataset, but when you run it you this functionality requires a paid plan. However, you could do this for free too if we export the dataset and then import it into the new dataset, just a couple more steps.

Check sanity version or install a the cli tool.

npm install –global sanity@latest

# Alternatively
yarn global add sanity@latest
pnpm install –global sanity@latest

# Running the CLI without global installation
npx -y sanity@latest [command]

Login to your Sanity account using the cli tool.

sanity login

Remember to login with the correct user 😅.

Setup your cli env.

In the root of your project create a file called sanity.cli.ts or sanity.cli.js with the following content:

// sanity.cli.ts
import { defineCliConfig } from “sanity/cli”;

export default defineCliConfig({
api: {
projectId: “<your-project-id>”,
}
});

cd into the root of your project
Export your dataset

sanity dataset export <dataset-name> <export-file.tar.gz> [–types <type1,type2,…>]

dataset-name: Replace this with the actual name of the dataset you want to export (e.g., “production”).
export-file.tar.gz: Specify the desired filename and location for the exported data archive (e.g., “my-data.tar.gz”).
–types (optional): This flag allows you to export specific document types only. Separate multiple types with commas (e.g., “–types post, product”).

in my case this means:

sanity dataset export production production-file.tar.gz

when its done we can now create the new dataset.

sanity dataset create development

select your choice, for me this is a public dataset as it’s going to be used for development only.

import your downloaded file into your dataset

sanity dataset import [FILE | FOLDER | URL] [TARGET_DATASET]

for me that’s going to be:

sanity dataset import production-file.tar.gz development

Once done you have duplicated or copied your dataset for FREE!

Setup a SANITY_DATASET environment variable in your env file.

//.env.local
SANITY_DATASET=development

Update your sanity config file.

// sanity.config.ts
export const studioConfig = defineConfig({
//…rest of your config
dataset: process.env.SANITY_DATASET || “”,
})

If you have a deployed version of the site you will want to update the environment variables of your deployed site.

You probably will want to delete that file from your project root, or store it somewhere else.

Thank you for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *