WOrmReladb

WOrmReladb

new WOrmReladb(optopt) → {Object}

Description:
  • 操作關聯式資料庫

    注意: 各model內id欄位不是主鍵(primary key)時需要強制更改成為主鍵,否則sequelize無法匯入

Source:
Parameters:
Name Type Attributes Default Description
opt Object <optional>
{}

輸入設定物件,預設{}

Properties
Name Type Attributes Default Description
url String <optional>
'mssql://username:password@localhost:1433'

輸入連接資料庫字串,資料庫可選'mssql'、'sqlite'、'mysql'、'mariadb'、'postgres',預設'mssql://username:password@localhost:1433'

storage String <optional>
'./worm.db'

輸入為sqlite時,資料庫檔案所在位置字串,預設'./worm.db'

useEncryption Boolean <optional>
false

輸入為sqlite時是否使用加密套件[@journeyapps/sqlcipher],型別為布林值,預設false,若是的話因依賴預設不安裝,得自行人工安裝

db String <optional>
'worm'

輸入使用資料庫名稱字串,預設'worm'

cl String <optional>
'test'

輸入使用資料表名稱字串,預設'test'

fdModels String <optional>
'./models'

輸入資料表設定檔所在資料夾字串,預設'./models'

modelType String <optional>
'js'

輸入資料表設定檔類型字串,可有'js'、'json',預設'js'

logging Boolean <optional>
false

輸入是否輸出實際執行的sql指令布林值,預設false

pk String <optional>
'id'

輸入數據主鍵字串,預設'id'

autoGenPk Boolean <optional>
true

輸入若數據pk(id)欄位沒給時則自動給予隨機uuid,型別為布林值,預設true

useStable Boolean <optional>
true

輸入是否使用穩定模式,使用佇列管理同時只能進行一種操作故會犧牲效能,sqlite需開啟穩定模式才不會有非預期錯誤,型別為布林值,預設true

Returns:

回傳操作資料庫物件,各事件功能詳見說明

Type
Object

Methods

(async, static) closeSequelize() → {Promise}

Description:
  • 關閉sequelize

Source:
Returns:

回傳Promise,resolve代表關閉成功,reject回傳錯誤訊息

Type
Promise

(async, static) createStorage() → {Promise}

Description:
  • 創建sqlite資料庫檔案

Source:
Returns:

回傳Promise,resolve回傳創建結果,reject回傳錯誤訊息

Type
Promise

(async, static) del(data, optionopt) → {Promise}

Description:
  • 刪除數據

    註: 回傳陣列恆與輸入等長,各筆之n與nDeleted皆為主鍵命中筆數,值為0或1,未命中為0且屬正常結果 註: 判斷某筆是否真的被刪除一律以nDeleted為準 註: 未帶有效主鍵者為該筆之輸入問題,回ok為0與err且不送查詢條件(以免無效值被轉為null而誤中其他數據), 不中斷其餘筆數,以此與[主鍵未命中]之ok為1區辨 註: 本函數不受opt.autoGenPk影響,於任一設定下皆不補值

Source:
Parameters:
Name Type Attributes Default Description
data Object | Array

輸入數據物件或陣列,會查找各數據的opt.pk值,有存在者就刪除

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

Returns:

回傳Promise,resolve回傳與輸入等長之刪除結果陣列[{n,nDeleted,ok}],reject回傳錯誤訊息

Type
Promise

(async, static) delAll(findopt, optionopt) → {Promise}

Description:
  • 依條件刪除多筆數據,需與del分開,避免未傳數據導致直接刪除全表

    註: n與nDeleted皆為實際刪除筆數,兩者恆相等,n不得取全表筆數 註: find未給或為空物件時刪除全部數據;條件無命中時回{n:0,nDeleted:0,ok:1},不視為錯誤

Source:
Parameters:
Name Type Attributes Default Description
find Object <optional>
{}

輸入刪除條件物件,不給予find則代表刪除全部數據

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

Returns:

回傳Promise,resolve回傳刪除結果{n,nDeleted,ok},reject回傳錯誤訊息

Type
Promise

(static) genModelsByDB(optionopt) → {Promise}

