Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BetterSqliteQueryRunner

Runs queries on a single sqlite database connection.

Does not support compose primary keys with autoincrement field. todo: need to throw exception for this case.

Hierarchy

  • AbstractSqliteQueryRunner
    • BetterSqliteQueryRunner

Implements

  • QueryRunner

Index

Constructors

Properties

Methods

Constructors

constructor

Properties

broadcaster

broadcaster: Broadcaster

Broadcaster used on this query runner to broadcast entity events.

connection

connection: Connection

Connection used by this query runner.

data

data: {}

Stores temporarily user data. Useful for sharing data with subscribers.

Type declaration

Protected databaseConnection

databaseConnection: any

Real database connection from a connection pool used to perform queries.

driver

driver: AbstractSqliteDriver

Database driver used by connection.

isReleased

isReleased: boolean

Indicates if connection for this query runner is released. Once its released, query runner cannot run queries anymore.

isTransactionActive

isTransactionActive: boolean

Indicates if transaction is in progress.

loadedTables

loadedTables: Table[]

All synchronized tables in the database.

loadedViews

loadedViews: View[]

All synchronized views in the database.

manager

manager: EntityManager

Entity manager working only with current query runner.

Protected mode

mode: "master" | "slave"

Mode in which query runner executes. Used for replication. If replication is not setup its value is ignored.

Protected sqlInMemory

sqlInMemory: SqlInMemory

Sql-s stored if "sql in memory" mode is enabled.

Protected sqlMemoryMode

sqlMemoryMode: boolean

Indicates if special query runner mode in which sql queries won't be executed is enabled.

Methods

addColumn

  • addColumn(tableOrName: Table | string, column: TableColumn): Promise<void>
  • Creates a new column from the column in the table.

    Parameters

    • tableOrName: Table | string
    • column: TableColumn

    Returns Promise<void>

addColumns

  • addColumns(tableOrName: Table | string, columns: TableColumn[]): Promise<void>
  • Creates a new columns from the column in the table.

    Parameters

    • tableOrName: Table | string
    • columns: TableColumn[]

    Returns Promise<void>

Protected buildCreateColumnSql

  • buildCreateColumnSql(column: TableColumn, skipPrimary?: boolean): string
  • Builds a query for create column.

    Parameters

    • column: TableColumn
    • Optional skipPrimary: boolean

    Returns string

changeColumn

  • changeColumn(tableOrName: Table | string, oldTableColumnOrName: TableColumn | string, newColumn: TableColumn): Promise<void>
  • Changes a column in the table.

    Parameters

    • tableOrName: Table | string
    • oldTableColumnOrName: TableColumn | string
    • newColumn: TableColumn

    Returns Promise<void>

changeColumns

  • changeColumns(tableOrName: Table | string, changedColumns: { newColumn: TableColumn; oldColumn: TableColumn }[]): Promise<void>
  • Changes a column in the table. Changed column looses all its keys in the db.

    Parameters

    • tableOrName: Table | string
    • changedColumns: { newColumn: TableColumn; oldColumn: TableColumn }[]

    Returns Promise<void>

clearDatabase

  • clearDatabase(): Promise<void>
  • Removes all tables from the currently connected database.

    Returns Promise<void>

clearSqlMemory

  • clearSqlMemory(): void

clearTable

  • clearTable(tableName: string): Promise<void>
  • Clears all table contents. Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.

    Parameters

    • tableName: string

    Returns Promise<void>

commitTransaction

  • commitTransaction(): Promise<void>
  • Commits transaction. Error will be thrown if transaction was not started.

    Returns Promise<void>

connect

  • connect(): Promise<any>
  • Creates/uses database connection from the connection pool to perform further operations. Returns obtained database connection.

    Returns Promise<any>

createCheckConstraint

  • createCheckConstraint(tableOrName: Table | string, checkConstraint: TableCheck): Promise<void>
  • Creates new check constraint.

    Parameters

    • tableOrName: Table | string
    • checkConstraint: TableCheck

    Returns Promise<void>

createCheckConstraints

  • createCheckConstraints(tableOrName: Table | string, checkConstraints: TableCheck[]): Promise<void>
  • Creates new check constraints.

    Parameters

    • tableOrName: Table | string
    • checkConstraints: TableCheck[]

    Returns Promise<void>

