1. 程式人生 > >sails 資料庫欄位

sails 資料庫欄位

屬性值

概述

模型的屬性是一個模型的基本的資訊。一個Person的模型可能有屬性叫做firstName,lastName,phoneNumber,age,birthDate和emailAddress。

屬性選項

這些選項可以用來執行各種約束條件並且新增特殊的增強功能到我們的模型中。

Type

指定該屬性儲存的資料型別,可以使如下的一種:

    string
    text
    integer
    float
    date
    datetime
    boolean
    binary
    array
    json
    defaultsTo

當建立一條記錄的時候,如果沒有設定任何值,該記錄將會建立一個defaultsTo指定的值:

attributes: {
  phoneNumber: {
  type: 'string',
  defaultsTo: '111-222-3333'
},
orderNumber: {
  type: 'text',
  defaultsTo: function() {
  return uuid.v4();
}
}
}
autoIncrement

設定該屬性是一個自動遞增的關鍵詞。當一條記錄被新增到模型中去,如果該屬性的值沒有指定,它將會根據最近的記錄的值遞增加1.注意:指定autoIncrement的屬性的資料型別應該是type: 'integer'。同時請牢記於心:在不同的資料儲存支援的程度各有不同。比如,MySQL不允許每張表多餘一個的自動遞增列。

attributes: {
  placeInLine: {
  type: 'integer',
  autoIncrement: true
}
}
unique

確保目標屬性沒有出現兩條記錄有同樣的值。這是一個介面卡層的約束條件,所以在大部分情況下這會讓該屬性成為一個唯一的索引在底層資料庫建立的時候。

attributes: {
  username: {
  type: 'string',
  unique: true
}
}
當使用帶有utf8mb4字符集的MySQL,你會需要通過MySQL直接在你的表中新增 size約束條件到合適的列中。否則,因為在MySQL介面卡中type: 'string'會被轉換為varchar(255),所以unique: true的約束條件將會導致一個'index too long'的錯誤: ER_INDEX_COLUMN_TOO_LONG: Index column size too large. The maximum column size is 767 bytes.

primaryKey

使用了這個那麼該記錄的這條屬性就會成為主要關鍵詞。一條模型中只能有一個屬性是primaryKey。注意:除非autoPK設定為false,否則不要去使用該關鍵詞。

attributes: {
  uuid: {
    type: 'string',
    primaryKey: true,
    required: true
  }
}
enum

一個特殊的屬性驗證,也就是隻會儲存那些匹配白名單中的那些值。

attributes: {
  state: {
    type: 'string',
    enum: ['pending', 'approved', 'denied']
  }
}
size

如果在介面卡中支援,那麼可以用來定義屬性的長度,比如在MySQL,size可以指定為一個數字來建立一個帶有SQL 資料型別varchar(n)的一列。

attributes: {
  name: {
    type: 'string',
    size: 24
  }
}
columnName

在一個屬性內部定義中,你可以指定一個columnName來強制讓Sails或Waterline來儲存該屬性在一個配置的集合中一個指定列的資料。我們知道這在SQL-特有中不是必須的--它也可以工作在MongoDb等。

當columnName屬性主要設計為與已經存在的或者是傳統的資料庫工作,它也可以在你的資料庫和其他應用共共享的時候這種情況下有用,或者你沒有許可權改變schema。

為了儲存或者獲取你的模型的numberOfWheels屬性到/從number_of_round_rotating_things列中:

// An attribute in one of your models:
// ...
numberOfWheels: {
    type: 'integer',
    columnName: 'number_of_round_rotating_things'
}
// ...
接下去是有更形象的例子。 讓我們假設你有一個User模型在你的Sails app中,如下:

// api/models/User.js
module.exports = {
connection: 'shinyNewMySQLDatabase',
attributes: {
name: {
type: 'string'
},
password: {
type: 'string'
},
email: {
type: 'email',
unique: true
}
}
};
所有的一切都工作得很好除了使用一個已經存在的MySQL資料庫放在一個你想存放你想要的使用者的地方:

// config/connections.js
module.exports = {
// ...

// Existing users are in here!
rustyOldMySQLDatabase: {
adapter: 'sails-mysql',
user: 'bofh',
host: 'db.eleven.sameness.foo',
password: 'Gh19R!?had9gzQ#Q#Q#%AdsghaDABAMR>##G<[email protected])$([email protected](',
database: 'jonas'
},
// ...
};
讓我們假設這裡有一張叫做our_users的表放在舊的MySQL資料庫,如下:

the_primary_key    email_address    full_name    seriously_encrypted_password
7    [email protected]    Mike McNeil    ranchdressing
14    [email protected]    Nick Crumrine    thousandisland
為了從Sails中使用它,你需要改變你的User模型成這樣:

// api/models/User.js
module.exports = {
connection: 'rustyOldMySQLDatabase',
tableName: 'our_users',
attributes: {
id: {
type: 'integer',
unique: true,
primaryKey: true,
columnName: 'the_primary_key'
},
name: {
type: 'string',
columnName: 'full_name'
},
password: {
type: 'string',
columnName: 'seriously_encrypted_password'
},
email: {
type: 'email',
unique: true,
columnName: 'email_address'
}
}
};
你也許注意到了我們在這個例子中也使用了tableName屬性。這允許我們去控制將會用於放置我們的資料的表的名字。