# 模型屬性
> 譯者:[飛龍](https://github.com/wizardforcel)
> 來源:[Model Properties](https://github.com/dresende/node-orm2/wiki/Model-Properties)
模型和一些關聯具有一個或多個屬性,每個屬性有類型以及一些可選設置,你可以自行選擇它們(或使用默認設置)。
## 類型
受支持的類型是:
+ `text`:文本字符串;
+ `number`:浮點數。你可以指定`size`為`2 | 4 | 8`;
+ `integer`:整數。你可以指定`size`為`2 | 4 | 8`;
+ `boolean`:`true`或`false`的值;
+ `date`:日期對象。你可以指定`time`為`true`;
+ `enum`:一個備選列表中的值;
+ `object`:JSON對象;
+ `point`:N維的點(不被廣泛支持);
+ `binary`:二進制數據;
+ `serial`:自增長的整數,用于主鍵。
每個類型都有額外的選項。這個模型定義使用了它們中的絕大多數:
```
var Person = db.define("person", {
name : { type: "text", size: 50 },
surname : { type: "text", defaultValue: "Doe" },
male : { type: "boolean" },
vat : { type: "integer", unique: true },
country : { type: "enum", values: [ "USA", "Canada", "Rest of the World" ] },
birth : { type: "date", time: false }
});
```
所有類型都支持`required`(布爾值),`unique`(布爾值)和`defaultValue`(文本)。文本類型也支持最大尺寸(數值)和`big`(布爾值,用于非常長的字符串)。數值類型是浮點數,支持`size`(數值,字節大小)和`unsigned`(布爾值)。日期類型支持`time`(布爾值)。
**要注意8字節的數值[有其局限性](http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t)。**
如果你打算用默認選項,你可以使用原生類型來指定屬性類型:
```
var Person = db.define("person", {
name : String,
male : Boolean,
vat : Number, // FLOAT
birth : Date,
country : [ "USA", "Canada", "Rest of the World" ],
meta : Object, // JSON
photo : Buffer // binary
});
```
## 將ORM字段映射到不同名稱的數據庫列中
```
var Person = db.define("person", {
name : { type: 'text', mapsTo: 'fullname' }
});
```
ORM屬性`name`映射`person`表的`fullname`列。
## 自定義類型
你可以向ORM添加你自己的類型,像這樣:
```
db.defineType('numberArray', {
datastoreType: function(prop) {
return 'TEXT'
},
// This is optional
valueToProperty: function(value, prop) {
if (Array.isArray(value)) {
return value;
} else {
return value.split(',').map(function (v) {
return Number(v);
});
}
},
// This is also optional
propertyToValue: function(value, prop) {
return value.join(',')
}
});
var LottoTicket = db.define('lotto_ticket', {
numbers: { type: 'numberArray' }
});
```
一些可用的高級自定義類型,能夠讓你像 PostGIS 那樣使用模型。請見[這個 spec](https://github.com/dresende/node-orm2/blob/master/test/integration/property-custom.js) 。