Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(client): [DPGA] implement --data-proxy #13484

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
62a09ae
fix(lint): failing on sub-projects and simplify it
millsp May 24, 2022
b332e65
chore(deps): bump typescript
millsp May 24, 2022
245e81f
chore: update locks
millsp May 24, 2022
e2db143
feat(client, prisma): enable the data proxy via --data-proxy
millsp May 25, 2022
a4dacab
skip engine copying when --data-proxy is passed
millsp May 25, 2022
4093ab3
finish fix webworker type leakage
millsp May 25, 2022
c66bda1
pass dataProxy boolean in missing places
millsp May 25, 2022
4ace46f
Merge branch 'main' of github.com:prisma/prisma into chore/data-proxy-ga
millsp May 25, 2022
47f2002
fix sdk compiling test types
millsp May 25, 2022
3954378
chore(build): fix watch on delete file
millsp May 26, 2022
b8b50e5
fix missing dataProxy parameter option
millsp May 26, 2022
355dc27
remove unnecessary tsconfig.json
millsp May 26, 2022
7be0448
fix serializing array buffer view
millsp May 26, 2022
0376135
fix generateInFolder missing dataProxy option
millsp May 26, 2022
de80079
remove last instances of data proxy engine type
millsp May 26, 2022
d575b57
remove data proxy engine type in tests
millsp May 26, 2022
3c393c7
fix missing dataproxy arguments
millsp May 26, 2022
bf62a31
update snaps
millsp May 26, 2022
77a6e71
make linting more performant
millsp May 26, 2022
5fa27f8
attempt to fix linting and type performance
millsp May 26, 2022
c67fb85
undo linting changes & ts 4.7
millsp May 26, 2022
4b0a0b8
revert linting related changes
millsp May 27, 2022
5b54c76
Merge branch 'main' of github.com:prisma/prisma into chore/data-proxy-ga
millsp May 27, 2022
7d9db1c
fix types on inlineDatasources
millsp May 27, 2022
78240b0
bump typescript
millsp May 27, 2022
01d2823
update linting
millsp May 27, 2022
f530901
Merge branch 'chore/ts-4.7' into chore/data-proxy-ga
millsp May 27, 2022
ae6f4cc
fix types
millsp May 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions helpers/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const watch =
}, 10)

// triggers a full rebuild on added file
const onAdd = debounce(async () => {
const onAddOrUnlink = debounce(async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change to restart esbuild compiler on file deletion.

void changeWatcher.close() // stop all

// only one watcher will do this task
Expand All @@ -194,7 +194,8 @@ const watch =
}, 10)

changeWatcher.on('change', onChange)
restartWatcher.once('add', onAdd)
restartWatcher.once('add', onAddOrUnlink)
restartWatcher.once('unlink', onAddOrUnlink)

return undefined
}
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/src/Generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getCommandWithExecutor,
getGenerators,
getGeneratorSuccessMessage,
getPlatform,
HelpError,
highlightTS,
isError,
Expand All @@ -18,7 +17,6 @@ import {
logger,
missingGeneratorMessage,
parseEnvValue,
Platform,
} from '@prisma/sdk'
import chalk from 'chalk'
import fs from 'fs'
Expand Down Expand Up @@ -49,9 +47,10 @@ ${chalk.bold('Usage')}

${chalk.bold('Options')}

-h, --help Display this help message
--schema Custom path to your Prisma schema
--watch Watch the Prisma schema and rerun after a change
-h, --help Display this help message
--schema Custom path to your Prisma schema
--data-proxy Enable the Data Proxy in the Prisma Client
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New way to enable --data-proxy. There will be a followup later to also enable this for prisma studio.

--watch Watch the Prisma schema and rerun after a change

${chalk.bold('Examples')}

