Vendure - Importing data

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Product imports are an essential element that I found great when working with Vendure. It was super important to me to be able to import products and their data.
Many people might have the exact needs as they often already have a webshop or system to keep track of these things.
In this article, we'll look at how we can use the data import feature of Vendure.
When importing products, Vendure accepts a flat .csv format, and it can hold products, assets, variants, and even custom fields.
These elements will be created during the import, which is excellent.
You can find an example of the CSV format on the Vendure docs
The are multiple ways of doing the import, and I decided I wanted to be able to run it manually, so I created my script for it.
I called the script populate-server.ts.
It's important to note that this uses the Vendure core populate function, which takes two parameters, the first being the initial data and the second your import.
My complete file looks like this.
// populate-server.ts
import { bootstrap } from '@vendure/core';
import { populate } from '@vendure/core/cli';
import path from 'path';
import { config } from './src/vendure-config';
import { initialData } from './my-initial-data';
const productsCsvFile = path.join(__dirname, 'import.csv');
populate(() => bootstrap(config), initialData, productsCsvFile)
.then((app) => {
return app.close();
})
.then(
() => process.exit(0),
(err) => {
console.log(err);
process.exit(1);
}
);
Let's go ahead and also create this my-initial-data.ts file.
This file can be used to set up the admin section of your Vendure server.
In my case, I limited quite a bit of the setup, and my file looks like this.
import { InitialData, LanguageCode } from '@vendure/core';
export const initialData: InitialData = {
paymentMethods: [],
defaultLanguage: LanguageCode.en,
countries: [],
defaultZone: 'SA',
taxRates: [],
shippingMethods: [],
collections: [],
};
You can find the complete object you can construct on the Vendure docs.
Note: Every time you run your import, these will be executed, so running it more than once will result in duplicates.
To run the actual import, we can use the following command:
yarn ts-node populate-server.ts
Depending on the number of products, it can take some time to finish.
I often ran the script for testing purposes to get the proper import set. You might have to play around with your CSV format if importing many custom fields this way.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter