Skip to content

Commit 2746325

Browse files
committed
Use Planetscale Client instead of raw Connection, to enable concurrent queries.
1 parent fb1baf0 commit 2746325

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/index.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {cast, Config, connect, Connection, Field} from '@planetscale/database'
1+
import {Client, Config, Connection, Field, cast} from '@planetscale/database'
22
import {parseJSON} from 'date-fns'
33
import {
44
CompiledQuery,
@@ -71,7 +71,7 @@ export class PlanetScaleDialect implements Dialect {
7171
return new MysqlQueryCompiler()
7272
}
7373

74-
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
74+
createIntrospector(db: Kysely<unknown>): DatabaseIntrospector {
7575
return new MysqlIntrospector(db)
7676
}
7777
}
@@ -110,15 +110,24 @@ const sharedConnections = new WeakMap<PlanetScaleDialectConfig, Connection>()
110110

111111
class PlanetScaleConnection implements DatabaseConnection {
112112
#config: PlanetScaleDialectConfig
113-
#conn: Connection
113+
#client: Client
114114
#transactionClient?: PlanetScaleConnection
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()
120+
}
121+
122+
get #useSharedConnection(): boolean {
123+
return Boolean(this.#config.useSharedConnection && !this.#transactionClient)
124+
}
125+
116126
constructor(config: PlanetScaleDialectConfig, isForTransaction = false) {
117127
this.#config = config
118128
const useSharedConnection = config.useSharedConnection && !isForTransaction
119-
const sharedConnection = useSharedConnection ? sharedConnections.get(config) : undefined
120-
this.#conn = sharedConnection ?? connect({cast: inflateDates, ...config})
121-
if (useSharedConnection) sharedConnections.set(config, this.#conn)
129+
this.#client = new Client({cast: inflateDates, ...config})
130+
if (useSharedConnection) sharedConnections.set(config, this.#client.connection())
122131
}
123132

124133
async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {

0 commit comments

Comments
 (0)