指南 參考 原始碼
public class | source

QueryInterface

Sequelize 用來與所有資料庫溝通的介面

方法摘要

公開方法
public

async addColumn(table: string, key: string, attribute: object, options: object): Promise

為資料表新增一個欄位

public

async addConstraint(tableName: string, options: object): Promise

為資料表新增一個約束條件

public

async addIndex(tableName: string | object, attributes: Array, options: object, rawTablename: string): Promise

為欄位新增一個索引

public

async bulkDelete(tableName: string, where: object, options: object, model: Model): Promise

從資料表中刪除多筆記錄

public

async bulkInsert(tableName: string, records: Array, options: object, attributes: object): Promise

將多筆記錄插入到資料表中

public

async bulkUpdate(tableName: string, values: object, identifier: object, options: object, attributes: object): Promise

更新資料表中的多筆記錄

public

async changeColumn(tableName: string, attributeName: string, dataTypeOrOptions: object, options: object): *

變更欄位定義

public

async createDatabase(database: string, options: object): Promise

建立一個資料庫

public

async createFunction(functionName: string, params: Array, returnType: string, language: string, body: string, optionsArray: Array, options: object): Promise

建立一個 SQL 函數

public

async createSchema(schema: string, options: object): Promise

建立一個 schema

public

async createTable(tableName: string, attributes: object, options: object, model: Model): Promise

使用給定的屬性集建立一個資料表

public

async describeTable(tableName: string, options: object): Promise<object>

描述資料表結構

public

async dropAllSchemas(options: object): Promise

刪除所有 schema

public

async dropAllTables(options: object): Promise

從資料庫中刪除所有資料表

public

async dropDatabase(database: string, options: object): Promise

刪除一個資料庫

public

async dropFunction(functionName: string, params: Array, options: object): Promise

刪除一個 SQL 函數

public

async dropSchema(schema: string, options: object): Promise

刪除一個 schema

public

async dropTable(tableName: string, options: object): Promise

從資料庫中刪除一個資料表

public

async getForeignKeyReferencesForTable(tableName: string, options: object): *

取得資料表的外鍵參考詳細資訊

public

async getForeignKeysForTables(tableNames: string[], options: object): Promise

傳回請求資料表的所有外鍵約束

public

quoteIdentifier(identifier: string, force: boolean): string

使用 "." 分隔識別符列表,並引用每個部分

public

quoteIdentifiers(identifiers: string): string

使用 "." 分隔識別符列表,並引用每個部分。

public

async removeColumn(tableName: string, attributeName: string, options: object): *

從資料表中移除一個欄位

public

async removeConstraint(tableName: string, constraintName: string, options: object): *

從表格中移除約束條件

public

async removeIndex(tableName: string, indexNameOrAttributes: string | string[], options: object): Promise

從表格中移除已存在的索引

public

async renameColumn(tableName: string, attrNameBefore: string, attrNameAfter: string, options: object): Promise

重新命名欄位

public

async renameFunction(oldFunctionName: string, params: Array, newFunctionName: string, options: object): Promise

重新命名 SQL 函數

public

async renameTable(before: string, after: string, options: object): Promise

重新命名表格

public

async showAllSchemas(options: object): Promise<Array>

顯示所有綱要

public

async tableExists(tableName: TableName, options: QueryOptions): Promise<boolean>

回傳一個 Promise,如果表格存在於資料庫中則解析為 true,否則為 false。

public

async upsert(tableName: string, insertValues: object, updateValues: object, where: object, options: object): Promise<boolean, ?number>

Upsert(更新或插入)

公開方法

public async addColumn(table: string, key: string, attribute: object, options: object): Promise source

為資料表新增一個欄位

queryInterface.addColumn('tableA', 'columnC', Sequelize.STRING, {
   after: 'columnB' // after option is only supported by MySQL
});

參數

名稱類型屬性描述
table string

要新增欄位的表格

key string

欄位名稱

attribute object

屬性定義

options object
  • optional

查詢選項

