2021-06-18 00:35:31 +00:00
|
|
|
import {Connection} from '../connection/Connection'
|
|
|
|
import {Awaitable} from '../../util'
|
2021-07-25 14:15:01 +00:00
|
|
|
import {TableBuilder} from './TableBuilder'
|
2021-06-18 00:35:31 +00:00
|
|
|
|
2021-07-25 14:15:01 +00:00
|
|
|
/**
|
|
|
|
* Represents a SQL-schema implementation.
|
|
|
|
*/
|
2021-06-18 00:35:31 +00:00
|
|
|
export abstract class Schema {
|
|
|
|
constructor(
|
2021-07-25 14:15:01 +00:00
|
|
|
/** The SQL connection to execute against. */
|
2021-06-18 00:35:31 +00:00
|
|
|
protected readonly connection: Connection,
|
|
|
|
) { }
|
|
|
|
|
2021-07-25 14:15:01 +00:00
|
|
|
/**
|
|
|
|
* Resolve true if the schema has a table with the given name.
|
|
|
|
* @param name
|
|
|
|
*/
|
2021-06-18 00:35:31 +00:00
|
|
|
public abstract hasTable(name: string): Awaitable<boolean>
|
|
|
|
|
2021-07-25 14:15:01 +00:00
|
|
|
/**
|
|
|
|
* Resolve true if the schema table with the given name has a column with the given name.
|
|
|
|
* @param table
|
|
|
|
* @param name
|
|
|
|
*/
|
2021-06-18 00:35:31 +00:00
|
|
|
public abstract hasColumn(table: string, name: string): Awaitable<boolean>
|
|
|
|
|
2021-07-25 14:15:01 +00:00
|
|
|
/**
|
|
|
|
* Resolve true if the schema table with the given name has all the specified columns.
|
|
|
|
* @param table
|
|
|
|
* @param name
|
|
|
|
*/
|
2021-06-18 00:35:31 +00:00
|
|
|
public abstract hasColumns(table: string, name: string[]): Awaitable<boolean>
|
2021-07-25 14:15:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a TableBuilder instance for a table on the schema.
|
|
|
|
* @param table
|
|
|
|
*/
|
|
|
|
public abstract table(table: string): Awaitable<TableBuilder>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the table to the schema.
|
|
|
|
* @param schema
|
|
|
|
*/
|
|
|
|
public async commit(schema: TableBuilder): Promise<void> {
|
|
|
|
const query = this.connection
|
|
|
|
.dialect()
|
|
|
|
.renderCommitSchemaTransaction(schema)
|
|
|
|
|
|
|
|
await this.connection.query(query)
|
|
|
|
}
|
2021-06-18 00:35:31 +00:00
|
|
|
}
|