You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to create a generic function called softDelete(id: string) that can be used across multiple tables. The function should perform a transaction and, if the deletion fails, attempt to “soft-delete” the record. The function should take a generic table that must have two columns: id: string and deletedAt: Date.
Here’s an example of how the function could work:
/** * Soft-deletes an existing location by id */exportasyncfunctiontrashLocation(id: string){awaitdb.transaction(async(tx)=>{try{constdeleted=awaittx.delete(location).where(eq(location.id,id));if(deleted.rowCount===0){awaittx.update(location).set({deletedAt: newDate()}).where(eq(location.id,id));}}catch(err){if(errinstanceofError&&err.message.includes(“foreignkeyconstraint”)){awaittx.update(location).set({deletedAt: newDate()}).where(eq(location.id,id));}else{throwerr;}}});}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I need to create a generic function called
softDelete(id: string)
that can be used across multiple tables. The function should perform a transaction and, if the deletion fails, attempt to “soft-delete” the record. The function should take a generic table that must have two columns:id: string
anddeletedAt: Date
.Here’s an example of how the function could work:
Beta Was this translation helpful? Give feedback.
All reactions