@@ -77,16 +77,16 @@ export class PlanetScaleDialect implements Dialect {
77
77
}
78
78
79
79
class PlanetScaleDriver implements Driver {
80
- #config: PlanetScaleDialectConfig
80
+ #client: Client
81
81
82
82
constructor ( config : PlanetScaleDialectConfig ) {
83
- this . #config = config
83
+ this . #client = new Client ( { cast : inflateDates , ... config } )
84
84
}
85
85
86
86
async init ( ) : Promise < void > { }
87
87
88
88
async acquireConnection ( ) : Promise < DatabaseConnection > {
89
- return new PlanetScaleConnection ( this . #config )
89
+ return new PlanetScaleConnection ( this . #client )
90
90
}
91
91
92
92
async beginTransaction ( conn : PlanetScaleConnection ) : Promise < void > {
@@ -109,36 +109,35 @@ class PlanetScaleDriver implements Driver {
109
109
const sharedConnections = new WeakMap < PlanetScaleDialectConfig , Connection > ( )
110
110
111
111
class PlanetScaleConnection implements DatabaseConnection {
112
- #config: PlanetScaleDialectConfig
113
112
#client: Client
114
- #transactionClient?: PlanetScaleConnection
113
+ #transactionConn?: Connection
114
+ #useSharedConnection: boolean
115
115
116
- get #conn( ) : Connection {
117
- if ( this . #transactionClient) return this . #transactionClient. #conn
118
- if ( this . #useSharedConnection) return sharedConnections . get ( this . #config) as Connection
119
- return this . #client. connection ( )
116
+ get #config( ) : Config {
117
+ return this . #client. config
120
118
}
121
119
122
- get #useSharedConnection( ) : boolean {
123
- return Boolean ( this . #config. useSharedConnection && ! this . #transactionClient)
124
- }
125
-
126
- constructor ( config : PlanetScaleDialectConfig , isForTransaction = false ) {
127
- this . #config = config
128
- const useSharedConnection = config . useSharedConnection && ! isForTransaction
129
- this . #client = new Client ( { cast : inflateDates , ...config } )
130
- if ( useSharedConnection ) sharedConnections . set ( config , this . #client. connection ( ) )
120
+ constructor ( client : Client , useSharedConnection = false , isForTransaction = false ) {
121
+ this . #client = client
122
+ this . #useSharedConnection = useSharedConnection && ! isForTransaction
123
+ if ( this . #useSharedConnection) sharedConnections . set ( this . #config, this . #client. connection ( ) )
131
124
}
132
125
133
126
async executeQuery < O > ( compiledQuery : CompiledQuery ) : Promise < QueryResult < O > > {
134
- if ( this . #transactionClient) return this . #transactionClient. executeQuery ( compiledQuery )
127
+ if ( this . #transactionConn) return this . execute ( compiledQuery , this . #transactionConn)
128
+
129
+ return this . #useSharedConnection
130
+ ? this . execute ( compiledQuery , sharedConnections . get ( this . #config) || this . #client)
131
+ : this . execute ( compiledQuery , this . #client)
132
+ }
135
133
134
+ private async execute < O > ( compiledQuery : CompiledQuery , conn : Pick < Connection , 'execute' > ) : Promise < QueryResult < O > > {
136
135
// If no custom formatter is provided, format dates as DB date strings
137
136
const parameters = this . #config. format
138
137
? compiledQuery . parameters
139
138
: compiledQuery . parameters . map ( ( param ) => ( param instanceof Date ? formatDate ( param ) : param ) )
140
139
141
- const results = await this . # conn. execute ( compiledQuery . sql , parameters )
140
+ const results = await conn . execute ( compiledQuery . sql , parameters )
142
141
143
142
// @planetscale /database versions older than 1.3.0 return errors directly, rather than throwing
144
143
if ( ( results as any ) . error ) {
@@ -159,25 +158,25 @@ class PlanetScaleConnection implements DatabaseConnection {
159
158
}
160
159
161
160
async beginTransaction ( ) {
162
- this . #transactionClient = this . #transactionClient ?? new PlanetScaleConnection ( this . #config , true )
163
- await this . #transactionClient . #conn . execute ( 'BEGIN' )
161
+ this . #transactionConn = this . #transactionConn ?? this . #client . connection ( )
162
+ await this . #transactionConn . execute ( 'BEGIN' )
164
163
}
165
164
166
165
async commitTransaction ( ) {
167
- if ( ! this . #transactionClient ) throw new Error ( 'No transaction to commit' )
166
+ if ( ! this . #transactionConn ) throw new Error ( 'No transaction to commit' )
168
167
try {
169
- await this . #transactionClient . #conn . execute ( 'COMMIT' )
168
+ await this . #transactionConn . execute ( 'COMMIT' )
170
169
} finally {
171
- this . #transactionClient = undefined
170
+ this . #transactionConn = undefined
172
171
}
173
172
}
174
173
175
174
async rollbackTransaction ( ) {
176
- if ( ! this . #transactionClient ) throw new Error ( 'No transaction to rollback' )
175
+ if ( ! this . #transactionConn ) throw new Error ( 'No transaction to rollback' )
177
176
try {
178
- await this . #transactionClient . #conn . execute ( 'ROLLBACK' )
177
+ await this . #transactionConn . execute ( 'ROLLBACK' )
179
178
} finally {
180
- this . #transactionClient = undefined
179
+ this . #transactionConn = undefined
181
180
}
182
181
}
183
182
0 commit comments