sails.config.blueprints
這些可配置的設定讓您能夠在 Sails 中配置藍圖 API。有些設定 (例如 sails.config.blueprints.autoWatch
) 控制內建藍圖動作的行為,而其他設定 (例如 sails.config.blueprints.shortcuts
) 則調整隱含藍圖路由的行為,和/或決定 Sails 是否自動綁定某些種類的藍圖路由。
請記住,藍圖動作可以附加到您的自訂路由,無論您是否啟用任何種類的隱含藍圖路由。
屬性 | 類型 | 預設值 | 詳細資訊 |
---|---|---|---|
actions |
false |
是否為應用程式中的每個動作自動產生隱含藍圖 ("影子") 路由。例如,擁有 api/controllers/foo/bar.js 檔案或 api/controllers/FooController.js 中的 bar 函式,將自動將傳入的請求路由到 /foo/bar 到該動作,只要它沒有被自訂路由覆寫。當啟用時,此設定也會將額外的特殊隱含 ("影子") 路由綁定到任何名為 index 的動作,以及應用程式及其每個控制器的相對 "根" URL。例如,api/controllers/foo/index.js 的 /foo 影子路由,或 api/controllers/index.js 的 / 影子路由。 |
|
rest |
true |
是否啟用自動 REST 藍圖?例如:'get /:model/:id?' 'post /:model' 'put /:model/:id' 'delete /:model/:id' 。 |
|
shortcuts |
true |
這些 CRUD 捷徑在開發期間為您提供方便,但您會在生產環境中想要停用它們: '/:model/find/:id?' 、'/:model/create' 、'/:model/update/:id' 和 '/:model/destroy/:id' 。 |
|
prefix |
'' |
所有藍圖路由的可選掛載路徑前綴 (例如 '/api/v2'),包括 rest 、actions 和 shortcuts 。這僅適用於隱含藍圖 ("影子") 路由,而不適用於您的自訂路由。 |
|
restPrefix |
'' |
控制器上所有 REST 藍圖路由的可選掛載路徑前綴,例如 '/api/v2'。(不包括 actions 和 shortcuts 路由。) 這讓您可以利用 REST 藍圖路由,即使您需要命名空間您的 RESTful API 方法。將會與您的 prefix 配置合併,例如 prefix: '/api' 和 restPrefix: '/rest' 。RESTful 動作將在 /api/rest 下可用。 |
|
pluralize |
false | 是否在藍圖路由中使用複數模型名稱,例如 User 模型的 /users 。(這僅適用於藍圖自動路由,不適用於來自 sails.config.routes 的手動路由。) |
屬性 | 類型 | 預設值 | 詳細資訊 |
---|---|---|---|
autoWatch |
true |
是否在 find 和 findOne 藍圖動作中,將請求套接字訂閱為接收關於透過藍圖 API 新建立記錄的通知。 |
|
parseBlueprintOptions |
(請參閱下方) | 提供此函式以覆寫藍圖動作的預設行為 (包括搜尋條件、skip、limit、sort 和 population)。 |
parseBlueprintOptions
每個藍圖動作的核心都包含 Waterline 模型方法呼叫。例如,當為 User
模型執行 find
藍圖時,會執行 User.find()
以檢索一些使用者記錄。傳遞給這些 Waterline 方法的選項由對 parseBlueprintOptions()
的呼叫決定。此方法的預設版本 (可透過 sails.hooks.blueprints.parseBlueprintOptions()
取得) 決定藍圖的預設行為。您可以覆寫您的藍圖配置 (在 config/blueprints.js
中) 中的 parseBlueprintOptions
,以自訂所有藍圖動作的行為,或在每個路由的基礎上自訂單一路由的行為。
parseBlueprintOptions()
方法接受單一引數 (請求物件),並預期傳回 Waterline 查詢選項的字典。(您可以檢閱此類字典的不切實際的擴展範例這裡,但請記住,並非所有鍵都適用於所有藍圖動作。請參閱Sails 程式碼中的原始碼以取得完整詳細資訊)。
新增您自己的 parseBlueprintOptions()
是一個進階概念,建議您先熟悉預設方法程式碼,並將其用作起點。對於藍圖行為的微小修改,最好先在您的覆寫中呼叫預設方法,然後變更傳回的查詢選項
parseBlueprintOptions: function(req) {
// Get the default query options.
var queryOptions = req._sails.hooks.blueprints.parseBlueprintOptions(req);
// If this is the "find" or "populate" blueprint action, and the normal query options
// indicate that the request is attempting to set an exceedingly high `limit` clause,
// then prevent it (we'll say `limit` must not exceed 100).
if (req.options.blueprintAction === 'find' || req.options.blueprintAction === 'populate') {
if (queryOptions.criteria.limit > 100) {
queryOptions.criteria.limit = 100;
}
}
return queryOptions;
}