commerce/lib/orama/index.ts
2023-08-07 14:40:49 +02:00

33 lines
837 B
TypeScript

import { create, insertMultiple } from '@orama/orama';
import { getCollectionProducts, getCollections } from 'lib/shopify';
export async function createOramaInstance() {
const collections = await getCollections();
const products = await Promise.all(
collections.map(({ handle, title }) => {
return getCollectionProducts({ collection: handle }).then((products) =>
products.map((product) => ({ ...product, collection: { handle, title } }))
);
})
);
const allProducts = products
.flat()
.filter((product, index, self) => self.findIndex(({ id }) => id === product.id) === index);
const db = await create({
schema: {
title: 'string',
collection: {
handle: 'string',
title: 'string'
}
}
});
await insertMultiple(db, allProducts);
return db;
}