From 74642f093018b934d75a251d01d9d042612c3a27 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 22 Dec 2023 06:05:06 -0500 Subject: [PATCH] feat: initial search --- app/lib/prisma.ts | 20 ++++++++++++++++---- app/lib/search.ts | 5 +++++ app/utils.ts | 32 +++++++++++++++----------------- package-lock.json | 25 +++++++++++++++++++------ package.json | 1 + 5 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 app/lib/search.ts diff --git a/app/lib/prisma.ts b/app/lib/prisma.ts index 5161e45..f4815cf 100644 --- a/app/lib/prisma.ts +++ b/app/lib/prisma.ts @@ -1,12 +1,24 @@ import { PrismaClient, Article } from "@prisma/client"; +import search from "./search.js"; const globalForPrisma = global as unknown as { prisma: PrismaClient }; -export const prisma = globalForPrisma.prisma || new PrismaClient(); +export const prisma = (globalForPrisma.prisma || new PrismaClient()).$extends({ + query: { + article: { + async create({ args, query }) { + // Perform the create operation using Prisma + const createdRecord = await query(args); -if (process.env.NODE_ENV !== "production") { - globalForPrisma.prisma = prisma; -} + // Index the created record in MeiliSearch + const index = search.index("articles"); + await index.addDocuments([createdRecord]); + + return createdRecord; + }, + }, + }, +}); export default prisma; export type { Article }; diff --git a/app/lib/search.ts b/app/lib/search.ts new file mode 100644 index 0000000..1d2ca4d --- /dev/null +++ b/app/lib/search.ts @@ -0,0 +1,5 @@ +import { MeiliSearch } from "meilisearch"; + +const meili = new MeiliSearch({ host: "http://127.0.0.1:7700" }); + +export default meili; diff --git a/app/utils.ts b/app/utils.ts index 447eb65..9fccc98 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -3,6 +3,7 @@ import { type ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; import { SearchResult, SiteList } from "@/types.js"; import fs from "fs"; +import search from "@/lib/search"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); @@ -25,24 +26,21 @@ export async function getResults({ }: { query?: string; }): Promise { - if (!query) return []; + if (!query) { + return []; + } - return [ - { - id: 1, - timestamp: new Date(), - title: "Mock Title 1", - description: "Mock Description 1", - slug: "hello-world", - }, - { - id: 2, - timestamp: new Date(), - title: "Mock Title 2", - description: "Mock Description 2", - slug: "hello-world-2", - }, - ]; + const results = await search.index("articles").search(query); + + return results.hits.map((item) => { + return { + id: item.id, + timestamp: item.createdAt, + title: item.title, + description: "", + slug: item.slug, + }; + }); } export function getAvailableSites() { diff --git a/package-lock.json b/package-lock.json index 6f87721..1ccd8ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "cmdk": "^0.2.0", "date-fns": "^2.30.0", "isbot": "^3.7.1", + "meilisearch": "^0.36.0", "react": "^18.2.0", "react-dom": "^18.2.0", "slugify": "^1.6.6", @@ -5154,6 +5155,14 @@ "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", "dev": true }, + "node_modules/cross-fetch": { + "version": "3.1.8", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", + "integrity": "sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==", + "dependencies": { + "node-fetch": "^2.6.12" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -8522,6 +8531,14 @@ "node": ">= 0.6" } }, + "node_modules/meilisearch": { + "version": "0.36.0", + "resolved": "https://registry.npmjs.org/meilisearch/-/meilisearch-0.36.0.tgz", + "integrity": "sha512-swcvEYrct0/zsGj3jlbPm1OYxbH14IURnlysKlXywNicIQ5EMkSYLYCLCwOuBKAaGcdISWdgdylH9TXVLegmOQ==", + "dependencies": { + "cross-fetch": "^3.1.6" + } + }, "node_modules/memorystream": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz", @@ -9526,7 +9543,6 @@ "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", - "dev": true, "dependencies": { "whatwg-url": "^5.0.0" }, @@ -12332,8 +12348,7 @@ "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "dev": true + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, "node_modules/trim-lines": { "version": "3.0.1", @@ -13993,14 +14008,12 @@ "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "dev": true + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "node_modules/whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "dev": true, "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" diff --git a/package.json b/package.json index 41f99be..06fba3f 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "cmdk": "^0.2.0", "date-fns": "^2.30.0", "isbot": "^3.7.1", + "meilisearch": "^0.36.0", "react": "^18.2.0", "react-dom": "^18.2.0", "slugify": "^1.6.6",