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:
  • 刪除數據

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回傳刪除結果,reject回傳錯誤訊息

Type
Promise

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

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

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回傳刪除結果,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:
  • 插入數據,插入同樣數據會自動產生不同_id,故insert前需自行判斷有無重複

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回傳插入結果,reject回傳錯誤訊息

Type
Promise

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

Description:
  • 儲存數據

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回傳儲存結果,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