createDatabase

  • createDatabase(database: string, ifNotExist?: boolean): Promise<void>
  • Creates a new database.

    Parameters

    • database: string
    • Optional ifNotExist: boolean

    Returns Promise<void>

createExclusionConstraint

  • createExclusionConstraint(tableOrName: Table | string, exclusionConstraint: TableExclusion): Promise<void>
  • Creates a new exclusion constraint.

    Parameters

    • tableOrName: Table | string
    • exclusionConstraint: TableExclusion

    Returns Promise<void>

createExclusionConstraints

  • createExclusionConstraints(tableOrName: Table | string, exclusionConstraints: TableExclusion[]): Promise<void>
  • Creates a new exclusion constraints.

    Parameters

    • tableOrName: Table | string
    • exclusionConstraints: TableExclusion[]

    Returns Promise<void>

createForeignKey

  • createForeignKey(tableOrName: Table | string, foreignKey: TableForeignKey): Promise<void>
  • Creates a new foreign key.

    Parameters

    • tableOrName: Table | string
    • foreignKey: TableForeignKey

    Returns Promise<void>

createForeignKeys

  • createForeignKeys(tableOrName: Table | string, foreignKeys: TableForeignKey[]): Promise<void>
  • Creates a new foreign keys.

    Parameters

    • tableOrName: Table | string
    • foreignKeys: TableForeignKey[]

    Returns Promise<void>

createIndex

  • createIndex(tableOrName: Table | string, index: TableIndex): Promise<void>
  • Creates a new index.

    Parameters

    • tableOrName: Table | string
    • index: TableIndex

    Returns Promise<void>

Protected createIndexSql

  • createIndexSql(table: Table, index: TableIndex): Query
  • Builds create index sql.

    Parameters

    • table: Table
    • index: TableIndex

    Returns Query

createIndices

  • createIndices(tableOrName: Table | string, indices: TableIndex[]): Promise<void>
  • Creates a new indices

    Parameters

    • tableOrName: Table | string
    • indices: TableIndex[]

    Returns Promise<void>

createPrimaryKey

  • createPrimaryKey(tableOrName: Table | string, columnNames: string[]): Promise<void>
  • Creates a new primary key.

    Parameters

    • tableOrName: Table | string
    • columnNames: string[]

    Returns Promise<void>

createSchema

  • createSchema(schema: string, ifNotExist?: boolean): Promise<void>
  • Creates a new table schema.

    Parameters

    • schema: string
    • Optional ifNotExist: boolean

    Returns Promise<void>

createTable

  • createTable(table: Table, ifNotExist?: boolean, createForeignKeys?: boolean, createIndices?: boolean): Promise<void>
  • Creates a new table.

    Parameters

    • table: Table
    • Optional ifNotExist: boolean
    • Optional createForeignKeys: boolean
    • Optional createIndices: boolean

    Returns Promise<void>

Protected createTableSql

  • createTableSql(table: Table, createForeignKeys?: boolean): Query
  • Builds create table sql.

    Parameters

    • table: Table
    • Optional createForeignKeys: boolean

    Returns Query

createUniqueConstraint

  • createUniqueConstraint(tableOrName: Table | string, uniqueConstraint: TableUnique): Promise<void>
  • Creates a new unique constraint.

    Parameters

    • tableOrName: Table | string
    • uniqueConstraint: TableUnique

    Returns Promise<void>

createUniqueConstraints

  • createUniqueConstraints(tableOrName: Table | string, uniqueConstraints: TableUnique[]): Promise<void>
  • Creates a new unique constraints.

    Parameters

    • tableOrName: Table | string
    • uniqueConstraints: TableUnique[]

    Returns Promise<void>

createView

  • createView(view: View): Promise<void>
  • Creates a new view.

    Parameters

    • view: View

    Returns Promise<void>

Protected createViewSql

  • createViewSql(view: View): Query
  • Parameters

    • view: View

    Returns Query

Protected deleteViewDefinitionSql

  • deleteViewDefinitionSql(viewOrPath: View | string): Query

disableSqlMemory

  • disableSqlMemory(): void
  • Disables special query runner mode in which sql queries won't be executed started by calling enableSqlMemory() method.

    Previously memorized sql will be flushed.

    Returns void

