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

@acomagu/strapi-data-migrator

v0.0.2

Published

This helps you to dump data from Strapi, transform it to fit the destination Strapi and import it.

Downloads

6

Readme

The data migrator from Strapi V3 to Strapi V3

This helps you to dump data from Strapi, transform it to fit the destination Strapi and import it.

The npm package includes the 4 commands:

  • strapi-dump-data: Dumps all data as JSON from Strapi.
  • strapi-generate-types: Generates TypeScript types of the data.
  • strapi-generate-transformer: Generate a boilerplate helps you to write your own data transformer(fully typed TypeScript code).
  • strapi-import-data: Imports a JSON data into Strapi.

Typical Usecase: Dump data from Strapi, transform and import to another Strapi.

This example assumes that you have two Strapi projects, one is the source and the other is the destination.

.
├── source-strapi/
└── destination-strapi/

Step 1: Dump data and generate types from the data source Strapi

First, install the npm package at the source Strapi source code:

cd source-strapi
npm install -D @acomagu/strapi-data-migrator

Then, dump data and generate the types:

npx strapi-dump-data
npx strapi-generate-types

data.json and models.ts will be generated.

You can export data through REST API also by adding flag like npx strapi-dump-data --useapi <URL>, but it's not recommended. The internal data of some plugins can't be fetched, and if the API controller is customized, this tool may not be able to find the endpoint URL correctly.

Step 2: Generate types from the destination Strapi

Move to the destination Strapi directory and generates models.ts for destination Strapi also:

cd ../destination-strapi
npm install -D @acomagu/strapi-data-migrator
npx strapi-generate-types

Step 3: Generate transformer

Still in the destination Strapi directory, execute:

npx strapi-generate-transformer ../data-transformer ../source-strapi/models.ts ./models.ts

This command creates the boilerplate at ../data-transformer.

.
├── source-strapi/
├── destination-strapi/
└── data-transformer/
    ├── index.ts
    ├── sourceModels.ts
    ├── destinationModels.ts
    ├── package.json
    └── node_modules/

And copy data.json at Step 1 into it.

cp ../source-strapi/data.json ../data-transformer

Step 4: Complete the transformer and run

The generated index.ts is like this:

import fs from 'fs';
import typia from 'typia';
import * as source from './sourceModels.js';
import * as dest from './destModels.js';

type Data<T> = { [Name in keyof T]: T[Name][] };

// import data
const uncheckedData = JSON.parse((fs.readFileSync('data.json')).toString());
const data = typia.assert<Data<source.ModelByName>>(uncheckedData);

// Transform data to destination format.
// Write code to transform data here and fix all type errors.
// For example, if the destination user model has `first_name` and `last_name` fields instead of `name`:
//
// ```
//   "user": data.user.map((user) => ({
//     ...user,
//     first_name: user.name.split(' ')[0],
//     last_name: user.name.split(' ')[1],
//   })),
// ```
let transformedData: Data<dest.ModelByName> = {
  'post': data['post'],
  'user': data['user'],
  ...
};


// Save data to file as JSON to be imported to destination Strapi.
fs.writeFileSync('transformedData.json', JSON.stringify(transformedData));

Let's try running it.

cd ../data-transformer
npm start

If the source and destination Strapi models are different, TypeScript compile error should be present.

Fixing them is your task. Write data migration code to fit to the destination Strapi model.

If the conversion is successful, transformedData.json will be generated.

Step 5: Import data to destination Strapi

During import, several data transformations are performed (mainly ID rewriting).

We recommend that you pre-check your data with --dry-run before actually running the import.

cd ../destination-strapi
cp ../transformedData.json ./data.json
npx strapi-import-data --dry-run

written-data.json are generated. If it seems OK, then actually run the import.

npx strapi-import-data

Trouble Shooting

Importing was succeeded, but data is not present in admin panel

Check internationalization setting. Setting up the same as source Strapi may display the data.

Error while importing: Cannot find new id for ...

The entry has relation to other entry, but can't find the related entry. The relation field in which the error occurred is null, but the other fields are imported.

Correct the field mannualy after importing or correct them to the correct relation in the Transform step.

Error while importing: Couldn't create the item because of duplication error

Entry could not be imported because one of the fields in the destination model schema had a unique constraint, which was violated.

IDs of entries are changed

This is by design.