Open Hansenq opened 9 months ago
This is already supported in a different way. You can do:
// To insert a single value:
db.insert(myTable).values({});
// To insert 3 values:
db.insert(myTable).values([{}, {}, {}]);
I'm aware that exists, but that doesn't solve my issue. Basically, sometimes I'll copy some data from one table into another table (in order to backfill it). Sometimes a local dev DB doesn't have data in the first table, so the table will be empty:
let oldModels = await db.query.oldModels.findMany({}) // has length 0
await db
.insert(newModels)
.values(oldModels.map((om) => ({
name: om.name,
description: om.description,
})))
But this code fails when oldModels
is empty, forcing me to change the code to the following, which is unergonomic.
let oldModels = await db.query.oldModels.findMany({}) // has length 0
if (oldModels.length !== 0) {
await db
.insert(newModels)
.values(oldModels.map((om) => ({
name: om.name,
description: om.description,
})))
}
The most annoying thing is, because this happens to different local/test DBs that don't have prod data, this bug doesn't surface until later during runtime when another engineer is running the code or tests are being run, so it's super easy to forget.
I have run into the same issue with MySQL. Although this is more of a QoL change, I was disappointed to find that the existing DX is lacking. Small FRs like these are still valuable and can set one library apart from another.
Describe what you want
When inserting values into the database, sometimes I end up needing to add an empty array. Currently this throws an error:
Supporting
[]
insidedb.insert(myTable).values([])
would be more ergonomically friendly and allow me to skip pulling the array into a variable and checking the length of that array before callingdb.insert
.This is similar in spirit to #1295. Edit: Looks like this is the same as a previous issue https://github.com/drizzle-team/drizzle-orm/issues/1078