How do I get the values of an insert inside a transaction?

I have this transaction where I create a project and add variables and a history of those variables. I need the IDs of the variables for variables_history. How can I get those values?

await db.transaction(async (tx) => {
  // Add project to DB
  await tx.insert(projects).values({
    // ...
  });

  // Add variables to DB
  await tx.insert(variables).values(
    repoVariables.data
      .filter((v) => v.variable_type === 'env_var')
      .map((v) => ({
        // ...
      })),
  );

  // Add variables history to DB
  const projectVariables = await getProjectVariables(projectId);

  console.log(projectVariables); // <-- empty array

  await tx.insert(variablesHistory).values(
    projectVariables.map((v) => ({
      // ...
    })),
  );
});
Was this page helpful?