MCPcopy Create free account
hub / github.com/ethanniser/NextFaster / getBody

Function getBody

scripts/fill.ts:36–109  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

34}
35
36const getBody = async () => {
37 const fileStream = fs.createReadStream("scripts/out.jsonl");
38 const rl = readline.createInterface({
39 input: fileStream,
40 });
41
42 const body = [] as any[];
43 rl.on("line", (line: string) => {
44 try {
45 const parsedLine = JSON.parse(line);
46 const subcategory_slug = parsedLine.custom_id;
47 const response = JSON.parse(
48 parsedLine.response.body.choices[0].message.content,
49 );
50
51 const products = response.products;
52
53 const productsToAdd = products.map(
54 (product: { name: string; description: string }) => {
55 const price = parseFloat((Math.random() * 20 + 5).toFixed(1));
56 return {
57 slug: slugify(product.name, { lower: true }),
58 name: product.name,
59 description: product.description ?? "",
60 price,
61 subcategory_slug,
62 };
63 },
64 );
65 body.push(...productsToAdd);
66 } catch (err) {
67 console.error("Error parsing JSON:", err);
68 fs.appendFile("scripts/errors.txt", line + "\n", (err: any) => {
69 if (err) {
70 console.error(err);
71 return;
72 }
73 });
74 }
75 });
76
77 rl.on("close", async () => {
78 console.log(body.length);
79 for (let i = 0; i < body.length; i += 10000) {
80 const chunk = body.slice(i, i + 10000);
81 await db.insert(products).values(chunk).onConflictDoNothing();
82 console.log(`Inserted products ${i} to ${i + chunk.length}`);
83 await new Promise((resolve) => setTimeout(resolve, 100)); // 100 ms
84 }
85
86 // const data = [] as any[];
87 // const subcategories = await getEmptySubcategories();
88 // subcategories.forEach((subcat) => {
89 // // get 30 random products from body, regardless of subcategory_slug
90 // const products = getRandomObjects(body, 30).map((product) => {
91 // return {
92 // ...product,
93 // subcategory_slug: subcat,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected