chronos
chronos
DTDrizzle Team
Created by chronos on 1/11/2024 in #help
Can't save json via prepared statements
I am getting a strange entry in my db while trying to save json through a prepared statement This is my simplified prepared statement
const saveData = db.
insert(tableName)
.values({
jsonData: sql.placeholder("jsonData")
})
.prepare()
const saveData = db.
insert(tableName)
.values({
jsonData: sql.placeholder("jsonData")
})
.prepare()
This is how I am calling it
// Neither of these work
await saveData.execute({ jsonData: jsonData })
await saveData.execute({
jsonData: JSON.stringify(jsonData)
})
// Neither of these work
await saveData.execute({ jsonData: jsonData })
await saveData.execute({
jsonData: JSON.stringify(jsonData)
})
Finally this is what shows up in the json column in the db
{"name": "jsonData"}
{"name": "jsonData"}
What am I missing?
2 replies
DTDrizzle Team
Created by chronos on 1/11/2024 in #help
Correct way of handling upserts in MySql
Hey folks! I am using MySql and cannot use onConflictDoUpdate. In leu of that I am wondering if this is a correct way of doing upserts
const updated = await db
.update(...)
.set(...)
.where(eq(..))

// If no rows updated, create new row
if (updated[0].affectedRows === 0) {
await db.insert(...).values(...)
}
const updated = await db
.update(...)
.set(...)
.where(eq(..))

// If no rows updated, create new row
if (updated[0].affectedRows === 0) {
await db.insert(...).values(...)
}
In my use case this record is going to be created once and then updated several times.
5 replies