From 30796e5e300e3c37490ab995e120ec6c3689f033 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Mon, 19 Feb 2024 17:01:49 +0100 Subject: [PATCH 01/12] [Assistants] Filter on names --- src/lib/server/database.ts | 1 + src/lib/utils/debounce.ts | 17 +++++++++++++ src/routes/assistants/+page.server.ts | 3 +++ src/routes/assistants/+page.svelte | 36 ++++++++++++++++++++++++--- 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/lib/utils/debounce.ts diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index ecd5effe18..97cdba7b6f 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -85,6 +85,7 @@ client.on("open", () => { assistants.createIndex({ userCount: 1 }).catch(console.error); assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error); assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error); + assistants.createIndex({ name: 1, userCount: -1 }).catch(console.error); reports.createIndex({ assistantId: 1 }).catch(console.error); reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error); }); diff --git a/src/lib/utils/debounce.ts b/src/lib/utils/debounce.ts new file mode 100644 index 0000000000..c8b7560a63 --- /dev/null +++ b/src/lib/utils/debounce.ts @@ -0,0 +1,17 @@ +/** + * A debounce function that works in both browser and Nodejs. + * For pure Nodejs work, prefer the `Debouncer` class. + */ +export function debounce( + callback: (...rest: T) => unknown, + limit: number +): (...rest: T) => void { + let timer: ReturnType; + + return function (...rest) { + clearTimeout(timer); + timer = setTimeout(() => { + callback(...rest); + }, limit); + }; +} diff --git a/src/routes/assistants/+page.server.ts b/src/routes/assistants/+page.server.ts index 6d54eb0b7e..a1fbe190a6 100644 --- a/src/routes/assistants/+page.server.ts +++ b/src/routes/assistants/+page.server.ts @@ -16,6 +16,7 @@ export const load = async ({ url, locals }) => { const modelId = url.searchParams.get("modelId"); const pageIndex = parseInt(url.searchParams.get("p") ?? "0"); const username = url.searchParams.get("user"); + const query = url.searchParams.get("q"); const createdByCurrentUser = locals.user?.username && locals.user.username === username; let user: Pick | null = null; @@ -34,6 +35,7 @@ export const load = async ({ url, locals }) => { ...(modelId && { modelId }), ...(!createdByCurrentUser && { userCount: { $gt: 1 } }), ...(user ? { createdById: user._id } : { featured: true }), + ...(query && { name: { $regex: query, $options: "i" } }), }; const assistants = await collections.assistants .find(filter) @@ -49,5 +51,6 @@ export const load = async ({ url, locals }) => { selectedModel: modelId ?? "", numTotalItems, numItemsPerPage: NUM_PER_PAGE, + query, }; }; diff --git a/src/routes/assistants/+page.svelte b/src/routes/assistants/+page.svelte index c58ac9d675..bb54befff1 100644 --- a/src/routes/assistants/+page.svelte +++ b/src/routes/assistants/+page.svelte @@ -14,15 +14,20 @@ import CarbonArrowUpRight from "~icons/carbon/arrow-up-right"; import CarbonEarthAmerica from "~icons/carbon/earth-americas-filled"; import CarbonUserMultiple from "~icons/carbon/user-multiple"; + import CarbonSearch from "~icons/carbon/search"; import Pagination from "$lib/components/Pagination.svelte"; import { formatUserCount } from "$lib/utils/formatUserCount"; import { getHref } from "$lib/utils/getHref"; + import { debounce } from "$lib/utils/debounce"; export let data: PageData; $: assistantsCreator = $page.url.searchParams.get("user"); $: createdByMe = data.user?.username && data.user.username === assistantsCreator; + const DEBOUNCE_DELAY = 400; + let filterInputEl: HTMLInputElement; + const onModelChange = (e: Event) => { const newUrl = getHref($page.url, { newKeys: { modelId: (e.target as HTMLSelectElement).value }, @@ -30,6 +35,15 @@ }); goto(newUrl); }; + + const filterOnName = debounce(async (e: Event) => { + const value = (e.target as HTMLInputElement).value; + const newUrl = getHref($page.url, { newKeys: { q: value } }); + await goto(newUrl); + setTimeout(() => { + filterInputEl.focus(); + }, 0); + }, DEBOUNCE_DELAY); @@ -96,7 +110,7 @@ {assistantsCreator}'s Assistants {/if} {/if} +
+ + +
From 00784d448b26c4deaff70fef75c211f51c6f2a64 Mon Sep 17 00:00:00 2001 From: Mishig Date: Tue, 20 Feb 2024 01:44:09 -0800 Subject: [PATCH 02/12] Add `$text` index on `assistant.name` (#844) --- src/lib/server/database.ts | 2 +- src/routes/assistants/+page.server.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index 97cdba7b6f..7aec4954ba 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -85,7 +85,7 @@ client.on("open", () => { assistants.createIndex({ userCount: 1 }).catch(console.error); assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error); assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error); - assistants.createIndex({ name: 1, userCount: -1 }).catch(console.error); + assistants.createIndex({ name: "text" }).catch(console.error); reports.createIndex({ assistantId: 1 }).catch(console.error); reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error); }); diff --git a/src/routes/assistants/+page.server.ts b/src/routes/assistants/+page.server.ts index a1fbe190a6..144f13fcb8 100644 --- a/src/routes/assistants/+page.server.ts +++ b/src/routes/assistants/+page.server.ts @@ -35,7 +35,7 @@ export const load = async ({ url, locals }) => { ...(modelId && { modelId }), ...(!createdByCurrentUser && { userCount: { $gt: 1 } }), ...(user ? { createdById: user._id } : { featured: true }), - ...(query && { name: { $regex: query, $options: "i" } }), + ...(query && { $text: { $search: query } }), }; const assistants = await collections.assistants .find(filter) From 3dd82dfb36de7562f0bbfd0dffebbb42a062fbbb Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 20 Feb 2024 10:47:34 +0100 Subject: [PATCH 03/12] add maxlength --- src/routes/assistants/+page.svelte | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes/assistants/+page.svelte b/src/routes/assistants/+page.svelte index bb54befff1..44f8391bb8 100644 --- a/src/routes/assistants/+page.svelte +++ b/src/routes/assistants/+page.svelte @@ -165,6 +165,7 @@ value={data.query} on:input={filterOnName} bind:this={filterInputEl} + maxlength="150" />
From c5757086dbbbfd7266c13be8b8d72c32cfb2c455 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 20 Feb 2024 10:51:40 +0100 Subject: [PATCH 04/12] better experience --- src/routes/assistants/+page.svelte | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/routes/assistants/+page.svelte b/src/routes/assistants/+page.svelte index 44f8391bb8..bb889d7c2f 100644 --- a/src/routes/assistants/+page.svelte +++ b/src/routes/assistants/+page.svelte @@ -4,6 +4,7 @@ import { PUBLIC_APP_ASSETS, PUBLIC_ORIGIN } from "$env/static/public"; import { isHuggingChat } from "$lib/utils/isHuggingChat"; + import { tick } from "svelte"; import { goto } from "$app/navigation"; import { base } from "$app/paths"; import { page } from "$app/stores"; @@ -25,8 +26,9 @@ $: assistantsCreator = $page.url.searchParams.get("user"); $: createdByMe = data.user?.username && data.user.username === assistantsCreator; - const DEBOUNCE_DELAY = 400; + const SEARCH_DEBOUNCE_DELAY = 400; let filterInputEl: HTMLInputElement; + let searchDisabled = false; const onModelChange = (e: Event) => { const newUrl = getHref($page.url, { @@ -37,13 +39,16 @@ }; const filterOnName = debounce(async (e: Event) => { + searchDisabled = true; const value = (e.target as HTMLInputElement).value; const newUrl = getHref($page.url, { newKeys: { q: value } }); await goto(newUrl); - setTimeout(() => { + setTimeout(async () => { + searchDisabled = false; + await tick(); filterInputEl.focus(); }, 0); - }, DEBOUNCE_DELAY); + }, SEARCH_DEBOUNCE_DELAY); @@ -166,6 +171,7 @@ on:input={filterOnName} bind:this={filterInputEl} maxlength="150" + disabled={searchDisabled} /> From 285b6fe667314798578bc87563d82d4cd70887fc Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 20 Feb 2024 11:44:36 +0100 Subject: [PATCH 05/12] use `$meta: "textScore"` --- src/routes/assistants/+page.server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/assistants/+page.server.ts b/src/routes/assistants/+page.server.ts index 144f13fcb8..d8f6c917ef 100644 --- a/src/routes/assistants/+page.server.ts +++ b/src/routes/assistants/+page.server.ts @@ -40,7 +40,7 @@ export const load = async ({ url, locals }) => { const assistants = await collections.assistants .find(filter) .skip(NUM_PER_PAGE * pageIndex) - .sort({ userCount: -1 }) + .sort({ ...(query && { score: { $meta: "textScore" } }), userCount: -1 }) .limit(NUM_PER_PAGE) .toArray(); From c0387ce8eb8bcb8da9a002a5d5d3531a6e67154f Mon Sep 17 00:00:00 2001 From: Mishig Date: Tue, 20 Feb 2024 03:59:59 -0800 Subject: [PATCH 06/12] Update src/routes/assistants/+page.server.ts Co-authored-by: Eliott C. --- src/routes/assistants/+page.server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/assistants/+page.server.ts b/src/routes/assistants/+page.server.ts index d8f6c917ef..014fedc8ab 100644 --- a/src/routes/assistants/+page.server.ts +++ b/src/routes/assistants/+page.server.ts @@ -16,7 +16,7 @@ export const load = async ({ url, locals }) => { const modelId = url.searchParams.get("modelId"); const pageIndex = parseInt(url.searchParams.get("p") ?? "0"); const username = url.searchParams.get("user"); - const query = url.searchParams.get("q"); + const query = url.searchParams.get("q")?.trim(); const createdByCurrentUser = locals.user?.username && locals.user.username === username; let user: Pick | null = null; From d65f7c17ecb4e904e6dfff0e6e83d08ab079ff16 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 20 Feb 2024 14:42:31 +0100 Subject: [PATCH 07/12] null, not undefined --- src/routes/assistants/+page.server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/assistants/+page.server.ts b/src/routes/assistants/+page.server.ts index 014fedc8ab..a692b3bcbb 100644 --- a/src/routes/assistants/+page.server.ts +++ b/src/routes/assistants/+page.server.ts @@ -16,7 +16,7 @@ export const load = async ({ url, locals }) => { const modelId = url.searchParams.get("modelId"); const pageIndex = parseInt(url.searchParams.get("p") ?? "0"); const username = url.searchParams.get("user"); - const query = url.searchParams.get("q")?.trim(); + const query = url.searchParams.get("q")?.trim() ?? null; const createdByCurrentUser = locals.user?.username && locals.user.username === username; let user: Pick | null = null; From 08e825618d417b0a8b481fb42b5fa70203fbccc7 Mon Sep 17 00:00:00 2001 From: Mishig Date: Thu, 29 Feb 2024 06:08:48 -0800 Subject: [PATCH 08/12] [Assistants] Filter on names (using searchTokens) (#873) Filter with `searchTokens` --- src/lib/server/database-migrations.ts | 43 +++++++++++++++++++ src/lib/server/database.ts | 29 ++++++++++++- src/lib/types/Assistant.ts | 1 + src/lib/utils/searchTokens.ts | 33 ++++++++++++++ src/routes/assistants/+page.server.ts | 5 ++- .../[assistantId]/edit/+page.server.ts | 2 + .../settings/assistants/new/+page.server.ts | 2 + 7 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/lib/server/database-migrations.ts create mode 100644 src/lib/utils/searchTokens.ts diff --git a/src/lib/server/database-migrations.ts b/src/lib/server/database-migrations.ts new file mode 100644 index 0000000000..176e5bbd7f --- /dev/null +++ b/src/lib/server/database-migrations.ts @@ -0,0 +1,43 @@ +import type { AnyBulkWriteOperation } from "mongodb"; +import type { Assistant } from "$lib/types/Assistant"; +import { generateSearchTokens } from "$lib/utils/searchTokens"; +import { collections } from "./database"; + +async function refreshSearchTokens() { + console.log("Refreshing assistants search tokens started..."); + let ops: AnyBulkWriteOperation[] = []; + for await (const assistant of collections.assistants + .find() + .project>({ _id: 1, name: 1 })) { + ops.push({ + updateOne: { + filter: { + _id: assistant._id, + }, + update: { + $set: { + searchTokens: generateSearchTokens(assistant.name), + }, + }, + }, + }); + + if (ops.length >= 1000) { + process.stdout.write("."); + await collections.assistants.bulkWrite(ops, { ordered: false }); + ops = []; + } + } + + if (ops.length) { + await collections.assistants.bulkWrite(ops, { ordered: false }); + } + + console.log("Refreshing assistants search tokens done"); +} + +const migrationFunctions: Record Promise> = { + refreshSearchTokens, +}; + +export default migrationFunctions; diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index fb1d65aa67..42ecfffd18 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -10,6 +10,7 @@ import type { Session } from "$lib/types/Session"; import type { Assistant } from "$lib/types/Assistant"; import type { Report } from "$lib/types/Report"; import type { ConversationStats } from "$lib/types/ConversationStats"; +import migrationFunctions from "./database-migrations"; if (!MONGODB_URL) { throw new Error( @@ -54,7 +55,33 @@ export const collections = { bucket, }; +/** + * Migrations to run. Get the names of the migrations from env var 'migrations'. + * Example: migrations=refreshSearchTokens npm run dev + * In this case, we expect 'refreshSearchTokens' to be defined in './database-migrations.ts' + */ +const migrationsToRun: string[] | undefined = process.env.migrations + ?.split(",") + .map((el) => el.trim()); + client.on("open", () => { + // run migrations + if (migrationsToRun) { + for (const migrationName of migrationsToRun) { + const migrationFunction = migrationFunctions[migrationName]; + + if (!migrationFunction) { + console.warn( + `Migration '${migrationName}' does NOT exist in 'server/database-migrations.ts'` + ); + continue; + } + + migrationFunction().catch(console.error); + } + } + + // create indices conversations .createIndex( { sessionId: 1, updatedAt: -1 }, @@ -117,7 +144,7 @@ client.on("open", () => { assistants.createIndex({ userCount: 1 }).catch(console.error); assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error); assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error); - assistants.createIndex({ name: "text" }).catch(console.error); + assistants.createIndex({ searchTokens: 1 }).catch(console.error); reports.createIndex({ assistantId: 1 }).catch(console.error); reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error); }); diff --git a/src/lib/types/Assistant.ts b/src/lib/types/Assistant.ts index a462538f01..69bd3327f0 100644 --- a/src/lib/types/Assistant.ts +++ b/src/lib/types/Assistant.ts @@ -14,4 +14,5 @@ export interface Assistant extends Timestamps { preprompt: string; userCount?: number; featured?: boolean; + searchTokens: string[]; } diff --git a/src/lib/utils/searchTokens.ts b/src/lib/utils/searchTokens.ts new file mode 100644 index 0000000000..012df9b644 --- /dev/null +++ b/src/lib/utils/searchTokens.ts @@ -0,0 +1,33 @@ +const PUNCTUATION_REGEX = /\p{P}/gu; + +function removeDiacritics(s: string, form: "NFD" | "NFKD" = "NFD"): string { + return s.normalize(form).replace(/[\u0300-\u036f]/g, ""); +} + +export function generateSearchTokens(value: string): string[] { + const fullTitleToken = removeDiacritics(value) + .replace(PUNCTUATION_REGEX, "") + .replaceAll(/\s+/g, "") + .toLowerCase(); + return [ + ...new Set([ + ...removeDiacritics(value) + .split(/\s+/) + .map((word) => word.replace(PUNCTUATION_REGEX, "").toLowerCase()) + .filter((word) => word.length), + ...(fullTitleToken.length ? [fullTitleToken] : []), + ]), + ]; +} + +function escapeForRegExp(s: string): string { + return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string +} + +export function generateQueryTokens(query: string): RegExp[] { + return removeDiacritics(query) + .split(/\s+/) + .map((word) => word.replace(PUNCTUATION_REGEX, "").toLowerCase()) + .filter((word) => word.length) + .map((token) => new RegExp(`^${escapeForRegExp(token)}`)); +} diff --git a/src/routes/assistants/+page.server.ts b/src/routes/assistants/+page.server.ts index a692b3bcbb..60ca1dc82a 100644 --- a/src/routes/assistants/+page.server.ts +++ b/src/routes/assistants/+page.server.ts @@ -3,6 +3,7 @@ import { ENABLE_ASSISTANTS } from "$env/static/private"; import { collections } from "$lib/server/database.js"; import type { Assistant } from "$lib/types/Assistant"; import type { User } from "$lib/types/User"; +import { generateQueryTokens } from "$lib/utils/searchTokens.js"; import { error, redirect } from "@sveltejs/kit"; import type { Filter } from "mongodb"; @@ -35,12 +36,12 @@ export const load = async ({ url, locals }) => { ...(modelId && { modelId }), ...(!createdByCurrentUser && { userCount: { $gt: 1 } }), ...(user ? { createdById: user._id } : { featured: true }), - ...(query && { $text: { $search: query } }), + ...(query && { searchTokens: { $all: generateQueryTokens(query) } }), }; const assistants = await collections.assistants .find(filter) .skip(NUM_PER_PAGE * pageIndex) - .sort({ ...(query && { score: { $meta: "textScore" } }), userCount: -1 }) + .sort({ userCount: -1 }) .limit(NUM_PER_PAGE) .toArray(); diff --git a/src/routes/settings/assistants/[assistantId]/edit/+page.server.ts b/src/routes/settings/assistants/[assistantId]/edit/+page.server.ts index 9310324ab6..83f8cc7f24 100644 --- a/src/routes/settings/assistants/[assistantId]/edit/+page.server.ts +++ b/src/routes/settings/assistants/[assistantId]/edit/+page.server.ts @@ -8,6 +8,7 @@ import { z } from "zod"; import { sha256 } from "$lib/utils/sha256"; import sharp from "sharp"; +import { generateSearchTokens } from "$lib/utils/searchTokens"; const newAsssistantSchema = z.object({ name: z.string().min(1), @@ -130,6 +131,7 @@ export const actions: Actions = { exampleInputs, avatar: deleteAvatar ? undefined : hash ?? assistant.avatar, updatedAt: new Date(), + searchTokens: generateSearchTokens(parse.data.name), }, } ); diff --git a/src/routes/settings/assistants/new/+page.server.ts b/src/routes/settings/assistants/new/+page.server.ts index f58adabd91..678c52457c 100644 --- a/src/routes/settings/assistants/new/+page.server.ts +++ b/src/routes/settings/assistants/new/+page.server.ts @@ -7,6 +7,7 @@ import { ObjectId } from "mongodb"; import { z } from "zod"; import { sha256 } from "$lib/utils/sha256"; import sharp from "sharp"; +import { generateSearchTokens } from "$lib/utils/searchTokens"; const newAsssistantSchema = z.object({ name: z.string().min(1), @@ -99,6 +100,7 @@ export const actions: Actions = { updatedAt: new Date(), userCount: 1, featured: false, + searchTokens: generateSearchTokens(parse.data.name), }); // add insertedId to user settings From 34ed43b9f9a465c260e9b6cb11f88726528c9374 Mon Sep 17 00:00:00 2001 From: Victor Mustar Date: Tue, 5 Mar 2024 16:03:43 +0100 Subject: [PATCH 09/12] input --- src/routes/assistants/+page.svelte | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/routes/assistants/+page.svelte b/src/routes/assistants/+page.svelte index 5e7efbe319..0071fd982f 100644 --- a/src/routes/assistants/+page.svelte +++ b/src/routes/assistants/+page.svelte @@ -162,18 +162,17 @@ {/if} {/if}
- +
From 1d0da522ebc716b06beb208ee3eff426176030b5 Mon Sep 17 00:00:00 2001 From: Victor Mustar Date: Tue, 5 Mar 2024 16:05:50 +0100 Subject: [PATCH 10/12] rm extra whitespace --- src/routes/assistants/+page.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/assistants/+page.svelte b/src/routes/assistants/+page.svelte index 0071fd982f..21bb44be3e 100644 --- a/src/routes/assistants/+page.svelte +++ b/src/routes/assistants/+page.svelte @@ -164,7 +164,7 @@
- + Date: Tue, 5 Mar 2024 15:52:34 +0000 Subject: [PATCH 11/12] hide UI before migration --- src/routes/assistants/+page.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/assistants/+page.svelte b/src/routes/assistants/+page.svelte index 21bb44be3e..2108047a7c 100644 --- a/src/routes/assistants/+page.svelte +++ b/src/routes/assistants/+page.svelte @@ -162,7 +162,7 @@ {/if} {/if}
Date: Tue, 5 Mar 2024 16:02:18 +0000 Subject: [PATCH 12/12] rm ad-hoc migration --- src/lib/server/database-migrations.ts | 43 --------------------------- src/lib/server/database.ts | 27 ----------------- 2 files changed, 70 deletions(-) delete mode 100644 src/lib/server/database-migrations.ts diff --git a/src/lib/server/database-migrations.ts b/src/lib/server/database-migrations.ts deleted file mode 100644 index 176e5bbd7f..0000000000 --- a/src/lib/server/database-migrations.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { AnyBulkWriteOperation } from "mongodb"; -import type { Assistant } from "$lib/types/Assistant"; -import { generateSearchTokens } from "$lib/utils/searchTokens"; -import { collections } from "./database"; - -async function refreshSearchTokens() { - console.log("Refreshing assistants search tokens started..."); - let ops: AnyBulkWriteOperation[] = []; - for await (const assistant of collections.assistants - .find() - .project>({ _id: 1, name: 1 })) { - ops.push({ - updateOne: { - filter: { - _id: assistant._id, - }, - update: { - $set: { - searchTokens: generateSearchTokens(assistant.name), - }, - }, - }, - }); - - if (ops.length >= 1000) { - process.stdout.write("."); - await collections.assistants.bulkWrite(ops, { ordered: false }); - ops = []; - } - } - - if (ops.length) { - await collections.assistants.bulkWrite(ops, { ordered: false }); - } - - console.log("Refreshing assistants search tokens done"); -} - -const migrationFunctions: Record Promise> = { - refreshSearchTokens, -}; - -export default migrationFunctions; diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index 42ecfffd18..7259f1e73a 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -10,7 +10,6 @@ import type { Session } from "$lib/types/Session"; import type { Assistant } from "$lib/types/Assistant"; import type { Report } from "$lib/types/Report"; import type { ConversationStats } from "$lib/types/ConversationStats"; -import migrationFunctions from "./database-migrations"; if (!MONGODB_URL) { throw new Error( @@ -55,33 +54,7 @@ export const collections = { bucket, }; -/** - * Migrations to run. Get the names of the migrations from env var 'migrations'. - * Example: migrations=refreshSearchTokens npm run dev - * In this case, we expect 'refreshSearchTokens' to be defined in './database-migrations.ts' - */ -const migrationsToRun: string[] | undefined = process.env.migrations - ?.split(",") - .map((el) => el.trim()); - client.on("open", () => { - // run migrations - if (migrationsToRun) { - for (const migrationName of migrationsToRun) { - const migrationFunction = migrationFunctions[migrationName]; - - if (!migrationFunction) { - console.warn( - `Migration '${migrationName}' does NOT exist in 'server/database-migrations.ts'` - ); - continue; - } - - migrationFunction().catch(console.error); - } - } - - // create indices conversations .createIndex( { sessionId: 1, updatedAt: -1 },