@@ -78,22 +78,41 @@ export function getBunSql(): SQL {
7878 }
7979}
8080
81- // Note: This is created once when the module loads
82- // Using a fallback in-memory SQLite if main database is unavailable
81+ // Note: Connection is created lazily on first access, not at module load time
8382let _bunSqlInstance : SQL | null = null
83+ let _currentDialect : string | null = null
84+ let _currentDatabase : string | null = null
8485
8586export function getOrCreateBunSql ( forceNew = false ) : SQL {
86- // If forceNew is true or we don't have an instance, create a new one
87- if ( forceNew || ! _bunSqlInstance ) {
87+ // Check if config has changed since we created the connection
88+ const configChanged = _bunSqlInstance !== null && (
89+ _currentDialect !== config . dialect
90+ || _currentDatabase !== config . database . database
91+ )
92+
93+ // If forceNew is true, config changed, or we don't have an instance, create a new one
94+ if ( forceNew || configChanged || ! _bunSqlInstance ) {
8895 _bunSqlInstance = getBunSql ( )
96+ _currentDialect = config . dialect
97+ _currentDatabase = config . database . database
8998 }
9099 return _bunSqlInstance
91100}
92101
102+ /**
103+ * Resets the cached database connection.
104+ * Call this after changing config via setConfig() to ensure the new config is used.
105+ */
106+ export function resetConnection ( ) : void {
107+ _bunSqlInstance = null
108+ _currentDialect = null
109+ _currentDatabase = null
110+ }
111+
93112// Wrapper that catches "Connection closed" errors and retries with a fresh connection
94113export async function withFreshConnection < T > ( fn : ( sql : SQL ) => Promise < T > ) : Promise < T > {
95114 try {
96- return await fn ( _bunSqlInstance || getOrCreateBunSql ( ) )
115+ return await fn ( getOrCreateBunSql ( ) )
97116 }
98117 catch ( error : any ) {
99118 // If connection is closed, create a fresh connection and retry once
@@ -106,7 +125,33 @@ export async function withFreshConnection<T>(fn: (sql: SQL) => Promise<T>): Prom
106125 }
107126}
108127
109- export const bunSql = getOrCreateBunSql ( )
128+ /**
129+ * Lazy SQL connection proxy - connection is only created when first accessed.
130+ * This allows setConfig() to be called before any database connection is made.
131+ */
132+ function createLazyBunSql ( ) : SQL {
133+ // Create a proxy that lazily initializes the connection on first use
134+ return new Proxy ( { } as SQL , {
135+ get ( _target , prop ) {
136+ // Get or create the actual SQL instance
137+ const sql = getOrCreateBunSql ( )
138+ const value = ( sql as any ) [ prop ]
139+ // If it's a function, bind it to the sql instance
140+ if ( typeof value === 'function' ) {
141+ return value . bind ( sql )
142+ }
143+ return value
144+ } ,
145+ apply ( _target , _thisArg , args ) {
146+ // Handle tagged template literal calls: bunSql`SELECT ...`
147+ const sql = getOrCreateBunSql ( )
148+ return ( sql as any ) ( ...args )
149+ } ,
150+ } )
151+ }
152+
153+ // Export a lazy proxy - no connection is made until first use
154+ export const bunSql : SQL = createLazyBunSql ( )
110155
111156// Add global error handler for unhandled rejections from SQL connections
112157if ( typeof process !== 'undefined' && process . on ) {
@@ -123,7 +168,6 @@ if (typeof process !== 'undefined' && process.on) {
123168 || reason . code === '3D000' )
124169 ) {
125170 // Suppress these errors - they're expected when database isn't available
126-
127171 }
128172 }
129173 Object . defineProperty ( sqlConnectionErrorHandler , 'name' , { value : 'sqlConnectionErrorHandler' } )
0 commit comments