Skip to content

Commit 586d6b8

Browse files
committed
Refactor to have a single Client, and more carefully maintain connection for transaction.
1 parent 2746325 commit 586d6b8

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/index.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ export class PlanetScaleDialect implements Dialect {
7777
}
7878

7979
class PlanetScaleDriver implements Driver {
80-
#config: PlanetScaleDialectConfig
80+
#client: Client
8181

8282
constructor(config: PlanetScaleDialectConfig) {
83-
this.#config = config
83+
this.#client = new Client({cast: inflateDates, ...config})
8484
}
8585

8686
async init(): Promise<void> {}
8787

8888
async acquireConnection(): Promise<DatabaseConnection> {
89-
return new PlanetScaleConnection(this.#config)
89+
return new PlanetScaleConnection(this.#client)
9090
}
9191

9292
async beginTransaction(conn: PlanetScaleConnection): Promise<void> {
@@ -109,36 +109,35 @@ class PlanetScaleDriver implements Driver {
109109
const sharedConnections = new WeakMap<PlanetScaleDialectConfig, Connection>()
110110

111111
class PlanetScaleConnection implements DatabaseConnection {
112-
#config: PlanetScaleDialectConfig
113112
#client: Client
114-
#transactionClient?: PlanetScaleConnection
113+
#transactionConn?: Connection
114+
#useSharedConnection: boolean
115115

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
120118
}
121119

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())
131124
}
132125

133126
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+
}
135133

134+
private async execute<O>(compiledQuery: CompiledQuery, conn: Pick<Connection, 'execute'>): Promise<QueryResult<O>> {
136135
// If no custom formatter is provided, format dates as DB date strings
137136
const parameters = this.#config.format
138137
? compiledQuery.parameters
139138
: compiledQuery.parameters.map((param) => (param instanceof Date ? formatDate(param) : param))
140139

141-
const results = await this.#conn.execute(compiledQuery.sql, parameters)
140+
const results = await conn.execute(compiledQuery.sql, parameters)
142141

143142
// @planetscale/database versions older than 1.3.0 return errors directly, rather than throwing
144143
if ((results as any).error) {
@@ -159,25 +158,25 @@ class PlanetScaleConnection implements DatabaseConnection {
159158
}
160159

161160
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')
164163
}
165164

166165
async commitTransaction() {
167-
if (!this.#transactionClient) throw new Error('No transaction to commit')
166+
if (!this.#transactionConn) throw new Error('No transaction to commit')
168167
try {
169-
await this.#transactionClient.#conn.execute('COMMIT')
168+
await this.#transactionConn.execute('COMMIT')
170169
} finally {
171-
this.#transactionClient = undefined
170+
this.#transactionConn = undefined
172171
}
173172
}
174173

175174
async rollbackTransaction() {
176-
if (!this.#transactionClient) throw new Error('No transaction to rollback')
175+
if (!this.#transactionConn) throw new Error('No transaction to rollback')
177176
try {
178-
await this.#transactionClient.#conn.execute('ROLLBACK')
177+
await this.#transactionConn.execute('ROLLBACK')
179178
} finally {
180-
this.#transactionClient = undefined
179+
this.#transactionConn = undefined
181180
}
182181
}
183182

0 commit comments

Comments
 (0)