dj_klashndjksad
dj_klashndjksad
DTDrizzle Team
Created by dj_klashndjksad on 10/26/2024 in #help
After commit hook for transactions
and delaying the queue invocation after a few seconds sounds just too unreliable
4 replies
DTDrizzle Team
Created by dj_klashndjksad on 10/26/2024 in #help
After commit hook for transactions
It would be nice to have this at it would allow us to collocate this kind of logic, instead of invoking all the BullMQ queues later at once in the controller
4 replies
DTDrizzle Team
Created by dj_klashndjksad on 10/26/2024 in #help
After commit hook for transactions
For example imagine simple scenario like this. But it could get way more complex.
app.post('job-offer', async (req, res) => {
return req.db.transaction(async (trx) => {
const jobOffer = await createJobOffer(trx, req.body)
})
})

// Transaction like is just a custom type, imagine it like `typeof db`
async function createJobOffer(trx: TransactionLike, data: any) {
const authorId = await upsertAuthor(trx, data.author)
const jobId = await upsertJobCategory(trx, data.job)

const jobOffer = await trx
.insert(jobOfferTable)
.values({...data, authorId, jobId})
.returning()

// now the same as in upsert job, we want to make the jobOffer more searchable
// so we want to trigger BullMQ queue that uses AI to generate tags,
// categories and embeddings for the jobOffer, only problem the jobOffer isn't committed yet so it wouldn't be available inside the job if it starts sooner
// so to fix this we could 'schedule it' with the 'afterCommit` hook
trx.afterCommit(() => generateJobOfferAttributesFlow.invoke(jobOffer.id))

return jobOffer
}

export async function upsertJobCategory(trx: TransactionLike, data: any) {
const existingJobCategory = await trx.select()....
if(existingJobCategory) return existingJob.id

// if job doesn't exist, create it
const jobCategory = await trx.insert()..

// now after we create new jobCategory, I want to trigger BullMQ queue that uses AI to generate tags,
// other attributes and embeddings for the jobCategory, only problem the transaction isn't committed yet, so if the generate flow starts sooner before the transaction commits, the newly created row wouldn't be available
// so to fix this we could 'schedule it' with the 'afterCommit` hook
trx.afterCommit(() => generateJobCategoryAttributesFlow.invoke(job.id))

return jobCategory.id
}
app.post('job-offer', async (req, res) => {
return req.db.transaction(async (trx) => {
const jobOffer = await createJobOffer(trx, req.body)
})
})

// Transaction like is just a custom type, imagine it like `typeof db`
async function createJobOffer(trx: TransactionLike, data: any) {
const authorId = await upsertAuthor(trx, data.author)
const jobId = await upsertJobCategory(trx, data.job)

const jobOffer = await trx
.insert(jobOfferTable)
.values({...data, authorId, jobId})
.returning()

// now the same as in upsert job, we want to make the jobOffer more searchable
// so we want to trigger BullMQ queue that uses AI to generate tags,
// categories and embeddings for the jobOffer, only problem the jobOffer isn't committed yet so it wouldn't be available inside the job if it starts sooner
// so to fix this we could 'schedule it' with the 'afterCommit` hook
trx.afterCommit(() => generateJobOfferAttributesFlow.invoke(jobOffer.id))

return jobOffer
}

export async function upsertJobCategory(trx: TransactionLike, data: any) {
const existingJobCategory = await trx.select()....
if(existingJobCategory) return existingJob.id

// if job doesn't exist, create it
const jobCategory = await trx.insert()..

// now after we create new jobCategory, I want to trigger BullMQ queue that uses AI to generate tags,
// other attributes and embeddings for the jobCategory, only problem the transaction isn't committed yet, so if the generate flow starts sooner before the transaction commits, the newly created row wouldn't be available
// so to fix this we could 'schedule it' with the 'afterCommit` hook
trx.afterCommit(() => generateJobCategoryAttributesFlow.invoke(job.id))

return jobCategory.id
}
4 replies
DTDrizzle Team
Created by addamsson on 6/3/2024 in #help
Is there a way to do a "down" migration with Drizzle?
Hello, I just made this small lib today that adds up and down in drizzle using some drizzle-kit internals, that they are using to make migrations work inside payloadcms. This is very early and work still in progress, but give it a try and maybe try to contribute too. https://www.npmjs.com/package/@drepkovsky/drizzle-migrations?activeTab=readme
18 replies
DTDrizzle Team
Created by cosbgn on 1/25/2024 in #help
Why do I need to duplicate names in my schema?
You could have a separate naming conventions between your db layer and application layer (eg. camelCase for JS, snake_case for SQL). Drizzle lets you have full control over it. In other ORMs where it is done automatically by some magical naming strategies implementations, it could get really frustrating having to think every time, just to get the naming right. There can be many occasions when you want to do raw queries directly to your db. So to have the ability to know exactly what are the columns named in your db is just a big plus. You don't have to do any mental gymnastics around it. I remember it was really painfull to write raw queries to DB schemas build upon typeorm or prisma (didn't know about the mapping option back then), because the naming wasn't so straightforward. In my opinion, having the explicit control over your DB layer and APP layer naming is just something you could only benefit from in the long run.
2 replies