# 設置
> 譯者:[飛龍](https://github.com/wizardforcel)
> 來源:[Settings](https://github.com/dresende/node-orm2/wiki/Settings)
設置用于儲存鍵值對。設置對象是`orm`(默認值)上的實例,之后會為每個`db`連接和每個定義過的`Model`建立快照。所以`orm.settings`上的更改只會作用于更改之后建立的連接,而`db.settings`會作用于更改之后定義的模型。
```
var orm = require("orm");
orm.settings.set("some.deep.value", 123);
orm.connect("....", function (err, db) {
// db.settings is a snapshot of the settings at the moment
// of orm.connect(). changes to it don't affect orm.settings
console.log(db.settings.get("some.deep.value")); // 123
console.log(db.settings.get("some.deep")); // { value: 123 }
db.settings.set("other.value", { some: "object" });
console.log(db.settings.get("other.value")); // { some: "object" }
console.log(orm.settings.get("other.value")); // undefined
});
```
默認設置的結構是這樣的:
```
var Settings = {
properties : {
primary_key : "id",
association_key : "{name}_{field}",
required : false
},
instance : {
cache : true,
cacheSaveCheck : true,
autoSave : false,
autoFetch : false,
autoFetchLimit : 1,
cascadeRemove : true,
returnAllErrors : false
},
connection : {
reconnect : true,
pool : false,
debug : false
}
};
```
| 設置 | 描述 |
| --- | --- |
| `properties.primary_key` | 在沒有定義id屬性的模型中,定義主鍵的名稱 |
| `properties.association_key` | 關聯鍵的屬性名稱(例如`user_id`) |
| `properties.required` | 屬性是否擁有默認行為 |
| `instance.cache` | 實例是否應該被緩存 (并不是真的緩存,和單例模式相關) |
| `instance.cacheSaveCheck` | 被緩存的對象是否應該從緩存中返回 (不要修改這個設置,除非你知道自己在做什么) |
| `instance.autoSave` | 如果開啟的話,修改實例的任何屬性時會自動保存 |
| `instance.autoFetch` | 是否需要自動獲取關聯 |
| `instance.autoFetchLimit` | 如果開啟了自動獲取關聯,這個設置是獲取關聯的深度 |
| `instance.cascadeRemove` | 刪除實例時是否要刪除關聯 |
| `instance.returnAllErrors` | 如果開啟,實例保存時會記錄下所有的錯誤并以數組形式返回,而不是遇到第一個錯誤就中止并返回 |
| `connection.reconnect` | 連接失效時是否嘗試重新連接 |
| `connection.pool` | 是否使用驅動帶有的連接池(如果支持的話) |
| `connection.debug` | 向控制臺打印帶顏色的查詢信息 |