Description:
  • 由指定資料庫生成各資料表的models資料

    目前僅能產生js檔格式,且mssql欄位為nvarchar(MAX)無法自動轉成TEXT格式

    include from: w-auto-sequelize

Source:
Parameters:
Name Type Attributes Default Description
option Object <optional>
{}

輸入設定物件,預設{}

Properties
Name Type Attributes Default Description
storage String <optional>
'./worm.db'

輸入sqlite資料庫檔案位置字串,預設'./worm.db'

db String <optional>
'worm'

輸入資料庫名稱字串,預設'worm'

username String <optional>
'username'

輸入使用者名稱字串,預設'username'

password String <optional>
'password'

輸入密碼字串,預設'password'

dialect String <optional>
'mssql'

輸入資料庫種類字串,預設'mssql',可選'mssql', 'sqlite', 'mysql', 'mariadb', 'postgres'

fdModels String <optional>
'./models'

輸入models儲存的資料夾名稱字串,預設'./models'

host String <optional>
'localhost'

輸入連線主機host位址字串,預設'localhost'

port Integer <optional>
1433

輸入連線主機port整數,預設1433

Returns:

回傳Promise,resolve回傳產生的models資料,reject回傳錯誤訊息

Type
Promise

(static) genModelsByTabs(fdopt, tabsopt, optopt)

Description:
  • 由資料表物件生成各資料表的models資料

Source:
Parameters:
Name Type Attributes Default Description
fd String <optional>
'./models'

輸入models儲存的資料夾名稱字串,預設'./models'

tabs Object <optional>
{}

輸入各資料表物件,預設{}

opt Object <optional>
{}

輸入設定物件,預設{}

Properties
Name Type Attributes Default Description
type String <optional>
'js'

輸入資料表類型字串,預設'js'

(async, static) genTransaction() → {Promise}

Description:
  • 產生交易transaction狀態物件,當使用transaction時資料庫會上鎖,只能供調用的連線操作處理

Source:
Returns:

回傳Promise,resolve回傳交易transaction物件,reject回傳錯誤訊息

Type
Promise

(async, static) initSequelize(syncopt) → {Promise}

Description:
  • 初始化sequelize

Source:
Parameters:
Name Type Attributes Default Description
sync Boolean <optional>
false

輸入當importModels匯入models時是否使用同步方式,將models資料變更至資料庫當中,此功能提供給createStorage之用

Returns:

回傳Promise,resolve代表關閉成功,reject回傳錯誤訊息

Type
Promise

(async, static) insert(data, optionopt) → {Promise}

Description:
  • 插入數據,僅於主鍵不存在時寫入,已存在者跳過且不覆寫

    由資料庫於主鍵之唯一約束上原子完成[檢查主鍵不存在]與[寫入],併發時同一主鍵僅有一次成功

    註: n為輸入筆數即本次嘗試插入之基準,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果 註: 同批含重複主鍵時僅首筆計入nInserted,其餘視為已存在 註: opt.autoGenPk為true(預設)時未帶有效主鍵者自動產生,為false時未帶有效主鍵即reject且同批皆不寫入 註: option.returnList開啟時改回與輸入等長且保序之逐筆結果陣列[{n,nInserted,ok}], 聚合計數只答得出[有幾筆是新的]而答不出[是哪幾筆],而後者正是去重之產出物; 逐筆元素之n與ok恆為1,資訊由nInserted承載,不變式為 陣列長度等於輸入筆數且filter(v => v.nInserted === 1).length等於聚合模式之nInserted

Source:
Parameters:
Name Type Attributes Default Description
data Object | Array

輸入數據物件或陣列

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

returnList Boolean <optional>
false

輸入是否改回逐筆結果陣列布林值,預設false

Returns:

回傳Promise,resolve回傳插入結果{n,nInserted,ok},或於returnList開啟時回傳[{n,nInserted,ok}],reject回傳錯誤訊息

Type
Promise

(async, static) insertBulk(data, optionopt) → {Promise}

