Strapi: Import / export data

Created on 5 Mar 2018  ·  40Comments  ·  Source: strapi/strapi

Node.js version:
8.9.4
npm version:
5.6.0
Strapi version:
latest
Operating system:
Windows 10

Do you want to request a feature or report a bug?
request a feature

Is there a simple way to upload data from csv file? For example, I would like to create new API called "products" with couple of features (size, color and so on). I do not want to add entry by entry, but prefer to upload all the products from csv file.

Is that currently possible or I need to create a plugin/develop it by myself/etc?

low feature request

Most helpful comment

Hello guy's ! @pouyamiralayi a member of the makers community made a tutorial on how to create such a plugin that allows you to import your content with csv files, url or even raw input

Here it is: https://strapi.io/blog/how-to-create-your-own-plugin-part-1-4?redirectPage=1

The code source is available here: https://github.com/strapi/strapi-tutorials/tree/master/tutorials/import-content-plugin-tutorial

He's planning on writing a second part that includes data exportation

Show him some love ❤️

All 40 comments

@Jakku7 very good idea. I think import/export from/to CSV, JSON and some other general format can be very useful.

/CC: @lauriejim, @Aurelsicoko

Hi ! Actually the best way is to create a Short script that convert your CSV data in an array of JSON object and then to loop on and make await strapi.services.product.add(entryObject);

Or find a tools that make directly an import in your database.

@lauriejim; For users who are fresh to the node ecosystem, could you do explain how they would hook a script in and be able to use that await strapi.services.product.add(entryObject); successfully?

i'd like to bump this feature request. export and import with csv would be great

I also 2nd this, it would be extremely helpful to be able to dump data from csv in quickly (export would also be very helpful but I digress, stability > feature additions)

@derrickmehaffy @kitsunekyo Did you up-vote the feature on the vote page? We're currently working on the issues which are causing the feeling that Strapi is unstable. We need to stabilize all the work we've done during the last months to get back to the development new features then.

sure :) was just wondering how you'd propose us to maybe help building such features. as this is open source after all

feel free to close the issue though if the intent is to build this inhouse for you.

@Aurelsicoko If it were 'import existing data' instead of just CSV import, it would probably be ranked higher. Many of us know how convert our existing data to CSV and import, but others might only vote if they see more general terms.

@kitsunekyo @Joshfindit If you want to help us, feel free to reach me on Slack (@aureliengeorget), it would be a pleasure to discuss with you about this feature and how we could integrate it in the Content Manager plugin. The goal is to define what we want exactly and where we should implement it in the codebase and the UI.

+1
Import/export a resource or all app resources is significant feature in many projects.
For example - cloning a base project backend.

Thanks to everyone for reminding us that you're waiting for this kind of features, I added it to my personal list of the important things to do to make Strapi a better product. I hope we will be able to release it this year 💪

@Aurelsicoko This issue can probably be closed in favor of https://portal.productboard.com/strapi/c/23-import-export-data

@derrickmehaffy Thank you 👍

Don't hesitate to give us more insights... This feature is critical and we need to understand all the use cases to build it correctly (https://portal.productboard.com/strapi/c/23-import-export-data).

I'd like to second that. End-clients like to have the ability to export their data for use in other CRMs. I believe keystoneJS has this out the box, so I think it would be good to follow suit on this!

For one-time export I used something like that:

const _ = require('lodash');
const request = require('request');
const parse = require('csv-parse');
var strapi = require('strapi')();
var fs = require('fs');

// const remoteLink = "..."
const localFile = "./data.csv"

async function fetchData() {
  return new Promise( resolve => {
    data = []
    header = null;
    // source = request(remoteLink)
    source = fs.createReadStream(localFile)
    source.pipe(parse()).on('data', (row) => {
      if(!header) { header = row; return; }
      row = row.map(item=>item=='N/A' ? '' : item);
      data.push(_.zipObject(header, row))
    }).on('end', () => {
      resolve(data);
    });
  });
}

async function initStrapi() {
  await strapi.load();
  await strapi.runBootstrapFunctions();
  return strapi;
}

async function main() {
  var [strapi, data] = await Promise.all([initStrapi(),fetchData()])
  for(var i=0;i<data.length;i++) {
      await strapi.services.shop.create(data[i]);
  }
}
main()

How kind of you! Thank you so very much!

Prior to this, my workaround was to export directly from the the database I prefer this solution because I can wire it to a custom button on the dashboard!

On Jul 8, 2019, at 8:08 PM, Pavel Razumovskiy notifications@github.com wrote:

For one-time export I used something like that:

const _ = require('lodash');
const request = require('request');
const parse = require('csv-parse');
var strapi = require('strapi')();
var fs = require('fs');

// const remoteLink = "..."
const localFile = "./data.csv"

async function fetchData() {
return new Promise( resolve => {
data = []
header = null;
// source = request(remoteLink)
source = fs.createReadStream(localFile)
source.pipe(parse()).on('data', (row) => {
if(!header) { header = row; return; }
row = row.map(item=>item=='N/A' ? '' : item);
data.push(_.zipObject(header, row))
}).on('end', () => {
resolve(data);
});
});
}

async function initStrapi() {
await strapi.load();
await strapi.runBootstrapFunctions();
return strapi;
}

async function main() {
var [strapi, data] = await Promise.all([initStrapi(),fetchData()])
for(var i=0;i await strapi.services.shop.create(data[i]);
}
}
main()

