npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dotdev/cross-dataset-duplicator

v1.0.3

Published

Empower content editors to migrate Documents and Assets between Sanity Projects and Datasets from inside Sanity Studio

Downloads

9

Readme

Fork information

This package is a fork allowing to duplicate document across dataset using a field rather than the document ID. See "2.2 Fork extra configuration"

Cross Dataset Duplicator

Sanity Studio Tool and Document Action for empowering content editors to migrate Documents and Assets between Sanity Datasets and Projects from inside the Studio.

Install

From the root directory of your studio

sanity install @dotdev/cross-dataset-duplicator

Important Notes

This plugin is designed as a convenience for Authors to make small, infrequent content migrations between Datasets.

  • This plugin should be used in conjunction with a reliable backup strategy.
  • Proceed with caution as this plugin can instantly write changes to Datasets.
  • Larger migrations may take more time, especially with Assets. The plugin tries to mitigate this with rate limiting asset uploads to 3 at a time.
  • If an Asset is already present at the destination, there's no need to duplicate it again.
  • Before starting a Duplication you can select which Documents and Assets to include. Migrations will fail if every Referenced Document or Asset is not included in the transaction or already present at the destination Dataset.

Tool

The Duplicate Tool allows you to migrate Documents that are returned from any GROQ query.

2022-04-04 13 23 57

Document Action

The Duplicate to... Document Action allows you to migrate an individual Document.

2022-04-04 13 52 14

Note: If your Studio registered its own Document Actions, the plugin config will be overruled. See "Importing the Document Action" below.

Required Setup

1. Spaces

You must have Spaces configured to use this plugin. Spaces are still listed as an experimental feature but have been supported for some time.

All Datasets setup in Spaces will become selectable "destinations" for Migrations.

Once setup, you will see a dropdown menu next to the Search bar in the Studio with the Datasets you have configured in Spaces.

2. Configuration

The plugin has some configuration options. These can be set by adding a config file to your Studio

2.1 Base configuration

// ./config/@sanity/cross-dataset-duplicator.json
{
  "tool": true,
  "types": ["article", "page"]
}

Options:

  • tool (boolean, default: true) – Set whether the Migration Tool is enabled.
  • types (Array[String], default: []) – Set which Schema Types the Migration Action should be enabled in.

2.2 Fork extra configuration

With our update, developers can now additionally customize the fetchBy settings to copy content across datasets based on any field they wish to use as a reference, instead of only the id.

Example to duplicate products and collections by handle:

{
  "tool": true,
  "fetchBy": {
    "product": {
      "fetchBy": "shopify.handle",
      "ignore": ["shopify", "title", "_type"]
    },
    "collection": {
      "fetchBy": "shopify.handle",
      "ignore": ["shopify", "title", "_type"]
    }
  }
}

The ignore option allow the plugin to prevent the override of specific fields. For instance, when you migrate the content from a dataset to another you don't wan't override the shopify fields.

3. Authentication Key

To Duplicate the original files of Assets, an API Token with Viewer permissions is required. You will be prompted for this the first time you attempt to use either the Tool or Document Action on any Dataset.

This plugin uses Sanity Secrets to store the token in the Dataset itself.

You can create API tokens in manage

4. CORS origins

If you want to duplicate data across different projects, you need to enable CORS for the different hosts. This allows different projects to connect to each other through the project API. CORS origins configuration can be found in your project page, under the API tab.

Importing the Document Action

In your Studio's sanity.json file, look for the document-actions/resolver part, it will look like this:

{
  "implements": "part:@sanity/base/document-actions/resolver",
  "path": "./src/document-actions"
}

Now update your Studio's Document Actions resolver to be something like this

import defaultResolve from 'part:@sanity/base/document-actions'
import {DuplicateToAction} from '@sanity/cross-dataset-duplicator'
import config from 'config:@sanity/cross-dataset-duplicator'

export default function resolveDocumentActions(props) {
  const defaultActions = defaultResolve(props)

  // This will look through the "types" array in your migration.json config file
  // If the type of this document is found in that array, the Migrate Action will show
  if (config?.types?.length && config.types.includes(props.type)) {
    return [...defaultActions, DuplicateToAction]
  }

  // ...all your other document action code

  return defaultActions
}

Future feature ideas

  • Save predefined GROQ queries in the Tool to make bulk repeated Migrations simpler
  • Config options for allowed migrations (eg Dev -> Staging but not Dev -> Live)
  • Config options for permissions/user role checks