Class AbstractQueryInterface<Dialect>

This interface exposes low-level APIs to interact with the database. Typically useful in contexts where models are not available, such as migrations.

This interface is available through Sequelize#queryInterface.

Type Parameters

Hierarchy (view full)

Constructors

Properties

#internalQueryInterface: AbstractQueryInterfaceInternal
dialect: Dialect

Accessors

Methods

  • Add a constraint to a table

    Available constraints:

    • UNIQUE
    • DEFAULT (MSSQL only)
    • CHECK (Not supported by MySQL)
    • FOREIGN KEY
    • PRIMARY KEY

    Parameters

    Returns Promise<void>

    Example: UNIQUE

    queryInterface.addConstraint('Users', {
    fields: ['email'],
    type: 'UNIQUE',
    name: 'custom_unique_constraint_name'
    });

    Example: CHECK

    queryInterface.addConstraint('Users', {
    fields: ['roles'],
    type: 'CHECK',
    where: {
    roles: ['user', 'admin', 'moderator', 'guest']
    }
    });

    Example: Default - MSSQL only

    queryInterface.addConstraint('Users', {
    fields: ['roles'],
    type: 'DEFAULT',
    defaultValue: 'guest'
    });

    Example: Primary Key

    queryInterface.addConstraint('Users', {
    fields: ['username'],
    type: 'PRIMARY KEY',
    name: 'custom_primary_constraint_name'
    });

    Example: Composite Primary Key

    queryInterface.addConstraint('Users', {
    fields: ['first_name', 'last_name'],
    type: 'PRIMARY KEY',
    name: 'custom_primary_constraint_name'
    });

    Example: Foreign Key

    queryInterface.addConstraint('Posts', {
    fields: ['username'],
    type: 'FOREIGN KEY',
    name: 'custom_fkey_constraint_name',
    references: { //Required field
    table: 'target_table_name',
    field: 'target_column_name'
    },
    onDelete: 'cascade',
    onUpdate: 'cascade'
    });

    Example: Composite Foreign Key

    queryInterface.addConstraint('TableName', {
    fields: ['source_column_name', 'other_source_column_name'],
    type: 'FOREIGN KEY',
    name: 'custom_fkey_constraint_name',
    references: { //Required field
    table: 'target_table_name',
    fields: ['target_column_name', 'other_target_column_name']
    },
    onDelete: 'cascade',
    onUpdate: 'cascade'
    });
  • Adds a new index to a table

    Parameters

    Returns Promise<void>

  • Parameters

    • tableName: TableOrModel
    • options: {
          [COMPLETES_TRANSACTION]?: boolean;
          benchmark?: boolean;
          bind?: BindOrReplacements;
          concurrently?: boolean;
          connection?: null | AbstractConnection;
          fieldMap?: FieldMap;
          fields: (string | Fn | Literal | IndexField)[];
          include?: Literal | (string | Literal)[];
          instance?: Model<any, any>;
          logging?: false | ((sql, timing?) => void);
          mapToModel?: boolean;
          msg?: string;
          name?: string;
          nest?: boolean;
          operator?: string;
          parser?: null | string;
          plain?: boolean;
          prefix?: string;
          raw?: boolean;
          replacements?: {
              [key: string]: unknown;
          };
          retry?: Options;
          supportsSearchPath?: boolean;
          transaction?: null | Transaction;
          type?: IndexType;
          unique?: boolean;
          useMaster?: boolean;
          using?: string;
          where?: WhereOptions;
      }
      • Private Optional [COMPLETES_TRANSACTION]?: boolean

        Indicates if the query completes the transaction Internal only

      • Optional benchmark?: boolean

        Pass query execution time in milliseconds as second argument to logging function (options.logging).

      • Optional bind?: BindOrReplacements

        Either an object of named parameter bindings in the format $param or an array of unnamed values to bind to $1, $2, etc in your SQL.

      • Optional concurrently?: boolean

        PostgreSQL will build the index without taking any write locks. Postgres only.

        Default

        false
        
      • Optional connection?: null | AbstractConnection

        The connection on which this query must be run. Mutually exclusive with Transactionable.transaction.

        Can be used to ensure that a query is run on the same connection as a previous query, which is useful when configuring session options.

        Specifying this option takes precedence over CLS Transactions. If a transaction is running in the current AsyncLocalStorage context, it will be ignored in favor of the specified connection.

      • Optional fieldMap?: FieldMap

        Map returned fields to arbitrary names for SELECT query type if options.fieldMaps is present.

      • fields: (string | Fn | Literal | IndexField)[]

        The fields to index.

      • Optional include?: Literal | (string | Literal)[]

        Non-key columns to be added to the lead level of the nonclustered index.

      • Optional instance?: Model<any, any>

        A sequelize instance used to build the return instance

      • Optional logging?: false | ((sql, timing?) => void)

        A function that gets executed while running the query to log the sql.

      • Optional mapToModel?: boolean

        Map returned fields to model's fields if options.model or options.instance is present. Mapping will occur before building the model instance.

      • Optional msg?: string

        The message to display if the unique constraint is violated.

      • Optional name?: string

        The name of the index. Defaults to model name + _ + fields concatenated

      • Optional nest?: boolean

        If true, transforms objects with . separated property names into nested objects using dottie.js. For example { 'user.username': 'john' } becomes { user: { username: 'john' }}. When nest is true, the query type is assumed to be 'SELECT', unless otherwise specified

        Default

        false
        
      • Optional operator?: string

        Index operator type. Postgres only

      • Optional parser?: null | string

        For FULLTEXT columns set your parser

      • Optional plain?: boolean

        Sets the query type to SELECT and return a single row

      • Optional prefix?: string

        Prefix to append to the index name.

      • Optional raw?: boolean

        If true, sequelize will not try to format the results of the query, or build an instance of a model from the result

      • Optional replacements?: {
            [key: string]: unknown;
        }

        Only named replacements are allowed in query interface methods.

        • [key: string]: unknown
      • Optional retry?: Options
      • Optional supportsSearchPath?: boolean

        If false do not prepend the query with the search_path (Postgres only)

      • Optional transaction?: null | Transaction

        The transaction in which this query must be run. Mutually exclusive with Transactionable.connection.

        If the Sequelize disableClsTransactions option has not been set to true, and a transaction is running in the current AsyncLocalStorage context, that transaction will be used, unless null or another Transaction is manually specified here.

      • Optional type?: IndexType

        Index type. Only used by mysql. One of UNIQUE, FULLTEXT and SPATIAL

      • Optional unique?: boolean

        Should the index by unique? Can also be triggered by setting type to UNIQUE

        Default

        false
        
      • Optional useMaster?: boolean

        Force the query to use the write pool, regardless of the query type.

        Default

        false
        
      • Optional using?: string

        The method to create the index by (USING statement in SQL). BTREE and HASH are supported by mysql and postgres. Postgres additionally supports GIST, SPGIST, BRIN and GIN.

      • Optional where?: WhereOptions

        Optional where parameter for index. Can be used to limit the index to certain rows.

    • Optional rawTablename: string

    Returns Promise<void>

  • Escape an identifier (e.g. a table or attribute name). If force is true, the identifier will be quoted even if the quoteIdentifiers option is false.

    Parameters

    • identifier: string
    • Optional force: boolean

    Returns string