Expand Down Expand Up @@ -102,6 +101,7 @@ ${chalk.bold('Examples')}
'-h': '--help',
'--watch': Boolean,
'--schema': String,
'--data-proxy': Boolean,
// Only used for checkpoint information
'--postinstall': String,
'--telemetry-information': String,
Expand Down Expand Up @@ -137,6 +137,7 @@ ${chalk.bold('Examples')}
printDownloadProgress: !watchMode,
version: enginesVersion,
cliVersion: pkg.version,
dataProxy: !!args['--data-proxy'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the info down to the generators.

})

if (!generators || generators.length === 0) {
Expand Down Expand Up @@ -259,6 +260,7 @@ Please run \`${getCommandWithExecutor('prisma generate')}\` to see the errors.`)
printDownloadProgress: !watchMode,
version: enginesVersion,
cliVersion: pkg.version,
dataProxy: !!args['--data-proxy'],
})

if (!generatorsWatch || generatorsWatch.length === 0) {
Expand Down
1 change: 0 additions & 1 deletion packages/client/fixtures/dataproxy/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ datasource db {

generator client {
provider = "prisma-client-js"
engineType = "dataproxy"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a valid entry anymore.

}

model Post {
Expand Down
14 changes: 8 additions & 6 deletions packages/client/src/__tests__/buildNFTAnnotations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function normalizePaths(snapshot: string): string {

describe('library', () => {
it('generates annotations for a schema and a single engine', () => {
const annotations = buildNFTAnnotations(ClientEngineType.Library, ['debian-openssl-1.1.x'], 'out')
const annotations = buildNFTAnnotations(false, ClientEngineType.Library, ['debian-openssl-1.1.x'], 'out')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataProxy = false


expect(normalizePaths(annotations)).toMatchInlineSnapshot(`

Expand All @@ -24,6 +24,7 @@ describe('library', () => {

it('generates annotations for a schema and multiple engines', () => {
const annotations = buildNFTAnnotations(
false,
ClientEngineType.Library,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
Expand All @@ -47,7 +48,7 @@ describe('library', () => {

describe('binary', () => {
it('generates annotations for a schema and a single engine', () => {
const annotations = buildNFTAnnotations(ClientEngineType.Binary, ['debian-openssl-1.1.x'], 'out')
const annotations = buildNFTAnnotations(false, ClientEngineType.Binary, ['debian-openssl-1.1.x'], 'out')

expect(normalizePaths(annotations)).toMatchInlineSnapshot(`

Expand All @@ -60,6 +61,7 @@ describe('binary', () => {

it('generates annotations for a schema and multiple engines', () => {
const annotations = buildNFTAnnotations(
false,
ClientEngineType.Binary,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
Expand All @@ -84,16 +86,15 @@ describe('binary', () => {
describe('dataproxy', () => {
it('generates no annotations', () => {
const annotations = buildNFTAnnotations(
ClientEngineType.DataProxy,
true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataProxy = true

ClientEngineType.Library,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
)

// TODO: when using .toMatchInlineSnapshot(), this fails after updating snapshots.
// Probably an issue with the snapshot serializer?
expect(normalizePaths(annotations)).toBe(`

`)
expect(normalizePaths(annotations)).toBe('')
})
})

Expand All @@ -106,6 +107,7 @@ describe('special cases', () => {
process.env.NETLIFY = 'true'

const annotations = buildNFTAnnotations(
false,
ClientEngineType.Library,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
Expand Down
4 changes: 4 additions & 0 deletions packages/client/src/__tests__/generation/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('generator', () => {
baseDir: __dirname,
printDownloadProgress: false,
skipDownload: true,
dataProxy: false,
})

const manifest = omit<any, any>(generator.manifest, ['version']) as any
Expand Down Expand Up @@ -114,6 +115,7 @@ describe('generator', () => {
baseDir: __dirname,
printDownloadProgress: false,
skipDownload: true,
dataProxy: false,
})
} catch (e) {
expect(stripAnsi(e.message)).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -163,6 +165,7 @@ describe('generator', () => {
baseDir: __dirname,
printDownloadProgress: false,
skipDownload: true,
dataProxy: false,
})
} catch (e) {
doesnNotExistError = e
Expand Down Expand Up @@ -192,6 +195,7 @@ describe('generator', () => {
baseDir: __dirname,
printDownloadProgress: false,
skipDownload: true,
dataProxy: false,
})

const manifest = omit<any, any>(generator.manifest, ['version']) as any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import path from 'path'

import { generateTestClient } from '../../../../utils/getTestClient'

if (getClientEngineType() === ClientEngineType.DataProxy) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

// eslint-disable-next-line no-global-assign
test = test.skip
}

test('custom engine binary path (internal API)', async () => {
await generateTestClient()

Expand Down
19 changes: 11 additions & 8 deletions packages/client/src/generation/TSClient/TSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface TSClientOptions {
schemaDir: string
outputDir: string
activeProvider: string
dataProxy: boolean
}

export class TSClient implements Generatable {
Expand All @@ -62,6 +63,7 @@ export class TSClient implements Generatable {
runtimeDir,
runtimeName,
datasources,
dataProxy,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed by the generator.

} = this.options
const schemaPath = path.join(schemaDir, 'schema.prisma')
const envPaths = getEnvPaths(schemaPath, { cwd: outputDir })
Expand All @@ -86,15 +88,16 @@ export class TSClient implements Generatable {
engineVersion: this.options.engineVersion,
datasourceNames: datasources.map((d) => d.name),
activeProvider: this.options.activeProvider,
dataProxy: this.options.dataProxy,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataProxy is now stored in the generated config instead of being in the generator config block (via schema).

}

// get relative output dir for it to be preserved even after bundling, or
// being moved around as long as we keep the same project dir structure.
const relativeOutdir = path.relative(process.cwd(), outputDir)

const code = `${commonCodeJS({ ...this.options, browser: false })}
${buildRequirePath(engineType)}
${buildDirname(engineType, relativeOutdir, runtimeDir)}
${buildRequirePath(dataProxy)}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the engine type in most cases anymore as that was used to do the data proxy distinction at generation time.

${buildDirname(dataProxy, relativeOutdir, runtimeDir)}
/**
* Enums
*/
Expand All @@ -112,22 +115,22 @@ ${new Enum(
},
true,
).toJS()}
${buildDMMF(engineType, this.dmmfString)}
${buildDMMF(dataProxy, this.dmmfString)}

/**
* Create the Client
*/
const config = ${JSON.stringify(config, null, 2)}
config.document = dmmf
config.dirname = dirname
${buildInlineDatasource(engineType, datasources)}
${await buildInlineSchema(engineType, schemaPath)}
${buildInlineEnv(engineType, datasources, envPaths)}
${buildWarnEnvConflicts(engineType, runtimeDir, runtimeName)}
${buildInlineDatasource(dataProxy, datasources)}
${await buildInlineSchema(dataProxy, schemaPath)}
${buildInlineEnv(dataProxy, datasources, envPaths)}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be removed in the followup PR.

${buildWarnEnvConflicts(dataProxy, runtimeDir, runtimeName)}
const PrismaClient = getPrismaClient(config)
exports.PrismaClient = PrismaClient
Object.assign(exports, Prisma)
${buildNFTAnnotations(engineType, platforms, relativeOutdir)}
${buildNFTAnnotations(dataProxy, engineType, platforms, relativeOutdir)}
`
return code
}
Expand Down
13 changes: 9 additions & 4 deletions packages/client/src/generation/generateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface GenerateClientOptions {
engineVersion: string
clientVersion: string
activeProvider: string
dataProxy: boolean
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed to generateClient via the generator.

}

export interface BuildClientResult {
Expand All @@ -57,7 +58,6 @@ export interface BuildClientResult {

// eslint-disable-next-line @typescript-eslint/require-await
export async function buildClient({
datamodel,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used.

schemaDir = process.cwd(),
runtimeDir = '@prisma/client/runtime',
runtimeName = 'index',
Expand All @@ -70,6 +70,7 @@ export async function buildClient({
clientVersion,
projectRoot,
activeProvider,
dataProxy,
}: GenerateClientOptions): Promise<BuildClientResult> {
const document = getPrismaClientDMMF(dmmf)
const clientEngineType = getClientEngineType(generator!)
Expand All @@ -90,6 +91,7 @@ export async function buildClient({
engineVersion,
projectRoot: projectRoot!,
activeProvider,
dataProxy,
})

const fileMap = {
Expand Down Expand Up @@ -146,14 +148,15 @@ export async function generateClient({
clientVersion,
engineVersion,
activeProvider,
dataProxy,
}: GenerateClientOptions): Promise<void> {
const useDotPrisma = testMode ? !runtimeDir : !generator?.isCustomOutput
const clientEngineType = getClientEngineType(generator!)
runtimeDir = runtimeDir || (useDotPrisma ? '@prisma/client/runtime' : './runtime')

// we make sure that we point to the right engine build
if (clientEngineType === ClientEngineType.DataProxy) {
runtimeName = 'proxy'
if (dataProxy === true) {
runtimeName = 'proxy' // TODO: decouple the runtimes
}

const finalOutputDir = useDotPrisma ? await getDotPrismaDir(outputDir) : outputDir
Expand All @@ -177,6 +180,7 @@ export async function generateClient({
engineVersion,
projectRoot,
activeProvider,
dataProxy,
})

const denylistsErrors = validateDmmfAgainstDenylists(prismaClientDmmf)
Expand Down Expand Up @@ -238,7 +242,8 @@ export async function generateClient({
}\` in the \`binaryPaths\` object.`,
)
}
if (transpile) {

if (transpile === true && dataProxy === false) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't copy the query engine files if the data proxy is in use.

if (process.env.NETLIFY) {
await makeDir('/tmp/prisma-engines')
}
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/generation/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if (process.argv[1] === __filename) {
clientVersion,
transpile: true,
activeProvider: options.datasources[0]?.activeProvider,
dataProxy: options.dataProxy,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed by the cli.

})
},
})
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/generation/utils/buildDMMF.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ClientEngineType } from '@prisma/sdk'
import lzString from 'lz-string'

/**
Expand All @@ -8,8 +7,8 @@ import lzString from 'lz-string'
* @param dmmf
* @returns
*/
export function buildDMMF(engineType: ClientEngineType, dmmf: string) {
if (engineType === ClientEngineType.DataProxy) {
export function buildDMMF(dataProxy: boolean, dmmf: string) {
if (dataProxy === true) {
return buildCompressedDMMF(dmmf)
}

Expand Down
7 changes: 3 additions & 4 deletions packages/client/src/generation/utils/buildDirname.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ClientEngineType } from '@prisma/sdk'
import path from 'path'

/**
* Builds a `dirname` variable that holds the location of the generated client.
* @param clientEngineType
* @param dataProxy
* @param relativeOutdir
* @param runtimeDir
* @returns
*/
export function buildDirname(clientEngineType: ClientEngineType, relativeOutdir: string, runtimeDir: string) {
if (clientEngineType !== ClientEngineType.DataProxy) {
export function buildDirname(dataProxy: boolean, relativeOutdir: string, runtimeDir: string) {
if (dataProxy === false) {
return buildDirnameFind(relativeOutdir, runtimeDir)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ClientEngineType } from '@prisma/sdk'

import type { InternalDatasource } from '../../runtime/utils/printDatasources'

// that is all we need for the data proxy
Expand All @@ -16,8 +14,8 @@ export type InlineDatasources = {
* @param internalDatasources
* @returns
*/
export function buildInlineDatasource(engineType: ClientEngineType, internalDatasources: InternalDatasource[]) {
if (engineType === ClientEngineType.DataProxy) {
export function buildInlineDatasource(dataProxy: boolean, internalDatasources: InternalDatasource[]) {
if (dataProxy === true) {
const datasources = internalToInlineDatasources(internalDatasources)

return `
Expand Down