From 88ccc0756374078830fb22c0729243ca555506f5 Mon Sep 17 00:00:00 2001 From: dillon Date: Sun, 13 Oct 2019 21:27:55 -0700 Subject: [PATCH] improve processing of brands, categories, collections, and products by making processing concurrent --- gatsby-node.js | 57 +++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 8158eed..44f049d 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -202,13 +202,17 @@ exports.sourceNodes = async ( } } - const { data: brands } = await moltin.get('brands') - const { data: categories } = await getPaginatedResource('categories') - const { data: collections } = await getPaginatedResource('collections') - const { - data: products, - included: { main_images = {}, files = [] } = {} - } = await getPaginatedResource('products', {}, `?include=main_image,files`) + const [ + { data: brands }, + { data: categories }, + { data: collections }, + { data: products, included: { main_images = {}, files = [] } = {} } + ] = await Promise.all([ + moltin.get('brands'), + getPaginatedResource('categories'), + getPaginatedResource('collections'), + getPaginatedResource('products', {}, `?include=main_image,files`) + ]) const createCategories = async ({ categories }) => { categories.forEach(async category => @@ -226,39 +230,30 @@ exports.sourceNodes = async ( brands.forEach(async brand => createNode(await processBrand({ brand }))) } - const createProducts = async ({ - products, - main_images, - categories, - collections, - brands, - files - }) => { + const createProducts = async ({ products, ...rest }) => { products.forEach(async product => createNode( await processProduct({ product, - main_images, - categories, - collections, - brands, - files + ...rest }) ) ) } - await createProducts({ - products, - main_images, - categories, - collections, - brands, - files - }) - await createCollections({ collections }) - await createCategories({ categories }) - await createBrands({ brands }) + await Promise.all([ + createProducts({ + products, + main_images, + categories, + collections, + brands, + files + }), + createCollections({ collections }), + createCategories({ categories }), + createBrands({ brands }) + ]) } exports.onCreateNode = async ({