dropCheckConstraint

  • dropCheckConstraint(tableOrName: Table | string, checkOrName: TableCheck | string): Promise<void>
  • Drops check constraint.

    Parameters

    • tableOrName: Table | string
    • checkOrName: TableCheck | string

    Returns Promise<void>

dropCheckConstraints

  • dropCheckConstraints(tableOrName: Table | string, checkConstraints: TableCheck[]): Promise<void>
  • Drops check constraints.

    Parameters

    • tableOrName: Table | string
    • checkConstraints: TableCheck[]

    Returns Promise<void>

dropColumn

  • dropColumn(tableOrName: Table | string, columnOrName: TableColumn | string): Promise<void>
  • Drops column in the table.

    Parameters

    • tableOrName: Table | string
    • columnOrName: TableColumn | string

    Returns Promise<void>

dropColumns

  • dropColumns(tableOrName: Table | string, columns: TableColumn[]): Promise<void>
  • Drops the columns in the table.

    Parameters

    • tableOrName: Table | string
    • columns: TableColumn[]

    Returns Promise<void>

dropDatabase

  • dropDatabase(database: string, ifExist?: boolean): Promise<void>
  • Drops database.

    Parameters

    • database: string
    • Optional ifExist: boolean

    Returns Promise<void>

dropExclusionConstraint

  • dropExclusionConstraint(tableOrName: Table | string, exclusionOrName: TableExclusion | string): Promise<void>
  • Drops exclusion constraint.

    Parameters

    • tableOrName: Table | string
    • exclusionOrName: TableExclusion | string

    Returns Promise<void>

dropExclusionConstraints

  • dropExclusionConstraints(tableOrName: Table | string, exclusionConstraints: TableExclusion[]): Promise<void>
  • Drops exclusion constraints.

    Parameters

    • tableOrName: Table | string
    • exclusionConstraints: TableExclusion[]

    Returns Promise<void>

dropForeignKey

  • dropForeignKey(tableOrName: Table | string, foreignKeyOrName: TableForeignKey | string): Promise<void>
  • Drops a foreign key from the table.

    Parameters

    • tableOrName: Table | string
    • foreignKeyOrName: TableForeignKey | string

    Returns Promise<void>

dropForeignKeys

  • dropForeignKeys(tableOrName: Table | string, foreignKeys: TableForeignKey[]): Promise<void>
  • Drops a foreign keys from the table.

    Parameters

    • tableOrName: Table | string
    • foreignKeys: TableForeignKey[]

    Returns Promise<void>

dropIndex

  • dropIndex(tableOrName: Table | string, indexOrName: TableIndex | string): Promise<void>
  • Drops an index from the table.

    Parameters

    • tableOrName: Table | string
    • indexOrName: TableIndex | string

    Returns Promise<void>

Protected dropIndexSql

  • dropIndexSql(indexOrName: TableIndex | string): Query
  • Builds drop index sql.

    Parameters

    • indexOrName: TableIndex | string

    Returns Query

dropIndices

  • dropIndices(tableOrName: Table | string, indices: TableIndex[]): Promise<void>
  • Drops an indices from the table.

    Parameters

    • tableOrName: Table | string
    • indices: TableIndex[]

    Returns Promise<void>

dropPrimaryKey

  • dropPrimaryKey(tableOrName: Table | string): Promise<void>
  • Drops a primary key.

    Parameters

    • tableOrName: Table | string

    Returns Promise<void>

dropSchema

  • dropSchema(schemaPath: string, ifExist?: boolean): Promise<void>
  • Drops table schema.

    Parameters

    • schemaPath: string
    • Optional ifExist: boolean

    Returns Promise<void>

dropTable

  • dropTable(tableOrName: Table | string, ifExist?: boolean, dropForeignKeys?: boolean, dropIndices?: boolean): Promise<void>
  • Drops the table.

    Parameters

    • tableOrName: Table | string
    • Optional ifExist: boolean
    • Optional dropForeignKeys: boolean
    • Optional dropIndices: boolean

    Returns Promise<void>

Protected dropTableSql

  • dropTableSql(tableOrName: Table | string, ifExist?: boolean): Query
  • Builds drop table sql.

    Parameters

    • tableOrName: Table | string
    • Optional ifExist: boolean

    Returns Query