回傳

Promise

public async addConstraint(tableName: string, options: object): Promise source

為資料表新增一個約束條件

可用的約束條件

  • UNIQUE
  • DEFAULT (僅限 MSSQL)
  • CHECK (MySQL - 資料庫引擎會忽略)
  • FOREIGN KEY
  • PRIMARY KEY

參數

名稱類型屬性描述
tableName string

您要新增約束條件的表格名稱

options object

一個定義約束條件名稱、類型等的物件

options.type string

約束條件的類型。可用約束條件中的其中一個值(不區分大小寫)

options.fields Array

套用約束條件的欄位名稱陣列

options.name string
  • optional

約束條件的名稱。如果未指定,sequelize 會根據約束條件類型、表格和欄位名稱自動建立具名約束條件

options.defaultValue string
  • optional

預設約束條件的值

options.where object
  • optional

CHECK 約束條件的 Where 子句/表達式

options.references object
  • optional

指定目標表格、欄位名稱的物件,以建立外鍵約束條件

options.references.table string
  • optional

目標表格名稱

options.references.field string
  • optional

目標欄位名稱

options.references.fields string
  • optional

複合主鍵的目標欄位名稱。必須符合 options.fields 中欄位的順序。

options.deferrable string
  • optional

設定約束條件為延遲或立即檢查。請參閱 Sequelize.Deferrable。僅限 PostgreSQL

回傳

Promise

範例

UNIQUE
queryInterface.addConstraint('Users', {
  fields: ['email'],
  type: 'unique',
  name: 'custom_unique_constraint_name'
});
CHECK
queryInterface.addConstraint('Users', {
  fields: ['roles'],
  type: 'check',
  where: {
     roles: ['user', 'admin', 'moderator', 'guest']
  }
});
Default - 僅限 MSSQL
queryInterface.addConstraint('Users', {
   fields: ['roles'],
   type: 'default',
   defaultValue: 'guest'
});
Primary Key
queryInterface.addConstraint('Users', {
   fields: ['username'],
   type: 'primary key',
   name: 'custom_primary_constraint_name'
});
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'
});
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'
});

public async addIndex(tableName: string | object, attributes: Array, options: object, rawTablename: string): Promise source

為欄位新增一個索引

參數

名稱類型屬性描述
tableName string | object

要在其上新增索引的表格名稱,可以是包含綱要的物件

attributes Array
  • optional

請改用 options.fields,要新增索引的屬性清單

options object

索引選項

options.fields Array

要新增索引的屬性清單

options.concurrently boolean
  • optional

傳遞 CONCURRENT,以便在建立索引時執行其他操作

options.unique boolean
  • optional

建立唯一索引

options.using string
  • optional

適用於 GIN 索引

options.operator string
  • optional

索引運算子

options.type string
  • optional

索引類型,可用的選項為 UNIQUE|FULLTEXT|SPATIAL

options.name string
  • optional

索引的名稱。預設值為 <table><attr1><attr2>

options.where object
  • optional

索引的 Where 條件,適用於部分索引

rawTablename string
  • optional

表格名稱,這只是為了向後相容性

回傳

Promise

public async bulkDelete(tableName: string, where: object, options: object, model: Model): Promise source

從資料表中刪除多筆記錄

參數

名稱類型屬性描述
tableName string

要從中刪除記錄的表格名稱

where object

尋找要刪除記錄的 where 條件

options object
  • optional

options

options.truncate boolean
  • optional

使用 truncate table 命令

options.cascade boolean
  • optional
  • 預設值:false

僅與 TRUNCATE 搭配使用。截斷所有具有參照已命名表格的外鍵參考的表格,或任何因 CASCADE 而新增至群組的表格。

options.restartIdentity boolean
  • optional
  • 預設值:false

僅與 TRUNCATE 搭配使用。自動重新啟動截斷表格欄位擁有的序列。

model 模型
  • optional

模型

回傳

Promise

public async bulkInsert(tableName: string, records: Array, options: object, attributes: object): Promise source

