Skip to content

Commit

Permalink
Merge pull request #6 from prisma/feat/no-datasource
Browse files Browse the repository at this point in the history
feat: Work without datasource name
  • Loading branch information
SevInf authored Aug 21, 2023
2 parents ce7c251 + 0a1100f commit 07029b9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 40 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This [Prisma Client Extension](https://www.prisma.io/docs/concepts/components/pr

## Requirements

Works best with Prisma 5.1+. Can work with earlier versions (4.16.2+) if [no result extensions](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/result) are used at the same time.
Requires Prisma 5.2+.

## Installation

Expand Down Expand Up @@ -38,13 +38,11 @@ import { readReplicas } from '@prisma/extension-read-replicas

const prisma = new PrismaClient().$extends(
readReplicas({
db: 'postgresql://replica.example.com:5432/db',
url: 'postgresql://replica.example.com:5432/db',
}),
)
```

Where `db` is the name of your datasource (`datasource` block in the schema file).

All non-transactional read queries will now be executed against the defined replica.
Write queries and transactions will be executed against the primary server.

Expand All @@ -55,7 +53,7 @@ You can also initialize the extension with an array of replica connection string
```ts
const prisma = new PrismaClient().$extends(
readReplicas({
db: [
url: [
'postgresql://replica-1.example.com:5432/db',
'postgresql://replica-2.example.com:5432/db',
],
Expand Down
2 changes: 1 addition & 1 deletion demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client'
import { readReplicas } from '..'

const client = new PrismaClient().$extends(readReplicas({ db: process.env.REPLICA_URL! }))
const client = new PrismaClient().$extends(readReplicas({ url: process.env.REPLICA_URL! }))

async function main() {
await client.user.deleteMany()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@prisma/client": "5.1.0-integration-fix-result-query-ext.1",
"@prisma/client": "5.2.0-dev.72",
"@swc/core": "^1.3.73",
"@swc/jest": "^0.2.27",
"@types/debug": "^4.1.8",
Expand All @@ -57,7 +57,7 @@
"jest": "^29.6.2",
"lint-staged": "^13.2.3",
"prettier": "^3.0.0",
"prisma": "5.1.0-integration-fix-result-query-ext.1",
"prisma": "5.2.0-dev.72",
"typescript": "^5.1.6"
},
"dependencies": {
Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions src/ReplicaManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client/extension'

type PrismaConstructorOptions = {
datasources?: Record<string, { url: string }>
datasourceUrl?: string
}

export type ConfigureReplicaCallback = (client: PrismaClient) => PrismaClient
Expand All @@ -12,21 +12,16 @@ interface PrismaClientConstructor {
type ReplicaManagerOptions = {
clientConstructor: PrismaClientConstructor
replicaUrls: string[]
datasourceName: string
configureCallback: ConfigureReplicaCallback | undefined
}

export class ReplicaManager {
private _replicaClients: PrismaClient[]

constructor({ replicaUrls, datasourceName, clientConstructor, configureCallback }: ReplicaManagerOptions) {
this._replicaClients = replicaUrls.map((url) => {
constructor({ replicaUrls, clientConstructor, configureCallback }: ReplicaManagerOptions) {
this._replicaClients = replicaUrls.map((datasourceUrl) => {
const client = new clientConstructor({
datasources: {
[datasourceName]: {
url,
},
},
datasourceUrl,
})

if (configureCallback) {
Expand Down
13 changes: 6 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Prisma } from '@prisma/client/extension'
import { ConfigureReplicaCallback, ReplicaManager } from './ReplicaManager'

export type ReplicasOptions = {
[datasource: string]: string | string[]
url: string | string[]
}

const debug = createDebug('prisma:replicasExtension')
Expand All @@ -31,17 +31,16 @@ export const readReplicas = (options: ReplicasOptions, configureReplicaClient?:
if (!datasourceName) {
throw new Error(`Read replicas options must specify a datasource`)
}
let urls = options[datasourceName]
if (typeof urls === 'string') {
urls = [urls]
} else if (!Array.isArray(urls)) {
let replicaUrls = options.url
if (typeof replicaUrls === 'string') {
replicaUrls = [replicaUrls]
} else if (!Array.isArray(replicaUrls)) {
throw new Error(`Replica URLs must be a string or list of strings`)
}

const replicaManager = new ReplicaManager({
replicaUrls: urls,
replicaUrls,
clientConstructor: PrismaClient,
datasourceName,
configureCallback: configureReplicaClient,
})

Expand Down
2 changes: 1 addition & 1 deletion tests/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function createPrisma() {
.$extends(
readReplicas(
{
db: process.env.REPLICA_URL!,
url: process.env.REPLICA_URL!,
},
(client) =>
(client as PrismaClient).$extends({
Expand Down

0 comments on commit 07029b9

Please sign in to comment.