-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathcreate-type-builder.ts
61 lines (53 loc) · 1.68 KB
/
create-type-builder.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
import { OperationNodeSource } from '../operation-node/operation-node-source.js'
import { CompiledQuery } from '../query-compiler/compiled-query.js'
import { Compilable } from '../util/compilable.js'
import { preventAwait } from '../util/prevent-await.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { QueryId } from '../util/query-id.js'
import { freeze } from '../util/object-utils.js'
import { CreateTypeNode } from '../operation-node/create-type-node.js'
export class CreateTypeBuilder implements OperationNodeSource, Compilable {
readonly #props: CreateTypeBuilderProps
constructor(props: CreateTypeBuilderProps) {
this.#props = freeze(props)
}
toOperationNode(): CreateTypeNode {
return this.#props.executor.transformQuery(
this.#props.node,
this.#props.queryId
)
}
/**
* Creates an anum type.
*
* ### Examples
*
* ```ts
* db.schema.createType('species').asEnum(['cat', 'dog', 'frog'])
* ```
*/
asEnum(values: string[]): CreateTypeBuilder {
return new CreateTypeBuilder({
...this.#props,
node: CreateTypeNode.cloneWithEnum(this.#props.node, values),
})
}
compile(): CompiledQuery {
return this.#props.executor.compileQuery(
this.toOperationNode(),
this.#props.queryId
)
}
async execute(): Promise<void> {
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId)
}
}
preventAwait(
CreateTypeBuilder,
"don't await CreateTypeBuilder instances directly. To execute the query you need to call `execute`"
)
export interface CreateTypeBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
readonly node: CreateTypeNode
}