feat: some shit

This commit is contained in:
2023-08-07 13:42:00 +02:00
parent aff3019768
commit 990db3ae50
8 changed files with 36 additions and 25 deletions

View File

@ -12,6 +12,7 @@ import * as cache from "@lib/cache/documents.ts";
import { SILVERBULLET_SERVER } from "@lib/env.ts";
import { fixRenderedMarkdown } from "@lib/helpers.ts";
import { createLogger } from "@lib/log.ts";
import * as typesense from "@lib/typesense.ts";
export type Document = {
name: string;
@ -37,6 +38,8 @@ export async function getDocuments(): Promise<Document[]> {
const documents = await response.json();
cache.setDocuments(documents);
typesense.synchronize();
return documents;
}

View File

@ -5,6 +5,7 @@ export function useEventListener<T extends Event>(
handler: (event: T) => void,
element = window,
) {
console.log("Add Eventlistener", { eventName, element, handler });
// Create a ref that stores handler
const savedHandler = useRef<(event: Event) => void>();

View File

@ -1,9 +1,10 @@
import { Client } from "https://raw.githubusercontent.com/bradenmacdonald/typesense-deno/main/mod.ts";
import { Client } from "typesense";
import { TYPESENSE_API_KEY, TYPESENSE_URL } from "@lib/env.ts";
import { getAllMovies } from "@lib/resource/movies.ts";
import { getAllRecipes } from "@lib/resource/recipes.ts";
import { getAllArticles } from "@lib/resource/articles.ts";
import { createLogger } from "@lib/log.ts";
import { debounce } from "https://deno.land/std@0.193.0/async/mod.ts";
const log = createLogger("typesense");
@ -73,6 +74,7 @@ async function initializeTypesense() {
{ name: "name", type: "string" },
{ name: "type", type: "string", facet: true },
{ name: "date", type: "string", optional: true },
{ name: "author", type: "string", facet: true },
{ name: "rating", type: "int32", facet: true },
{ name: "tags", type: "string[]", facet: true },
{ name: "description", type: "string", optional: true },
@ -111,10 +113,11 @@ async function synchronizeWithTypesense() {
description: sanitizeStringForTypesense(
resource?.description || resource?.content || "",
),
image: resource?.meta?.image,
author: resource.meta?.author,
image: resource.meta?.image,
tags: resource?.tags || [],
rating: resource?.meta?.rating || 0,
date: resource?.meta?.date?.toString() || "",
rating: resource.meta?.rating || 0,
date: resource.meta?.date?.toString() || "",
type: resource.type,
};
});
@ -133,21 +136,19 @@ async function synchronizeWithTypesense() {
limit_hits: 9999,
});
const documentIds = allTypesenseDocuments.hits?.map((doc) =>
doc?.document?.id
) as string[];
// Find deleted document IDs by comparing the Typesense document IDs with the current list of resources
const deletedDocumentIds = documentIds?.filter((id) =>
!allResources.some((resource) => resource.id.toString() === id)
);
const deletedDocumentIds = allTypesenseDocuments.hits
?.map((doc) => doc?.document?.id)
?.filter((id) =>
// Find deleted document IDs by comparing the Typesense document IDs with the current list of resources
!allResources.some((resource) => resource.id.toString() === id)
).map((id) => client.collections("resources").documents(id).delete());
// Delete the documents with IDs found in deletedDocumentIds
await Promise.all(
deletedDocumentIds?.map((id) =>
client.collections("resources").documents(id).delete()
),
);
if (deletedDocumentIds) {
await Promise.all(
deletedDocumentIds,
);
}
log.info("data synchronized");
} catch (error) {
@ -157,3 +158,5 @@ async function synchronizeWithTypesense() {
// Call the synchronizeWithTypesense function to trigger the synchronization
synchronizeWithTypesense();
export const synchronize = debounce(synchronizeWithTypesense, 1000 * 60 * 5);