Skip to content

Commit c9c08d3

Browse files
Merge pull request #33 from cortexclick/planetscale_client
Use Planetscale Client instead of raw Connection, to enable concurrent queries.
2 parents fb1baf0 + d9a4e78 commit c9c08d3

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/index.ts

Lines changed: 32 additions & 24 deletions
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,22 +71,22 @@ 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
}
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,27 +109,35 @@ class PlanetScaleDriver implements Driver {
109109
const sharedConnections = new WeakMap<PlanetScaleDialectConfig, Connection>()
110110

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

116-
constructor(config: PlanetScaleDialectConfig, isForTransaction = false) {
117-
this.#config = config
118-
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)
116+
get #config(): Config {
117+
return this.#client.config
118+
}
119+
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, sharedConnections.get(this.#config) ?? this.#client.connection())
122124
}
123125

124126
async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
125-
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+
}
126133

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

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

134142
// @planetscale/database versions older than 1.3.0 return errors directly, rather than throwing
135143
if ((results as any).error) {
@@ -150,25 +158,25 @@ class PlanetScaleConnection implements DatabaseConnection {
150158
}
151159

152160
async beginTransaction() {
153-
this.#transactionClient = this.#transactionClient ?? new PlanetScaleConnection(this.#config, true)
154-
await this.#transactionClient.#conn.execute('BEGIN')
161+
this.#transactionConn = this.#transactionConn ?? this.#client.connection()
162+
await this.#transactionConn.execute('BEGIN')
155163
}
156164

157165
async commitTransaction() {
158-
if (!this.#transactionClient) throw new Error('No transaction to commit')
166+
if (!this.#transactionConn) throw new Error('No transaction to commit')
159167
try {
160-
await this.#transactionClient.#conn.execute('COMMIT')
168+
await this.#transactionConn.execute('COMMIT')
161169
} finally {
162-
this.#transactionClient = undefined
170+
this.#transactionConn = undefined
163171
}
164172
}
165173

166174
async rollbackTransaction() {
167-
if (!this.#transactionClient) throw new Error('No transaction to rollback')
175+
if (!this.#transactionConn) throw new Error('No transaction to rollback')
168176
try {
169-
await this.#transactionClient.#conn.execute('ROLLBACK')
177+
await this.#transactionConn.execute('ROLLBACK')
170178
} finally {
171-
this.#transactionClient = undefined
179+
this.#transactionConn = undefined
172180
}
173181
}
174182

0 commit comments

Comments
 (0)