-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Count.ts
280 lines (262 loc) · 8.88 KB
/
Count.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { GeneratorConfig } from '@prisma/generator-helper'
import indent from 'indent-string'
import { DMMFClass } from '../../runtime/dmmf'
import { DMMF } from '../../runtime/dmmf-types'
import {
getAggregateArgsName,
getAggregateGetName,
getCountAggregateInputName,
getCountAggregateOutputName,
getFieldArgName,
getGroupByArgsName,
getGroupByPayloadName,
getModelArgName,
getSelectName,
getSelectReturnType,
Projection,
} from '../utils'
import { ArgsType } from './Args'
import { TAB_SIZE } from './constants'
import { Generatable, TS } from './Generatable'
import {
ExportCollector,
getArgs,
getGenericMethod,
getMethodJSDoc,
} from './helpers'
import { OutputType } from './Output'
import { PayloadType } from './Payload'
export class Count implements Generatable {
constructor(
protected readonly type: DMMF.OutputType,
protected readonly dmmf: DMMFClass,
protected readonly generator?: GeneratorConfig,
protected readonly collector?: ExportCollector,
) {}
protected get argsTypes(): Generatable[] {
const argsTypes: Generatable[] = []
argsTypes.push(new ArgsType([], this.type))
return argsTypes
}
public toTS(): string {
const { type } = this
const { name } = type
const outputType = new OutputType(this.dmmf, this.type)
return `
/**
* Count Type ${name}
*/
${outputType.toTS()}
export type ${getSelectName(name)} = {
${indent(
type.fields
.map(
(f) =>
`${f.name}?: boolean` +
(f.outputType.location === 'outputObjectTypes'
? ` | ${getFieldArgName(f)}`
: ''),
)
.join('\n'),
TAB_SIZE,
)}
}
${new PayloadType(outputType, true).toTS()}
${/*new CountDelegate(outputType, this.dmmf, this.generator).toTS()*/ ''}
// Custom InputTypes
${this.argsTypes.map(TS).join('\n')}
`
}
}
class CountDelegate implements Generatable {
constructor(
protected readonly outputType: OutputType,
protected readonly dmmf: DMMFClass,
protected readonly generator?: GeneratorConfig,
) {}
public toTS(): string {
const { fields, name } = this.outputType
const mapping = this.dmmf.mappingsMap[name]
if (!mapping) {
return ''
}
const model = this.dmmf.modelMap[name]
const actions = Object.entries(mapping).filter(
([key, value]) =>
key !== 'model' &&
key !== 'plural' &&
key !== 'aggregate' &&
key !== 'groupBy' &&
value,
)
const groupByArgsName = getGroupByArgsName(name)
const countArgsName = getModelArgName(name, DMMF.ModelAction.count)
return `\
type ${countArgsName} = Merge<
Omit<${getModelArgName(
name,
DMMF.ModelAction.findMany,
)}, 'select' | 'include'> & {
select?: ${getCountAggregateInputName(name)} | true
}
>
export interface ${name}Delegate<GlobalRejectSettings> {
${indent(
actions
.map(
([actionName]: [any, any]): string =>
`${getMethodJSDoc(actionName, mapping, model)}
${actionName}${getGenericMethod(name, actionName)}(
${getArgs(name, actionName)}
): ${getSelectReturnType({ name, actionName, projection: Projection.select })}`,
)
.join('\n\n'),
TAB_SIZE,
)}
${indent(getMethodJSDoc(DMMF.ModelAction.count, mapping, model), TAB_SIZE)}
count<T extends ${countArgsName}>(
args?: Subset<T, ${countArgsName}>,
): PrismaPromise<
T extends _Record<'select', any>
? T['select'] extends true
? number
: GetScalarType<T['select'], ${getCountAggregateOutputName(name)}>
: number
>
${indent(getMethodJSDoc(DMMF.ModelAction.aggregate, mapping, model), TAB_SIZE)}
aggregate<T extends ${getAggregateArgsName(
name,
)}>(args: Subset<T, ${getAggregateArgsName(
name,
)}>): PrismaPromise<${getAggregateGetName(name)}<T>>
${indent(getMethodJSDoc(DMMF.ModelAction.groupBy, mapping, model), TAB_SIZE)}
groupBy<
T extends ${groupByArgsName},
HasSelectOrTake extends Or<
Extends<'skip', Keys<T>>,
Extends<'take', Keys<T>>
>,
OrderByArg extends True extends HasSelectOrTake
? { orderBy: ${groupByArgsName}['orderBy'] }
: { orderBy?: ${groupByArgsName}['orderBy'] },
OrderFields extends Keys<MaybeTupleToUnion<T['orderBy']>>,
ByFields extends TupleToUnion<T['by']>,
ByValid extends Has<ByFields, OrderFields>,
HavingFields extends GetHavingFields<T['having']>,
HavingValid extends Has<ByFields, HavingFields>,
ByEmpty extends T['by'] extends never[] ? True : False,
InputErrors extends ByEmpty extends True
? \`Error: "by" must not be empty.\`
: HavingValid extends False
? {
[P in HavingFields]: P extends ByFields
? never
: P extends string
? \`Error: Field "$\{P}" used in "having" needs to be provided in "by".\`
: [
Error,
'Field ',
P,
\` in "having" needs to be provided in "by"\`,
]
}[HavingFields]
: 'take' extends Keys<T>
? 'orderBy' extends Keys<T>
? ByValid extends True
? {}
: {
[P in OrderFields]: P extends ByFields
? never
: \`Error: Field "$\{P}" in "orderBy" needs to be provided in "by"\`
}[OrderFields]
: 'Error: If you provide "take", you also need to provide "orderBy"'
: 'skip' extends Keys<T>
? 'orderBy' extends Keys<T>
? ByValid extends True
? {}
: {
[P in OrderFields]: P extends ByFields
? never
: \`Error: Field "$\{P}" in "orderBy" needs to be provided in "by"\`
}[OrderFields]
: 'Error: If you provide "skip", you also need to provide "orderBy"'
: ByValid extends True
? {}
: {
[P in OrderFields]: P extends ByFields
? never
: \`Error: Field "$\{P}" in "orderBy" needs to be provided in "by"\`
}[OrderFields]
>(args: SubsetIntersection<T, ${groupByArgsName}, OrderByArg> & InputErrors): {} extends InputErrors ? ${getGroupByPayloadName(
name,
)}<T> : Promise<InputErrors>
}
/**
* The delegate class that acts as a "Promise-like" for ${name}.
* Why is this prefixed with \`Prisma__\`?
* Because we want to prevent naming conflicts as mentioned in
* https://github.com/prisma/prisma-client-js/issues/707
*/
export class Prisma__${name}Client<T> implements PrismaPromise<T> {
[prisma]: true;
private readonly _dmmf;
private readonly _fetcher;
private readonly _queryType;
private readonly _rootField;
private readonly _clientMethod;
private readonly _args;
private readonly _dataPath;
private readonly _errorFormat;
private readonly _measurePerformance?;
private _isList;
private _callsite;
private _requestPromise?;
constructor(_dmmf: runtime.DMMFClass, _fetcher: PrismaClientFetcher, _queryType: 'query' | 'mutation', _rootField: string, _clientMethod: string, _args: any, _dataPath: string[], _errorFormat: ErrorFormat, _measurePerformance?: boolean | undefined, _isList?: boolean);
readonly [Symbol.toStringTag]: 'PrismaClientPromise';
${indent(
fields
.filter((f) => f.outputType.location === 'outputObjectTypes')
.map((f) => {
const fieldTypeName = (f.outputType.type as DMMF.OutputType).name
return `
${f.name}<T extends ${getFieldArgName(
f,
)} = {}>(args?: Subset<T, ${getFieldArgName(f)}>): ${getSelectReturnType({
name: fieldTypeName,
actionName: f.outputType.isList
? DMMF.ModelAction.findMany
: DMMF.ModelAction.findUnique,
hideCondition: false,
isField: true,
renderPromise: true,
fieldName: f.name,
projection: Projection.select,
})};`
})
.join('\n'),
2,
)}
private get _document();
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
/**
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The
* resolved value cannot be modified from the callback.
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
* @returns A Promise for the completion of the callback.
*/
finally(onfinally?: (() => void) | undefined | null): Promise<T>;
}`
}
}