-
Notifications
You must be signed in to change notification settings - Fork 45
/
prisma.ts
57 lines (48 loc) · 1.82 KB
/
prisma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { DMMF } from '@prisma/client/runtime'
import dedent from 'dindist'
import ono from 'ono'
import { detectProjectPackageManager, renderRunBin } from '../lib/packageManager'
import { d } from './debugNexusPrisma'
import { GITHUB_NEW_DISCUSSION_LINK } from './errorMessages'
import kleur = require('kleur')
export const getPrismaClientDmmf = (importId = '@prisma/client'): DMMF.Document => {
d('get dmmf from @prisma/client')
let maybeDmmf: DMMF.Document | undefined
try {
// We duck type check below
// eslint-disable-next-line
maybeDmmf = require(importId).dmmf
} catch (error) {
// prettier-ignore
throw ono(error, dedent`
Failed to get Prisma Client DMMF. An error occured while trying to import it from ${kleur.yellow(importId)}.
`)
}
if (maybeDmmf === undefined) {
// prettier-ignore
throw new Error(dedent`
Failed to get Prisma Client DMMF. It was imported from ${kleur.yellow(importId)} but was \`undefined\`.
This usually means that you need to run Prisma Client generation. Please run ${renderRunBin(detectProjectPackageManager(), `prisma generate`)}.
If that does not solve your problem, you can get community help by opening a discussion at ${kleur.yellow(GITHUB_NEW_DISCUSSION_LINK)}.
`)
}
/** Simple duck type to sanity check we got good data at runtime. */
const dmmf = maybeDmmf
const expectedFields = ['datamodel', 'schema', 'mappings'] as const
if (expectedFields.find((fieldName) => dmmf[fieldName] && typeof dmmf[fieldName] !== 'object')) {
throw new Error(dedent`
The DMMF imported from ${importId} appears to be invalid. Missing one/all of expected fields:
`)
}
return maybeDmmf
}
export type PrismaScalarType =
| 'String'
| 'Boolean'
| 'Int'
| 'BigInt'
| 'Float'
| 'Decimal'
| 'DateTime'
| 'Json'
| 'Bytes'