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

@cg-marketing/strapi-plugin-migrations

v1.1.0

Published

Initialize or update your data easly with a version controller.

Downloads

43

Readme

⚠️ The current version of this plugin is working for Strapi v4. For Strapi V3 use "0.0.8" version

If you want to initialize or update automatically your data in Strapi for all of your environments, this plugin is made for you.

⏳ Installation

# with npm
npm i strapi-plugin-migrations
# with yarn
yarn add strapi-plugin-migrations

🔧 Configuration

Create the "migrations" folder in the Strapi project root or set the environment variable MIGRATION_FOLDER_PATH which is the path to your files migration folder.

Configure the plugin to your Strapi from ./config/plugins.js

// ./config/plugins.js
module.exports = {
  'migrations': {
    enabled: true,
    config: {
      autoStart: true,
      migrationFolderPath : 'migrations'
    },
  },
}

Variables

| Variable | Description | Default | |:--------------------|:-------------------------------------------------------|:-------------| | autoStart | For auto start migrations when Strapi start or restart | false | | migrationFolderPath | Path to migrations files | 'migrations' |

If you don't use autoStart your can call migrations where you want from this method :

await strapi.plugin('migrations').service('migrations').migrations()

💡 Why use it ?

Currently, with Strapi, the only way to initialize your data is to use bootstrap files, but they are launched on every reboot. Bootstraps are problematic when you want to edit existing data, such as email_reset_password.

In the native strapi code, the initialization of this data is as follows:

const pluginStore = strapi.store({
  environment: '',
  type: 'plugin',
  name: 'users-permissions',
});

if (!(await pluginStore.get({ key: 'advanced' }))) {
  const value = {
    unique_email: true,
    allow_register: true,
    email_confirmation: false,
    email_reset_password: null,
    email_confirmation_redirection: null,
    default_role: 'authenticated',
  };

  await pluginStore.set({ key: 'advanced', value });
}

So, on your project, if you want to change the value of the email_reset_password you'll want to do this:

const pluginStore = strapi.store({
  environment: '',
  type: 'plugin',
  name: 'users-permissions',
});

const values = await pluginStore.get({ key: 'advanced' });
if (values) {
  values.email_reset_password = strapi.config.custom.FRONT_URL + ‘/reset-password’
  await pluginStore.set({ key: 'advanced', value: values });
}

By doing this, if you change the email_reset_password value in the Strapi's admin and restart your server, the value you have saved will always be overwritten by the contents of your bootstrap. Which is a problem because Strapi is a CMS, the admin must be master of the data.

To counter this problem, you can use this plugin, which has a javascript file versioning system. It allows you to play code only once, even after restarting the server several times.

💪 Usage

/migrations
  /v1_edit_reset_password_url.js
  /v2_create_roles.js

Your migrations files must start with the letter v and a number and must be a javascript file.

The files are executed in ascending order, so the v1_edit_reset_password_url.js will be played before v2_create_roles.js

Once the v1 and v2 files have been executed, they will never be played by Strapi again.

You can show the migration progress in your terminal during start or restart Strapi. You can see your current migration version thanks to the url <your-strapi-url>/admin/settings/migrations

🥊 Example

You want to update automatically the reset password url for all Strapi environments. For that, you will create a javascript file in your migrations folder which start by v1 like that v1_edit_reset_password_url.js

/migrations
  /v1_edit_reset_password_url.js

You put your code to change the reset password url in this file.

// v1_edit_reset_password_url.js

module.exports = async () => {
  const pluginStore = strapi.store({
    environment: '',
    type: 'plugin',
    name: 'users-permissions',
  });

  const values = await pluginStore.get({ key: 'advanced' });
  if (values) {
    values.email_reset_password = strapi.config.custom.FRONT_URL + '/reset-password';
    
    await pluginStore.set({ key: 'advanced', value : values });
  }
};

Wait for Strapi to restart or do it manually. I advise turning off Strapi during your code implementation or add watchIgnoreFiles configuration in ./config/admin.js to cancel auto restart Strapi for each modification of the migrations folder

You should see a migration report in your terminal ⬇️

If your restart your Strapi, the file v1_edit_reset_password_url.js will not be played again.

🤝 Contributing

Feel free to fork and make a pull request of this plugin. All the input is welcome!

⭐️ Show your support

Give a star if this project helped you.