dropUniqueConstraint

  • dropUniqueConstraint(tableOrName: Table | string, uniqueOrName: TableUnique | string): Promise<void>
  • Drops an unique constraint.

    Parameters

    • tableOrName: Table | string
    • uniqueOrName: TableUnique | string

    Returns Promise<void>

dropUniqueConstraints

  • dropUniqueConstraints(tableOrName: Table | string, uniqueConstraints: TableUnique[]): Promise<void>
  • Creates an unique constraints.

    Parameters

    • tableOrName: Table | string
    • uniqueConstraints: TableUnique[]

    Returns Promise<void>

dropView

  • dropView(target: View | string): Promise<void>
  • Drops the view.

    Parameters

    • target: View | string

    Returns Promise<void>

Protected dropViewSql

  • dropViewSql(viewOrPath: View | string): Query
  • Builds drop view sql.

    Parameters

    • viewOrPath: View | string

    Returns Query

enableSqlMemory

  • enableSqlMemory(): void
  • Enables special query runner mode in which sql queries won't be executed, instead they will be memorized into a special variable inside query runner. You can get memorized sql using getMemorySql() method.

    Returns void

executeMemoryDownSql

  • executeMemoryDownSql(): Promise<void>

executeMemoryUpSql

  • executeMemoryUpSql(): Promise<void>

Protected executeQueries

  • executeQueries(upQueries: Query | Query[], downQueries: Query | Query[]): Promise<void>
  • Executes sql used special for schema build.

    Parameters

    • upQueries: Query | Query[]
    • downQueries: Query | Query[]

    Returns Promise<void>

Protected getCachedTable

  • getCachedTable(tableName: string): Promise<Table>
  • Gets table from previously loaded tables, otherwise loads it from database.

    Parameters

    • tableName: string

    Returns Promise<Table>

Protected getCachedView

  • getCachedView(viewName: string): Promise<View>
  • Gets view from previously loaded views, otherwise loads it from database.

    Parameters

    • viewName: string

    Returns Promise<View>

getDatabases

  • getDatabases(): Promise<string[]>
  • Returns all available database names including system databases.

    Returns Promise<string[]>

getMemorySql

  • getMemorySql(): SqlInMemory
  • Gets sql stored in the memory. Parameters in the sql are already replaced.

    Returns SqlInMemory

getSchemas

  • getSchemas(database?: string): Promise<string[]>
  • Returns all available schema names including system schemas. If database parameter specified, returns schemas of that database.

    Parameters

    • Optional database: string

    Returns Promise<string[]>

getTable

  • getTable(tablePath: string): Promise<Table | undefined>
  • Loads given table's data from the database.

    Parameters

    • tablePath: string

    Returns Promise<Table | undefined>

getTables

  • getTables(tableNames: string[]): Promise<Table[]>
  • Loads all tables (with given names) from the database.

    Parameters

    • tableNames: string[]

    Returns Promise<Table[]>

Protected getTypeormMetadataTableName

  • getTypeormMetadataTableName(): string

getView

  • getView(viewPath: string): Promise<View | undefined>
  • Loads given view's data from the database.

    Parameters

    • viewPath: string

    Returns Promise<View | undefined>

getViews

  • getViews(viewPaths: string[]): Promise<View[]>
  • Loads given view's data from the database.

    Parameters

    • viewPaths: string[]

    Returns Promise<View[]>

hasColumn

  • hasColumn(tableOrName: Table | string, columnName: string): Promise<boolean>
  • Checks if column with the given name exist in the given table.

    Parameters

    • tableOrName: Table | string
    • columnName: string

    Returns Promise<boolean>

hasDatabase

  • hasDatabase(database: string): Promise<boolean>
  • Checks if database with the given name exist.

    Parameters

    • database: string

    Returns Promise<boolean>

hasSchema

  • hasSchema(schema: string): Promise<boolean>
  • Checks if schema with the given name exist.

    Parameters

    • schema: string

    Returns Promise<boolean>

hasTable

  • hasTable(tableOrName: Table | string): Promise<boolean>
  • Checks if table with the given name exist in the database.

    Parameters

    • tableOrName: Table | string

    Returns Promise<boolean>

Protected insertViewDefinitionSql

  • insertViewDefinitionSql(view: View): Query

