QueryInterface
直接子類別
Sequelize 用來與所有資料庫溝通的介面
方法摘要
公開方法 | ||
public |
為資料表新增一個欄位 |
|
public |
async addConstraint(tableName: string, options: object): Promise 為資料表新增一個約束條件 |
|
public |
async addIndex(tableName: string | object, attributes: Array, options: object, rawTablename: string): Promise 為欄位新增一個索引 |
|
public |
從資料表中刪除多筆記錄 |
|
public |
將多筆記錄插入到資料表中 |
|
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 |
使用給定的屬性集建立一個資料表 |
|
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 |
從資料庫中刪除一個資料表 |
|
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
});
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 |
|
約束條件的名稱。如果未指定,sequelize 會根據約束條件類型、表格和欄位名稱自動建立具名約束條件 |
options.defaultValue | string |
|
預設約束條件的值 |
options.where | object |
|
CHECK 約束條件的 Where 子句/表達式 |
options.references | object |
|
指定目標表格、欄位名稱的物件,以建立外鍵約束條件 |
options.references.table | string |
|
目標表格名稱 |
options.references.field | string |
|
目標欄位名稱 |
options.references.fields | string |
|
複合主鍵的目標欄位名稱。必須符合 options.fields 中欄位的順序。 |
options.deferrable | string |
|
設定約束條件為延遲或立即檢查。請參閱 Sequelize.Deferrable。僅限 PostgreSQL |
範例
queryInterface.addConstraint('Users', {
fields: ['email'],
type: 'unique',
name: 'custom_unique_constraint_name'
});
queryInterface.addConstraint('Users', {
fields: ['roles'],
type: 'check',
where: {
roles: ['user', 'admin', 'moderator', 'guest']
}
});
queryInterface.addConstraint('Users', {
fields: ['roles'],
type: 'default',
defaultValue: 'guest'
});
queryInterface.addConstraint('Users', {
fields: ['username'],
type: 'primary key',
name: 'custom_primary_constraint_name'
});
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'
});
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 |
|
請改用 options.fields,要新增索引的屬性清單 |
options | object | 索引選項 |
|
options.fields | Array | 要新增索引的屬性清單 |
|
options.concurrently | boolean |
|
傳遞 CONCURRENT,以便在建立索引時執行其他操作 |
options.unique | boolean |
|
建立唯一索引 |
options.using | string |
|
適用於 GIN 索引 |
options.operator | string |
|
索引運算子 |
options.type | string |
|
索引類型,可用的選項為 UNIQUE|FULLTEXT|SPATIAL |
options.name | string |
|
索引的名稱。預設值為 <table><attr1><attr2> |
options.where | object |
|
索引的 Where 條件,適用於部分索引 |
rawTablename | string |
|
表格名稱,這只是為了向後相容性 |
public async bulkDelete(tableName: string, where: object, options: object, model: Model): Promise source
從資料表中刪除多筆記錄
參數
名稱 | 類型 | 屬性 | 描述 |
tableName | string | 要從中刪除記錄的表格名稱 |
|
where | object | 尋找要刪除記錄的 where 條件 |
|
options | object |
|
options |
options.truncate | boolean |
|
使用 truncate table 命令 |
options.cascade | boolean |
|
僅與 TRUNCATE 搭配使用。截斷所有具有參照已命名表格的外鍵參考的表格,或任何因 CASCADE 而新增至群組的表格。 |
options.restartIdentity | boolean |
|
僅與 TRUNCATE 搭配使用。自動重新啟動截斷表格欄位擁有的序列。 |
model | 模型 |
|
模型 |
public async bulkInsert(tableName: string, records: Array, options: object, attributes: object): Promise source
將多筆記錄插入到資料表中
範例
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
更新資料表中的多筆記錄
範例
queryInterface.bulkUpdate('roles', {
label: 'admin',
}, {
userType: 3,
},
);
public async changeColumn(tableName: string, attributeName: string, dataTypeOrOptions: object, options: object): * source
變更欄位定義
回傳
* |
public async createDatabase(database: string, options: object): Promise source
建立一個資料庫
參數
名稱 | 類型 | 屬性 | 描述 |
database | string | 要建立的資料庫名稱 |
|
options | object |
|
查詢選項 |
options.charset | string |
|
資料庫預設字元集,僅限 MYSQL |
options.collate | string |
|
資料庫預設排序規則 |
options.encoding | string |
|
資料庫預設字元集,僅限 PostgreSQL |
options.ctype | string |
|
資料庫字元分類,僅限 PostgreSQL |
options.template | string |
|
用來建立新資料庫的範本名稱,僅限 PostgreSQL |
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 |
|
查詢選項 |
options.force | boolean | 如果 force 為 true,則會替換任何具有相同參數的現有函式。對於 postgres,這表示使用 |
|
options.variables | Array<object> | 已宣告的變數列表。每個變數都應該是一個物件,其中包含字串欄位 |
範例
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 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
}
)
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
}
}
public async dropAllSchemas(options: object): Promise source
刪除所有 schema
參數
名稱 | 類型 | 屬性 | 描述 |
options | object |
|
查詢選項 |
public async dropFunction(functionName: string, params: Array, options: object): Promise source
刪除一個 SQL 函數
範例
queryInterface.dropFunction(
'someFunction',
[
{type: 'varchar', name: 'param1', direction: 'IN'},
{type: 'integer', name: 'param2', direction: 'INOUT'}
]
);
public async getForeignKeyReferencesForTable(tableName: string, options: object): * source
取得資料表的外鍵參考詳細資訊
這些詳細資訊包含 constraintSchema、constraintName、constraintCatalog tableCatalog、tableSchema、tableName、columnName、referencedTableCatalog、referencedTableCatalog、referencedTableSchema、referencedTableName、referencedColumnName。提醒:如果它是 sqlite,則不會傳回約束資訊。
回傳
* |
public async getForeignKeysForTables(tableNames: string[], options: object): Promise source
傳回請求資料表的所有外鍵約束
公開 非同步 removeIndex(tableName: 字串, indexNameOrAttributes: 字串 | 字串[], options: 物件): Promise 來源
從表格中移除已存在的索引
公開 非同步 renameColumn(tableName: 字串, attrNameBefore: 字串, attrNameAfter: 字串, options: 物件): Promise 來源
重新命名欄位
公開 非同步 renameFunction(oldFunctionName: 字串, params: 陣列, newFunctionName: 字串, options: 物件): Promise 來源
重新命名 SQL 函數
範例
queryInterface.renameFunction(
'fooFunction',
[
{type: 'varchar', name: 'param1', direction: 'IN'},
{type: 'integer', name: 'param2', direction: 'INOUT'}
],
'barFunction'
);
公開 非同步 tableExists(tableName: TableName, options: QueryOptions): Promise<布林值> 來源
回傳一個 Promise,如果表格存在於資料庫中則解析為 true,否則為 false。
參數
名稱 | 類型 | 屬性 | 描述 |
tableName | TableName | 資料表的名稱 |
|
options | QueryOptions | 查詢選項 |