Make Meilisearch optional and don't connect if unconfigured

This commit is contained in:
PrivateGER 2023-05-29 19:45:55 +02:00
parent 8dd99a2d1d
commit 0524789185

View file

@ -1,63 +1,68 @@
import { Health, MeiliSearch, Stats } from "meilisearch"; import { Health, Index, MeiliSearch, Stats } from "meilisearch";
import { dbLogger } from "./logger.js"; import { dbLogger } from "./logger.js";
import config from "@/config/index.js"; import config from "@/config/index.js";
import { Note } from "@/models/entities/note.js"; import { Note } from "@/models/entities/note.js";
import * as url from "url"; 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"; import { Followings, Users } from "@/models/index.js";
const logger = dbLogger.createSubLogger("meilisearch", "gray", false); const logger = dbLogger.createSubLogger("meilisearch", "gray", false);
let posts: Index;
let client: MeiliSearch;
const hasConfig = const hasConfig =
config.meilisearch && config.meilisearch &&
(config.meilisearch.host || (config.meilisearch.host ||
config.meilisearch.port || config.meilisearch.port ||
config.meilisearch.apiKey); config.meilisearch.apiKey);
const host = hasConfig ? config.meilisearch.host ?? "localhost" : ""; if(hasConfig) {
const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; const host = hasConfig ? config.meilisearch.host ?? "localhost" : "";
const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; const port = hasConfig ? config.meilisearch.port ?? 7700 : 0;
const ssl = hasConfig ? config.meilisearch.ssl ?? false : false; 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({ client = new MeiliSearch({
host: `${ssl ? "https" : "http"}://${host}:${port}`, host: `${ssl ? "https" : "http"}://${host}:${port}`,
apiKey: auth, apiKey: auth,
}); });
const posts = client.index("posts"); posts = client.index("posts");
posts posts
.updateSearchableAttributes(["text"]) .updateSearchableAttributes(["text"])
.catch((e) => .catch((e) =>
logger.error(`Setting searchable attr failed, searches won't work: ${e}`), logger.error(`Setting searchable attr failed, searches won't work: ${e}`),
); );
posts posts
.updateFilterableAttributes([ .updateFilterableAttributes([
"userName", "userName",
"userHost", "userHost",
"mediaAttachment", "mediaAttachment",
"createdAt", "createdAt",
"userId", "userId",
]) ])
.catch((e) => .catch((e) =>
logger.error( logger.error(
`Setting filterable attr failed, advanced searches won't work: ${e}`, `Setting filterable attr failed, advanced searches won't work: ${e}`,
), ),
); );
posts posts
.updateSortableAttributes(["createdAt"]) .updateSortableAttributes(["createdAt"])
.catch((e) => .catch((e) =>
logger.error( logger.error(
`Setting sortable attr failed, placeholder searches won't sort properly: ${e}`, `Setting sortable attr failed, placeholder searches won't sort properly: ${e}`,
), ),
); );
logger.info("Connected to MeiliSearch"); logger.info("Connected to MeiliSearch");
}
export type MeilisearchNote = { export type MeilisearchNote = {
id: string; id: string;