將多筆記錄插入到資料表中

參數

名稱類型屬性描述
tableName string

要將記錄插入的表格名稱

records Array

要插入的記錄清單

options object

各種選項,請參閱 Model.bulkCreate 選項

attributes object

各種按欄位名稱對應的屬性

回傳

Promise

範例

queryInterface.bulkInsert('roles', [{
   label: 'user',
   createdAt: new Date(),
   updatedAt: new Date()
 }, {
   label: 'admin',
   createdAt: new Date(),
   updatedAt: new Date()
 }]);

public async bulkUpdate(tableName: string, values: object, identifier: object, options: object, attributes: object): Promise source

更新資料表中的多筆記錄

參數

名稱類型屬性描述
tableName string

要更新的表格名稱

values object

要插入的值,對應到欄位名稱

identifier object

包含條件的雜湊表,或是作為整數的 ID,或是包含條件的字串

options object
  • optional

各種選項,請參閱 Model.bulkCreate 選項

attributes object
  • optional

如果 SQL 資料庫支援,則回傳物件的屬性

回傳

Promise

範例

queryInterface.bulkUpdate('roles', {
    label: 'admin',
  }, {
    userType: 3,
  },
);

public async changeColumn(tableName: string, attributeName: string, dataTypeOrOptions: object, options: object): * source

變更欄位定義

參數

名稱類型屬性描述
tableName string

要變更的表格名稱

attributeName string

欄位名稱

dataTypeOrOptions object

新欄位的屬性定義

options object
  • optional

查詢選項

回傳

*

public async createDatabase(database: string, options: object): Promise source

建立一個資料庫

參數

名稱類型屬性描述
database string

要建立的資料庫名稱

options object
  • optional

查詢選項

options.charset string
  • optional

資料庫預設字元集,僅限 MYSQL

options.collate string
  • optional

資料庫預設排序規則

options.encoding string
  • optional

資料庫預設字元集,僅限 PostgreSQL

options.ctype string
  • optional

資料庫字元分類,僅限 PostgreSQL

options.template string
  • optional

用來建立新資料庫的範本名稱,僅限 PostgreSQL

回傳

Promise

public async createFunction(functionName: string, params: Array, returnType: string, language: string, body: string, optionsArray: Array, options: object): Promise source

建立一個 SQL 函數

參數

名稱類型屬性描述
functionName string

要建立的 SQL 函式名稱

params Array

為 SQL 函式宣告的參數列表

returnType string

函式傳回值的 SQL 類型

language string

實作函式的語言名稱

body string

函式的原始碼

optionsArray Array

建立函式的額外選項

options object
  • optional

查詢選項

options.force boolean

如果 force 為 true,則會替換任何具有相同參數的現有函式。對於 postgres,這表示使用 CREATE OR REPLACE FUNCTION 而不是 CREATE FUNCTION。預設為 false

options.variables Array<object>

已宣告的變數列表。每個變數都應該是一個物件,其中包含字串欄位 typename,並且可以選擇性地包含 default 欄位。

回傳

Promise

範例

queryInterface.createFunction(
  'someFunction',
  [
    {type: 'integer', name: 'param', direction: 'IN'}
  ],
  'integer',
  'plpgsql',
  'RETURN param + 1;',
  [
    'IMMUTABLE',
    'LEAKPROOF'
  ],
  {
   variables:
     [
       {type: 'integer', name: 'myVar', default: 100}
     ],
     force: true
  };
);

public async createSchema(schema: string, options: object): Promise source

建立一個 schema

參數

名稱類型屬性描述
schema string

要建立的 Schema 名稱

options object
  • optional

查詢選項

回傳

Promise

public async createTable(tableName: string, attributes: object, options: object, model: Model): Promise source

使用給定的屬性集建立一個資料表

