I am trying to create an SQL bulk insert query using sql helper from @vercel/postgres. I intend to build the query from array of objects. Objects has number/string/date typed properties. Creating a string and passing to sql helper won't work, I believe due to parametrized query. So, is there really any way to build a query dynamically and run using Vercel's Postgres package (without using an ORM) ?
I tried something like this, which obviously doesn't work:
import { sql } from "@vercel/postgres";
// Adds same charge for multiple users
export async function bulkInsert(userIds, charge) {
const values = userIds.map((id) => `(${id}, ${charge.created_date}, ${charge.amount}, ${charge.note})`
);
await sql`INSERT INTO charges (user_id, date_created, amount, note)
VALUES ${values.join(",")}`;
}