chronos
chronos
DTDrizzle Team
Created by chronos on 12/7/2024 in #help
How to make a nullable field from table schema be notNull using drizzle-zod?
In this case, I want to make the last_name a required field in the updateProfileSchema.
export const profile = mysqlTable("profile", {
first_name: varchar("first_name").notNull(),
last_name: varchar("last_name")
})

const updateProfileSchema = createInsertSchema(profile, {
last_name: schema => schema.last_name.min(1)
// last_name: z.string().min(1) <-- THIS DONT WORK EITHER
}).required({
last_name: true
})

type UpdateProfile = z.infer<typeof updateProfileSchema>
// type UpdateProfile = {
// first_name: string
// last_name: string | null <-- HOW TO REMOVE NULL FROM HERE???????????
// }
export const profile = mysqlTable("profile", {
first_name: varchar("first_name").notNull(),
last_name: varchar("last_name")
})

const updateProfileSchema = createInsertSchema(profile, {
last_name: schema => schema.last_name.min(1)
// last_name: z.string().min(1) <-- THIS DONT WORK EITHER
}).required({
last_name: true
})

type UpdateProfile = z.infer<typeof updateProfileSchema>
// type UpdateProfile = {
// first_name: string
// last_name: string | null <-- HOW TO REMOVE NULL FROM HERE???????????
// }
1 replies
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