Description:
  • 批次插入數據,全批視為一個單位:全部插入成功,或一筆都不寫入

    本函數非insert之加速版,兩者衝突政策不同:insert於主鍵已存在時跳過該筆且整批ok為1, 本函數則整批reject且不寫入任何一筆,同批含重複主鍵者亦視為衝突。 確無衝突時兩者之可觀察結果完全相同,差異僅於有衝突時顯現。

    註: n為輸入筆數即本次嘗試插入之基準;nInserted於成功時恆等於n,雖無額外資訊仍保留, 令呼叫端得與insert共用同一段結果處理程式碼 註: 不提供逐筆結果,故不出現ok為0與err;需要逐筆處置者改用insert 註: opt.autoGenPk為true(預設)時未帶有效主鍵者自動產生,為false時未帶有效主鍵即reject且同批皆不寫入 註: [全有全無]為獨立於T7之額外要求——T7僅保證每筆之[檢查主鍵不存在]與[寫入]為原子,不保證整批。 mssql因綁定參數數量上限會由驅動層自動拆為多語句送出,實測1000筆之批次於末筆撞主鍵時已有946筆落盤, 故一律以交易包覆並於失敗時回滾;呼叫端已給transaction時改以巢狀交易(SAVEPOINT)包覆, 令回滾範圍限於本次呼叫而不影響呼叫端交易內先前之寫入

Source:
Parameters:
Name Type Attributes Default Description
data Object | Array

輸入數據物件或陣列

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

Returns:

回傳Promise,resolve回傳插入結果{n,nInserted,ok},reject回傳錯誤訊息

Type
Promise

(async, static) save(data, optionopt) → {Promise}

Description:
  • 儲存數據,以主鍵為準更新既有數據,未給之欄位保留;主鍵不存在且option.autoInsert為true(預設)時改為插入

    註: 回傳陣列恆與輸入等長,輸入單一物件亦回傳長度1之陣列 註: n為主鍵命中筆數,值為0或1,命中(不論內容有無變更)或經插入而產生皆為1;nInserted與nModified恆同時出現 註: [內容相同]之判定基準為將待寫入物件合併進現值後與現值相同,非待寫入物件與現值全等, 故只給部份欄位且該些欄位值皆與現值相同時,合併結果等於現值,nModified為0 註: 本筆失敗不中斷整批,該筆以ok為0並附err回報 註: opt.autoGenPk為true(預設)時未帶有效主鍵者自動產生,為false時未帶有效主鍵即reject且同批皆不寫入

Source:
Parameters:
Name Type Attributes Default Description
data Object | Array

輸入數據物件或陣列

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

autoInsert boolean <optional>
true

輸入是否於儲存時發現原本無數據,則自動改以插入處理,預設為true

Returns:

回傳Promise,resolve回傳與輸入等長之儲存結果陣列[{n,nInserted,nModified,ok}],reject回傳錯誤訊息

Type
Promise

(async, static) select(findopt, optionopt) → {Promise}

Description:
  • 查詢數據

Source:
Parameters:
Name Type Attributes Default Description
find Object <optional>
{}

輸入查詢條件物件

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

Returns:

回傳Promise,resolve回傳數據,reject回傳錯誤訊息

Type
Promise

(async, static) selectByPk(pk, optionopt) → {Promise}

Description:
  • 由主鍵查詢單筆數據,因由資料庫依主鍵索引取值且僅回傳單筆,不需如select提取全部符合數據再處理,故數據量大時效能較佳

    註: 主鍵欄位取自opt.pk,預設為id,支援由呼叫端指定 註: 主鍵未命中或主鍵值無效皆回傳null而不reject,[命中]之判定基準與insert、save、del內對既有數據之認定一致 註: 本函數不得有副作用,故不創建資料表

Source:
Parameters:
Name Type Attributes Default Description
pk String | Number

輸入主鍵值,即數據內opt.pk欄位之值

option Object <optional>
{}

輸入設定物件,預設為{}

Properties
Name Type Attributes Default Description
instance Object <optional>
null

輸入實例instance物件,預設為null

transaction Object <optional>
null

輸入交易(transaction)物件,預設為null

Returns:

回傳Promise,resolve回傳數據物件,若無此主鍵或主鍵值無效則回傳null,reject回傳錯誤訊息

Type
Promise