Guías

Sync results

Pull a whole season into your database without duplicating rows on a re-run.

  1. 1

    Walk every page

    The listing answers with `total`, so you know when to stop without guessing.

    javascript
    async function fetchAllPages(path) {
      const out = [];
      let page = 1;
      for (;;) {
        const url = new URL(`https://www.kaax-agritech.com${path}`);
        url.searchParams.set("page", String(page));
        url.searchParams.set("limit", "100");
    
        const res = await fetch(url, {
          headers: { apiKey: process.env.KAAX_API_KEY },
        });
        if (!res.ok) throw new Error(`Kaax API ${res.status}: ${await res.text()}`);
    
        const { total, data } = await res.json();
        out.push(...data);
        if (out.length >= total || data.length === 0) return out;
        page += 1;
      }
    }
  2. 2

    Upsert by the Kaax id

    That way re-running the sync duplicates nothing, which is what makes it safe to put on a cron.

    javascript
    for (const a of await fetchAllPages("/api/v2/analyses?type=counting")) {
      await db.analyses.updateOne(
        { kaaxId: a._id },
        { $set: { ...a.analysis, name: a.name, syncedAt: new Date() } },
        { upsert: true },
      );
    }