Skip to content

Commit

Permalink
feat(medusa-plugin-meilisearch): Update + improve Meilisearch plugin (#…
Browse files Browse the repository at this point in the history
…3377)

* feat(medusa-plugin-meilisearch): Upgrade meilisearch deps + migrate plugin to TS

* fix version

* Remove transaction base service from search service

* Create .changeset/strange-mails-pump.md

* Backward compatibility

* Address PR feedback

* Fix folder structure

* Update readme

* Move types

* fix deps

* Change version in changeset

---------

Co-authored-by: adrien2p <[email protected]>
  • Loading branch information
olivermrbl and adrien2p authored Mar 16, 2023
1 parent 4213326 commit 7e17e0d
Show file tree
Hide file tree
Showing 21 changed files with 377 additions and 217 deletions.
65 changes: 65 additions & 0 deletions .changeset/strange-mails-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
"medusa-plugin-meilisearch": major
"@medusajs/medusa": patch
---

feat(medusa-plugin-meilisearch): Update + improve Meilisearch plugin

**What**
- Bumps `meilisearch` dep to latest major
- Migrates plugin to TypeScript
- Changes the way indexes are configured in `medusa-config.js`:

**Before**
```
{
resolve: `medusa-plugin-meilisearch`,
options: {
config: { host: "...", apiKey: "..." },
settings: {
products: {
searchableAttributes: ["title"],
displayedAttributes: ["title"],
},
},
},
},
```

**After**
```
{
resolve: `medusa-plugin-meilisearch`,
options: {
config: { host: "...", apiKey: "..." },
settings: {
products: {
**indexSettings**: {
searchableAttributes: ["title"],
displayedAttributes: ["title"],
},
},
},
},
},
```

This is done to allow for additional configuration of indexes, that are not necessarily passed on query-time.

We introduce two new settings:
```
settings: {
products: {
indexSettings: {
searchableAttributes: ["title"],
displayedAttributes: ["title"],,
},
primaryKey: "id"
transformer: (document) => ({ id: "yo" })
},
},
```

Meilisearch changed their primary key inference in the major release. Now we must be explicit when multiple properties end with `id`. Read more in their [docs](https://docs.meilisearch.com/learn/core_concepts/primary_key.html#primary-field).

The transformer allows developers to specify how their documents are stored in Meilisearch. It is configurable for each index.
13 changes: 0 additions & 13 deletions packages/medusa-plugin-meilisearch/.babelrc

This file was deleted.

8 changes: 0 additions & 8 deletions packages/medusa-plugin-meilisearch/.eslintignore

This file was deleted.

19 changes: 4 additions & 15 deletions packages/medusa-plugin-meilisearch/.gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
/dist
.env
.DS_Store
/uploads
/node_modules
yarn-error.log

/dist

/api
/services
/models
/subscribers
/loaders
/utils
dist
node_modules
.DS_store
yarn.lock
9 changes: 0 additions & 9 deletions packages/medusa-plugin-meilisearch/.npmignore

This file was deleted.

8 changes: 6 additions & 2 deletions packages/medusa-plugin-meilisearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ Learn more about how you can use this plugin in the [documentaion](https://docs.
"[indexName]": "[meilisearch settings passed to meilisearch's `updateSettings()` method]"
// example
products: {
searchableAttributes: ["title", "description", "variant_sku"],
displayedAttributes: ["title", "description", "variant_sku", "thumbnail", "handle"],
indexSettings: {
searchableAttributes: ["title", "description", "variant_sku"],
displayedAttributes: ["title", "description", "variant_sku", "thumbnail", "handle"],
},
primaryKey: "some_id"
transformer: (product: Product) => ({ id: product.id })
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/medusa-plugin-meilisearch/index.js

This file was deleted.

12 changes: 11 additions & 1 deletion packages/medusa-plugin-meilisearch/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module.exports = {
testEnvironment: "node",
globals: {
"ts-jest": {
tsconfig: "tsconfig.spec.json",
isolatedModules: false,
},
},
transform: {
"^.+\\.[jt]s?$": "ts-jest",
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
}
31 changes: 13 additions & 18 deletions packages/medusa-plugin-meilisearch/package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
{
"name": "medusa-plugin-meilisearch",
"version": "1.0.4",
"description": "A starter for Medusa projects.",
"main": "index.js",
"description": "Meilisearch search plugin for Medusa",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-plugin-meilisearch"
},
"author": "Zakaria El Asri",
"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__'"
"build": "tsc",
"watch": "tsc --watch"
},
"peerDependencies": {
"medusa-interfaces": "1.3.6"
"@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",
"meilisearch": "0.27.0"
"meilisearch": "^0.31.1"
},
"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",
"client-sessions": "^0.8.0",
"@medusajs/medusa": "^1.7.12",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"medusa-interfaces": "^1.3.6"
"medusa-interfaces": "^1.3.6",
"typescript": "^4.9.5"
},
"gitHead": "cd1f5afa5aa8c0b15ea957008ee19f1d695cbd2e",
"keywords": [
Expand Down
14 changes: 0 additions & 14 deletions packages/medusa-plugin-meilisearch/src/loaders/index.js

This file was deleted.

26 changes: 26 additions & 0 deletions packages/medusa-plugin-meilisearch/src/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Logger, MedusaContainer } from "@medusajs/modules-sdk"
import MeiliSearchService from "../services/meilisearch"
import { MeilisearchPluginOptions } from "../types"

export default async (
container: MedusaContainer,
options: MeilisearchPluginOptions
) => {
const logger: Logger = container.resolve("logger")

try {
const meilisearchService: MeiliSearchService =
container.resolve("meilisearchService")

const { settings } = options

await Promise.all(
Object.entries(settings ?? []).map(([indexName, value]) =>
meilisearchService.updateSettings(indexName, value)
)
)
} catch (err) {
// ignore
logger.warn(err)
}
}
73 changes: 0 additions & 73 deletions packages/medusa-plugin-meilisearch/src/services/meilisearch.js

This file was deleted.

Loading

0 comments on commit 7e17e0d

Please sign in to comment.