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

@fourlights/strapi-plugin-deep-copy

v1.0.3

Published

This plugin enables copying entities including their relations.

Downloads

167

Readme

Strapi plugin deep-copy

The default behaviour in Strapi is to create a shallow copy when duplicating an entity. This means that any relations are lost and have to be duplicated separately and connected manually to the newly created entities.

This plugin provides a custom copy action when viewing an entity in the Strapi admin which clones the entity and all it's related models, and connects the newly created entities in the same way as the original.

Screenshot

image

Usage

Install using your favourite package manager

pnpm install @fourlights/strapi-plugin-deep-copy

Then in config/plugins.js add the contentTypes for which you want to enable creating a deep copy. Any content-types not listed here will not be copied. Relations to these content types will be the same as on the original entity.

Take a look at the following config:

{
  // ...
  'deep-copy': {
    enabled: true,
    config: {
      contentTypes: {
        'api::page.page': {
          enabled: true,
          showButtonInAdmin: true,
        },
        'api::section.section': {
          enabled: true,
          showButtonInAdmin: false,
        }
      },
    },
  },
  // ...
}

This config will enable the deep copy admin button for the api::page.page content type. If you choose the copy the api::page.page entity, any relations to api::section.section will be copied as well. With this config, the copy action can only be started from the api::page.page content type.

Any other relations which are not defined in this config will be set to the same value as on the original entity. For example, the strapi internal createdBy and updatedBy will not create a new copied user, but will be set to the same user as the original entity.

Advanced usage

Unique fields

By default, the plugin will append a random hex number (copy#) to any unique fields on the newly created entities. You can override this behaviour by providing a default value function, which is used for all non-specified unique fields.

For more control you can explicitly specify unique fields and provide a special value function for each field.

Note that the strapi instance is passed to these value resolve functions, so you can also use all strapi's tooling to generate the value.

{
  // ...
  'deep-copy': {
    // ...
    config: {
      // ...
      contentTypes: {
        'api::page.page': {
          // default value function for all implicit unique fields
          defaultUniqueFieldValue: (strapi, src, name) => `${src[name]} (${new Date().toIsoFormat()})`,
          // specify explicit unique fields
          uniqueFields: {
            slug: {
              value: (strapi, src, name) => slugify(`${src.title} (copy)`),
            }
          },
        },
      },
    },
    // ...
  },
  // ...
}

Editable fields

By default, all fields are copied from the original entity. However, you can also specify which fields should be editable from the admin before deep copying an entity. This follows a similar approach to the uniquefields, but with some extra functionality.

{
  // ...
  'deep-copy': {
    // ...
    config: {
      // ...
      contentTypes: {
        'api::page.page': {
          // ...
          editableFields: {
            slug: {
              required: true,  // optional, mark field as required for deep-copy when it's not actually required
              // initial value for the editable slug field
              initialValue: (strapi, src, name) => slugify(`${src[name]} (copy)`, { strict: true }),
              // optional button for filling the field using the currently available data for the new entity (so, original + other editable fields)
              fillButton: {
                label: 'Copy from title',
                value: (data) => slugify(data.title, { strict: true }),
              },
            },  
          }
        },
      },
    },
    // ...
  },
  // ...
}

Development

You can use the playground directory for a strapi instance which is configured to use the plugin.

Automatic compilation of server part

pnpm run develop:server

Automatic compilation of admin part

pnpm run develop:admin

Start strapi server

pnpm run playground

Login to the admin with

[email protected]
Test1234