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

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 schemearrow-up-right.

To deserialize the base64 encoded CSV file, we use the NPM package parse-data-uriarrow-up-right. We also use the csv parserarrow-up-right 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 repositoryarrow-up-right.

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

Uploading large files

circle-exclamation

Last updated