feat: initial search

This commit is contained in:
Derrick Hammer 2023-12-22 06:05:06 -05:00
parent 369996ff2c
commit 74642f0930
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 56 additions and 27 deletions

View File

@ -1,12 +1,24 @@
import { PrismaClient, Article } from "@prisma/client"; import { PrismaClient, Article } from "@prisma/client";
import search from "./search.js";
const globalForPrisma = global as unknown as { prisma: PrismaClient }; 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") { // Index the created record in MeiliSearch
globalForPrisma.prisma = prisma; const index = search.index("articles");
} await index.addDocuments([createdRecord]);
return createdRecord;
},
},
},
});
export default prisma; export default prisma;
export type { Article }; export type { Article };

5
app/lib/search.ts Normal file
View File

@ -0,0 +1,5 @@
import { MeiliSearch } from "meilisearch";
const meili = new MeiliSearch({ host: "http://127.0.0.1:7700" });
export default meili;

View File

@ -3,6 +3,7 @@ import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge"; import { twMerge } from "tailwind-merge";
import { SearchResult, SiteList } from "@/types.js"; import { SearchResult, SiteList } from "@/types.js";
import fs from "fs"; import fs from "fs";
import search from "@/lib/search";
export function cn(...inputs: ClassValue[]) { export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)); return twMerge(clsx(inputs));
@ -25,24 +26,21 @@ export async function getResults({
}: { }: {
query?: string; query?: string;
}): Promise<SearchResult[]> { }): Promise<SearchResult[]> {
if (!query) return []; if (!query) {
return [];
}
return [ const results = await search.index("articles").search(query);
{
id: 1, return results.hits.map((item) => {
timestamp: new Date(), return {
title: "Mock Title 1", id: item.id,
description: "Mock Description 1", timestamp: item.createdAt,
slug: "hello-world", title: item.title,
}, description: "",
{ slug: item.slug,
id: 2, };
timestamp: new Date(), });
title: "Mock Title 2",
description: "Mock Description 2",
slug: "hello-world-2",
},
];
} }
export function getAvailableSites() { export function getAvailableSites() {

25
package-lock.json generated
View File

@ -27,6 +27,7 @@
"cmdk": "^0.2.0", "cmdk": "^0.2.0",
"date-fns": "^2.30.0", "date-fns": "^2.30.0",
"isbot": "^3.7.1", "isbot": "^3.7.1",
"meilisearch": "^0.36.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"slugify": "^1.6.6", "slugify": "^1.6.6",
@ -5154,6 +5155,14 @@
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true "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": { "node_modules/cross-spawn": {
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -8522,6 +8531,14 @@
"node": ">= 0.6" "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": { "node_modules/memorystream": {
"version": "0.3.1", "version": "0.3.1",
"resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz", "resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz",
@ -9526,7 +9543,6 @@
"version": "2.7.0", "version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"dev": true,
"dependencies": { "dependencies": {
"whatwg-url": "^5.0.0" "whatwg-url": "^5.0.0"
}, },
@ -12332,8 +12348,7 @@
"node_modules/tr46": { "node_modules/tr46": {
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
"dev": true
}, },
"node_modules/trim-lines": { "node_modules/trim-lines": {
"version": "3.0.1", "version": "3.0.1",
@ -13993,14 +14008,12 @@
"node_modules/webidl-conversions": { "node_modules/webidl-conversions": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
"dev": true
}, },
"node_modules/whatwg-url": { "node_modules/whatwg-url": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"dev": true,
"dependencies": { "dependencies": {
"tr46": "~0.0.3", "tr46": "~0.0.3",
"webidl-conversions": "^3.0.0" "webidl-conversions": "^3.0.0"

View File

@ -38,6 +38,7 @@
"cmdk": "^0.2.0", "cmdk": "^0.2.0",
"date-fns": "^2.30.0", "date-fns": "^2.30.0",
"isbot": "^3.7.1", "isbot": "^3.7.1",
"meilisearch": "^0.36.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"slugify": "^1.6.6", "slugify": "^1.6.6",