Import data from a CSV file

This example shows you how to create a Smart Action "Import data" to import data from a CSV file.

Forest Admin natively supports data creation but it’s sometimes more efficient to simply import it.

Requirements

  • An admin backend running on forest-express-sequelize/forest-express-mongoose

  • bluebird npm package

  • parse-data-uri npm package

  • csv npm package

How it works

Directory: /models

This directory contains the products.js file where the model is declared.

Directory: /forest

This directory contains the products.js file where the Smart Action Import datais declared.

Directory: /routes

This directory contains the products.js file where the implementation of the route is handled. The POST /forest/actions/import-data API call is triggered when you click on the Smart Action in the Forest UI.

The CSV file passed into the body of the API call is serialized using a base64 encoding Data URI scheme.

To deserialize the base64 encoded CSV file, we use the NPM package parse-data-uri. We also use the csv parser NPM package to iterate over each line of the CSV file.

You can find a sample CSV file we use here to feed our products table on the Live demo Github repository.

You may find below the coding examples you need to make this Smart action work:

Uploading large files

Last updated

Was this helpful?