.leaseConnection()
從資料儲存區租用一個新的連線,以便在同一個連線上執行多個查詢(即,以便在 during
中提供的邏輯可以重複使用資料庫連線)。
await datastore.leaseConnection(during);
_或_
var result = await datastore.leaseConnection(during);
參數 | 類型 | 詳細資訊 | |
---|---|---|---|
1 | during | 一個 程序參數,Sails 將在取得連線並準備就緒後自動呼叫。它將接收在下面的「During」用法表格中指定的參數。 |
參數 | 類型 | 詳細資訊 | |
---|---|---|---|
1 | db | 您新租用的資料庫連線。(有關如何使用此連線的更多資訊,請參閱 .usingConnection() 。) |
請注意,在 Sails 1.1.0 之前的版本中,建議的
.leaseConnection()
用法期望您的 "during" 程式碼在完成時呼叫一個回呼函式 (proceed
)。只要您實際上不在 "during" 程式碼的函式簽名中包含第二個參數,這就不再是必要的。
類型 | 詳細資訊 |
---|---|
從 during 傳回的可選結果資料。換句話說,如果在您的 during 函式中,您執行了 return 'foo'; ,那麼這將會是 'foo' 。 |
名稱 | 類型 | 何時? |
---|---|---|
UsageError | 如果傳入了無效的內容,則拋出此錯誤。 | |
AdapterError | 如果資料庫配接器中發生錯誤,則拋出此錯誤。 | |
Error | 如果發生任何其他意外情況,則拋出此錯誤。 |
有關在 Sails 和 Waterline 中處理錯誤的範例,請參閱 概念 > 模型和 ORM > 錯誤。
從預設資料儲存區租用一個資料庫連線,然後使用它發送兩個查詢,之後再將其釋放回連接池。
var inventory = await sails.getDatastore()
.leaseConnection(async (db)=> {
var location = await Location.findOne({ id: inputs.locationId })
.usingConnection(db);
if (!location) {
let err = new Error('Cannot find location with that id (`'+inputs.locationId+'`)');
err.code = 'E_NO_SUCH_LOCATION';
throw err;
}
// Get all products at the location
var productOfferings = await ProductOffering.find({ location: inputs.locationId })
.populate('productType')
.usingConnection(db);
return productOfferings;
})
.intercept('E_NO_SUCH_LOCATION', 'notFound');
// All done! Whatever we were doing with that database connection worked.
// Now we can proceed with our business.