Skip to content

Commit

Permalink
Merge pull request #12 from depot/global-connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie authored Feb 10, 2023
2 parents c93a194 + 9a837ae commit 2272f4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ function inflate(field, value) {
}
```

#### Experimental: `useSharedConnection`

As of version 1.3.0, `PlanetScaleDialect` supports using a shared `@planetscale/database` connection for all non-transaction queries, to improve query performance. This option is not enabled by default, but can be enabled by setting the `useSharedConnection` option to `true`. Transaction queries will always run using their own connection.

This is an experimental feature, and may be removed in a future version.

```typescript
import {Kysely} from 'kysely'
import {PlanetScaleDialect} from 'kysely-planetscale'

const db = new Kysely<Database>({
dialect: new PlanetScaleDialect({
url: process.env.DATABASE_URL,
useSharedConnection: true,
}),
})
```

## License

MIT License, see `LICENSE`.
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ import {
*
* @see https://github.com/planetscale/database-js#usage
*/
export interface PlanetScaleDialectConfig extends Config {}
export interface PlanetScaleDialectConfig extends Config {
/**
* Use a single `@planetscale/database` connection for all non-transaction queries.
*
* @default false
*/
useSharedConnection?: boolean
}

/**
* PlanetScale dialect that uses the [PlanetScale Serverless Driver for JavaScript][0].
Expand Down Expand Up @@ -99,14 +106,19 @@ class PlanetScaleDriver implements Driver {
async destroy(): Promise<void> {}
}

const sharedConnections = new WeakMap<PlanetScaleDialectConfig, Connection>()

class PlanetScaleConnection implements DatabaseConnection {
#config: PlanetScaleDialectConfig
#conn: Connection
#transactionClient?: PlanetScaleConnection

constructor(config: PlanetScaleDialectConfig) {
constructor(config: PlanetScaleDialectConfig, isForTransaction = false) {
this.#config = config
this.#conn = connect({cast: inflateDates, ...config})
const useSharedConnection = config.useSharedConnection && !isForTransaction
const sharedConnection = useSharedConnection ? sharedConnections.get(config) : undefined
this.#conn = sharedConnection ?? connect({cast: inflateDates, ...config})
if (useSharedConnection) sharedConnections.set(config, this.#conn)
}

async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
Expand Down Expand Up @@ -138,7 +150,7 @@ class PlanetScaleConnection implements DatabaseConnection {
}

async beginTransaction() {
this.#transactionClient = this.#transactionClient ?? new PlanetScaleConnection(this.#config)
this.#transactionClient = this.#transactionClient ?? new PlanetScaleConnection(this.#config, true)
await this.#transactionClient.#conn.execute('BEGIN')
}

Expand Down

0 comments on commit 2272f4c

Please sign in to comment.