Skip to content

Commit

Permalink
chore: limit AlterColumnNode.create available params, change AlterCol…
Browse files Browse the repository at this point in the history
…umnBuilder fields names
  • Loading branch information
soanvig authored and koskimas committed May 28, 2023
1 parent e7eb9b3 commit 1235cc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/operation-node/alter-column-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const AlterColumnNode = freeze({
return node.kind === 'AlterColumnNode'
},

create<T extends keyof AlterColumnNode>(column: string, prop: T, value: Required<AlterColumnNode>[T]): AlterColumnNode {
create<T extends keyof AlterColumnNodeProps>(column: string, prop: T, value: Required<AlterColumnNodeProps>[T]): AlterColumnNode {
return freeze({
kind: 'AlterColumnNode',
column: ColumnNode.create(column),
Expand Down
24 changes: 11 additions & 13 deletions src/schema/alter-column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ import {
} from '../parser/default-value-parser.js'

export class AlterColumnBuilder {
protected column: string
readonly #column: string

constructor(column: string) {
this.column = column
this.#column = column
}

setDataType(dataType: DataTypeExpression): AlteredColumnBuilder {
return new AlteredColumnBuilder(
AlterColumnNode.create(this.column, 'dataType', parseDataTypeExpression(dataType))
AlterColumnNode.create(this.#column, 'dataType', parseDataTypeExpression(dataType))
)
}

setDefault(value: DefaultValueExpression): AlteredColumnBuilder {
return new AlteredColumnBuilder(
AlterColumnNode.create(this.column, 'setDefault', parseDefaultValueExpression(value))
AlterColumnNode.create(this.#column, 'setDefault', parseDefaultValueExpression(value))
)
}

dropDefault(): AlteredColumnBuilder {
return new AlteredColumnBuilder(
AlterColumnNode.create(this.column, 'dropDefault', true)
AlterColumnNode.create(this.#column, 'dropDefault', true)
)
}

setNotNull(): AlteredColumnBuilder {
return new AlteredColumnBuilder(
AlterColumnNode.create(this.column, 'setNotNull', true)
AlterColumnNode.create(this.#column, 'setNotNull', true)
)
}

dropNotNull(): AlteredColumnBuilder {
return new AlteredColumnBuilder(
AlterColumnNode.create(this.column, 'dropNotNull', true)
AlterColumnNode.create(this.#column, 'dropNotNull', true)
)
}

Expand All @@ -56,9 +56,7 @@ export class AlterColumnBuilder {
}

/**
* Allows us to force consumers to do something, anything, when altering a column,
* and also disallows chaining more methods on AlterColumnBuilder, because
* SQL syntax for chaining ALTER COLUMN operations would be not WYSIWYG
* Allows us to force consumers to do exactly one alteration to a column.
*
* Basically, deny the following:
*
Expand All @@ -74,14 +72,14 @@ export class AlterColumnBuilder {
*/
export class AlteredColumnBuilder implements OperationNodeSource
{
protected readonly alterColumnNode: AlterColumnNode
readonly #alterColumnNode: AlterColumnNode

constructor(alterColumnNode: AlterColumnNode) {
this.alterColumnNode = alterColumnNode
this.#alterColumnNode = alterColumnNode
}

toOperationNode(): AlterColumnNode {
return this.alterColumnNode
return this.#alterColumnNode
}
}

Expand Down

0 comments on commit 1235cc2

Please sign in to comment.