4

我创建了基于用户的管理模型:

{
  "name": "Admin",
  "plural": "admins",
  "base": "User",
  "strict": true,
  "properties": {
    "email": {
      "type": "string",
      "mysql": {
        "dataType": "VARCHAR",
        "dataLength": 50
      }
    },
    "password": {
      "type": "string",
      "mysql": {
        "dataType": "VARCHAR",
        "dataLength": 60
      }
    },
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

在 MySQL db 中描述 Admin 表会显示来自 User 模型的额外列:

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| email             | varchar(50)  | YES  |     | NULL    |                |
| password          | varchar(60)  | YES  |     | NULL    |                |
| realm             | varchar(512) | YES  |     | NULL    |                |
| username          | varchar(512) | YES  |     | NULL    |                |
| credentials       | text         | YES  |     | NULL    |                |
| challenges        | text         | YES  |     | NULL    |                |
| emailVerified     | tinyint(1)   | YES  |     | NULL    |                |
| verificationToken | varchar(512) | YES  |     | NULL    |                |
| status            | varchar(512) | YES  |     | NULL    |                |
| created           | datetime     | YES  |     | NULL    |                |
| lastUpdated       | datetime     | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+

有什么办法可以避免用一些额外的道具填充表格?

4

2 回答 2

2

您可以将以下代码添加到 common/models/admin.js

module.exports = function(Admin) {

var excludedProperties = [
  'realm',
  'emailVerified',
  'verificationToken',
  'credentials',
  'challenges',
  'lastUpdated'
];

// Remove the properties from base User model that doesn't have mapped columns
excludedProperties.forEach(function (p) {
  delete Admin.definition.rawProperties[p];
  delete Admin.definition.properties[p];
  delete Admin.prototype[p];
});
}
于 2014-10-22T16:32:43.697 回答
1

现在您可以直接从 JSON 模型定义中排除这些属性,如 strongloop 文档所述。 "base": "User", "properties": { "lastUpdated": false, "credentials": null, "challenges": null, "modified": "date" }

http://docs.strongloop.com/display/public/LB/Model+definition+JSON+file

于 2015-07-22T15:35:05.703 回答