Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
improve processing of brands, categories, collections, and products b…
Browse files Browse the repository at this point in the history
…y making processing concurrent
  • Loading branch information
dillonstreator committed Oct 14, 2019
1 parent 1934285 commit 88ccc07
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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 ({
Expand Down

1 comment on commit 88ccc07

@dillonstreator
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if there is any downside to using Promise.all to perform the resource gathering and node creations. Open to any and all feedback!

Please sign in to comment.