Skip to content

Commit

Permalink
fix(client): data proxy custom output (#17772)
Browse files Browse the repository at this point in the history
* fix(client): data proxy custom output

* add type and more runtime tests
  • Loading branch information
millsp committed Feb 7, 2023
1 parent 73a2102 commit a0eb65c
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 7 deletions.
11 changes: 5 additions & 6 deletions packages/client/src/generation/generateClient.ts
Expand Up @@ -274,7 +274,6 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo
await copyRuntimeFiles({
from: runtimeSourceDir,
to: copyTarget,
edge: dataProxy,
sourceMaps: copyRuntimeSourceMaps,
runtimeName: getNodeRuntimeName(clientEngineType, dataProxy),
})
Expand Down Expand Up @@ -559,17 +558,17 @@ function getNodeRuntimeName(engineType: ClientEngineType, dataProxy: boolean): s
type CopyRuntimeOptions = {
from: string
to: string
edge: boolean
runtimeName: string
sourceMaps: boolean
}

async function copyRuntimeFiles({ from, to, edge, runtimeName, sourceMaps }: CopyRuntimeOptions) {
async function copyRuntimeFiles({ from, to, runtimeName, sourceMaps }: CopyRuntimeOptions) {
const files = ['index.d.ts']
if (edge) {

files.push(`${runtimeName}.js`, `${runtimeName}.d.ts`)

if (runtimeName === 'data-proxy') {
files.push('edge.js', 'edge-esm.js')
} else {
files.push(`${runtimeName}.js`, `${runtimeName}.d.ts`)
}

if (sourceMaps) {
Expand Down
7 changes: 7 additions & 0 deletions packages/client/tests/e2e/_utils/docker-compose.yml
Expand Up @@ -21,3 +21,10 @@ services:
service: test
environment:
- NAME=community/publish-extensions

runtimes-data-proxy-custom-output:
extends:
file: docker-compose-common.yml
service: test
environment:
- NAME=runtimes/data-proxy-custom-output
3 changes: 2 additions & 1 deletion packages/client/tests/e2e/_utils/standard.cmd.sh
Expand Up @@ -14,5 +14,6 @@
node -r 'esbuild-register' _steps.ts \
) > /$LOGS_FILE.logs.txt 2>&1 ; \
EXIT_CODE=$? && \
mv /$LOGS_FILE.logs.txt /e2e/$NAME/.logs.$EXIT_CODE.txt && \
mv /$LOGS_FILE.logs.txt /e2e/$NAME/.logs.$EXIT_CODE.txt && \ # copy logs and append exit code
cp -r /test/tests/* /e2e/$NAME/tests/ && \ # copy jest files for snapshots
exit $EXIT_CODE
@@ -0,0 +1,20 @@
import { $ } from 'zx'

import { executeSteps } from '../_utils/executeSteps'

void executeSteps({
setup: async () => {
await $`pnpm install`
await $`pnpm exec prisma generate --data-proxy`
},
test: async () => {
await $`pnpm exec prisma -v`
await $`ts-node src/index.ts`
await $`pnpm exec jest`
await $`pnpm exec tsc`
},
finish: async () => {
await $`echo "done"`
},
// keep: true, // keep docker open to debug it
})
@@ -0,0 +1,14 @@
{
"private": true,
"version": "0.0.0",
"main": "index.js",
"scripts": {},
"dependencies": {
"@prisma/client": "../prisma-client-0.0.0.tgz"
},
"devDependencies": {
"@types/node": "16.18.11 ",
"prisma": "../prisma-0.0.0.tgz",
"expect-type": "0.15.0"
}
}
@@ -0,0 +1,36 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "client"
}

datasource db {
provider = "sqlite"
url = "file:./db"
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}

model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
@@ -0,0 +1,3 @@
# Readme

This test makes sure that generating a custom output for the data proxy works.
@@ -0,0 +1,21 @@
import { expectTypeOf } from 'expect-type'

import { PrismaClient } from '../prisma/client'
import { PrismaClient as PrismaClientEdge } from '../prisma/client/edge'

// we only use this to test the types
async function main() {
const prisma = new PrismaClient()
const prismaEdge = new PrismaClientEdge()

const user = await prisma.user.create({
data: { email: 'john@doe.io' },
})

const userEdge = await prismaEdge.user.create({
data: { email: 'john@doe.io' },
})

expectTypeOf(user).toHaveProperty('email').not.toBeAny()
expectTypeOf(userEdge).toHaveProperty('email').not.toBeAny()
}
@@ -0,0 +1,61 @@
import fs from 'fs/promises'
import path from 'path'

import { PrismaClient } from '../prisma/client'
import { PrismaClient as PrismaClientEdge } from '../prisma/client/edge'

test('assert node data proxy runtime can be used', async () => {
try {
const prisma = new PrismaClient()

const data = await prisma.user.create({
data: { email: 'jane@doe.io' },
})
} catch (e) {
expect(e.message).toMatchInlineSnapshot(`"Datasource URL must use prisma:// protocol when --data-proxy is used"`)
}
})

test('assert node data proxy index requires the right file', async () => {
const data = await fs.readFile(path.join(__dirname, '..', 'prisma', 'client', 'index.js'))

expect(data.includes('./runtime/data-proxy')).toBe(true)
})

test('assert edge data proxy runtime can be used', async () => {
try {
const prisma = new PrismaClientEdge()

const data = await prisma.user.create({
data: { email: 'jane@doe.io' },
})
} catch (e) {
expect(e.message).toMatchInlineSnapshot(`"Datasource URL must use prisma:// protocol when --data-proxy is used"`)
}
})

test('assert edge data proxy index requires the right file', async () => {
const data = await fs.readFile(path.join(__dirname, '..', 'prisma', 'client', 'edge.js'))

expect(data.includes('./runtime/edge')).toBe(true)
})

test('runtime files exists', async () => {
const files = await fs.readdir(path.join(__dirname, '..', 'prisma', 'client', 'runtime'))

console.log(files)

expect(files).toMatchInlineSnapshot(`
[
"data-proxy.d.ts",
"data-proxy.js",
"edge-esm.js",
"edge.js",
"index.d.ts",
"library.d.ts",
"library.js",
]
`)
})

export {}
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/*"]
}

0 comments on commit a0eb65c

Please sign in to comment.