Skip to content

Commit

Permalink
fix error with custom schema location not found
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Jun 1, 2022
1 parent 3206067 commit ca0c722
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
9 changes: 4 additions & 5 deletions packages/client/src/generation/TSClient/TSClient.ts
Expand Up @@ -39,7 +39,7 @@ export interface TSClientOptions {
generator?: GeneratorConfig
platforms?: Platform[] // TODO: consider making it non-nullable
sqliteDatasourceOverrides?: DatasourceOverwrite[]
schemaDir: string
schemaPath: string
outputDir: string
activeProvider: string
dataProxy?: boolean
Expand All @@ -59,13 +59,12 @@ export class TSClient implements Generatable {
generator,
sqliteDatasourceOverrides,
outputDir,
schemaDir,
schemaPath,
runtimeDir,
runtimeName,
datasources,
dataProxy,
} = this.options
const schemaPath = path.join(schemaDir, 'schema.prisma')
const envPaths = getEnvPaths(schemaPath, { cwd: outputDir })

const relativeEnvPaths = {
Expand All @@ -83,7 +82,7 @@ export class TSClient implements Generatable {
generator,
relativeEnvPaths,
sqliteDatasourceOverrides,
relativePath: path.relative(outputDir, schemaDir),
relativePath: path.relative(outputDir, path.dirname(schemaPath)),
clientVersion: this.options.clientVersion,
engineVersion: this.options.engineVersion,
datasourceNames: datasources.map((d) => d.name),
Expand Down Expand Up @@ -142,7 +141,7 @@ ${buildNFTAnnotations(dataProxy, engineType, platforms, relativeOutdir)}
this.options.browser,
this.options.generator,
this.options.sqliteDatasourceOverrides,
this.options.schemaDir,
path.dirname(this.options.schemaPath),
)

const collector = new ExportCollector()
Expand Down
25 changes: 11 additions & 14 deletions packages/client/src/generation/generateClient.ts
Expand Up @@ -33,8 +33,7 @@ export class DenylistError extends Error {
export interface GenerateClientOptions {
projectRoot?: string
datamodel: string
datamodelPath: string
schemaDir?: string
schemaPath: string
transpile?: boolean
runtimeDirs?: { node: string; edge: string }
outputDir: string
Expand All @@ -57,7 +56,7 @@ export interface BuildClientResult {

// eslint-disable-next-line @typescript-eslint/require-await
export async function buildClient({
schemaDir,
schemaPath,
runtimeDirs,
binaryPaths,
outputDir,
Expand All @@ -69,7 +68,7 @@ export async function buildClient({
projectRoot,
activeProvider,
dataProxy,
}: O.Required<GenerateClientOptions, 'schemaDir' | 'runtimeDirs'>): Promise<BuildClientResult> {
}: O.Required<GenerateClientOptions, 'runtimeDirs'>): Promise<BuildClientResult> {
// we define the basic options for the client generation
const document = getPrismaClientDMMF(dmmf)
const clientEngineType = getClientEngineType(generator!)
Expand All @@ -81,7 +80,7 @@ export async function buildClient({
clientEngineType === ClientEngineType.Library
? (Object.keys(binaryPaths.libqueryEngine ?? {}) as Platform[])
: (Object.keys(binaryPaths.queryEngine ?? {}) as Platform[]),
schemaDir,
schemaPath,
outputDir,
clientVersion,
engineVersion,
Expand Down Expand Up @@ -171,8 +170,7 @@ async function getDefaultOutdir(outputDir: string): Promise<string> {
export async function generateClient(options: GenerateClientOptions): Promise<void> {
const {
datamodel,
datamodelPath,
schemaDir = datamodelPath ? path.dirname(datamodelPath) : process.cwd(),
schemaPath,
outputDir,
transpile,
generator,
Expand All @@ -192,8 +190,7 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo

const { prismaClientDmmf, fileMap } = await buildClient({
datamodel,
datamodelPath,
schemaDir,
schemaPath,
transpile,
runtimeDirs,
outputDir: finalOutputDir,
Expand All @@ -213,7 +210,7 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo
if (denylistsErrors) {
let message = `${chalk.redBright.bold(
'Error: ',
)}The schema at "${datamodelPath}" contains reserved keywords.\n Rename the following items:`
)}The schema at "${schemaPath}" contains reserved keywords.\n Rename the following items:`

for (const error of denylistsErrors) {
message += '\n - ' + error.message
Expand Down Expand Up @@ -316,9 +313,9 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo
}
}

const datamodelTargetPath = path.join(finalOutputDir, 'schema.prisma')
if (datamodelPath !== datamodelTargetPath) {
await copyFile(datamodelPath, datamodelTargetPath)
const schemaTargetPath = path.join(finalOutputDir, 'schema.prisma')
if (schemaPath !== schemaTargetPath) {
await copyFile(schemaPath, schemaTargetPath)
}

const proxyIndexJsPath = path.join(outputDir, 'index.js')
Expand Down Expand Up @@ -436,7 +433,7 @@ async function getGenerationDirs({ testMode, runtimeDirs, generator, outputDir }
const useDefaultOutdir = testMode ? !runtimeDirs : !generator?.isCustomOutput

const _runtimeDirs = {
// if we have an override we use it, but if not then use the defaults
// if we have an override, we use it, but if not then use the defaults
node: runtimeDirs?.node || (useDefaultOutdir ? '@prisma/client/runtime' : './runtime'),
edge: runtimeDirs?.edge || (useDefaultOutdir ? '@prisma/client/runtime' : '../runtime'),
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/generation/generator.ts
Expand Up @@ -40,7 +40,7 @@ if (process.argv[1] === __filename) {

return generateClient({
datamodel: options.datamodel,
datamodelPath: options.schemaPath,
schemaPath: options.schemaPath,
binaryPaths: options.binaryPaths!,
datasources: options.datasources,
outputDir,
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/utils/generateInFolder.ts
Expand Up @@ -133,11 +133,10 @@ export async function generateInFolder({
dmmf,
...config,
outputDir,
schemaDir: path.dirname(schemaPath),
runtimeDirs,
transpile,
testMode: true,
datamodelPath: schemaPath,
schemaPath,
copyRuntime: false,
generator: config.generators[0],
clientVersion: 'local',
Expand Down
Expand Up @@ -35,7 +35,7 @@ export async function setupTestSuiteClient(suiteMeta: TestSuiteMeta, suiteConfig

await generateClient({
datamodel: schema,
datamodelPath: getTestSuiteSchemaPath(suiteMeta, suiteConfig),
schemaPath: getTestSuiteSchemaPath(suiteMeta, suiteConfig),
binaryPaths: { libqueryEngine: {}, queryEngine: {} },
datasources: config.datasources,
outputDir: path.join(suiteFolderPath, 'node_modules/@prisma/client'),
Expand Down

0 comments on commit ca0c722

Please sign in to comment.