When it comes to functions related to database operations, I have a handy utility/wrapper function that handles specific tasks. It takes a function fn
of type
Promise<PromiseReturnType<GENERIC>>
, performs some preliminary actions (such as checking for connection), and then executes the function:
// Implementation in file `A.ts`
type PromiseReturnType<T = undefined> = {success: boolean, response: T }
const DatabaseOperation = <Fn extends (...args: any) => Promise<PromiseReturnType<Data>>, Data>(fn: (...args: any) => Promise<PromiseReturnType<Data>>) => {
return async function (...args: Parameters<Fn>): Promise<PromiseReturnType<Data>> {
if (!isConnected) return Promise.reject({ success: false, response: new Error("Not connected to the Database!") })
return await fn(...args)
}
}
This utility function is then utilized as follows:
// Usage in file `B.ts`
const dbOperation = async (myArg: number): Promise<PromiseReturnType<string> => {
return Promise.resolve({ success: true, response: `hello world ${myArg}` })
}
export default DatabaseOperation<typeof dbOperation, string>(dbOperation)
One issue encountered is that the return type cannot be inferred accurately, resulting in Promise<any>
instead of
Promise<PromiseReturnType<string>>
.
When using this wrapped function, the syntax would be as follows:
import dbOperation from "B.ts"
(function () {
await dbOperation() // Type: dbOperation(myArg: number): Promise<any>
})()
If you have any suggestions on how to address this challenge or improve the implementation in general, your insights are greatly appreciated. Thank you!