You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/strapi/strapi/issues/744?email_source=notifications&email_token=ABLSSB7ZP3Q2NONMFUKSKPTP6PJI7A5CNFSM4ETUKFR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZOWLEA#issuecomment-509437328, or mute the thread https://github.com/notifications/unsubscribe-auth/ABLSSB6H6VWY47JLPLDA4MTP6PJI7ANCNFSM4ETUKFRQ.

Hi Everyone,
I was looking for something similar and stumbled upon this plugin someone has written that seems to do the job.

https://github.com/jbeuckm/strapi-plugin-import-content

Note @JStyle21 that plugin was built for the alpha and may not work with the beta.

Thanks for pointing that out @derrickmehaffy , but it still should be easier to redo than do it from scratch or wait for this feature to be added.

For one-time export I used something like that:

const _ = require('lodash');
const request = require('request');
const parse = require('csv-parse');
var strapi = require('strapi')();
var fs = require('fs');

// const remoteLink = "..."
const localFile = "./data.csv"

async function fetchData() {
  return new Promise( resolve => {
    data = []
    header = null;
    // source = request(remoteLink)
    source = fs.createReadStream(localFile)
    source.pipe(parse()).on('data', (row) => {
      if(!header) { header = row; return; }
      row = row.map(item=>item=='N/A' ? '' : item);
      data.push(_.zipObject(header, row))
    }).on('end', () => {
      resolve(data);
    });
  });
}

async function initStrapi() {
  await strapi.load();
  await strapi.runBootstrapFunctions();
  return strapi;
}

async function main() {
  var [strapi, data] = await Promise.all([initStrapi(),fetchData()])
  for(var i=0;i<data.length;i++) {
      await strapi.services.shop.create(data[i]);
  }
}
main()

Interesting! Where do you execute this code?

  • Just another bump for this feature 👍🏼

Just stumbled upon Strapi, I think it is amazing from what I have seen so far and what I have been looking for recently, but I would love a Import/Export feature to easily create a new and more organized schema with my previous data on Strapi, love the work and keep it coming!

Hi.

yes this feature will be huge!

I Second this feature!

+1 - pretty please!

+1

It would be a great feature, +1 :)

Hello guy's ! @pouyamiralayi a member of the makers community made a tutorial on how to create such a plugin that allows you to import your content with csv files, url or even raw input

Here it is: https://strapi.io/blog/how-to-create-your-own-plugin-part-1-4?redirectPage=1

The code source is available here: https://github.com/strapi/strapi-tutorials/tree/master/tutorials/import-content-plugin-tutorial

He's planning on writing a second part that includes data exportation

Show him some love ❤️

FYI - The tutorial referenced in @Mcastres comment above has been moved to here https://strapi.io/blog/how-to-create-an-import-content-plugin-part-1-4?redirectPage=3

and thanks @pouyamiralayi

Thanks @bskimball !

Is this something that could be added to the CLI like Sanity have? https://www.sanity.io/docs/importing-data

I'm trying to import programmatically instead of via the UI.

Thank you for this feedback

Thanks for the detailed guide in the above comments, I created a plugin with very basic export / import / delete content features. Feel free to have a try.
https://github.com/lazurey/strapi-plugin-content-export-import

I was trying few plugins for excel export and they are not satisfactory. After some try i added the feature myself with help Sheetjs plugin and some customization in content-manager code.

If anyone wants you can do it by

  1. Installing Sheetjs plugin with
    yarn add xlsx
  1. Unzip and place the index.zip file in path project_directory/extensions/content-manager/admin/src/containers/ListView
    Create the directory if it does not exist.

  2. You will get a export button near add new button.

@lazurey Can you make it installable from yarn/npm?

Hi @SalahAdDin not sure how you gonna use it via npm, but it is available on npm now.
https://www.npmjs.com/package/strapi-plugin-content-export-import

Thanks @lazurey !

Is it possible to have export option for selected Content Types? How to filter it?

Simple one minute solution for export users:

'use strict';

const fs = require('fs');
const { ExportToCsv } =  require('export-to-csv');

/**
 * Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
 * to customize this controller
 */

module.exports = {
  users: async (ctx, next) => {

    const user = ctx.state.user;

    if (!user) {
      return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
    }

    const users = await strapi.plugins['users-permissions'].services.user.fetchAll({ _limit: 1000000});

    const exportOptions = {
      fieldSeparator: ',',
      quoteStrings: '"',
      decimalSeparator: '.',
      showLabels: true,
      showTitle: false,
      title: '',
      useTextFile: false,
      useBom: true,
      useKeysAsHeaders: true,
      // headers: ['Column 1', 'Column 2', etc...] <-- Won't work with useKeysAsHeaders present!
    };
    const rootDir = process.cwd();
    const csvExporter = new ExportToCsv(exportOptions);
    const csvData = csvExporter.generateCsv(users, true);
    fs.writeFileSync(rootDir + '/public/mtmqppziixnwahbk.csv',csvData)
    ctx.body = fs.createReadStream(rootDir + '/public/mtmqppziixnwahbk.csv');
    ctx.set('Content-disposition', 'attachment; filename=users.csv');
    ctx.set('Content-type', 'text/csv');
  }
};

@Dres90 Did you handle to deploy strapi with that plugin?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hkovacs picture hkovacs  ·  43Comments

lucaperret picture lucaperret  ·  36Comments

niallobrien picture niallobrien  ·  47Comments

dsheyp picture dsheyp  ·  46Comments

Cpaczek picture Cpaczek  ·  44Comments