is this type of singleton to not re-initialize connections on every requests disencouraged in Worker

is this type of singleton to not re-initialize connections on every requests disencouraged in Workers? Unless I change it to be a map by bookmark I'm assuming implementing Sessions API implicitly means I'll be always connecting on each request (and/or potentially some internal optimizations makes this unnecessary)

let db: Kysely<DB>;
export async function getDB() {
  if (db) return db;
  const d1Binding = await getDatabaseProvider();
  db = new Kysely<DB>({
    dialect: new D1Dialect({ database: d1Binding }),
  });
  return db;
}
Was this page helpful?