fix cursor pagination + flatten variant options field
This commit is contained in:
1 parent
451e3bcdcf
commit
307b1c9342
4 files changed
+42
-7
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
export const indexTypes = {
|
||||||
|
products: "products",
|
||||||
|
}
|
||||||
@@ -60,7 +60,15 @@ class ProductSearchSubscriber {
|
|||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
],
|
],
|
||||||
relations: ["variants", "tags", "type", "collection"],
|
relations: [
|
||||||
|
"variants",
|
||||||
|
"tags",
|
||||||
|
"type",
|
||||||
|
"collection",
|
||||||
|
"variants.prices",
|
||||||
|
"variants.options",
|
||||||
|
"options",
|
||||||
|
],
|
||||||
})
|
})
|
||||||
const transformedProduct = transformProduct(product)
|
const transformedProduct = transformProduct(product)
|
||||||
return transformedProduct
|
return transformedProduct
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
const variantKeys = ["sku", "title", "upc", "ean", "mid_code", "hs_code"]
|
const variantKeys = [
|
||||||
|
"sku",
|
||||||
|
"title",
|
||||||
|
"upc",
|
||||||
|
"ean",
|
||||||
|
"mid_code",
|
||||||
|
"hs_code",
|
||||||
|
"options",
|
||||||
|
]
|
||||||
const prefix = `variant`
|
const prefix = `variant`
|
||||||
|
|
||||||
export const transformProduct = (product) => {
|
export const transformProduct = (product) => {
|
||||||
@@ -6,17 +14,25 @@ export const transformProduct = (product) => {
|
|||||||
obj[`${prefix}_${key}`] = []
|
obj[`${prefix}_${key}`] = []
|
||||||
return obj
|
return obj
|
||||||
}, {})
|
}, {})
|
||||||
|
initialObj[`${prefix}_options_value`] = []
|
||||||
|
|
||||||
const flattenedVariantFields = product.variants.reduce((obj, variant) => {
|
const flattenedVariantFields = product.variants.reduce((obj, variant) => {
|
||||||
variantKeys.forEach(
|
variantKeys.forEach((k) => {
|
||||||
(k) => variant[k] && obj[`${prefix}_${k}`].push(variant[k])
|
if (k === "options" && variant[k]) {
|
||||||
|
const values = variant[k].map((option) => option.value)
|
||||||
|
obj[`${prefix}_options_value`] = obj[`${prefix}_options_value`].concat(
|
||||||
|
values
|
||||||
)
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return variant[k] && obj[`${prefix}_${k}`].push(variant[k])
|
||||||
|
})
|
||||||
return obj
|
return obj
|
||||||
}, initialObj)
|
}, initialObj)
|
||||||
|
|
||||||
product.type_value = product.type && product.type.value
|
product.type_value = product.type && product.type.value
|
||||||
product.collection_title = product.collection && product.collection.title
|
product.collection_title = product.collection && product.collection.title
|
||||||
product.tags_value = product.tags.map((t) => t.value)
|
product.tags_value = product.tags ? product.tags.map((t) => t.value) : []
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...product,
|
...product,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ async function loadProductsIntoSearchEngine(container) {
|
|||||||
const TAKE = 20
|
const TAKE = 20
|
||||||
const totalCount = await productService.count()
|
const totalCount = await productService.count()
|
||||||
let iterCount = 0,
|
let iterCount = 0,
|
||||||
lastSeenId = "prod"
|
lastSeenId = ""
|
||||||
|
|
||||||
while (iterCount < totalCount) {
|
while (iterCount < totalCount) {
|
||||||
const products = await productService.list(
|
const products = await productService.list(
|
||||||
@@ -29,7 +29,15 @@ async function loadProductsIntoSearchEngine(container) {
|
|||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
],
|
],
|
||||||
relations: ["variants", "tags", "type", "collection"],
|
relations: [
|
||||||
|
"variants",
|
||||||
|
"tags",
|
||||||
|
"type",
|
||||||
|
"collection",
|
||||||
|
"variants.prices",
|
||||||
|
"variants.options",
|
||||||
|
"options",
|
||||||
|
],
|
||||||
take: TAKE,
|
take: TAKE,
|
||||||
order: { id: "ASC" },
|
order: { id: "ASC" },
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user