Protected isColumnChanged

  • isColumnChanged(oldColumn: TableColumn, newColumn: TableColumn, checkDefault?: boolean, checkComment?: boolean): boolean
  • Checks if at least one of column properties was changed. Does not checks column type, length and autoincrement, because these properties changes separately.

    Parameters

    • oldColumn: TableColumn
    • newColumn: TableColumn
    • Optional checkDefault: boolean
    • Optional checkComment: boolean

    Returns boolean

Protected isDefaultColumnLength

  • isDefaultColumnLength(table: Table, column: TableColumn, length: string): boolean
  • Checks if column length is by default.

    Parameters

    • table: Table
    • column: TableColumn
    • length: string

    Returns boolean

Protected isDefaultColumnPrecision

  • isDefaultColumnPrecision(table: Table, column: TableColumn, precision: number): boolean
  • Checks if column precision is by default.

    Parameters

    • table: Table
    • column: TableColumn
    • precision: number

    Returns boolean

Protected isDefaultColumnScale

  • isDefaultColumnScale(table: Table, column: TableColumn, scale: number): boolean
  • Checks if column scale is by default.

    Parameters

    • table: Table
    • column: TableColumn
    • scale: number

    Returns boolean

Protected isDefaultColumnWidth

  • isDefaultColumnWidth(table: Table, column: TableColumn, width: number): boolean
  • Checks if column display width is by default. Used only for MySQL.

    Parameters

    • table: Table
    • column: TableColumn
    • width: number

    Returns boolean

Protected loadTables

  • loadTables(tableNames: string[]): Promise<Table[]>
  • Loads all tables (with given names) from the database and creates a Table from them.

    Parameters

    • tableNames: string[]

    Returns Promise<Table[]>

Protected loadViews

  • loadViews(viewNames: string[]): Promise<View[]>
  • Parameters

    • viewNames: string[]

    Returns Promise<View[]>

query

  • query(query: string, parameters?: any[]): Promise<any>
  • Executes a given SQL query.

    Parameters

    • query: string
    • Optional parameters: any[]

    Returns Promise<any>

Protected recreateTable

  • recreateTable(newTable: Table, oldTable: Table, migrateData?: boolean): Promise<void>
  • Parameters

    • newTable: Table
    • oldTable: Table
    • Optional migrateData: boolean

    Returns Promise<void>

release

  • release(): Promise<void>
  • Releases used database connection. We just clear loaded tables and sql in memory, because sqlite do not support multiple connections thus query runners.

    Returns Promise<void>

renameColumn

  • renameColumn(tableOrName: Table | string, oldTableColumnOrName: TableColumn | string, newTableColumnOrName: TableColumn | string): Promise<void>
  • Renames column in the given table.

    Parameters

    • tableOrName: Table | string
    • oldTableColumnOrName: TableColumn | string
    • newTableColumnOrName: TableColumn | string

    Returns Promise<void>

renameTable

  • renameTable(oldTableOrName: Table | string, newTableName: string): Promise<void>
  • Renames the given table.

    Parameters

    • oldTableOrName: Table | string
    • newTableName: string

    Returns Promise<void>

Protected replaceCachedTable

  • replaceCachedTable(table: Table, changedTable: Table): void
  • Replaces loaded table with given changed table.

    Parameters

    • table: Table
    • changedTable: Table

    Returns void

rollbackTransaction

  • rollbackTransaction(): Promise<void>
  • Rollbacks transaction. Error will be thrown if transaction was not started.

    Returns Promise<void>

startTransaction

  • startTransaction(isolationLevel?: IsolationLevel): Promise<void>
  • Starts transaction.

    Parameters

    • Optional isolationLevel: IsolationLevel

    Returns Promise<void>

stream

  • stream(query: string, parameters?: any[], onEnd?: Function, onError?: Function): Promise<ReadStream>
  • Returns raw data stream.

    Parameters

    • query: string
    • Optional parameters: any[]
    • Optional onEnd: Function
    • Optional onError: Function

    Returns Promise<ReadStream>

updatePrimaryKeys

  • updatePrimaryKeys(tableOrName: Table | string, columns: TableColumn[]): Promise<void>
  • Updates composite primary keys.

    Parameters

    • tableOrName: Table | string
    • columns: TableColumn[]

    Returns Promise<void>