Skip to content

Commit

Permalink
feat(settings): allow customization of prisma client import (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Kuhrt <jasonkuhrt@me.com>
  • Loading branch information
nvitaterna and jasonkuhrt authored May 21, 2021
1 parent 2447f56 commit c8b5f6c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/cli/nexus-prisma.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

process.env.DEBUG_COLORS = 'true'
process.env.DEBUG_HIDE_DATE = 'true'

import { generatorHandler } from '@prisma/generator-helper'
import * as Path from 'path'
import { generateRuntimeAndEmit } from '../generator'
Expand All @@ -26,7 +25,17 @@ generatorHandler({
},
// async required by interface
// eslint-disable-next-line
async onGenerate({ dmmf }) {
async onGenerate({ dmmf, otherGenerators }) {
const prismaClientGenerator = otherGenerators.find((g) => g.provider.value === 'prisma-client-js')

// WARNING: Make sure this logic comes before `loadUserGentimeSettings` below
// otherwise we will overwrite the user's choice for this setting if they have set it.
if (prismaClientGenerator?.output?.value) {
Gentime.settings.change({
prismaClientImportId: prismaClientGenerator.output.value,
})
}

const internalDMMF = externalToInternalDmmf(dmmf)
loadUserGentimeSettings()
generateRuntimeAndEmit(internalDMMF, Gentime.settings)
Expand Down
21 changes: 21 additions & 0 deletions src/generator/gentime/settingsSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ export namespace Gentime {
*/
GraphQLDocs?: boolean
}
/**
* Where Nexus Prisma will try to import your generated Prisma Client from.
*
* You should not need to configure this normally because Nexus Prisma generator automatically reads the
* Prisma Client generator `output` setting if you have set it.
*
* The value here will be used in a dynamic import thus following Node's path resolution rules. You can
* pass a node_modules package like `foo` `@prisma/client`
* `@my/custom/thing` etc. or you can pass an absolute module/file path `/my/custom/thing` /
* `/my/custom/thing/index.js` or finally a relative path to be resolved relative to the location of Nexus
* Prisma source files (you probably don't want this).
*
* @remarks Nexus Prisma imports Prisma client internally for two reasons: 1) validation wherein a
* class reference to Prisma Client is needed for some `instanceof` checks and 2) for
* acquiring the DMMF as Nexus Prisma relies on some post-processing done by Prisma Client
* generator.
*/
prismaClientImportId?: string | null
}

export type SettingsData = Setset.InferDataFromInput<SettingsInput>
Expand All @@ -46,6 +64,9 @@ export namespace Gentime {
},
},
},
prismaClientImportId: {
initial: () => `@prisma/client`,
},
},
})

Expand Down
2 changes: 1 addition & 1 deletion src/generator/models/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createModuleSpec(gentimeSettings: Gentime.Settings): ModuleSpec
const gentimeSettings = ${JSON.stringify(gentimeSettings.data, null, 2)}
const dmmf = getPrismaClientDmmf()
const dmmf = getPrismaClientDmmf(gentimeSettings.prismaClientImportId)
const models = ModelsGenerator.JS.createNexusTypeDefConfigurations(dmmf, {
runtime: Runtime.settings,
Expand Down

0 comments on commit c8b5f6c

Please sign in to comment.