queryInterface.createTable(
  'nameOfTheNewTable',
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    createdAt: {
      type: Sequelize.DATE
    },
    updatedAt: {
      type: Sequelize.DATE
    },
    attr1: Sequelize.STRING,
    attr2: Sequelize.INTEGER,
    attr3: {
      type: Sequelize.BOOLEAN,
      defaultValue: false,
      allowNull: false
    },
    //foreign key usage
    attr4: {
      type: Sequelize.INTEGER,
      references: {
        model: 'another_table_name',
        key: 'id'
      },
      onUpdate: 'cascade',
      onDelete: 'cascade'
    }
  },
  {
    engine: 'MYISAM',    // default: 'InnoDB'
    charset: 'latin1',   // default: null
    schema: 'public',    // default: public, PostgreSQL only.
    comment: 'my table', // comment for table
    collate: 'latin1_danish_ci' // collation, MYSQL only
  }
)

參數

名稱類型屬性描述
tableName string

要建立的表格名稱

attributes object

代表要建立的表格屬性列表的物件

options object
  • optional

建立表格和查詢選項

model 模型
  • optional

模型類別

回傳

Promise

public async describeTable(tableName: string, options: object): Promise<object> source

描述資料表結構

此方法會傳回包含表格中所有屬性相關資訊的雜湊表陣列。

{
   name: {
     type:         'VARCHAR(255)', // this will be 'CHARACTER VARYING' for pg!
     allowNull:    true,
     defaultValue: null
   },
   isBetaMember: {
     type:         'TINYINT(1)', // this will be 'BOOLEAN' for pg!
     allowNull:    false,
     defaultValue: false
   }
}

參數

名稱類型屬性描述
tableName string

表格名稱

options object
  • optional

查詢選項

回傳

Promise<object>

public async dropAllSchemas(options: object): Promise source

刪除所有 schema

參數

名稱類型屬性描述
options object
  • optional

查詢選項

回傳

Promise

public async dropAllTables(options: object): Promise source

從資料庫中刪除所有資料表

參數

名稱類型屬性描述
options object
  • optional

查詢選項

options.skip Array
  • optional

要跳過的表格列表

回傳

Promise

public async dropDatabase(database: string, options: object): Promise source

刪除一個資料庫

參數

名稱類型屬性描述
database string

要刪除的資料庫名稱

options object
  • optional

查詢選項

回傳

Promise

public async dropFunction(functionName: string, params: Array, options: object): Promise source

刪除一個 SQL 函數

參數

名稱類型屬性描述
functionName string

要刪除的 SQL 函式名稱

params Array

為 SQL 函式宣告的參數列表

options object
  • optional

查詢選項

回傳

Promise

範例

queryInterface.dropFunction(
  'someFunction',
  [
    {type: 'varchar', name: 'param1', direction: 'IN'},
    {type: 'integer', name: 'param2', direction: 'INOUT'}
  ]
);

public async dropSchema(schema: string, options: object): Promise source

刪除一個 schema

參數

名稱類型屬性描述
schema string

要刪除的 Schema 名稱

options object
  • optional

查詢選項

回傳

Promise

public async dropTable(tableName: string, options: object): Promise source

從資料庫中刪除一個資料表

參數

名稱類型屬性描述
tableName string

要刪除的表格名稱

options object

查詢選項

回傳

Promise

public async getForeignKeyReferencesForTable(tableName: string, options: object): * source

取得資料表的外鍵參考詳細資訊

這些詳細資訊包含 constraintSchema、constraintName、constraintCatalog tableCatalog、tableSchema、tableName、columnName、referencedTableCatalog、referencedTableCatalog、referencedTableSchema、referencedTableName、referencedColumnName。提醒:如果它是 sqlite,則不會傳回約束資訊。

參數

名稱類型屬性描述
tableName string

表格名稱

options object
  • optional

查詢選項

回傳

*

public async getForeignKeysForTables(tableNames: string[], options: object): Promise source

傳回請求資料表的所有外鍵約束

參數

名稱類型屬性描述
tableNames 字串[]

資料表名稱

options object
  • optional

查詢選項

回傳

Promise

