-
Notifications
You must be signed in to change notification settings - Fork 280
/
create-schema-builder.ts
55 lines (47 loc) · 1.63 KB
/
create-schema-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
import { CreateSchemaNode } from '../operation-node/create-schema-node.js'
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'
export class CreateSchemaBuilder implements OperationNodeSource, Compilable {
readonly #props: CreateSchemaBuilderProps
constructor(props: CreateSchemaBuilderProps) {
this.#props = freeze(props)
}
ifNotExists(): CreateSchemaBuilder {
return new CreateSchemaBuilder({
...this.#props,
createSchemaNode: CreateSchemaNode.cloneWith(
this.#props.createSchemaNode,
{ modifier: 'IfNotExists' }
),
})
}
toOperationNode(): CreateSchemaNode {
return this.#props.executor.transformQuery(
this.#props.createSchemaNode,
this.#props.queryId
)
}
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(
CreateSchemaBuilder,
"don't await CreateSchemaBuilder instances directly. To execute the query you need to call `execute`"
)
export interface CreateSchemaBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
readonly createSchemaNode: CreateSchemaNode
}