@porulle/adapter-meilisearch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # @porulle/adapter-meilisearch
2
+
3
+ `SearchAdapter` for [Meilisearch](https://www.meilisearch.com). Indexes catalog entities and answers `/api/search` and `/api/search/suggest` queries.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { defineConfig } from "@porulle/core";
9
+ import { meilisearchAdapter } from "@porulle/adapter-meilisearch";
10
+ import { MeiliSearch } from "meilisearch";
11
+
12
+ const meili = new MeiliSearch({
13
+ host: process.env.MEILI_HOST!,
14
+ apiKey: process.env.MEILI_KEY!,
15
+ });
16
+
17
+ export default defineConfig({
18
+ search: {
19
+ adapter: meilisearchAdapter({
20
+ client: meili,
21
+ indexName: "porulle_catalog",
22
+ }),
23
+ },
24
+ // …
25
+ });
26
+ ```
27
+
28
+ ## What it does
29
+
30
+ - Adds documents on catalog create/update via the kernel's `catalog.afterCreate` / `afterUpdate` hooks
31
+ - Removes documents on `catalog.afterDelete`
32
+ - Configures filterable attributes per the indexed schema
33
+ - Maps Meili's hits + facetDistribution into Porulle's `SearchQueryResult` shape
34
+
35
+ ## Notes
36
+
37
+ - Bring your own Meilisearch instance — local Docker, Meilisearch Cloud, or self-hosted.
38
+ - The adapter assumes one index per organization or per tenant; configure the `indexName` accordingly.
39
+ - For Postgres-only deployments without a separate search service, use `@porulle/adapter-pg-search`.
40
+
41
+ ## See also
42
+
43
+ - [Meilisearch docs](https://www.meilisearch.com/docs)
44
+ - `@porulle/adapter-pg-search` — Postgres FTS alternative (no extra service)
@@ -0,0 +1,32 @@
1
+ import { type SearchAdapter, type SearchDocument } from "@porulle/core";
2
+ interface MeiliIndexLike {
3
+ addDocuments(documents: SearchDocument[]): Promise<unknown>;
4
+ deleteDocuments(ids: string[]): Promise<unknown>;
5
+ search(query: string, options?: {
6
+ filter?: string | string[];
7
+ facets?: string[];
8
+ limit?: number;
9
+ offset?: number;
10
+ attributesToRetrieve?: string[];
11
+ }): Promise<{
12
+ hits: Array<SearchDocument & {
13
+ _rankingScore?: number;
14
+ }>;
15
+ estimatedTotalHits?: number;
16
+ facetDistribution?: Record<string, Record<string, number>>;
17
+ }>;
18
+ updateFilterableAttributes(attributes: string[]): Promise<unknown>;
19
+ }
20
+ interface MeiliClientLike {
21
+ index(uid: string): MeiliIndexLike;
22
+ }
23
+ export interface MeilisearchAdapterOptions {
24
+ host: string;
25
+ apiKey?: string;
26
+ indexName?: string;
27
+ filterableAttributes?: string[];
28
+ client?: MeiliClientLike;
29
+ }
30
+ export declare function meilisearchAdapter(options: MeilisearchAdapterOptions): SearchAdapter;
31
+ export {};
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,KAAK,aAAa,EAAE,KAAK,cAAc,EAA4E,MAAM,eAAe,CAAC;AAExK,UAAU,cAAc;IACtB,YAAY,CAAC,SAAS,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5D,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACjD,MAAM,CACJ,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QACR,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QAC3B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;QAClB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;KACjC,GACA,OAAO,CAAC;QACT,IAAI,EAAE,KAAK,CAAC,cAAc,GAAG;YAAE,aAAa,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;QACzD,kBAAkB,CAAC,EAAE,MAAM,CAAC;QAC5B,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;KAC5D,CAAC,CAAC;IACH,0BAA0B,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACpE;AAED,UAAU,eAAe;IACvB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC;CACpC;AAED,MAAM,WAAW,yBAAyB;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;IAChC,MAAM,CAAC,EAAE,eAAe,CAAC;CAC1B;AAgDD,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,yBAAyB,GAAG,aAAa,CAgGpF"}
@@ -0,0 +1,125 @@
1
+ import { MeiliSearch } from "meilisearch";
2
+ import { Err, Ok } from "@porulle/core";
3
+ function toFilter(params) {
4
+ const filters = [];
5
+ if (params.filters?.type) {
6
+ filters.push(`type = \"${params.filters.type}\"`);
7
+ }
8
+ if (params.filters?.status) {
9
+ filters.push(`status = \"${params.filters.status}\"`);
10
+ }
11
+ if (params.filters?.category) {
12
+ filters.push(`categories = \"${params.filters.category}\"`);
13
+ }
14
+ if (params.filters?.brand) {
15
+ filters.push(`brands = \"${params.filters.brand}\"`);
16
+ }
17
+ return filters;
18
+ }
19
+ function normalizeSearchResult(params, result) {
20
+ const page = params.page ?? 1;
21
+ const limit = params.limit ?? 20;
22
+ return {
23
+ hits: result.hits.map((document) => ({
24
+ id: document.id,
25
+ ...(document._rankingScore !== undefined ? { score: document._rankingScore } : {}),
26
+ document,
27
+ })),
28
+ total: result.estimatedTotalHits ?? result.hits.length,
29
+ page,
30
+ limit,
31
+ facets: result.facetDistribution ?? {},
32
+ };
33
+ }
34
+ export function meilisearchAdapter(options) {
35
+ const client = options.client ?? new MeiliSearch({ host: options.host, ...(options.apiKey ? { apiKey: options.apiKey } : {}) });
36
+ const indexName = options.indexName ?? "catalog";
37
+ const filterable = options.filterableAttributes ?? ["type", "status", "categories", "brands"];
38
+ let filterableConfigured = false;
39
+ async function ensureFilterable(index) {
40
+ if (filterableConfigured)
41
+ return;
42
+ await index.updateFilterableAttributes(filterable);
43
+ filterableConfigured = true;
44
+ }
45
+ return {
46
+ providerId: "meilisearch",
47
+ async index(documents) {
48
+ try {
49
+ if (documents.length === 0)
50
+ return Ok(undefined);
51
+ const index = client.index(indexName);
52
+ await ensureFilterable(index);
53
+ await index.addDocuments(documents);
54
+ return Ok(undefined);
55
+ }
56
+ catch (error) {
57
+ return Err({
58
+ code: "MEILISEARCH_INDEX_FAILED",
59
+ message: error instanceof Error ? error.message : "Failed to index Meilisearch documents.",
60
+ });
61
+ }
62
+ },
63
+ async remove(ids) {
64
+ try {
65
+ if (ids.length === 0)
66
+ return Ok(undefined);
67
+ const index = client.index(indexName);
68
+ await index.deleteDocuments(ids);
69
+ return Ok(undefined);
70
+ }
71
+ catch (error) {
72
+ return Err({
73
+ code: "MEILISEARCH_DELETE_FAILED",
74
+ message: error instanceof Error ? error.message : "Failed to delete Meilisearch documents.",
75
+ });
76
+ }
77
+ },
78
+ async search(params) {
79
+ try {
80
+ const page = params.page ?? 1;
81
+ const limit = params.limit ?? 20;
82
+ const offset = (page - 1) * limit;
83
+ const filters = toFilter(params);
84
+ const index = client.index(indexName);
85
+ const result = await index.search(params.query, {
86
+ ...(filters.length > 0 ? { filter: filters } : {}),
87
+ facets: params.facets ?? ["type", "status", "categories", "brands"],
88
+ limit,
89
+ offset,
90
+ });
91
+ return Ok(normalizeSearchResult({ ...params, page, limit }, result));
92
+ }
93
+ catch (error) {
94
+ return Err({
95
+ code: "MEILISEARCH_QUERY_FAILED",
96
+ message: error instanceof Error ? error.message : "Meilisearch query failed.",
97
+ });
98
+ }
99
+ },
100
+ async suggest(params) {
101
+ try {
102
+ const index = client.index(indexName);
103
+ const result = await index.search(params.prefix, {
104
+ ...(params.type ? { filter: [`type = \"${params.type}\"`] } : {}),
105
+ attributesToRetrieve: ["title"],
106
+ limit: params.limit ?? 10,
107
+ });
108
+ const prefix = params.prefix.toLowerCase();
109
+ const suggestions = result.hits
110
+ .map((hit) => hit.title)
111
+ .filter((title) => typeof title === "string")
112
+ .filter((title, index, list) => list.indexOf(title) === index)
113
+ .filter((title) => title.toLowerCase().startsWith(prefix))
114
+ .slice(0, params.limit ?? 10);
115
+ return Ok(suggestions);
116
+ }
117
+ catch (error) {
118
+ return Err({
119
+ code: "MEILISEARCH_SUGGEST_FAILED",
120
+ message: error instanceof Error ? error.message : "Meilisearch suggest failed.",
121
+ });
122
+ }
123
+ },
124
+ };
125
+ }