Start routing and pipeline rewrite
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-01-17 15:57:40 -06:00
parent 9b8333295f
commit 8cf19792a6
34 changed files with 470 additions and 1654 deletions

View File

@@ -66,14 +66,14 @@ export class PostgresSchema extends Schema {
cols.each(col => {
table.column(col.column_name)
.type(col.data_type)
.pipe()
.when(col.is_nullable, builder => {
builder.isNullable()
return builder
})
.when(col.column_default, builder => {
builder.default(raw(col.column_default))
return builder
.pipe(line => {
return line
.when(col.is_nullable, builder => {
return builder.nullable()
})
.when(col.column_default, builder => {
return builder.default(raw(col.column_default))
})
})
})
@@ -92,26 +92,25 @@ export class PostgresSchema extends Schema {
table.constraint(key)
.type(ConstraintType.Unique)
.pipe()
.peek(constraint => {
.tap(constraint => {
collect<{column_name: string}>(uniques[key]) // eslint-disable-line camelcase
.pluck<string>('column_name')
.each(column => constraint.field(column))
})
.get()
.flagAsExistingInSchema()
}
// Apply the primary key constraints
constraints.where('constraint_type', '=', 'p')
.pipe()
.when(c => c.count() > 0, pk => {
pk.each(constraint => {
table.column(constraint.column_name)
.primary()
})
.pipe(line => {
return line.when(c => c.count() > 0, pk => {
pk.each(constraint => {
table.column(constraint.column_name)
.primary()
})
return pk
return pk
})
})
// Apply the non-null constraints
@@ -158,15 +157,16 @@ export class PostgresSchema extends Schema {
}
table.index(key)
.pipe()
.peek(idx => {
collect<{column_name: string}>(groupedIndexes[key]) // eslint-disable-line camelcase
.pluck<string>('column_name')
.each(col => idx.field(col))
.pipe(builder => {
return builder
.peek(idx => {
collect<{column_name: string}>(groupedIndexes[key]) // eslint-disable-line camelcase
.pluck<string>('column_name')
.each(col => idx.field(col))
})
.when(groupedIndexes[key]?.[0]?.indisprimary, idx => idx.primary())
.when(groupedIndexes[key]?.[0]?.indisunique, idx => idx.unique())
})
.when(groupedIndexes[key]?.[0]?.indisprimary, idx => idx.primary())
.when(groupedIndexes[key]?.[0]?.indisunique, idx => idx.unique())
.get()
.flagAsExistingInSchema()
}

View File

@@ -1,4 +1,4 @@
import {collect, Maybe, ParameterizedCallback, Pipe} from '../../util'
import {collect, Maybe, ParameterizedCallback, Pipeline} from '../../util'
import {FieldType} from '../types'
import {EscapeValue, QuerySafeValue, raw} from '../dialect/SQLDialect'
@@ -33,7 +33,7 @@ export abstract class SchemaBuilderBase {
protected existsInSchema = false
/** If the resource exists in the schema, the unaltered values it has. */
public originalFromSchema?: SchemaBuilderBase
public originalFromSchema?: this
constructor(
/** The name of the schema item. */
@@ -44,7 +44,7 @@ export abstract class SchemaBuilderBase {
* Clone the properties of this resource to a different instance.
* @param newBuilder
*/
public cloneTo(newBuilder: SchemaBuilderBase): SchemaBuilderBase {
public cloneTo(newBuilder: this): this {
newBuilder.shouldDrop = this.shouldDrop
newBuilder.shouldRenameTo = this.shouldRenameTo
newBuilder.shouldSkipIfExists = this.shouldSkipIfExists
@@ -129,9 +129,14 @@ export abstract class SchemaBuilderBase {
return this
}
/** Get a Pipe containing this instance. */
pipe(): Pipe<this> {
return Pipe.wrap<this>(this)
/** Build and apply a pipeline. */
pipe<TOut>(builder: (pipeline: Pipeline<this, this>) => Pipeline<this, TOut>): TOut {
return builder(Pipeline.id()).apply(this)
}
tap(op: (x: this) => unknown): this {
op(this)
return this
}
/**
@@ -163,8 +168,6 @@ export class ColumnBuilder extends SchemaBuilderBase {
/** True if this column should contain distinct values. */
protected shouldBeUnique = false
public originalFromSchema?: ColumnBuilder
constructor(
name: string,
@@ -174,7 +177,7 @@ export class ColumnBuilder extends SchemaBuilderBase {
super(name)
}
public cloneTo(newBuilder: ColumnBuilder): ColumnBuilder {
public cloneTo(newBuilder: this): this {
super.cloneTo(newBuilder)
newBuilder.targetType = this.targetType
newBuilder.shouldBeNullable = this.shouldBeNullable
@@ -324,7 +327,6 @@ export enum ConstraintType {
* Builder to specify the schema of a table constraint.
*/
export class ConstraintBuilder extends SchemaBuilderBase {
public originalFromSchema?: ConstraintBuilder
/** The fields included in this constraint. */
protected fields: Set<string> = new Set<string>()
@@ -359,7 +361,7 @@ export class ConstraintBuilder extends SchemaBuilderBase {
return this.constraintExpression
}
public cloneTo(newBuilder: ConstraintBuilder): ConstraintBuilder {
public cloneTo(newBuilder: this): this {
super.cloneTo(newBuilder)
newBuilder.fields = new Set<string>([...this.fields])
newBuilder.constraintType = this.constraintType
@@ -436,8 +438,6 @@ export class IndexBuilder extends SchemaBuilderBase {
/** True if this is a primary key index. */
protected shouldBePrimary = false
public originalFromSchema?: IndexBuilder
constructor(
name: string,
@@ -447,7 +447,7 @@ export class IndexBuilder extends SchemaBuilderBase {
super(name)
}
public cloneTo(newBuilder: IndexBuilder): IndexBuilder {
public cloneTo(newBuilder: this): this {
super.cloneTo(newBuilder)
newBuilder.fields = new Set<string>([...this.fields])
newBuilder.removedFields = new Set<string>([...this.removedFields])
@@ -555,9 +555,7 @@ export class TableBuilder extends SchemaBuilderBase {
*/
protected constraints: {[key: string]: ConstraintBuilder} = {}
public originalFromSchema?: TableBuilder
public cloneTo(newBuilder: TableBuilder): TableBuilder {
public cloneTo(newBuilder: this): this {
super.cloneTo(newBuilder)
newBuilder.columns = {...this.columns}
newBuilder.indexes = {...this.indexes}
@@ -743,8 +741,7 @@ export class TableBuilder extends SchemaBuilderBase {
const name = this.getNextAvailableConstraintName(ConstraintType.Unique)
this.constraint(name)
.type(ConstraintType.Unique)
.pipe()
.peek(constraint => {
.tap(constraint => {
fields.forEach(field => constraint.field(field))
})