From 0524789185ddef4cd73d0e9b9618b03e31b9d085 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Mon, 29 May 2023 19:45:55 +0200 Subject: [PATCH 1/2] Make Meilisearch optional and don't connect if unconfigured --- packages/backend/src/db/meilisearch.ts | 81 ++++++++++++++------------ 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/packages/backend/src/db/meilisearch.ts b/packages/backend/src/db/meilisearch.ts index 7f31789554..60491f8827 100644 --- a/packages/backend/src/db/meilisearch.ts +++ b/packages/backend/src/db/meilisearch.ts @@ -1,63 +1,68 @@ -import { Health, MeiliSearch, Stats } from "meilisearch"; +import { Health, Index, MeiliSearch, Stats } from "meilisearch"; import { dbLogger } from "./logger.js"; import config from "@/config/index.js"; import { Note } from "@/models/entities/note.js"; import * as url from "url"; -import { ILocalUser, User } from "@/models/entities/user.js"; +import { ILocalUser } from "@/models/entities/user.js"; import { Followings, Users } from "@/models/index.js"; const logger = dbLogger.createSubLogger("meilisearch", "gray", false); +let posts: Index; +let client: MeiliSearch; + const hasConfig = config.meilisearch && (config.meilisearch.host || config.meilisearch.port || config.meilisearch.apiKey); -const host = hasConfig ? config.meilisearch.host ?? "localhost" : ""; -const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; -const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; -const ssl = hasConfig ? config.meilisearch.ssl ?? false : false; +if(hasConfig) { + const host = hasConfig ? config.meilisearch.host ?? "localhost" : ""; + const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; + const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; + const ssl = hasConfig ? config.meilisearch.ssl ?? false : false; -logger.info("Connecting to MeiliSearch"); + logger.info("Connecting to MeiliSearch"); -const client: MeiliSearch = new MeiliSearch({ - host: `${ssl ? "https" : "http"}://${host}:${port}`, - apiKey: auth, -}); + client = new MeiliSearch({ + host: `${ssl ? "https" : "http"}://${host}:${port}`, + apiKey: auth, + }); -const posts = client.index("posts"); + posts = client.index("posts"); -posts - .updateSearchableAttributes(["text"]) - .catch((e) => - logger.error(`Setting searchable attr failed, searches won't work: ${e}`), - ); + posts + .updateSearchableAttributes(["text"]) + .catch((e) => + logger.error(`Setting searchable attr failed, searches won't work: ${e}`), + ); -posts - .updateFilterableAttributes([ - "userName", - "userHost", - "mediaAttachment", - "createdAt", - "userId", - ]) - .catch((e) => - logger.error( - `Setting filterable attr failed, advanced searches won't work: ${e}`, - ), - ); + posts + .updateFilterableAttributes([ + "userName", + "userHost", + "mediaAttachment", + "createdAt", + "userId", + ]) + .catch((e) => + logger.error( + `Setting filterable attr failed, advanced searches won't work: ${e}`, + ), + ); -posts - .updateSortableAttributes(["createdAt"]) - .catch((e) => - logger.error( - `Setting sortable attr failed, placeholder searches won't sort properly: ${e}`, - ), - ); + posts + .updateSortableAttributes(["createdAt"]) + .catch((e) => + logger.error( + `Setting sortable attr failed, placeholder searches won't sort properly: ${e}`, + ), + ); -logger.info("Connected to MeiliSearch"); + logger.info("Connected to MeiliSearch"); +} export type MeilisearchNote = { id: string; From 33ef39cd4a2523c9d6398f9419e6b014218684dc Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Mon, 29 May 2023 20:09:52 +0200 Subject: [PATCH 2/2] formatter --- packages/backend/src/db/meilisearch.ts | 64 +++++++++++++------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/packages/backend/src/db/meilisearch.ts b/packages/backend/src/db/meilisearch.ts index 60491f8827..01efaa0ba0 100644 --- a/packages/backend/src/db/meilisearch.ts +++ b/packages/backend/src/db/meilisearch.ts @@ -18,50 +18,50 @@ const hasConfig = config.meilisearch.port || config.meilisearch.apiKey); -if(hasConfig) { +if (hasConfig) { const host = hasConfig ? config.meilisearch.host ?? "localhost" : ""; - const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; - const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; - const ssl = hasConfig ? config.meilisearch.ssl ?? false : false; + const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; + const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; + const ssl = hasConfig ? config.meilisearch.ssl ?? false : false; - logger.info("Connecting to MeiliSearch"); + logger.info("Connecting to MeiliSearch"); client = new MeiliSearch({ host: `${ssl ? "https" : "http"}://${host}:${port}`, - apiKey: auth, - }); + apiKey: auth, + }); posts = client.index("posts"); posts - .updateSearchableAttributes(["text"]) - .catch((e) => - logger.error(`Setting searchable attr failed, searches won't work: ${e}`), - ); + .updateSearchableAttributes(["text"]) + .catch((e) => + logger.error(`Setting searchable attr failed, searches won't work: ${e}`), + ); - posts - .updateFilterableAttributes([ - "userName", - "userHost", - "mediaAttachment", - "createdAt", - "userId", - ]) - .catch((e) => - logger.error( - `Setting filterable attr failed, advanced searches won't work: ${e}`, - ), - ); + posts + .updateFilterableAttributes([ + "userName", + "userHost", + "mediaAttachment", + "createdAt", + "userId", + ]) + .catch((e) => + logger.error( + `Setting filterable attr failed, advanced searches won't work: ${e}`, + ), + ); - posts - .updateSortableAttributes(["createdAt"]) - .catch((e) => - logger.error( - `Setting sortable attr failed, placeholder searches won't sort properly: ${e}`, - ), - ); + posts + .updateSortableAttributes(["createdAt"]) + .catch((e) => + logger.error( + `Setting sortable attr failed, placeholder searches won't sort properly: ${e}`, + ), + ); - logger.info("Connected to MeiliSearch"); + logger.info("Connected to MeiliSearch"); } export type MeilisearchNote = {