Skip to content

Commit

Permalink
Fix super slow products… finally !!!
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 6, 2024
1 parent 3367045 commit 7931d34
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
17 changes: 6 additions & 11 deletions packages/core-filters/src/module/configureFilterSearchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,12 @@ export const configureFilterSearchModule = ({
return leftIndex - rightIndex;
});

const relevantProducts = await modules.products.findProducts(
{
productSelector,
productIds: totalProductIds,
includeDrafts: searchQuery.includeInactive,
},
{
projection: { _id: 1 },
},
);
const relevantProductIds = relevantProducts.map(({ _id }) => _id);
const relevantProductIds = await modules.products.findProductIds({
productSelector,
productIds: totalProductIds,
includeDrafts: searchQuery.includeInactive,
});

return Promise.all(
sortedFilters.map(async (filter) => {
return loadFilter(
Expand Down
17 changes: 4 additions & 13 deletions packages/core-filters/src/module/configureFiltersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,10 @@ export const configureFiltersModule = async ({
);

if (!productSelector) return [];
const products = await modules.products.findProducts(
{
productSelector,
includeDrafts: true,
sort: [],
offset: 0,
limit: 0,
},
{
projection: { _id: true },
},
);
return products.map(({ _id }) => _id);
return modules.products.findProductIds({
productSelector,
includeDrafts: true,
});
};

const buildProductIdMap = async (
Expand Down
7 changes: 6 additions & 1 deletion packages/core-products/src/module/configureProductsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,24 @@ export const configureProductsModule = async ({
return Products.findOne(selector, {});
},

findProducts: async ({ limit, offset, sort, ...query }) => {
findProducts: async ({ limit, offset, sort, ...query }, options) => {
const defaultSortOption: Array<SortOption> = [
{ key: 'sequence', value: SortDirection.ASC },
{ key: 'published', value: SortDirection.DESC },
];
const products = Products.find(buildFindSelector(query), {
...(options || {}),
limit,
skip: offset,
sort: buildSortOptions(sort || defaultSortOption),
});
return products.toArray();
},

findProductIds: async (query) => {
return Products.distinct('_id', buildFindSelector(query));
},

count: async (query) => {
return Products.countDocuments(buildFindSelector(query));
},
Expand Down
6 changes: 1 addition & 5 deletions packages/plugins/src/worker/zombie-killer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ export const ZombieKillerWorker: IWorkerAdapter<
});

// Remove unreferenced product entities
const products = await modules.products.findProducts(
{ includeDrafts: true },
{ projection: { _id: 1 } },
);
const productIds = products.map((a) => a._id);
const productIds = await modules.products.findProductIds({ includeDrafts: true });
const deletedProductTextsCount = await modules.products.texts.deleteMany({
excludedProductIds: productIds,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/types/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export type ProductsModule = {
options?: FindOptions,
) => Promise<Array<Product>>;

findProductIds: (params: ProductQuery) => Promise<Array<string>>;

count: (query: ProductQuery) => Promise<number>;
productExists: (params: { productId?: string; slug?: string }) => Promise<boolean>;

Expand Down

0 comments on commit 7931d34

Please sign in to comment.