Currently experiencing a problem when trying to create reusable functions in a service where I'm passing the database connection to a function.
createNote = async (input: CreateSupplementalNoteInput): Promise<CaseFile> => {
const { leadIdentifier, notes, createUserId, actor } = input;
const createNotesAction = async (db, caseId: string) => {
const ACTION_CODE = "UPDATENOTE";
const ACTION_TYPE_CODE = "CASEACTION";
const xref = await db.action_type_action_xref.create({
data: {
action_code: ACTION_CODE,
action_type_code: ACTION_TYPE_CODE,
create_user_id: createUserId,
create_utc_timestamp: new Date(),
},
});
const action = await db.action.create({
data: {
data: {
case_guid: caseId,
action_type_action_xref_guid: xref,
actor_guid: actor,
action_date: new Date(),
create_user_id: createUserId,
create_utc_timestamp: new Date(),
},
}
})
};
try {
await this.prisma.$transaction(async (db) => {
const caseFile = await this.findOneByLeadId(leadIdentifier);
if (caseFile) {
const { caseIdentifier } = caseFile;
const update = { ...caseFile, notes };
db.case_file.update({
where: { case_file_guid: caseIdentifier },
data: {
note_text: notes,
},
}).then(result => {
createNotesAction(db, caseIdentifier);
})
} else {
}
});
} catch (error) {
console.log("exception: unable to create supplemental note", error);
throw new GraphQLError("Exception occurred. See server log for details", {});
}
return Promise.resolve({} as CaseFile);
};
When I try and pass the db object into my function this is where I'm running into trouble and the connection closes before I get a chance to actually run that function.
I've tried to both await the promise originally, and have tried using the returned promise, but in both situations the transaction closes. New to working with prisma so I'm at a bit of a loss as to why this is happening.
This is a common error when using a Prisma transaction with slightly heavy data. There is a default timeout for the Prisma transaction, but you can custom-set the timeout to suit your case, as shown below. The issue is happening because the time needed for the transaction is taking more time than the default time.
Update the transaction as follow:
await this.prisma.$transaction(
async (db) => {
const caseFile = await this.findOneByLeadId(leadIdentifier);
if (caseFile) {
const { caseIdentifier } = caseFile;
const update = { ...caseFile, notes };
await db.case_file.update({
where: { case_file_guid: caseIdentifier },
data: {
note_text: notes,
},
});
await createNotesAction(db, caseIdentifier);
} else {
throw new Error("Case file not found");
}
},
{
maxWait: 5000, // 5 seconds max wait to connect to prisma
timeout: 20000, // 20 seconds
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With