|
1 |
| -import {cast, Config, connect, Connection, Field} from '@planetscale/database' |
| 1 | +import {Client, Config, Connection, Field, cast} from '@planetscale/database' |
2 | 2 | import {parseJSON} from 'date-fns'
|
3 | 3 | import {
|
4 | 4 | CompiledQuery,
|
@@ -71,7 +71,7 @@ export class PlanetScaleDialect implements Dialect {
|
71 | 71 | return new MysqlQueryCompiler()
|
72 | 72 | }
|
73 | 73 |
|
74 |
| - createIntrospector(db: Kysely<any>): DatabaseIntrospector { |
| 74 | + createIntrospector(db: Kysely<unknown>): DatabaseIntrospector { |
75 | 75 | return new MysqlIntrospector(db)
|
76 | 76 | }
|
77 | 77 | }
|
@@ -110,15 +110,24 @@ const sharedConnections = new WeakMap<PlanetScaleDialectConfig, Connection>()
|
110 | 110 |
|
111 | 111 | class PlanetScaleConnection implements DatabaseConnection {
|
112 | 112 | #config: PlanetScaleDialectConfig
|
113 |
| - #conn: Connection |
| 113 | + #client: Client |
114 | 114 | #transactionClient?: PlanetScaleConnection
|
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() |
| 120 | + } |
| 121 | + |
| 122 | + get #useSharedConnection(): boolean { |
| 123 | + return Boolean(this.#config.useSharedConnection && !this.#transactionClient) |
| 124 | + } |
| 125 | + |
116 | 126 | constructor(config: PlanetScaleDialectConfig, isForTransaction = false) {
|
117 | 127 | this.#config = config
|
118 | 128 | 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()) |
122 | 131 | }
|
123 | 132 |
|
124 | 133 | async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
|
|
0 commit comments