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 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 };

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 { 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<SearchResult[]> {
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() {

25
package-lock.json generated
View File

@ -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"

View File

@ -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",