-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
helpers.ts
109 lines (103 loc) · 3.13 KB
/
helpers.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import pluralize from 'pluralize'
import { DMMF } from '../../runtime/dmmf-types'
import { capitalize, lowerCase } from '../../runtime/utils/common'
import { getAggregateArgsName, getModelArgName, unique } from '../utils'
import { JSDocMethodBodyCtx, JSDocs } from './jsdoc'
export function getMethodJSDocBody(
action: DMMF.ModelAction,
mapping: DMMF.ModelMapping,
model: DMMF.Model,
): string {
const ctx: JSDocMethodBodyCtx = {
singular: capitalize(mapping.model),
plural: capitalize(mapping.plural),
firstScalar: model.fields.find((f) => f.kind === 'scalar'),
method: `prisma.${lowerCase(mapping.model)}.${action}`,
action,
mapping,
model,
}
const jsdoc = JSDocs[action]?.body(ctx)
return jsdoc ? jsdoc : ''
}
export function getMethodJSDoc(
action: DMMF.ModelAction,
mapping: DMMF.ModelMapping,
model: DMMF.Model,
): string {
return wrapComment(getMethodJSDocBody(action, mapping, model))
}
export function getGenericMethod(name: string, actionName: DMMF.ModelAction) {
if (actionName === 'count') {
return ''
}
if (actionName === 'aggregate') {
return `<T extends ${getAggregateArgsName(name)}>`
}
if (actionName === 'findFirst' || actionName === 'findUnique') {
return `<T extends ${getModelArgName(
name,
actionName,
)}, LocalRejectSettings = T["rejectOnNotFound"] extends RejectOnNotFound ? T['rejectOnNotFound'] : undefined>`
}
const modelArgName = getModelArgName(name, actionName)
if (!modelArgName) {
console.log({ name, actionName })
}
return `<T extends ${modelArgName}>`
}
export function getArgs(name: string, actionName: DMMF.ModelAction) {
if (actionName === 'count') {
return `args?: Omit<${getModelArgName(
name,
DMMF.ModelAction.findMany,
)}, 'select' | 'include'>`
}
if (actionName === 'aggregate') {
return `args: Subset<T, ${getAggregateArgsName(name)}>`
}
return `args${
actionName === DMMF.ModelAction.findMany ||
actionName === DMMF.ModelAction.findFirst ||
actionName === DMMF.ModelAction.deleteMany ||
actionName === DMMF.ModelAction.createMany
? '?'
: ''
}: SelectSubset<T, ${getModelArgName(name, actionName)}>`
}
export function wrapComment(str: string): string {
return `/**\n${str
.split('\n')
.map((l) => ' * ' + l)
.join('\n')}\n**/`
}
export function getArgFieldJSDoc(
type?: DMMF.OutputType,
action?: DMMF.ModelAction,
field?: DMMF.SchemaArg | string,
): string | undefined {
if (!field || !action || !type) return
const fieldName = typeof field === 'string' ? field : field.name
if (JSDocs[action] && JSDocs[action]?.fields[fieldName]) {
const singular = type.name
const plural = pluralize(type.name)
const comment = JSDocs[action]?.fields[fieldName](singular, plural)
return comment as string
}
return undefined
}
export function escapeJson(str: string): string {
return str
.replace(/\\n/g, '\\\\n')
.replace(/\\r/g, '\\\\r')
.replace(/\\t/g, '\\\\t')
}
export class ExportCollector {
symbols: string[] = []
addSymbol(symbol: string) {
this.symbols.push(symbol)
}
getSymbols() {
return unique(this.symbols)
}
}