san4d
san4d
Explore posts from servers
DTDrizzle Team
Created by san4d on 4/22/2024 in #help
Error with Nested Transactions
I'm experiencing an issue with nested transactions on neon serverless 0.9.1, drizzle-orm 0.30.9, and ws 8.16.0. The following works locally but not in production:
async function createOrGet<T>(
parentTxn: DbTransaction<T>,
entity: NewEntity,
): Promise<string | null> {
try {
return await parentTxn.transaction(async (childTxn) => {
return await createInTxn(childTxn, entity)
})
} catch (e: unknown) {
if (isDbErrorWithCode(e, PostgresErrorCode.UNIQUE_VIOLATION)) {
return await findWorkflowTemplateByNameInTxn(parentTxn, entity.id)
} else {
return null
}
}
}
async function createOrGet<T>(
parentTxn: DbTransaction<T>,
entity: NewEntity,
): Promise<string | null> {
try {
return await parentTxn.transaction(async (childTxn) => {
return await createInTxn(childTxn, entity)
})
} catch (e: unknown) {
if (isDbErrorWithCode(e, PostgresErrorCode.UNIQUE_VIOLATION)) {
return await findWorkflowTemplateByNameInTxn(parentTxn, entity.id)
} else {
return null
}
}
}
The following works in production but not locally:
async function createOrGet<T>(
parentTxn: DbTransaction<T>,
entity: NewEntity,
): Promise<string | null> {
try {
return await createInTxn(parentTxn, entity)
} catch (e: unknown) {
if (isDbErrorWithCode(e, PostgresErrorCode.UNIQUE_VIOLATION)) {
return await findWorkflowTemplateByNameInTxn(parentTxn, entity.id)
} else {
return null
}
}
}
async function createOrGet<T>(
parentTxn: DbTransaction<T>,
entity: NewEntity,
): Promise<string | null> {
try {
return await createInTxn(parentTxn, entity)
} catch (e: unknown) {
if (isDbErrorWithCode(e, PostgresErrorCode.UNIQUE_VIOLATION)) {
return await findWorkflowTemplateByNameInTxn(parentTxn, entity.id)
} else {
return null
}
}
}
I'd expect the 1st (parent-child transactions) to work. Instead, I get a 25P01 error NO_ACTIVE_SQL_TRANSACTION with message
error: ROLLBACK TO SAVEPOINT can only be used in transaction blocks\n at file:///var/task/dist/server/entry.mjs:148485:23\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async _NeonTransaction.transaction
5 replies
DTDrizzle Team
Created by san4d on 4/11/2024 in #help
Parameterized Interval in Where Clause
I'm trying to write a query that uses an interval as a parameter. The SQL looks like this and works fine:
where now() - interval '1 month' < table.date
where now() - interval '1 month' < table.date
When I try to write it using drizzle, it also works fine if a hard code the interval:
lt(
sql<Date>`now() - interval '1 month'`,
sql<Date>`table.date`
)
lt(
sql<Date>`now() - interval '1 month'`,
sql<Date>`table.date`
)
However, it does not work if I try to parameterize the interval like this:
lt(
sql<Date>`now() - interval '${interval}'`,
sql<Date>`table.date`
)
lt(
sql<Date>`now() - interval '${interval}'`,
sql<Date>`table.date`
)
or this:
sql<boolean>`now() - interval '${interval}' < table.date`,
sql<boolean>`now() - interval '${interval}' < table.date`,
I get this error
Error: could not determine data type of parameter $3
I tried this
lt(
sql<Date>`now() - ${interval}`,
sql<Date>`table.date`
)
lt(
sql<Date>`now() - ${interval}`,
sql<Date>`table.date`
)
and got this error
operator does not exist: interval < timestamp with time zone
Since the raw SQL works, I must be missing something in the ORM usage. Any suggestions? I'm running Postgres 16 on neon using drizzle 0.30.7.
4 replies
DTDrizzle Team
Created by san4d on 1/31/2024 in #help
Error when making a websocket-based transaction on an AWS Lambda Function
Problem I'm seeing this error when I try to make a connection:
{
"errorType":"TypeError","errorMessage":"bufferUtil2.mask is not a function",
"stack":[
"TypeError: bufferUtil2.mask is not a function",
"at bufferUtil$1.exports.mask (file:///var/task/dist/server/entry.mjs:111053:25)",
"at Sender.frame (file:///var/task/dist/server/entry.mjs:112347:11)",
"at Sender.send (file:///var/task/dist/server/entry.mjs:112536:20)",
"WebSocket2.send (file:///var/task/dist/server/entry.mjs:113273:22)",
"at Timeout._onTimeout (file:///var/task/dist/server/entry.mjs:109102:25)",
"at listOnTimeout (node:internal/timers:569:17)",
"at process.processTimers (node:internal/timers:512:7)"
]
}
{
"errorType":"TypeError","errorMessage":"bufferUtil2.mask is not a function",
"stack":[
"TypeError: bufferUtil2.mask is not a function",
"at bufferUtil$1.exports.mask (file:///var/task/dist/server/entry.mjs:111053:25)",
"at Sender.frame (file:///var/task/dist/server/entry.mjs:112347:11)",
"at Sender.send (file:///var/task/dist/server/entry.mjs:112536:20)",
"WebSocket2.send (file:///var/task/dist/server/entry.mjs:113273:22)",
"at Timeout._onTimeout (file:///var/task/dist/server/entry.mjs:109102:25)",
"at listOnTimeout (node:internal/timers:569:17)",
"at process.processTimers (node:internal/timers:512:7)"
]
}
I'm able to make HTTP calls no problem. Setup - Node 18.v20 Runtime on AWS Lambda - Postgres 16 on Neon - @Neondatabase/serverless v0.6.1 - drizzle-orm v0.29.3 - ws v8.16.0 Ask Has anyone ran into this before? If so, what did you you need to change? I'm looking into the bufferUtil2.mask error currently. Solution I figured out that I needed to add bufferutil as an optional dependency:
"optionalDependencies": {
"bufferutil": "^4.0.8"
}
"optionalDependencies": {
"bufferutil": "^4.0.8"
}
3 replies