Retrieving ids when in a batch?

I'm doing the following with a batch.

Where I am getting stuck is the very last join insert.

How can I get the resulting ids from the 2 separate inserts (categoryQuery and imageQuery) ? Thanks!

const allTransactions = [] as any[];
const categoryQuery = db
  .insert(schema.medusaCategories)
  .values({
    id: genId(),
    parentId:
      parsedData.parentId !== undefined ? parsedData.parentId : undefined,
    name: parsedData.name,
    enabled: parsedData.enabled,
    description: parsedData.description,
    pageTitle: parsedData.title,
    metaDescription: parsedData.metaDescription,
    slug: parsedData.slug,
    storeId: parsedData.storeId,
  })
  .returning({ id: schema.medusaCategories.id });

allTransactions.push(categoryQuery);

if (parsedData.images && parsedData.images.length > 0) {
  parsedData.images.forEach(async (image: any) => {
    const imageQuery = db
      .insert(schema.images)
      .values({
        id: image.id,
        name: image.name,
        key: image.key,
        url: image.url,
        size: image.size,
        extension: image.extension,
        hash: image.hash,
        type: image.type,
      })
      .returning({ id: schema.images.id });

    allTransactions.push(imageQuery);

    // const join = db.insert(schema.medusaCategoryImages).values({
    //   id: genId(),
    //   categoryId: [categoryQuery].id,
    //   imageId: [imageQuery].id,
    // });

    // allTransactions.push(join);
  });
}

const res = await db.batch(allTransactions);
Was this page helpful?