公開 quoteIdentifier(identifier: 字串, force: 布林值): 字串 來源

使用 "." 分隔識別符列表,並引用每個部分

參數

名稱類型屬性描述
identifier string
強制 boolean

回傳

string

公開 quoteIdentifiers(identifiers: 字串): 字串 來源

使用 "." 分隔識別符列表,並引用每個部分。

參數

名稱類型屬性描述
識別符 string

回傳

string

公開 非同步 removeColumn(tableName: 字串, attributeName: 字串, options: 物件): * 來源

從資料表中移除一個欄位

參數

名稱類型屬性描述
tableName string

要從中移除欄位的資料表

attributeName string

要移除的欄位名稱

options object
  • optional

查詢選項

回傳

*

公開 非同步 removeConstraint(tableName: 字串, constraintName: 字串, options: 物件): * 來源

從表格中移除約束條件

參數

名稱類型屬性描述
tableName string

要從中刪除約束的資料表名稱

constraintName string

約束名稱

options object

查詢選項

回傳

*

公開 非同步 removeIndex(tableName: 字串, indexNameOrAttributes: 字串 | 字串[], options: 物件): Promise 來源

從表格中移除已存在的索引

參數

名稱類型屬性描述
tableName string

要從中刪除索引的資料表名稱

indexNameOrAttributes 字串 | 字串[]

索引名稱或索引中屬性的清單

options object
  • optional

查詢選項

options.concurrently boolean
  • optional

傳遞 CONCURRENTLY 以便在建立索引時執行其他操作

回傳

Promise

公開 非同步 renameColumn(tableName: 字串, attrNameBefore: 字串, attrNameAfter: 字串, options: 物件): Promise 來源

重新命名欄位

參數

名稱類型屬性描述
tableName string

要重新命名欄位的資料表名稱

attrNameBefore string

目前的欄位名稱

attrNameAfter string

新的欄位名稱

options object
  • optional

查詢選項

回傳

Promise

公開 非同步 renameFunction(oldFunctionName: 字串, params: 陣列, newFunctionName: 字串, options: 物件): Promise 來源

重新命名 SQL 函數

參數

名稱類型屬性描述
oldFunctionName string

目前函式名稱

params Array

為 SQL 函式宣告的參數列表

newFunctionName string

新的函式名稱

options object
  • optional

查詢選項

回傳

Promise

範例

queryInterface.renameFunction(
  'fooFunction',
  [
    {type: 'varchar', name: 'param1', direction: 'IN'},
    {type: 'integer', name: 'param2', direction: 'INOUT'}
  ],
  'barFunction'
);

公開 非同步 renameTable(before: 字串, after: 字串, options: 物件): Promise 來源

重新命名表格

參數

名稱類型屬性描述
before string

目前的資料表名稱

after string

資料表的新名稱

options object
  • optional

查詢選項

回傳

Promise

公開 非同步 showAllSchemas(options: 物件): Promise<陣列> 來源

顯示所有綱要

參數

名稱類型屬性描述
options object
  • optional

查詢選項

回傳

Promise<陣列>

公開 非同步 tableExists(tableName: TableName, options: QueryOptions): Promise<布林值> 來源

回傳一個 Promise,如果表格存在於資料庫中則解析為 true,否則為 false。

參數

名稱類型屬性描述
tableName TableName

資料表的名稱

options QueryOptions

查詢選項

回傳

Promise<布林值>

公開 非同步 upsert(tableName: 字串, insertValues: 物件, updateValues: 物件, where: 物件, options: 物件): Promise<布林值, ?數字> 來源

Upsert(更新或插入)

參數

名稱類型屬性描述
tableName string

要進行 upsert 的資料表

insertValues object

要插入的值,對應到欄位名稱

updateValues object

要更新的值,對應到欄位名稱

where object

where 條件,當 INSERT 失敗時可以用於 UPDATE 部分

options object

查詢選項

回傳

Promise<布林值, ?數字>

解析一個包含 <created, primaryKey> 的陣列