feat(medusa-plugin-algolia): Revamp Algolia search plugin (#3510)

This commit is contained in:
Oliver Windall Juhl
2023-03-22 12:55:26 +01:00
committed by GitHub
parent ef5ef9f5a2
commit 74bc4b16a0
43 changed files with 518 additions and 454 deletions
-4
View File
@@ -35,9 +35,5 @@
"medusa-test-utils": "^1.1.37",
"typescript": "^4.4.4"
},
"peerDependencies": {
"medusa-core-utils": "^1.1.39",
"typeorm": "0.x"
},
"gitHead": "cd1f5afa5aa8c0b15ea957008ee19f1d695cbd2e"
}
-13
View File
@@ -1,13 +0,0 @@
{
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-instanceof",
"@babel/plugin-transform-classes"
],
"presets": ["@babel/preset-env"],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
@@ -1,9 +0,0 @@
.DS_store
src
dist
yarn.lock
.babelrc
jest.config.js
.turbo
.yarn
+7 -4
View File
@@ -8,14 +8,17 @@ Learn more about how you can use this plugin in the [documentaion](https://docs.
```js
{
application_id: "someId",
admin_api_key: "someApiKey",
applicationId: "someId",
adminApiKey: "someApiKey",
settings: {
[indexName]: [algolia settings passed to algolia's `updateSettings()` method]
// example
products: {
searchableAttributes: ["title", "description", "variant_sku", "type_value"],
attributesToRetrieve: ["title", "description", "variant_sku", "type_value"],
indexSettings: {
searchableAttributes: ["title", "description", "variant_sku", "type_value"],
attributesToRetrieve: ["title", "description", "variant_sku", "type_value"],
}
transformer: (product: Product) => ({ id: product.id })
}
}
}
-1
View File
@@ -1 +0,0 @@
//noop
+11 -24
View File
@@ -1,46 +1,33 @@
{
"name": "medusa-plugin-algolia",
"version": "0.2.9",
"description": "Search support for algolia",
"main": "index.js",
"description": "Algolia search plugin for Medusa",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-plugin-algolia"
},
"author": "rolwin100",
"files": [
"dist"
],
"author": "Medusa",
"license": "MIT",
"scripts": {
"prepare": "cross-env NODE_ENV=production yarn run build",
"test": "jest --passWithNoTests src",
"build": "babel src --out-dir . --ignore '**/__tests__','**/__mocks__'",
"watch": "babel -w src --out-dir . --ignore '**/__tests__','**/__mocks__'"
},
"peerDependencies": {
"medusa-interfaces": "1.3.6",
"typeorm": "0.x"
"build": "tsc",
"watch": "tsc --watch"
},
"dependencies": {
"algoliasearch": "^4.10.5",
"body-parser": "^1.19.0",
"lodash": "^4.17.21",
"medusa-core-utils": "^1.1.39",
"medusa-interfaces": "^1.3.6"
"@medusajs/utils": "*",
"algoliasearch": "^4.15.0"
},
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/node": "^7.7.4",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-instanceof": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"@babel/register": "^7.7.4",
"@babel/runtime": "^7.9.6",
"@medusajs/types": "*",
"client-sessions": "^0.8.0",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"medusa-interfaces": "^1.3.6"
"typescript": "^4.4.4"
},
"gitHead": "cd1f5afa5aa8c0b15ea957008ee19f1d695cbd2e",
"keywords": [
@@ -1,13 +0,0 @@
export default async (container, options) => {
try {
const algoliaService = container.resolve("algoliaService")
await Promise.all(
Object.entries(options.settings).map(([key, value]) =>
algoliaService.updateSettings(key, value)
)
)
} catch (err) {
console.log(err)
}
}
@@ -0,0 +1,24 @@
import { Logger, MedusaContainer } from "@medusajs/modules-sdk"
import AlgoliaService from "../services/algolia"
import { AlgoliaPluginOptions } from "../types"
export default async (
container: MedusaContainer,
options: AlgoliaPluginOptions
) => {
const logger: Logger = container.resolve("logger")
try {
const algoliaService: AlgoliaService = container.resolve("algoliaService")
const { settings } = options
await Promise.all(
Object.entries(settings || {}).map(async ([indexName, value]) => {
return await algoliaService.updateSettings(indexName, value)
})
)
} catch (err) {
// ignore
logger.warn(err)
}
}
@@ -1,24 +1,31 @@
import algoliasearch from "algoliasearch"
import { indexTypes } from "medusa-core-utils"
import { SearchService } from "medusa-interfaces"
import { transformProduct } from "../utils/transform-product"
import { SearchTypes } from "@medusajs/types"
import { SearchUtils } from "@medusajs/utils"
import Algolia, { SearchClient } from "algoliasearch"
import { AlgoliaPluginOptions, SearchOptions } from "../types"
import { transformProduct } from "../utils/transformer"
class AlgoliaService extends SearchService {
constructor(container, options) {
super()
class AlgoliaService extends SearchUtils.AbstractSearchService {
isDefault = false
this.options_ = options
const { application_id, admin_api_key } = this.options_
protected readonly config_: AlgoliaPluginOptions
protected readonly client_: SearchClient
if (!application_id) {
constructor(_, options: AlgoliaPluginOptions) {
super(_, options)
this.config_ = options
const { applicationId, adminApiKey } = options
if (!applicationId) {
throw new Error("Please provide a valid Application ID")
}
if (!admin_api_key) {
if (!adminApiKey) {
throw new Error("Please provide a valid Admin Api Key")
}
this.client_ = algoliasearch(application_id, admin_api_key)
this.client_ = Algolia(applicationId, adminApiKey)
}
/**
@@ -27,7 +34,7 @@ class AlgoliaService extends SearchService {
* @param {*} options - not required just to match the schema we are used it
* @return {*}
*/
createIndex(indexName, options = {}) {
createIndex(indexName: string, options: Record<string, unknown> = {}) {
return this.client_.initIndex(indexName)
}
@@ -36,8 +43,9 @@ class AlgoliaService extends SearchService {
* @param {string} indexName - the index name.
* @return {Promise<{object}>} - returns response from search engine provider
*/
async getIndex(indexName) {
let hits = []
async getIndex(indexName: string) {
let hits: Record<string, unknown>[] = []
return await this.client_
.initIndex(indexName)
.browseObjects({
@@ -56,8 +64,12 @@ class AlgoliaService extends SearchService {
* @param {*} type
* @return {*}
*/
async addDocuments(indexName, documents, type) {
const transformedDocuments = this.getTransformedDocuments(type, documents)
async addDocuments(indexName: string, documents: any, type: string) {
const transformedDocuments = await this.getTransformedDocuments(
type,
documents
)
return await this.client_
.initIndex(indexName)
.saveObjects(transformedDocuments)
@@ -70,8 +82,11 @@ class AlgoliaService extends SearchService {
* @param {Array.<Object>} type - type of documents to be replaced (e.g: products, regions, orders, etc)
* @return {Promise<{object}>} - returns response from search engine provider
*/
async replaceDocuments(indexName, documents, type) {
const transformedDocuments = this.getTransformedDocuments(type, documents)
async replaceDocuments(indexName: string, documents: any, type: string) {
const transformedDocuments = await this.getTransformedDocuments(
type,
documents
)
return await this.client_
.initIndex(indexName)
.replaceAllObjects(transformedDocuments)
@@ -80,11 +95,11 @@ class AlgoliaService extends SearchService {
/**
* Used to delete document
* @param {string} indexName - the index name
* @param {string} document_id - the id of the document
* @param {string} documentId - the id of the document
* @return {Promise<{object}>} - returns response from search engine provider
*/
async deleteDocument(indexName, document_id) {
return await this.client_.initIndex(indexName).deleteObject(document_id)
async deleteDocument(indexName: string, documentId: string) {
return await this.client_.initIndex(indexName).deleteObject(documentId)
}
/**
@@ -92,7 +107,7 @@ class AlgoliaService extends SearchService {
* @param {string} indexName - the index name
* @return {Promise<{object}>} - returns response from search engine provider
*/
async deleteAllDocuments(indexName) {
async deleteAllDocuments(indexName: string) {
return await this.client_.initIndex(indexName).delete()
}
@@ -105,9 +120,15 @@ class AlgoliaService extends SearchService {
* - additionalOptions contain any provider specific options
* @return {*} - returns response from search engine provider
*/
async search(indexName, query, options) {
async search(
indexName: string,
query: string,
options: SearchOptions & Record<string, unknown>
) {
const { paginationOptions, filter, additionalOptions } = options
if ("limit" in paginationOptions) {
// fit our pagination options to what Algolia expects
if ("limit" in paginationOptions && paginationOptions.limit != null) {
paginationOptions["length"] = paginationOptions.limit
delete paginationOptions.limit
}
@@ -125,25 +146,32 @@ class AlgoliaService extends SearchService {
* @param {object} settings - settings object
* @return {Promise<{object}>} - returns response from search engine provider
*/
async updateSettings(indexName, settings) {
return await this.client_.initIndex(indexName).setSettings(settings)
async updateSettings(
indexName: string,
settings: SearchTypes.IndexSettings & Record<string, unknown>
) {
// backward compatibility
const indexSettings = settings.indexSettings ?? settings ?? {}
return await this.client_.initIndex(indexName).setSettings(indexSettings)
}
getTransformedDocuments(type, documents) {
async getTransformedDocuments(type: string, documents: any[]) {
if (!documents?.length) {
return []
}
switch (type) {
case indexTypes.products:
return this.transformProducts(documents)
case SearchTypes.indexTypes.PRODUCTS:
const productsTransformer =
this.config_.settings?.[SearchTypes.indexTypes.PRODUCTS]
?.transformer ?? transformProduct
return documents.map(productsTransformer)
default:
return documents
}
}
transformProducts(products) {
if (!products) {
return []
}
return products.map(transformProduct)
}
}
export default AlgoliaService
@@ -0,0 +1,18 @@
import { SearchTypes } from "@medusajs/types"
export type SearchOptions = {
paginationOptions: Record<string, unknown>
filter: string
additionalOptions: Record<string, unknown>
}
export type AlgoliaPluginOptions = {
applicationId: string
adminApiKey: string
/**
* Index settings
*/
settings?: {
[key: string]: SearchTypes.IndexSettings
}
}
@@ -1,18 +1,8 @@
import { Product } from "@medusajs/medusa"
const variantKeys = [
"sku",
"title",
"upc",
"ean",
"mid_code",
"hs_code",
"options",
]
import { variantKeys } from "@medusajs/types"
const prefix = `variant`
export const transformProduct = (product: Product) => {
export const transformProduct = (product: any) => {
let transformedProduct = { ...product } as Record<string, unknown>
const initialObj = variantKeys.reduce((obj, key) => {
@@ -34,6 +24,7 @@ export const transformProduct = (product: Product) => {
return obj
}, initialObj)
transformedProduct.objectID = product.id
transformedProduct.type_value = product.type && product.type.value
transformedProduct.collection_title =
product.collection && product.collection.title
@@ -42,10 +33,10 @@ export const transformProduct = (product: Product) => {
transformedProduct.tags_value = product.tags
? product.tags.map((t) => t.value)
: []
transformedProduct.categories = (product?.categories || []).map(c => c.name)
transformedProduct.categories = (product?.categories || []).map((c) => c.name)
const prod = {
...product,
...transformedProduct,
...flattenedVariantFields,
}
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"lib": ["es2020"],
"target": "es2020",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true // to use ES5 specific tooling
},
"include": ["src"],
"exclude": [
"dist",
"src/**/__tests__",
"src/**/__mocks__",
"src/**/__fixtures__",
"node_modules"
]
}
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["node_modules"]
}
@@ -18,21 +18,14 @@
"build": "tsc",
"watch": "tsc --watch"
},
"peerDependencies": {
"@medusajs/medusa": "^1.7.12",
"medusa-interfaces": "^1.3.6"
},
"dependencies": {
"body-parser": "^1.19.0",
"lodash": "^4.17.21",
"medusa-core-utils": "^1.1.39",
"@medusajs/utils": "*",
"meilisearch": "^0.31.1"
},
"devDependencies": {
"@medusajs/medusa": "^1.7.12",
"@medusajs/types": "*",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"medusa-interfaces": "^1.3.6",
"typescript": "^4.9.5"
},
"gitHead": "cd1f5afa5aa8c0b15ea957008ee19f1d695cbd2e",
@@ -15,9 +15,9 @@ export default async (
const { settings } = options
await Promise.all(
Object.entries(settings ?? []).map(([indexName, value]) =>
meilisearchService.updateSettings(indexName, value)
)
Object.entries(settings || {}).map(async ([indexName, value]) => {
return await meilisearchService.updateSettings(indexName, value)
})
)
} catch (err) {
// ignore
@@ -1,10 +1,10 @@
import { AbstractSearchService } from "@medusajs/medusa"
import { indexTypes } from "medusa-core-utils"
import { SearchTypes } from "@medusajs/types"
import { SearchUtils } from "@medusajs/utils"
import { MeiliSearch, Settings } from "meilisearch"
import { IndexSettings, meilisearchErrorCodes, MeilisearchPluginOptions } from "../types"
import { transformProduct } from "../utils/transform-product"
import { meilisearchErrorCodes, MeilisearchPluginOptions } from "../types"
import { transformProduct } from "../utils/transformer"
class MeiliSearchService extends AbstractSearchService {
class MeiliSearchService extends SearchUtils.AbstractSearchService {
isDefault = false
protected readonly config_: MeilisearchPluginOptions
@@ -77,21 +77,17 @@ class MeiliSearchService extends AbstractSearchService {
async updateSettings(
indexName: string,
settings: IndexSettings | Record<string, unknown>
settings: SearchTypes.IndexSettings & Settings
) {
// backward compatibility
if (!("indexSettings" in settings)) {
settings = { indexSettings: settings }
}
const indexSettings = settings.indexSettings ?? settings ?? {}
await this.upsertIndex(indexName, settings as IndexSettings)
await this.upsertIndex(indexName, settings)
return await this.client_
.index(indexName)
.updateSettings(settings.indexSettings as Settings)
return await this.client_.index(indexName).updateSettings(indexSettings)
}
async upsertIndex(indexName: string, settings: IndexSettings) {
async upsertIndex(indexName: string, settings: SearchTypes.IndexSettings) {
try {
await this.client_.getIndex(indexName)
} catch (error) {
@@ -104,15 +100,15 @@ class MeiliSearchService extends AbstractSearchService {
}
getTransformedDocuments(type: string, documents: any[]) {
switch (type) {
case indexTypes.products:
if (!documents?.length) {
return []
}
if (!documents?.length) {
return []
}
switch (type) {
case SearchTypes.indexTypes.PRODUCTS:
const productsTransformer =
this.config_.settings?.[indexTypes.products]?.transformer ??
transformProduct
this.config_.settings?.[SearchTypes.indexTypes.PRODUCTS]
?.transformer ?? transformProduct
return documents.map(productsTransformer)
default:
@@ -1,4 +1,5 @@
import { Config, Settings } from "meilisearch"
import { SearchTypes } from "@medusajs/types"
import { Config } from "meilisearch"
export const meilisearchErrorCodes = {
INDEX_NOT_FOUND: "index_not_found",
@@ -13,21 +14,6 @@ export interface MeilisearchPluginOptions {
* Index settings
*/
settings?: {
[key: string]: IndexSettings
[key: string]: SearchTypes.IndexSettings
}
}
export type IndexSettings = {
/**
* Settings specific to the provider. E.g. `searchableAttributes`.
*/
indexSettings: Settings
/**
* Primary key for the index. Used to enforce unique documents in an index. See more in Meilisearch' https://docs.meilisearch.com/learn/core_concepts/primary_key.html.
*/
primaryKey?: string
/**
* Document transformer. Used to transform documents before they are added to the index.
*/
transformer?: (document: any) => any
}
}
@@ -1,15 +1,10 @@
const variantKeys = [
"sku",
"title",
"upc",
"ean",
"mid_code",
"hs_code",
"options",
]
import { variantKeys } from "@medusajs/types"
const prefix = `variant`
export const transformProduct = (product) => {
export const transformProduct = (product: any) => {
let transformedProduct = { ...product } as Record<string, unknown>
const initialObj = variantKeys.reduce((obj, key) => {
obj[`${prefix}_${key}`] = []
return obj
@@ -29,14 +24,20 @@ export const transformProduct = (product) => {
return obj
}, initialObj)
product.objectID = product.id
product.type_value = product.type && product.type.value
product.collection_title = product.collection && product.collection.title
product.collection_handle = product.collection && product.collection.handle
product.tags_value = product.tags ? product.tags.map((t) => t.value) : []
transformedProduct.type_value = product.type && product.type.value
transformedProduct.collection_title =
product.collection && product.collection.title
transformedProduct.collection_handle =
product.collection && product.collection.handle
transformedProduct.tags_value = product.tags
? product.tags.map((t) => t.value)
: []
transformedProduct.categories = (product?.categories || []).map((c) => c.name)
return {
...product,
const prod = {
...transformedProduct,
...flattenedVariantFields,
}
return prod
}
@@ -1,11 +1,7 @@
{
"compilerOptions": {
"lib": [
"es5",
"es6",
"es2019"
],
"target": "es5",
"lib": ["es2020"],
"target": "es2020",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["node_modules"]
}
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["node_modules"]
}
-1
View File
@@ -7,7 +7,6 @@ export * from "./notification-service"
export * from "./payment-processor"
export * from "./payment-service"
export * from "./price-selection-strategy"
export * from "./search-service"
export * from "./services"
export * from "./tax-calculation-strategy"
export * from "./tax-service"
@@ -1,120 +0,0 @@
import { SearchService } from "medusa-interfaces"
export interface ISearchService {
options: Record<string, unknown>
/**
* Used to create an index
* @param indexName the index name
* @param options the options
* @return returns response from search engine provider
*/
createIndex(indexName: string, options: unknown): unknown
/**
* Used to get an index
* @param indexName - the index name.
* @return returns response from search engine provider
*/
getIndex(indexName: string): unknown
/**
* Used to index documents by the search engine provider
* @param indexName the index name
* @param documents documents array to be indexed
* @param type of documents to be added (e.g: products, regions, orders, etc)
* @return returns response from search engine provider
*/
addDocuments(indexName: string, documents: unknown, type: string): unknown
/**
* Used to replace documents
* @param indexName the index name.
* @param documents array of document objects that will replace existing documents
* @param type type of documents to be replaced (e.g: products, regions, orders, etc)
* @return returns response from search engine provider
*/
replaceDocuments(indexName: string, documents: unknown, type: string): unknown
/**
* Used to delete document
* @param indexName the index name
* @param document_id the id of the document
* @return returns response from search engine provider
*/
deleteDocument(indexName: string, document_id: string | number): unknown
/**
* Used to delete all documents
* @param indexName the index name
* @return returns response from search engine provider
*/
deleteAllDocuments(indexName: string): unknown
/**
* Used to search for a document in an index
* @param indexName the index name
* @param query the search query
* @param options
* - any options passed to the request object other than the query and indexName
* - additionalOptions contain any provider specific options
* @return returns response from search engine provider
*/
search(indexName: string, query: string | null, options: unknown): unknown
/**
* Used to update the settings of an index
* @param indexName the index name
* @param settings settings object
* @return returns response from search engine provider
*/
updateSettings(indexName: string, settings: unknown): unknown
}
export abstract class AbstractSearchService implements ISearchService {
abstract readonly isDefault
protected readonly options_: Record<string, unknown>
get options(): Record<string, unknown> {
return this.options_
}
protected constructor(container, options) {
this.options_ = options
}
abstract createIndex(indexName: string, options: unknown): unknown
abstract getIndex(indexName: string): unknown
abstract addDocuments(
indexName: string,
documents: unknown,
type: string
): unknown
abstract replaceDocuments(
indexName: string,
documents: unknown,
type: string
): unknown
abstract deleteDocument(
indexName: string,
document_id: string | number
): unknown
abstract deleteAllDocuments(indexName: string): unknown
abstract search(
indexName: string,
query: string | null,
options: unknown
): unknown
abstract updateSettings(indexName: string, settings: unknown): unknown
}
export function isSearchService(obj: unknown): boolean {
return obj instanceof AbstractSearchService || obj instanceof SearchService
}
+2 -2
View File
@@ -1,3 +1,4 @@
import { SearchUtils } from "@medusajs/utils"
import { aliasTo, asFunction, asValue, Lifetime } from "awilix"
import { Express } from "express"
import fs from "fs"
@@ -20,7 +21,6 @@ import {
isFileService,
isNotificationService,
isPriceSelectionStrategy,
isSearchService,
isTaxCalculationStrategy,
} from "../interfaces"
import { MiddlewareService } from "../services"
@@ -443,7 +443,7 @@ export async function registerServices(
),
[`fileService`]: aliasTo(name),
})
} else if (isSearchService(loaded.prototype)) {
} else if (SearchUtils.isSearchService(loaded.prototype)) {
// Add the service directly to the container in order to make simple
// resolution if we already know which search provider we need to use
container.register({
+1 -1
View File
@@ -1,4 +1,4 @@
import { AbstractSearchService } from "../interfaces"
import { AbstractSearchService } from "@medusajs/utils"
import { EventBusService } from "../services"
import { Logger, MedusaContainer } from "../types/global"
+1 -1
View File
@@ -1,5 +1,5 @@
import { AbstractSearchService } from "@medusajs/utils"
import { EntityManager } from "typeorm"
import { AbstractSearchService } from "../interfaces/search-service"
import { Logger } from "../types/global"
type InjectedDependencies = {
@@ -1,6 +1,5 @@
import { EventBusTypes } from "@medusajs/types"
import { IEventBusService, ISearchService } from "@medusajs/types"
import { indexTypes } from "medusa-core-utils"
import { ISearchService } from "../interfaces"
import ProductCategoryFeatureFlag from "../loaders/feature-flags/product-categories"
import { SEARCH_INDEX_EVENT } from "../loaders/search-index"
import { Product } from "../models"
@@ -8,14 +7,14 @@ import ProductService from "../services/product"
import { FlagRouter } from "../utils/flag-router"
type InjectedDependencies = {
eventBusService: EventBusTypes.IEventBusService
eventBusService: IEventBusService
searchService: ISearchService
productService: ProductService
featureFlagRouter: FlagRouter
}
class SearchIndexingSubscriber {
private readonly eventBusService_: EventBusTypes.IEventBusService
private readonly eventBusService_: IEventBusService
private readonly searchService_: ISearchService
private readonly productService_: ProductService
private readonly featureFlagRouter_: FlagRouter
+1
View File
@@ -1,4 +1,5 @@
export * as CommonTypes from "./common"
export * as EventBusTypes from "./event-bus"
export * as SearchTypes from "./search"
export * as TransactionBaseTypes from "./transaction-base"
+2
View File
@@ -1,5 +1,7 @@
export * from "./bundles"
export * from "./common"
export * from "./event-bus"
export * from "./search"
export * from "./shared-context"
export * from "./transaction-base"
+3
View File
@@ -0,0 +1,3 @@
export const indexTypes = {
PRODUCTS: "products"
};
+20
View File
@@ -0,0 +1,20 @@
export * from "./index-types"
export * from "./interface"
export * from "./settings"
export * from "./variant-keys"
export type IndexSettings = {
/**
* Settings specific to the provider. E.g. `searchableAttributes`.
*/
indexSettings: Record<string, unknown>
/**
* Primary key for the index. Used to enforce unique documents in an index. See more in Meilisearch' https://docs.meilisearch.com/learn/core_concepts/primary_key.html.
*/
primaryKey?: string
/**
* Document transformer. Used to transform documents before they are added to the index.
*/
transformer?: (document: any) => any
}
+70
View File
@@ -0,0 +1,70 @@
export interface ISearchService {
options: Record<string, unknown>
/**
* Used to create an index
* @param indexName the index name
* @param options the options
* @return returns response from search engine provider
*/
createIndex(indexName: string, options: unknown): unknown
/**
* Used to get an index
* @param indexName - the index name.
* @return returns response from search engine provider
*/
getIndex(indexName: string): unknown
/**
* Used to index documents by the search engine provider
* @param indexName the index name
* @param documents documents array to be indexed
* @param type of documents to be added (e.g: products, regions, orders, etc)
* @return returns response from search engine provider
*/
addDocuments(indexName: string, documents: unknown, type: string): unknown
/**
* Used to replace documents
* @param indexName the index name.
* @param documents array of document objects that will replace existing documents
* @param type type of documents to be replaced (e.g: products, regions, orders, etc)
* @return returns response from search engine provider
*/
replaceDocuments(indexName: string, documents: unknown, type: string): unknown
/**
* Used to delete document
* @param indexName the index name
* @param document_id the id of the document
* @return returns response from search engine provider
*/
deleteDocument(indexName: string, document_id: string | number): unknown
/**
* Used to delete all documents
* @param indexName the index name
* @return returns response from search engine provider
*/
deleteAllDocuments(indexName: string): unknown
/**
* Used to search for a document in an index
* @param indexName the index name
* @param query the search query
* @param options
* - any options passed to the request object other than the query and indexName
* - additionalOptions contain any provider specific options
* @return returns response from search engine provider
*/
search(indexName: string, query: string | null, options: unknown): unknown
/**
* Used to update the settings of an index
* @param indexName the index name
* @param settings settings object
* @return returns response from search engine provider
*/
updateSettings(indexName: string, settings: unknown): unknown
}
+16
View File
@@ -0,0 +1,16 @@
export type IndexSettings = {
/**
* Settings specific to the provider. E.g. `searchableAttributes`.
*/
indexSettings: Record<string, unknown>
/**
* Primary key for the index. Used to enforce unique documents in an index. See more in Meilisearch' https://docs.meilisearch.com/learn/core_concepts/primary_key.html.
*/
primaryKey?: string
/**
* Document transformer. Used to transform documents before they are added to the index.
*/
transformer?: (document: any) => any
}
@@ -0,0 +1,9 @@
export const variantKeys = [
"sku",
"title",
"upc",
"ean",
"mid_code",
"hs_code",
"options",
]
+1
View File
@@ -1,3 +1,4 @@
export * as DecoratorUtils from "./decorators";
export * as EventBusUtils from "./event-bus";
export * as SearchUtils from "./search";
+1
View File
@@ -1,4 +1,5 @@
export * from "./bundles"
export * from "./decorators"
export * from "./event-bus"
export * from "./search"
@@ -0,0 +1,47 @@
import { SearchTypes } from "@medusajs/types"
export abstract class AbstractSearchService
implements SearchTypes.ISearchService
{
abstract readonly isDefault
protected readonly options_: Record<string, unknown>
get options(): Record<string, unknown> {
return this.options_
}
protected constructor(container, options) {
this.options_ = options
}
abstract createIndex(indexName: string, options: unknown): unknown
abstract getIndex(indexName: string): unknown
abstract addDocuments(
indexName: string,
documents: unknown,
type: string
): unknown
abstract replaceDocuments(
indexName: string,
documents: unknown,
type: string
): unknown
abstract deleteDocument(
indexName: string,
document_id: string | number
): unknown
abstract deleteAllDocuments(indexName: string): unknown
abstract search(
indexName: string,
query: string | null,
options: unknown
): unknown
abstract updateSettings(indexName: string, settings: unknown): unknown
}
+3
View File
@@ -0,0 +1,3 @@
export * from "./abstract-service"
export * from "./is-search-service"
@@ -0,0 +1,5 @@
import { AbstractSearchService } from "./abstract-service"
export function isSearchService(obj: unknown): boolean {
return obj instanceof AbstractSearchService
}
+2 -2
View File
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"lib": ["es2020"],
"target": "es2020",
"lib": ["es5", "es6", "es2019"],
"target": "es5",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,