transaction is not defined

When calling
const transaction = await db.transaction()
const transaction = await db.transaction()
it returns an error
TypeError: transaction is not a function
TypeError: transaction is not a function
This is my db file
const { drizzle } = require('drizzle-orm/node-postgres')
const { Client } = require('pg')
const { vehicles } = require('#schemas/vehicles.js')
const { technicianSchedules } = require('#schemas/technician_schedules.js')
const { stores } = require('#schemas/stores.js')
const { services } = require('#schemas/services.js')
const { technicians } = require('#schemas/technicians.js')
const { appointments } = require('#schemas/appointments.js')
const { zipcodes } = require('#schemas/zipcodes.js')
const { email_templates } = require('#schemas/email_templates.js')
const { emails } = require('#schemas/emails.js')
const { email_tracking } = require('#schemas/email_tracking.js')

const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})

client.connect(null).then(() => console.log('The Database has been connected, successfully.'))

module.exports = {
db: drizzle(client, {
schema: {
vehicles,
technicianSchedules,
stores,
services,
technicians,
appointments,
zipcodes,
email_templates,
emails,
email_tracking
}
}),
client
}
const { drizzle } = require('drizzle-orm/node-postgres')
const { Client } = require('pg')
const { vehicles } = require('#schemas/vehicles.js')
const { technicianSchedules } = require('#schemas/technician_schedules.js')
const { stores } = require('#schemas/stores.js')
const { services } = require('#schemas/services.js')
const { technicians } = require('#schemas/technicians.js')
const { appointments } = require('#schemas/appointments.js')
const { zipcodes } = require('#schemas/zipcodes.js')
const { email_templates } = require('#schemas/email_templates.js')
const { emails } = require('#schemas/emails.js')
const { email_tracking } = require('#schemas/email_tracking.js')

const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})

client.connect(null).then(() => console.log('The Database has been connected, successfully.'))

module.exports = {
db: drizzle(client, {
schema: {
vehicles,
technicianSchedules,
stores,
services,
technicians,
appointments,
zipcodes,
email_templates,
emails,
email_tracking
}
}),
client
}
However, everything else works..
5 Replies
brettlyc
brettlycOP•4mo ago
Am I missing something or am I just dumb?
Dari
Dari•4mo ago
transaction does not return the transaction. Instead it provides the transaction within the lambda function. So it definetly needs a function which executes within the transaction. Something like this:
await db.transaction((trx) => {
// use and pass trx where needed
trx.insert(helloTable).values({ name: "hello" });
await myRandomFunction(trx, randomParameters);
});
await db.transaction((trx) => {
// use and pass trx where needed
trx.insert(helloTable).values({ name: "hello" });
await myRandomFunction(trx, randomParameters);
});
Basically you need to wrap the code with
db.transaction((trx) => {
//...yourCode
});
db.transaction((trx) => {
//...yourCode
});
Hope that helps 🙂
brettlyc
brettlycOP•4mo ago
Thank you @Dari for the insight! I appreciate that bigtime. 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()
}
})
})
}
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. It's now giving an error
throw new import_errors.TransactionRollbackError();
^

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

TransactionRollbackError [DrizzleError]: Rollback
Dari
Dari•4mo ago
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(async (tx) => {
await Promise.all(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(async (tx) => {
await Promise.all(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()
}
}))
})
}
Try this, I think it should fix it. btw drizzle rollsback automatically if any error is thrown within the transaction function. So you wouldn need to try catch and if an error occurs it should rollback as expected
brettlyc
brettlycOP•4mo ago
I just figured that out! I didn't need to call the rollback function when throwing an error. Thank you for your help!
Want results from more Discord servers?
Add your server