transaction is not defined
When calling
it returns an error
This is my db file
However, everything else works..
const transaction = await db.transaction()
const transaction = await db.transaction()
TypeError: transaction is not a function
TypeError: transaction is not a function
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
}
5 Replies
Am I missing something or am I just dumb?
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);
});
db.transaction((trx) => {
//...yourCode
});
db.transaction((trx) => {
//...yourCode
});
Thank you @Dari for the insight! I appreciate that bigtime.
Here's my code:
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
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()
}
})
})
}
throw new import_errors.TransactionRollbackError();
^
TransactionRollbackError [DrizzleError]: Rollback
throw new import_errors.TransactionRollbackError();
^
TransactionRollbackError [DrizzleError]: 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()
}
}))
})
}
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()
}
}))
})
}
I just figured that out! I didn't need to call the rollback function when throwing an error. Thank you for your help!