This directory contains the products.js file where the Smart Action Import datais declared.
/forest/products.js
const { collection } = require('forest-express-sequelize');
const models = require('../models');
collection('products', {
actions: [{
name: 'Import data',
endpoint: '/forest/products/actions/import-data',
type: 'global',
fields: [{
field: 'CSV file',
description: 'A semicolon separated values file stores tabular data (numbers and text) in plain text',
type: 'File',
isRequired: true
}, {
field: 'Type',
description: 'Specify the product type to import',
type: 'Enum',
enums: ['phone', 'dress', 'toy'],
isRequired: true
}]
}],
// ...
});
/forest/products.js
const { collection } = require('forest-express-mongoose');
const models = require('../models');
Liana.collection('products', {
actions: [{
name: 'Import data',
endpoint: '/forest/products/actions/import-data',
type: 'global',
fields: [{
field: 'CSV file',
description: 'A semicolon separated values file stores tabular data (numbers and text) in plain text',
type: 'File',
isRequired: true
}, {
field: 'Type',
description: 'Specify the product type to import',
type: 'Enum',
enums: ['phone', 'dress', 'toy'],
isRequired: true
}]
}],
// ...
});
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.