Skip to content

Connect on first use

Connect to the database on first use, then reuse the connection. Two requests at startup both see no connection yet, and both connect.

src/lazy-connect.ts
export function lazyConnect<T>(connect: () => Promise<T>) {
let db: T | undefined;
return async () => {
if (!db) db = await connect();
return db;
};
}

See how TLA+ finds and fixes it.

Last updated: