brettlyc
brettlyc
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
I just figured that out! I didn't need to call the rollback function when throwing an error. Thank you for your help!
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
throw new import_errors.TransactionRollbackError();
^

TransactionRollbackError [DrizzleError]: Rollback
throw new import_errors.TransactionRollbackError();
^

TransactionRollbackError [DrizzleError]: Rollback
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
It's now giving an error
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
It basically creates a new entry in the database in the array. However, I want to validate each entry beforehand. If there's an error with one, I want to rollback all changes.
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
Here's my code:
const createNewSchedules = async (scheduleData, store) => {
const startTime = getDate(scheduleData.start_time)
const endTime = getDate(scheduleData.end_time)
const daysToSchedule = getDaysBetween(scheduleData.dates)

return await db.transaction((tx) => {
daysToSchedule.map(async (date) => {
scheduleData.date = new Date(date)
scheduleData.start_time = getDate(date).set('hour', startTime.get('hour')).set('minute', startTime.get('minute')).toDate()
scheduleData.end_time = getDate(date).set('hour', endTime.get('hour')).set('minute', endTime.get('minute')).toDate()

const validatedSchedule = await TechnicianSchedule.parseAsync(scheduleData)
if (validatedSchedule.errors) throw validatedSchedule.errors

try {
await verifyTechSchedules(validatedSchedule)

await tx.insert(technicianSchedules).values({
...validatedSchedule,
store,
schedule_created: new Date()
})
} catch (err) {
tx.rollback()
}
})
})
}
const createNewSchedules = async (scheduleData, store) => {
const startTime = getDate(scheduleData.start_time)
const endTime = getDate(scheduleData.end_time)
const daysToSchedule = getDaysBetween(scheduleData.dates)

return await db.transaction((tx) => {
daysToSchedule.map(async (date) => {
scheduleData.date = new Date(date)
scheduleData.start_time = getDate(date).set('hour', startTime.get('hour')).set('minute', startTime.get('minute')).toDate()
scheduleData.end_time = getDate(date).set('hour', endTime.get('hour')).set('minute', endTime.get('minute')).toDate()

const validatedSchedule = await TechnicianSchedule.parseAsync(scheduleData)
if (validatedSchedule.errors) throw validatedSchedule.errors

try {
await verifyTechSchedules(validatedSchedule)

await tx.insert(technicianSchedules).values({
...validatedSchedule,
store,
schedule_created: new Date()
})
} catch (err) {
tx.rollback()
}
})
})
}
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
Thank you @Dari for the insight! I appreciate that bigtime.
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
Am I missing something or am I just dumb?
11 replies