当前位置 : 主页 > 手机开发 > 其它 >

loopbackjs – LoopBack 2.4:继承自User模型,为mysql表添加额外的列

来源:互联网 收集:自由互联 发布时间:2021-06-19
我基于User创建了Admin模型: { "name": "Admin", "plural": "admins", "base": "User", "strict": true, "properties": { "email": { "type": "string", "mysql": { "dataType": "VARCHAR", "dataLength": 50 } }, "password": { "type": "string",
我基于User创建了Admin模型:

{
  "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数据库中的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    |                |
+-------------------+--------------+------+-----+---------+----------------+

有没有办法避免用一些额外的道具填充表格?

您可以将以下代码添加到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];
});
}
网友评论