xonecas.io
556
Bn

bees-nest / files

bees-nest / scripts / seed-search.ts

2.8 KB
1import { eq } from "drizzle-orm";
2import { domains, pages, urls } from "@/db/schema";
3import { db } from "@/lib/db";
4
5async function seed() {
6  console.log("Seeding test data...");
7
8  const domainName = "test.com";
9  const url = "https://test.com/hello-world";
10
11  try {
12    // 1. Insert Domain
13    let domainId: number;
14    const existingDomain = await db.query.domains.findFirst({
15      where: eq(domains.domain, domainName),
16    });
17
18    if (existingDomain) {
19      domainId = existingDomain.id;
20      console.log(`Domain ${domainName} already exists (ID: ${domainId})`);
21    } else {
22      const [newDomain] = await db
23        .insert(domains)
24        .values({
25          domain: domainName,
26        })
27        .returning();
28      domainId = newDomain.id;
29      console.log(`Created domain ${domainName} (ID: ${domainId})`);
30    }
31
32    // 2. Insert URL
33    let urlId: number;
34    const existingUrl = await db.query.urls.findFirst({
35      where: eq(urls.url, url),
36    });
37
38    if (existingUrl) {
39      urlId = existingUrl.id;
40      console.log(`URL ${url} already exists (ID: ${urlId})`);
41    } else {
42      const [newUrl] = await db
43        .insert(urls)
44        .values({
45          url: url,
46          domainId: domainId,
47          status: "completed",
48        })
49        .returning();
50      urlId = newUrl.id;
51      console.log(`Created URL ${url} (ID: ${urlId})`);
52    }
53
54    // 3. Insert Page
55    const existingPage = await db.query.pages.findFirst({
56      where: eq(pages.urlId, urlId),
57    });
58
59    if (existingPage) {
60      console.log(`Page for URL ID ${urlId} already exists. Updating...`);
61      await db
62        .update(pages)
63        .set({
64          title: "Hello World Test Page",
65          description:
66            "This is a mock page injected for testing the search UI and API integration.",
67          content:
68            "Welcome to the test page. We are strictly testing the search functionality here. The quick brown fox jumps over the lazy dog.",
69          siteName: "Test Site",
70          image: "/window.svg", // Use one of the public assets
71        })
72        .where(eq(pages.id, existingPage.id));
73      console.log("Updated existing page.");
74    } else {
75      await db.insert(pages).values({
76        urlId: urlId,
77        title: "Hello World Test Page",
78        description:
79          "This is a mock page injected for testing the search UI and API integration.",
80        content:
81          "Welcome to the test page. We are strictly testing the search functionality here. The quick brown fox jumps over the lazy dog.",
82        siteName: "Test Site",
83        image: "/window.svg",
84      });
85      console.log("Created new page.");
86    }
87
88    console.log("Seeding complete!");
89    process.exit(0);
90  } catch (error) {
91    console.error("Error seeding data:", error);
92    process.exit(1);
93  }
94}
95
96seed();